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 <sstream>
9 
10 namespace fml {
11 
12 // FileMapping
13 
15  return mutable_mapping_;
16 }
17 
18 std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
19  const std::string& path) {
20  return CreateReadOnly(OpenFile(path.c_str(), false, FilePermission::kRead),
21  "");
22 }
23 
24 std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
25  const fml::UniqueFD& base_fd,
26  const std::string& sub_path) {
27  if (sub_path.size() != 0) {
28  return CreateReadOnly(
29  OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
30  }
31 
32  auto mapping = std::make_unique<FileMapping>(
33  base_fd, std::initializer_list<Protection>{Protection::kRead});
34 
35  if (!mapping->IsValid()) {
36  return nullptr;
37  }
38 
39  return mapping;
40 }
41 
42 std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
43  const std::string& path) {
44  return CreateReadExecute(
45  OpenFile(path.c_str(), false, FilePermission::kRead));
46 }
47 
48 std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
49  const fml::UniqueFD& base_fd,
50  const std::string& sub_path) {
51  if (sub_path.size() != 0) {
52  return CreateReadExecute(
53  OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
54  }
55 
56  auto mapping = std::make_unique<FileMapping>(
57  base_fd, std::initializer_list<Protection>{Protection::kRead,
59 
60  if (!mapping->IsValid()) {
61  return nullptr;
62  }
63 
64  return mapping;
65 }
66 
67 // Data Mapping
68 
69 DataMapping::DataMapping(std::vector<uint8_t> data) : data_(std::move(data)) {}
70 
71 DataMapping::DataMapping(const std::string& string)
72  : data_(string.begin(), string.end()) {}
73 
74 DataMapping::~DataMapping() = default;
75 
76 size_t DataMapping::GetSize() const {
77  return data_.size();
78 }
79 
80 const uint8_t* DataMapping::GetMapping() const {
81  return data_.data();
82 }
83 
84 // NonOwnedMapping
86  size_t size,
87  const ReleaseProc& release_proc)
88  : data_(data), size_(size), release_proc_(release_proc) {}
89 
91  if (release_proc_) {
92  release_proc_(data_, size_);
93  }
94 }
95 
96 size_t NonOwnedMapping::GetSize() const {
97  return size_;
98 }
99 
100 const uint8_t* NonOwnedMapping::GetMapping() const {
101  return data_;
102 }
103 
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 
111  : data_(mapping.data_), size_(mapping.size_) {
112  mapping.data_ = nullptr;
113  mapping.size_ = 0;
114 }
115 
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 
138  uint8_t* result = data_;
139  data_ = nullptr;
140  size_ = 0;
141  return result;
142 }
143 
144 // Symbol Mapping
145 
147  const char* symbol_name)
148  : native_library_(std::move(native_library)) {
149  if (native_library_ && symbol_name != nullptr) {
150  mapping_ = native_library_->ResolveSymbol(symbol_name);
151 
152  if (mapping_ == nullptr) {
153  // Apparently, dart_bootstrap seems to account for the Mac behavior of
154  // requiring the underscore prefixed symbol name on non-Mac platforms as
155  // well. As a fallback, check the underscore prefixed variant of the
156  // symbol name and allow callers to not have handle this on a per platform
157  // toolchain quirk basis.
158 
159  std::stringstream underscore_symbol_name;
160  underscore_symbol_name << "_" << symbol_name;
161  mapping_ =
162  native_library_->ResolveSymbol(underscore_symbol_name.str().c_str());
163  }
164  }
165 }
166 
168 
169 size_t SymbolMapping::GetSize() const {
170  return 0;
171 }
172 
173 const uint8_t* SymbolMapping::GetMapping() const {
174  return mapping_;
175 }
176 
177 } // namespace fml
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
static std::unique_ptr< FileMapping > CreateReadExecute(const std::string &path)
Definition: mapping.cc:42
SymbolMapping(fml::RefPtr< fml::NativeLibrary > native_library, const char *symbol_name)
Definition: mapping.cc:146
const uint8_t * GetMapping() const override
Definition: mapping.cc:173
std::function< void(const uint8_t *data, size_t size)> ReleaseProc
Definition: mapping.h:107
const uint8_t * ResolveSymbol(const char *symbol)
Definition: ref_ptr.h:252
~NonOwnedMapping() override
Definition: mapping.cc:90
GAsyncResult * result
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
uint8_t * Release()
Definition: mapping.cc:137
~SymbolMapping() override
Definition: ascii_trie.cc:9
size_t GetSize() const override
Definition: mapping.cc:129
const uint8_t * GetMapping() const override
Definition: mapping.cc:133
~MallocMapping() override
Definition: mapping.cc:116
size_t GetSize() const override
Definition: mapping.cc:96
size_t GetSize() const override
Definition: mapping.cc:76
const uint8_t * GetMapping() const override
Definition: mapping.cc:80
DataMapping(std::vector< uint8_t > data)
Definition: mapping.cc:69
size_t length
~DataMapping() override
static MallocMapping Copy(const T *begin, const T *end)
Definition: mapping.h:147
#define FML_CHECK(condition)
Definition: logging.h:68
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition: mapping.h:129
uint8_t * GetMutableMapping()
Definition: mapping.cc:14
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:65
NonOwnedMapping(const uint8_t *data, size_t size, const ReleaseProc &release_proc=nullptr)
Definition: mapping.cc:85
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition: mapping.cc:18
size_t GetSize() const override
Definition: mapping.cc:169
const uint8_t * GetMapping() const override
Definition: mapping.cc:100