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

Commit 1745e82

Browse files
committed
switched to MallocMapping
1 parent 88f6ce6 commit 1745e82

37 files changed

+118
-113
lines changed

fml/mapping.cc

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,39 +82,10 @@ const uint8_t* DataMapping::GetMapping() const {
8282
}
8383

8484
// NonOwnedMapping
85-
NonOwnedMapping::NonOwnedMapping()
86-
: data_(nullptr), size_(0), release_proc_(), uses_free_(false) {}
87-
8885
NonOwnedMapping::NonOwnedMapping(const uint8_t* data,
8986
size_t size,
9087
const ReleaseProc& release_proc)
91-
: data_(data),
92-
size_(size),
93-
release_proc_(release_proc),
94-
uses_free_(false) {}
95-
96-
NonOwnedMapping::NonOwnedMapping(fml::NonOwnedMapping&& mapping)
97-
: data_(mapping.data_),
98-
size_(mapping.size_),
99-
release_proc_(mapping.release_proc_),
100-
uses_free_(mapping.uses_free_) {
101-
mapping.data_ = nullptr;
102-
mapping.size_ = 0;
103-
mapping.release_proc_ = nullptr;
104-
mapping.uses_free_ = false;
105-
}
106-
107-
namespace {
108-
void FreeProc(const uint8_t* data, size_t size) {
109-
free(const_cast<uint8_t*>(data));
110-
}
111-
} // namespace
112-
113-
NonOwnedMapping NonOwnedMapping::WithFree(const uint8_t* data, size_t size) {
114-
auto result = NonOwnedMapping(data, size, FreeProc);
115-
result.uses_free_ = true;
116-
return result;
117-
}
88+
: data_(data), size_(size), release_proc_(release_proc) {}
11889

