Skip to content

Commit

Permalink
Eliminates an extra copy when returning messages from the host platfo…
Browse files Browse the repository at this point in the history
…rm to dart.
  • Loading branch information
gaaclarke committed Jun 13, 2022
1 parent 10ff302 commit aac6765
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/ui/window/platform_message_response_dart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ void PlatformMessageResponseDart::Complete(std::unique_ptr<fml::Mapping> data) {
}
tonic::DartState::Scope scope(dart_state);

Dart_Handle byte_buffer =
tonic::DartByteData::Create(data->GetMapping(), data->GetSize());
size_t data_size = data->GetSize();
Dart_Handle byte_buffer = tonic::DartByteData::Adopt(
const_cast<uint8_t*>(data->GetMapping()), data_size);
// TODO(gaaclarke): fix the leak;
data.release();
tonic::DartInvoke(callback.Release(), {byte_buffer});
}));
}
Expand Down
5 changes: 5 additions & 0 deletions third_party/tonic/typed_data/dart_byte_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Dart_Handle DartByteData::Create(const void* data, size_t length) {
}
}

Dart_Handle DartByteData::Adopt(void* data, size_t length) {
return Dart_NewExternalTypedDataWithFinalizer(
Dart_TypedData_kByteData, data, length, data, length, FreeFinalizer);
}

DartByteData::DartByteData()
: data_(nullptr), length_in_bytes_(0), dart_handle_(nullptr) {}

Expand Down
2 changes: 2 additions & 0 deletions third_party/tonic/typed_data/dart_byte_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace tonic {
class DartByteData {
public:
static Dart_Handle Create(const void* data, size_t length);
/// Takes ownership over [data] and calls [free] on it when it is done.
static Dart_Handle Adopt(void* data, size_t length);

explicit DartByteData(Dart_Handle list);
DartByteData(DartByteData&& other);
Expand Down

0 comments on commit aac6765

Please sign in to comment.