[VM/Runtime] - Use 'const' qualifier for CObject typed data

Addresses https://github.com/dart-lang/sdk/issues/49827

TEST=ci

Change-Id: I525cc27d0bf01945d4f700f48355a3f17e297007
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256602
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Siva Annamalai <asiva@google.com>
This commit is contained in:
asiva 2022-09-13 22:38:22 +00:00 committed by Commit Bot
parent 2ead86ab9d
commit 3cdeb58c98
8 changed files with 21 additions and 30 deletions

View file

@ -897,19 +897,13 @@ Dart_CObject* CObject::NewArray(intptr_t length) {
return cobject;
}
Dart_CObject* CObject::NewUint8Array(intptr_t length) {
Dart_CObject* CObject::NewUint8Array(const void* data, intptr_t length) {
Dart_CObject* cobject = New(Dart_CObject_kTypedData, length);
memmove(reinterpret_cast<uint8_t*>(cobject + 1), data, length);
cobject->value.as_typed_data.type = Dart_TypedData_kUint8;
cobject->value.as_typed_data.length = length;
cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1);
return cobject;
}
Dart_CObject* CObject::NewUint32Array(intptr_t length) {
Dart_CObject* cobject = New(Dart_CObject_kTypedData, 4 * length);
cobject->value.as_typed_data.type = Dart_TypedData_kUint32;
cobject->value.as_typed_data.length = length;
cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1);
cobject->value.as_typed_data.values =
reinterpret_cast<const uint8_t*>(cobject + 1);
return cobject;
}

View file

