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

Commit b0bb8ea

Browse files
authored
Switch PlatformMessages to hold data in Mappings (#25867)
1 parent cba6c1e commit b0bb8ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+318
-157
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ FILE: ../../../flutter/fml/macros.h
173173
FILE: ../../../flutter/fml/make_copyable.h
174174
FILE: ../../../flutter/fml/mapping.cc
175175
FILE: ../../../flutter/fml/mapping.h
176+
FILE: ../../../flutter/fml/mapping_unittests.cc
176177
FILE: ../../../flutter/fml/memory/ref_counted.h
177178
FILE: ../../../flutter/fml/memory/ref_counted_internal.h
178179
FILE: ../../../flutter/fml/memory/ref_counted_unittest.cc

fml/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ if (enable_unittests) {
262262
"file_unittest.cc",
263263
"hash_combine_unittests.cc",
264264
"logging_unittests.cc",
265+
"mapping_unittests.cc",
265266
"memory/ref_counted_unittest.cc",
266267
"memory/task_runner_checker_unittest.cc",
267268
"memory/weak_ptr_unittest.cc",

fml/mapping.cc

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ const uint8_t* DataMapping::GetMapping() const {
8282
}
8383

8484
// NonOwnedMapping
85-
8685
NonOwnedMapping::NonOwnedMapping(const uint8_t* data,
8786
size_t size,
8887
const ReleaseProc& release_proc)
@@ -102,6 +101,46 @@ const uint8_t* NonOwnedMapping::GetMapping() const {
102101
return data_;
103102
}
104103

104+
// MallocMapping
105+
MallocMapping::MallocMapping() : data_(nullptr), size_(0) {}
106+
107+
MallocMapping::MallocMapping(uint8_t* data, size_t size)
108+
: data_(data), size_(size) {}
109+
110+
MallocMapping::MallocMapping(fml::MallocMapping&& mapping)
111+
: data_(mapping.data_), size_(mapping.size_) {
112+
mapping.data_ = nullptr;
113+
mapping.size_ = 0;
114+
}
115+
116+
MallocMapping::~MallocMapping() {
117+
free(data_);
118+
data_ = nullptr;
119+
}
120+
121+
MallocMapping MallocMapping::Copy(const void* begin, size_t length) {
122+
auto result =
123+
MallocMapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
124+
FML_CHECK(result.GetMapping() != nullptr);
125+
memcpy(const_cast<uint8_t*>(result.GetMapping()), begin, length);
126+
return result;
127+
}
128+
129+
size_t MallocMapping::GetSize() const {
130+
return size_;
131+
}
132+
133+
const uint8_t* MallocMapping::GetMapping() const {
134+
return data_;
135+
}
136+
137+
uint8_t* MallocMapping::Release() {
138+
uint8_t* result = data_;
139+
data_ = nullptr;
140+
size_ = 0;
141+
return result;
142+
}
143+
105144
// Symbol Mapping
106145

107146
SymbolMapping::SymbolMapping(fml::RefPtr<fml::NativeLibrary> native_library,

fml/mapping.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,54 @@ class NonOwnedMapping final : public Mapping {
125125
FML_DISALLOW_COPY_AND_ASSIGN(NonOwnedMapping);
126126
};
127127

128+
/// A Mapping like NonOwnedMapping, but uses Free as its release proc.
129+
class MallocMapping final : public Mapping {
130+
public:
131+
MallocMapping();
132+
133+
/// Creates a MallocMapping for a region of memory (without copying it).
134+
/// The function will `abort()` if the malloc fails.
135+
/// @param data The starting address of the mapping.
136+
/// @param size The size of the mapping in bytes.
137+
MallocMapping(uint8_t* data, size_t size);
138+
139+
MallocMapping(fml::MallocMapping&& mapping);
140+
141+
~MallocMapping() override;
142+
143+
/// Copies the data from `begin` to `end`.
144+
/// It's templated since void* arithemetic isn't allowed and we want support
145+
/// for `uint8_t` and `char`.
146+
template <typename T>
147+
static MallocMapping Copy(const T* begin, const T* end) {
148+
FML_DCHECK(end > begin);
149+
size_t length = end - begin;
150+
return Copy(begin, length);
151+
}
152+
153+
/// Copies a region of memory into a MallocMapping.
154+
/// The function will `abort()` if the malloc fails.
155+
/// @param begin The starting address of where we will copy.
156+
/// @param length The length of the region to copy in bytes.
157+
static MallocMapping Copy(const void* begin, size_t length);
158+
159+
// |Mapping|
160+
size_t GetSize() const override;
161+
162+
// |Mapping|
163+
const uint8_t* GetMapping() const override;
164+
165+
/// Removes ownership of the data buffer.
166+
/// After this is called; the mapping will point to nullptr.
167+
[[nodiscard]] uint8_t* Release();
168+
169+
private:
170+
uint8_t* data_;
171+
size_t size_;
172+
173+
FML_DISALLOW_COPY_AND_ASSIGN(MallocMapping);
174+
};
175+
128176
class SymbolMapping final : public Mapping {
129177
public:
130178
SymbolMapping(fml::RefPtr<fml::NativeLibrary> native_library,

fml/mapping_unittests.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/fml/mapping.h"
6+
#include "flutter/testing/testing.h"
7+
8+
namespace fml {
9+
10+
TEST(MallocMapping, EmptyContructor) {
11+
MallocMapping mapping;
12+
ASSERT_EQ(nullptr, mapping.GetMapping());
13+
ASSERT_EQ(0u, mapping.GetSize());
14+
}
15+
16+
TEST(MallocMapping, NotEmptyContructor) {
17+
size_t length = 10;
18+
MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
19+
ASSERT_NE(nullptr, mapping.GetMapping());
20+
ASSERT_EQ(length, mapping.GetSize());
21+
}
22+
23+
TEST(MallocMapping, MoveConstructor) {
24+
size_t length = 10;
25+
MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
26+
MallocMapping moved = std::move(mapping);
27+
28+
ASSERT_EQ(nullptr, mapping.GetMapping());
29+
ASSERT_EQ(0u, mapping.GetSize());
30+
ASSERT_NE(nullptr, moved.GetMapping());
31+
ASSERT_EQ(length, moved.GetSize());
32+
}
33+
34+
TEST(MallocMapping, Copy) {
35+
size_t length = 10;
36+
MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
37+
memset(const_cast<uint8_t*>(mapping.GetMapping()), 0xac, mapping.GetSize());
38+
MallocMapping copied =
39+
MallocMapping::Copy(mapping.GetMapping(), mapping.GetSize());
40+
41+
ASSERT_NE(mapping.GetMapping(), copied.GetMapping());
42+
ASSERT_EQ(mapping.GetSize(), copied.GetSize());
43+
ASSERT_EQ(
44+
0, memcmp(mapping.GetMapping(), copied.GetMapping(), mapping.GetSize()));
45+
}
46+
47+
TEST(MallocMapping, Release) {
48+
size_t length = 10;
49+
MallocMapping mapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
50+
free(const_cast<uint8_t*>(mapping.Release()));
51+
ASSERT_EQ(nullptr, mapping.GetMapping());
52+
ASSERT_EQ(0u, mapping.GetSize());
53+
}
54+
55+
} // namespace fml

lib/ui/window/platform_configuration.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Dart_Handle SendPlatformMessage(Dart_Handle window,
130130
const uint8_t* buffer = static_cast<const uint8_t*>(data.data());
131131
dart_state->platform_configuration()->client()->HandlePlatformMessage(
132132
std::make_unique<PlatformMessage>(
133-
name, std::vector<uint8_t>(buffer, buffer + data.length_in_bytes()),
133+
name, fml::MallocMapping::Copy(buffer, data.length_in_bytes()),
134134
response));
135135
}
136136

@@ -190,8 +190,8 @@ void _RespondToKeyData(Dart_NativeArguments args) {
190190
tonic::DartCallStatic(&RespondToKeyData, args);
191191
}
192192

193-
Dart_Handle ToByteData(const std::vector<uint8_t>& buffer) {
194-
return tonic::DartByteData::Create(buffer.data(), buffer.size());
193+
Dart_Handle ToByteData(const fml::Mapping& buffer) {
194+
return tonic::DartByteData::Create(buffer.GetMapping(), buffer.GetSize());
195195
}
196196

197197
} // namespace
@@ -338,15 +338,16 @@ void PlatformConfiguration::DispatchPlatformMessage(
338338

339339
void PlatformConfiguration::DispatchSemanticsAction(int32_t id,
340340
SemanticsAction action,
341-
std::vector<uint8_t> args) {
341+
fml::MallocMapping args) {
342342
std::shared_ptr<tonic::DartState> dart_state =
343343
dispatch_semantics_action_.dart_state().lock();
344344
if (!dart_state) {
345345
return;
346346
}
347347
tonic::DartState::Scope scope(dart_state);
348348

349-
Dart_Handle args_handle = (args.empty()) ? Dart_Null() : ToByteData(args);
349+
Dart_Handle args_handle =
350+
(args.GetSize() <= 0) ? Dart_Null() : ToByteData(args);
350351

351352
if (Dart_IsError(args_handle)) {
352353
return;

lib/ui/window/platform_configuration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class PlatformConfiguration final {
325325
///
326326
void DispatchSemanticsAction(int32_t id,
327327
SemanticsAction action,
328-
std::vector<uint8_t> args);
328+
fml::MallocMapping args);
329329

330330
//----------------------------------------------------------------------------
331331
/// @brief Registers a callback to be invoked when the framework has

lib/ui/window/platform_message.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace flutter {
1010

1111
PlatformMessage::PlatformMessage(std::string channel,
12-
std::vector<uint8_t> data,
12+
fml::MallocMapping data,
1313
fml::RefPtr<PlatformMessageResponse> response)
1414
: channel_(std::move(channel)),
1515
data_(std::move(data)),

lib/ui/window/platform_message.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,25 @@ namespace flutter {
1717
class PlatformMessage {
1818
public:
1919
PlatformMessage(std::string channel,
20-
std::vector<uint8_t> data,
20+
fml::MallocMapping data,
2121
fml::RefPtr<PlatformMessageResponse> response);
2222
PlatformMessage(std::string channel,
2323
fml::RefPtr<PlatformMessageResponse> response);
2424
~PlatformMessage();
2525

2626
const std::string& channel() const { return channel_; }
27-
const std::vector<uint8_t>& data() const { return data_; }
27+
const fml::MallocMapping& data() const { return data_; }
2828
bool hasData() { return hasData_; }
2929

3030
const fml::RefPtr<PlatformMessageResponse>& response() const {
3131
return response_;
3232
}
3333

34+
fml::MallocMapping releaseData() { return std::move(data_); }
35+
3436
private:
3537
std::string channel_;
36-
std::vector<uint8_t> data_;
38+
fml::MallocMapping data_;
3739
bool hasData_;
3840
fml::RefPtr<PlatformMessageResponse> response_;
3941
};

runtime/runtime_controller.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ bool RuntimeController::DispatchKeyDataPacket(const KeyDataPacket& packet,
278278

279279
bool RuntimeController::DispatchSemanticsAction(int32_t id,
280280
SemanticsAction action,
281-
std::vector<uint8_t> args) {
281+
fml::MallocMapping args) {
282282
TRACE_EVENT1("flutter", "RuntimeController::DispatchSemanticsAction", "mode",
283283
"basic");
284284
if (auto* platform_configuration = GetPlatformConfigurationIfAvailable()) {

0 commit comments

Comments
 (0)