Skip to content

Commit

Permalink
[VM/Runtime] - Use 'const' qualifier for CObject typed data
Browse files Browse the repository at this point in the history
Addresses #49827

TEST=ci

Change-Id: I525cc27d0bf01945d4f700f48355a3f17e297007
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256602
Reviewed-by: Ryan Macnak <[email protected]>
Commit-Queue: Siva Annamalai <[email protected]>
  • Loading branch information
a-siva authored and Commit Bot committed Sep 13, 2022
1 parent 2ead86a commit 3cdeb58
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 30 deletions.
14 changes: 4 additions & 10 deletions runtime/bin/dartutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 3 additions & 4 deletions runtime/bin/dartutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions runtime/bin/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1395,27 +1395,28 @@ 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());
length = length * SizeInBytes(typed_data.Type());
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();
}
}
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();
}
Expand Down
5 changes: 2 additions & 3 deletions runtime/bin/socket_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/include/dart_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions runtime/vm/dart_api_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/vm/kernel_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 3 additions & 5 deletions runtime/vm/message_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 3cdeb58

Please sign in to comment.