@ -344,8 +344,7 @@ class CObject {
static Dart_CObject* NewString(intptr_t length);
static Dart_CObject* NewString(const char* str);
static Dart_CObject* NewArray(intptr_t length);
static Dart_CObject* NewUint8Array(intptr_t length);
static Dart_CObject* NewUint32Array(intptr_t length);
static Dart_CObject* NewUint8Array(const void* data, intptr_t length);
static Dart_CObject* NewExternalUint8Array(intptr_t length,
uint8_t* data,
void* peer,
@ -547,7 +546,7 @@ class CObjectTypedData : public CObject {
return cobject_->value.as_typed_data.type;
}
intptr_t Length() const { return cobject_->value.as_typed_data.length; }
uint8_t* Buffer() const { return cobject_->value.as_typed_data.values; }
const uint8_t* Buffer() const { return cobject_->value.as_typed_data.values; }
private:
DISALLOW_COPY_AND_ASSIGN(CObjectTypedData);
@ -558,7 +557,7 @@ class CObjectUint8Array : public CObject {
DECLARE_COBJECT_TYPED_DATA_CONSTRUCTORS(Uint8)
intptr_t Length() const { return cobject_->value.as_typed_data.length; }
uint8_t* Buffer() const { return cobject_->value.as_typed_data.values; }
const uint8_t* Buffer() const { return cobject_->value.as_typed_data.values; }
private:
DISALLOW_COPY_AND_ASSIGN(CObjectUint8Array);

View file

@ -1395,7 +1395,7 @@ CObject* File::WriteFromRequest(const CObjectArray& request) {
int64_t start = CObjectInt32OrInt64ToInt64(request[2]);
int64_t end = CObjectInt32OrInt64ToInt64(request[3]);
int64_t length = end - start;
uint8_t* buffer_start;
const uint8_t* buffer_start;
if (request[1]->IsTypedData()) {
CObjectTypedData typed_data(request[1]);
start = start * SizeInBytes(typed_data.Type());
@ -1403,11 +1403,12 @@ CObject* File::WriteFromRequest(const CObjectArray& request) {
buffer_start = typed_data.Buffer() + start;
} else {
CObjectArray array(request[1]);
buffer_start = Dart_ScopeAllocate(length);
uint8_t* allocated_buffer = Dart_ScopeAllocate(length);
buffer_start = allocated_buffer;
for (int i = 0; i < length; i++) {
if (array[i + start]->IsInt32OrInt64()) {
int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]);
buffer_start[i] = static_cast<uint8_t>(value & 0xFF);
allocated_buffer[i] = static_cast<uint8_t>(value & 0xFF);
} else {
// Unsupported type.
return CObject::IllegalArgumentError();
@ -1415,7 +1416,7 @@ CObject* File::WriteFromRequest(const CObjectArray& request) {
}
start = 0;
}
return file->WriteFully(reinterpret_cast<void*>(buffer_start), length)
return file->WriteFully(reinterpret_cast<const void*>(buffer_start), length)
? new CObjectInt64(CObject::NewInt64(length))
: CObject::NewOSError();
}

View file

@ -218,14 +218,13 @@ Dart_Handle SocketAddress::ToTypedData(const RawAddr& addr) {
CObjectUint8Array* SocketAddress::ToCObject(const RawAddr& addr) {
int in_addr_len = SocketAddress::GetInAddrLength(addr);
const void* in_addr;
CObjectUint8Array* data =
new CObjectUint8Array(CObject::NewUint8Array(in_addr_len));
if (addr.addr.sa_family == AF_INET6) {
in_addr = reinterpret_cast<const void*>(&addr.in6.sin6_addr);
} else {
in_addr = reinterpret_cast<const void*>(&addr.in.sin_addr);
}
memmove(data->Buffer(), in_addr, in_addr_len);
CObjectUint8Array* data =
new CObjectUint8Array(CObject::NewUint8Array(in_addr, in_addr_len));
return data;
}
void SocketAddress::SetAddrScope(RawAddr* addr, intptr_t scope_id) {

View file

@ -80,7 +80,7 @@ typedef struct _Dart_CObject {
struct {
Dart_TypedData_Type type;
intptr_t length; /* in elements, not bytes */
uint8_t* values;
const uint8_t* values;
} as_typed_data;
struct {
Dart_TypedData_Type type;

View file

@ -8385,7 +8385,7 @@ static void NewNativePort_Transferrable1(Dart_Port dest_port_id,
EXPECT_EQ(Dart_TypedData_kUint8, message->value.as_typed_data.type);
EXPECT_EQ(10, message->value.as_typed_data.length);
EXPECT_EQ(42, message->value.as_typed_data.values[0]);
free(message->value.as_typed_data.values);
free(const_cast<uint8_t*>(message->value.as_typed_data.values));
}
static void NewNativePort_Transferrable2(Dart_Port dest_port_id,
@ -8399,7 +8399,7 @@ static void NewNativePort_Transferrable2(Dart_Port dest_port_id,
EXPECT_EQ(Dart_TypedData_kUint8, cobj->value.as_typed_data.type);
EXPECT_EQ(10, cobj->value.as_typed_data.length);
EXPECT_EQ(42, cobj->value.as_typed_data.values[0]);
free(cobj->value.as_typed_data.values);
free(const_cast<uint8_t*>(cobj->value.as_typed_data.values));
}
TEST_CASE(DartAPI_NativePortPostTransferrableTypedData) {

View file

@ -377,7 +377,7 @@ static Dart_CObject BuildFilesPairs(int source_files_count,
source_code->value.as_typed_data.type = Dart_TypedData_kUint8;
source_code->value.as_typed_data.length = strlen(source_files[i].source);
source_code->value.as_typed_data.values =
reinterpret_cast<uint8_t*>(const_cast<char*>(source_files[i].source));
reinterpret_cast<const uint8_t*>(source_files[i].source);
} else {
source_code->type = Dart_CObject_kNull;
}

View file

@ -1610,8 +1610,7 @@ class TypedDataMessageSerializationCluster
s->AssignRef(data);
intptr_t length = data->value.as_external_typed_data.length;
s->WriteUnsigned(length);
uint8_t* cdata =
reinterpret_cast<uint8_t*>(data->value.as_typed_data.values);
const uint8_t* cdata = data->value.as_typed_data.values;
s->WriteBytes(cdata, length * element_size);
}
}
@ -1700,8 +1699,7 @@ class TypedDataMessageDeserializationCluster
if (length == 0) {
data->value.as_typed_data.values = NULL;
} else {
data->value.as_typed_data.values =
const_cast<uint8_t*>(d->CurrentBufferAddress());
data->value.as_typed_data.values = d->CurrentBufferAddress();
d->Advance(length * element_size);
}
d->AssignRef(data);
@ -2305,7 +2303,7 @@ class TransferableTypedDataMessageDeserializationCluster
data->value.as_typed_data.type = Dart_TypedData_kUint8;
FinalizableData finalizable_data = d->finalizable_data()->Get();
data->value.as_typed_data.values =
reinterpret_cast<uint8_t*>(finalizable_data.data);
reinterpret_cast<const uint8_t*>(finalizable_data.data);
d->AssignRef(data);
}
}