11990
NonOwnedMapping::~NonOwnedMapping() {
12091
if (release_proc_) {
@@ -130,11 +101,37 @@ const uint8_t* NonOwnedMapping::GetMapping() const {
130101
return data_;
131102
}
132103

133-
const uint8_t* NonOwnedMapping::Release() {
104+
// MallocMapping
105+
MallocMapping::MallocMapping() : data_(nullptr), size_(0) {}
106+
107+
MallocMapping::MallocMapping(const 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+
if (data_) {
118+
free(const_cast<uint8_t*>(data_));
119+
data_ = nullptr;
120+
}
121+
}
122+
123+
size_t MallocMapping::GetSize() const {
124+
return size_;
125+
}
126+
127+
const uint8_t* MallocMapping::GetMapping() const {
128+
return data_;
129+
}
130+
131+
const uint8_t* MallocMapping::Release() {
134132
const uint8_t* result = data_;
135133
data_ = nullptr;
136134
size_ = 0;
137-
release_proc_ = nullptr;
138135
return result;
139136
}
140137

fml/mapping.h

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,45 @@ class DataMapping final : public Mapping {
105105
class NonOwnedMapping final : public Mapping {
106106
public:
107107
using ReleaseProc = std::function<void(const uint8_t* data, size_t size)>;
108-
NonOwnedMapping();
109-
110108
NonOwnedMapping(const uint8_t* data,
111109
size_t size,
112110
const ReleaseProc& release_proc = nullptr);
113111

114-
NonOwnedMapping(fml::NonOwnedMapping&& mapping);
115-
116112
~NonOwnedMapping() override;
117113

114+
// |Mapping|
115+
size_t GetSize() const override;
116+
117+
// |Mapping|
118+
const uint8_t* GetMapping() const override;
119+
120+
private:
121+
const uint8_t* data_;
122+
const size_t size_;
123+
const ReleaseProc release_proc_;
124+
125+
FML_DISALLOW_COPY_AND_ASSIGN(NonOwnedMapping);
126+
};
127+
128+
/// A Mapping like NonOwnedMapping, but uses Free as its release proc.
129+
class MallocMapping final : public Mapping {
130+
public:
131+
MallocMapping();
132+
133+
MallocMapping(const uint8_t* data, size_t size);
134+
135+
MallocMapping(fml::MallocMapping&& mapping);
136+
137+
~MallocMapping() override;
138+
118139
/// Copies the data from `begin` to `end`.
119140
/// It's templated since void* arithemetic isn't allowed and we want support
120141
/// for `uint8_t` and `char`.
121142
template <typename T>
122-
static NonOwnedMapping Copy(const T* begin, const T* end) {
143+
static MallocMapping Copy(const T* begin, const T* end) {
123144
size_t length = end - begin;
124-
auto result = NonOwnedMapping::WithFree(
125-
reinterpret_cast<uint8_t*>(malloc(length)), length);
145+
auto result =
146+
MallocMapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
126147
memcpy(const_cast<uint8_t*>(result.GetMapping()), begin, length);
127148
return result;
128149
}
@@ -136,21 +157,11 @@ class NonOwnedMapping final : public Mapping {
136157
/// Removes ownership of the data buffer.
137158
const uint8_t* Release();
138159

139-
/// Returns true if the Mapping uses `free` to delete the memory.
140-
/// This is important for passing ownership to C/ObjC code like NSData. It
141-
/// has to be a special case because std::function comparisions aren't
142-
/// possible.
143-
bool UsesFree() const { return uses_free_; }
144-
145160
private:
146-
static NonOwnedMapping WithFree(const uint8_t* data, size_t size);
147-
148161
const uint8_t* data_;
149162
size_t size_;
150-
ReleaseProc release_proc_;
151-
bool uses_free_;
152163

153-
FML_DISALLOW_COPY_AND_ASSIGN(NonOwnedMapping);
164+
FML_DISALLOW_COPY_AND_ASSIGN(MallocMapping);
154165
};
155166

156167
class SymbolMapping final : public Mapping {

lib/ui/window/platform_configuration.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Dart_Handle SendPlatformMessage(Dart_Handle window,
131131
dart_state->platform_configuration()->client()->HandlePlatformMessage(
132132
std::make_unique<PlatformMessage>(
133133
name,
134-
fml::NonOwnedMapping::Copy(buffer, buffer + data.length_in_bytes()),
134+
fml::MallocMapping::Copy(buffer, buffer + data.length_in_bytes()),
135135
response));
136136
}
137137

@@ -339,7 +339,7 @@ void PlatformConfiguration::DispatchPlatformMessage(
339339

340340
void PlatformConfiguration::DispatchSemanticsAction(int32_t id,
341341
SemanticsAction action,
342-
fml::NonOwnedMapping args) {
342+
fml::MallocMapping args) {
343343
std::shared_ptr<tonic::DartState> dart_state =
344344
dispatch_semantics_action_.dart_state().lock();
345345
if (!dart_state) {

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-
fml::NonOwnedMapping 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-
fml::NonOwnedMapping 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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ namespace flutter {
1717
class PlatformMessage {
1818
public:
1919
PlatformMessage(std::string channel,
20-
fml::NonOwnedMapping 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 fml::NonOwnedMapping& 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::NonOwnedMapping releaseData() { return std::move(data_); }
34+
fml::MallocMapping releaseData() { return std::move(data_); }
3535

3636
private:
3737
std::string channel_;
38-
fml::NonOwnedMapping data_;
38+
fml::MallocMapping data_;
3939
bool hasData_;
4040
fml::RefPtr<PlatformMessageResponse> response_;
4141
};

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-
fml::NonOwnedMapping args) {
281+
fml::MallocMapping args) {
282282
TRACE_EVENT1("flutter", "RuntimeController::DispatchSemanticsAction", "mode",
283283
"basic");
284284
if (auto* platform_configuration = GetPlatformConfigurationIfAvailable()) {

runtime/runtime_controller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ class RuntimeController : public PlatformConfigurationClient {
453453
///
454454
bool DispatchSemanticsAction(int32_t id,
455455
SemanticsAction action,
456-
fml::NonOwnedMapping args);
456+
fml::MallocMapping args);
457457

458458
//----------------------------------------------------------------------------
459459
/// @brief Gets the main port identifier of the root isolate.

shell/common/engine.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ static constexpr char kSettingsChannel[] = "flutter/settings";
3737
static constexpr char kIsolateChannel[] = "flutter/isolate";
3838

3939
namespace {
40-
fml::NonOwnedMapping MakeMapping(const std::string& str) {
41-
return fml::NonOwnedMapping::Copy(str.c_str(), str.c_str() + str.length());
40+
fml::MallocMapping MakeMapping(const std::string& str) {
41+
return fml::MallocMapping::Copy(str.c_str(), str.c_str() + str.length());
4242
}
4343
} // namespace
4444

@@ -433,7 +433,7 @@ void Engine::DispatchKeyDataPacket(std::unique_ptr<KeyDataPacket> packet,
433433

434434
void Engine::DispatchSemanticsAction(int id,
435435
SemanticsAction action,
436-
fml::NonOwnedMapping args) {
436+
fml::MallocMapping args) {
437437
runtime_controller_->DispatchSemanticsAction(id, action, std::move(args));
438438
}
439439

shell/common/engine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ class Engine final : public RuntimeDelegate,
757757
///
758758
void DispatchSemanticsAction(int id,
759759
SemanticsAction action,
760-
fml::NonOwnedMapping args);
760+
fml::MallocMapping args);
761761

762762
//----------------------------------------------------------------------------
763763
/// @brief Notifies the engine that the embedder has expressed an opinion

0 commit comments

Comments
 (0)