Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit aac6765

Browse files
committed
Eliminates an extra copy when returning messages from the host platform to dart.
1 parent 10ff302 commit aac6765

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

lib/ui/window/platform_message_response_dart.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ void PlatformMessageResponseDart::Complete(std::unique_ptr<fml::Mapping> data) {
4242
}
4343
tonic::DartState::Scope scope(dart_state);
4444

45-
Dart_Handle byte_buffer =
46-
tonic::DartByteData::Create(data->GetMapping(), data->GetSize());
45+
size_t data_size = data->GetSize();
46+
Dart_Handle byte_buffer = tonic::DartByteData::Adopt(
47+
const_cast<uint8_t*>(data->GetMapping()), data_size);
48+
// TODO(gaaclarke): fix the leak;
49+
data.release();
4750
tonic::DartInvoke(callback.Release(), {byte_buffer});
4851
}));
4952
}

third_party/tonic/typed_data/dart_byte_data.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ Dart_Handle DartByteData::Create(const void* data, size_t length) {
3636
}
3737
}
3838

39+
Dart_Handle DartByteData::Adopt(void* data, size_t length) {
40+
return Dart_NewExternalTypedDataWithFinalizer(
41+
Dart_TypedData_kByteData, data, length, data, length, FreeFinalizer);
42+
}
43+
3944
DartByteData::DartByteData()
4045
: data_(nullptr), length_in_bytes_(0), dart_handle_(nullptr) {}
4146

third_party/tonic/typed_data/dart_byte_data.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace tonic {
1515
class DartByteData {
1616
public:
1717
static Dart_Handle Create(const void* data, size_t length);
18+
/// Takes ownership over [data] and calls [free] on it when it is done.
19+
static Dart_Handle Adopt(void* data, size_t length);
1820

1921
explicit DartByteData(Dart_Handle list);
2022
DartByteData(DartByteData&& other);

0 commit comments

Comments
 (0)