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
85 
87  size_t size,
88  const ReleaseProc& release_proc)
89  : data_(data), size_(size), release_proc_(release_proc) {}
90 
92  if (release_proc_) {
93  release_proc_(data_, size_);
94  }
95 }
96 
97 size_t NonOwnedMapping::GetSize() const {
98  return size_;
99 }
100 
101 const uint8_t* NonOwnedMapping::GetMapping() const {
102  return data_;
103 }
104 
105 // Symbol Mapping
106 
108  const char* symbol_name)
109  : native_library_(std::move(native_library)) {
110  if (native_library_ && symbol_name != nullptr) {
111  mapping_ = native_library_->ResolveSymbol(symbol_name);
112 
113  if (mapping_ == nullptr) {
114  // Apparently, dart_bootstrap seems to account for the Mac behavior of
115  // requiring the underscore prefixed symbol name on non-Mac platforms as
116  // well. As a fallback, check the underscore prefixed variant of the
117  // symbol name and allow callers to not have handle this on a per platform
118  // toolchain quirk basis.
119 
120  std::stringstream underscore_symbol_name;
121  underscore_symbol_name << "_" << symbol_name;
122  mapping_ =
123  native_library_->ResolveSymbol(underscore_symbol_name.str().c_str());
124  }
125  }
126 }
127 
129 
130 size_t SymbolMapping::GetSize() const {
131  return 0;
132 }
133 
134 const uint8_t* SymbolMapping::GetMapping() const {
135  return mapping_;
136 }
137 
138 } // 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:107
const uint8_t * GetMapping() const override
Definition: mapping.cc:134
std::function< void(const uint8_t *data, size_t size)> ReleaseProc
Definition: mapping.h:107
Definition: ref_ptr.h:252
~NonOwnedMapping() override
Definition: mapping.cc:91
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
~SymbolMapping() override
Definition: ascii_trie.cc:9
size_t GetSize() const override
Definition: mapping.cc:97
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
~DataMapping() override
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:64
const uint8_t * ResolveSymbol(const char *symbol)
NonOwnedMapping(const uint8_t *data, size_t size, const ReleaseProc &release_proc=nullptr)
Definition: mapping.cc:86
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition: mapping.cc:18
size_t GetSize() const override
Definition: mapping.cc:130
const uint8_t * GetMapping() const override
Definition: mapping.cc:101