Flutter Engine
The Flutter Engine
mapping.cc
Go to the documentation of this file.
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
7#include <algorithm>
8#include <cstring>
9#include <memory>
10#include <sstream>
11
12namespace fml {
13
14// FileMapping
15
17 return mutable_mapping_;
18}
19
20std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
21 const std::string& path) {
22 return CreateReadOnly(OpenFile(path.c_str(), false, FilePermission::kRead),
23 "");
24}
25
26std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
27 const fml::UniqueFD& base_fd,
28 const std::string& sub_path) {
29 if (!sub_path.empty()) {
30 return CreateReadOnly(
31 OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
32 }
33
34 auto mapping = std::make_unique<FileMapping>(
35 base_fd, std::initializer_list<Protection>{Protection::kRead});
36
37 if (!mapping->IsValid()) {
38 return nullptr;
39 }
40
41 return mapping;
42}
43
44std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
45 const std::string& path) {
46 return CreateReadExecute(
47 OpenFile(path.c_str(), false, FilePermission::kRead));
48}
49
50std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
51 const fml::UniqueFD& base_fd,
52 const std::string& sub_path) {
53 if (!sub_path.empty()) {
54 return CreateReadExecute(
55 OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
56 }
57
58 auto mapping = std::make_unique<FileMapping>(
59 base_fd, std::initializer_list<Protection>{Protection::kRead,
61
62 if (!mapping->IsValid()) {
63 return nullptr;
64 }
65
66 return mapping;
67}
68
69// Data Mapping
70
71DataMapping::DataMapping(std::vector<uint8_t> data) : data_(std::move(data)) {}
72
73DataMapping::DataMapping(const std::string& string)
74 : data_(string.begin(), string.end()) {}
75
77
78size_t DataMapping::GetSize() const {
79 return data_.size();
80}
81
82const uint8_t* DataMapping::GetMapping() const {
83 return data_.data();
84}
85
87 return false;
88}
89
90// NonOwnedMapping
92 size_t size,
93 const ReleaseProc& release_proc,
94 bool dontneed_safe)
95 : data_(data),
96 size_(size),
97 release_proc_(release_proc),
98 dontneed_safe_(dontneed_safe) {}
99
101 if (release_proc_) {
102 release_proc_(data_, size_);
103 }
104}
105
107 return size_;
108}
109
110const uint8_t* NonOwnedMapping::GetMapping() const {
111 return data_;
112}
113
115 return dontneed_safe_;
116}
117
118// MallocMapping
119MallocMapping::MallocMapping() : data_(nullptr), size_(0) {}
120
122 : data_(data), size_(size) {}
123
125 : data_(mapping.data_), size_(mapping.size_) {
126 mapping.data_ = nullptr;
127 mapping.size_ = 0;
128}
129
131 free(data_);
132 data_ = nullptr;
133}
134
136 auto result =
137 MallocMapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
138 FML_CHECK(result.GetMapping() != nullptr);
139 memcpy(const_cast<uint8_t*>(result.GetMapping()), begin, length);
140 return result;
141}
142
144 return size_;
145}
146
147const uint8_t* MallocMapping::GetMapping() const {
148 return data_;
149}
150
152 return false;
153}
154
156 uint8_t* result = data_;
157 data_ = nullptr;
158 size_ = 0;
159 return result;
160}
161
162// Symbol Mapping
163
165 const char* symbol_name)
166 : native_library_(std::move(native_library)) {
167 if (native_library_ && symbol_name != nullptr) {
168 mapping_ = native_library_->ResolveSymbol(symbol_name);
169
170 if (mapping_ == nullptr) {
171 // Apparently, dart_bootstrap seems to account for the Mac behavior of
172 // requiring the underscore prefixed symbol name on non-Mac platforms as
173 // well. As a fallback, check the underscore prefixed variant of the
174 // symbol name and allow callers to not have handle this on a per platform
175 // toolchain quirk basis.
176
177 std::stringstream underscore_symbol_name;
178 underscore_symbol_name << "_" << symbol_name;
179 mapping_ =
180 native_library_->ResolveSymbol(underscore_symbol_name.str().c_str());
181 }
182 }
183}
184
186
188 return 0;
189}
190
191const uint8_t* SymbolMapping::GetMapping() const {
192 return mapping_;
193}
194
196 return true;
197}
198
199} // namespace fml
const uint8_t * GetMapping() const override
Definition: mapping.cc:82
bool IsDontNeedSafe() const override
Definition: mapping.cc:86
size_t GetSize() const override
Definition: mapping.cc:78
~DataMapping() override
DataMapping(std::vector< uint8_t > data)
Definition: mapping.cc:71
static std::unique_ptr< FileMapping > CreateReadExecute(const std::string &path)
Definition: mapping.cc:44
uint8_t * GetMutableMapping()
Definition: mapping.cc:16
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition: mapping.cc:20
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition: mapping.h:144
bool IsDontNeedSafe() const override
Definition: mapping.cc:151
static MallocMapping Copy(const T *begin, const T *end)
Definition: mapping.h:162
size_t GetSize() const override
Definition: mapping.cc:143
const uint8_t * GetMapping() const override
Definition: mapping.cc:147
~MallocMapping() override
Definition: mapping.cc:130
uint8_t * Release()
Definition: mapping.cc:155
const uint8_t * ResolveSymbol(const char *symbol)
std::function< void(const uint8_t *data, size_t size)> ReleaseProc
Definition: mapping.h:117
const uint8_t * GetMapping() const override
Definition: mapping.cc:110
size_t GetSize() const override
Definition: mapping.cc:106
NonOwnedMapping(const uint8_t *data, size_t size, const ReleaseProc &release_proc=nullptr, bool dontneed_safe=false)
Definition: mapping.cc:91
~NonOwnedMapping() override
Definition: mapping.cc:100
bool IsDontNeedSafe() const override
Definition: mapping.cc:114
bool IsDontNeedSafe() const override
Definition: mapping.cc:195
~SymbolMapping() override
SymbolMapping(fml::RefPtr< fml::NativeLibrary > native_library, const char *symbol_name)
Definition: mapping.cc:164
const uint8_t * GetMapping() const override
Definition: mapping.cc:191
size_t GetSize() const override
Definition: mapping.cc:187
static const char * begin(const StringSlice &s)
Definition: editor.cpp:252
glong glong end
GAsyncResult * result
#define FML_CHECK(condition)
Definition: logging.h:85
size_t length
void * malloc(size_t size)
Definition: allocation.cc:19
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
Definition: ascii_trie.cc:9
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition: file_posix.cc:66
Definition: ref_ptr.h:256
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63