[objects] Modern style DCHECKs in ByteArray accessors

Change-Id: I4ed4d0c53a90af4a8d3a58fa3f418576aadd09c3
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3234195
Commit-Queue: Patrick Thier <pthier@chromium.org>
Auto-Submit: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Patrick Thier <pthier@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77464}
This commit is contained in:
Jakob Gruber 2021-10-20 07:31:03 +02:00 committed by V8 LUCI CQ
parent 8bbb44e537
commit 8da845df71
2 changed files with 34 additions and 20 deletions

View File

@ -556,66 +556,80 @@ void ArrayList::Clear(int index, Object undefined) {
int ByteArray::Size() { return RoundUp(length() + kHeaderSize, kTaggedSize); }
byte ByteArray::get(int index) const {
DCHECK(index >= 0 && index < length());
DCHECK_GE(index, 0);
DCHECK_LT(index, length());
return ReadField<byte>(kHeaderSize + index * kCharSize);
}
void ByteArray::set(int index, byte value) {
DCHECK(index >= 0 && index < length());
DCHECK_GE(index, 0);
DCHECK_LT(index, length());
WriteField<byte>(kHeaderSize + index * kCharSize, value);
}
void ByteArray::copy_in(int index, const byte* buffer, int length) {
DCHECK(index >= 0 && length >= 0 && length <= kMaxInt - index &&
index + length <= this->length());
void ByteArray::copy_in(int index, const byte* buffer, int slice_length) {
DCHECK_GE(index, 0);
DCHECK_GE(slice_length, 0);
DCHECK_LE(slice_length, kMaxInt - index);
DCHECK_LE(index + slice_length, length());
Address dst_addr = field_address(kHeaderSize + index * kCharSize);
memcpy(reinterpret_cast<void*>(dst_addr), buffer, length);
memcpy(reinterpret_cast<void*>(dst_addr), buffer, slice_length);
}
void ByteArray::copy_out(int index, byte* buffer, int length) {
DCHECK(index >= 0 && length >= 0 && length <= kMaxInt - index &&
index + length <= this->length());
void ByteArray::copy_out(int index, byte* buffer, int slice_length) {
DCHECK_GE(index, 0);
DCHECK_GE(slice_length, 0);
DCHECK_LE(slice_length, kMaxInt - index);
DCHECK_LE(index + slice_length, length());
Address src_addr = field_address(kHeaderSize + index * kCharSize);
memcpy(buffer, reinterpret_cast<void*>(src_addr), length);
memcpy(buffer, reinterpret_cast<void*>(src_addr), slice_length);
}
int ByteArray::get_int(int index) const {
DCHECK(index >= 0 && index < length() / kIntSize);
DCHECK_GE(index, 0);
DCHECK_LT(index, length() / kIntSize);
return ReadField<int>(kHeaderSize + index * kIntSize);
}
void ByteArray::set_int(int index, int value) {
DCHECK(index >= 0 && index < length() / kIntSize);
DCHECK_GE(index, 0);
DCHECK_LT(index, length() / kIntSize);
WriteField<int>(kHeaderSize + index * kIntSize, value);
}
uint32_t ByteArray::get_uint32(int index) const {
DCHECK(index >= 0 && index < length() / kUInt32Size);
DCHECK_GE(index, 0);
DCHECK_LT(index, length() / kUInt32Size);
return ReadField<uint32_t>(kHeaderSize + index * kUInt32Size);
}
void ByteArray::set_uint32(int index, uint32_t value) {
DCHECK(index >= 0 && index < length() / kUInt32Size);
DCHECK_GE(index, 0);
DCHECK_LT(index, length() / kUInt32Size);
WriteField<uint32_t>(kHeaderSize + index * kUInt32Size, value);
}
uint32_t ByteArray::get_uint32_relaxed(int index) const {
DCHECK(index >= 0 && index < length() / kUInt32Size);
DCHECK_GE(index, 0);
DCHECK_LT(index, length() / kUInt32Size);
return RELAXED_READ_UINT32_FIELD(*this, kHeaderSize + index * kUInt32Size);
}
void ByteArray::set_uint32_relaxed(int index, uint32_t value) {
DCHECK(index >= 0 && index < length() / kUInt32Size);
DCHECK_GE(index, 0);
DCHECK_LT(index, length() / kUInt32Size);
RELAXED_WRITE_UINT32_FIELD(*this, kHeaderSize + index * kUInt32Size, value);
}
uint16_t ByteArray::get_uint16(int index) const {
DCHECK(index >= 0 && index < length() / kUInt16Size);
DCHECK_GE(index, 0);
DCHECK_LT(index, length() / kUInt16Size);
return ReadField<uint16_t>(kHeaderSize + index * kUInt16Size);
}
void ByteArray::set_uint16(int index, uint16_t value) {
DCHECK(index >= 0 && index < length() / kUInt16Size);
DCHECK_GE(index, 0);
DCHECK_LT(index, length() / kUInt16Size);
WriteField<uint16_t>(kHeaderSize + index * kUInt16Size, value);
}

View File

@ -508,8 +508,8 @@ class ByteArray : public TorqueGeneratedByteArray<ByteArray, FixedArrayBase> {
inline void set(int index, byte value);
// Copy in / copy out whole byte slices.
inline void copy_out(int index, byte* buffer, int length);
inline void copy_in(int index, const byte* buffer, int length);
inline void copy_out(int index, byte* buffer, int slice_length);
inline void copy_in(int index, const byte* buffer, int slice_length);
// Treat contents as an int array.
inline int get_int(int index) const;