forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping.cc
177 lines (136 loc) · 4.51 KB
/
mapping.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/mapping.h"
#include <algorithm>
#include <sstream>
namespace fml {
// FileMapping
uint8_t* FileMapping::GetMutableMapping() {
return mutable_mapping_;
}
std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
const std::string& path) {
return CreateReadOnly(OpenFile(path.c_str(), false, FilePermission::kRead),
"");
}
std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
const fml::UniqueFD& base_fd,
const std::string& sub_path) {
if (sub_path.size() != 0) {
return CreateReadOnly(
OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
}
auto mapping = std::make_unique<FileMapping>(
base_fd, std::initializer_list<Protection>{Protection::kRead});
if (!mapping->IsValid()) {
return nullptr;
}
return mapping;
}
std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
const std::string& path) {
return CreateReadExecute(
OpenFile(path.c_str(), false, FilePermission::kRead));
}
std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
const fml::UniqueFD& base_fd,
const std::string& sub_path) {
if (sub_path.size() != 0) {
return CreateReadExecute(
OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
}
auto mapping = std::make_unique<FileMapping>(
base_fd, std::initializer_list<Protection>{Protection::kRead,
Protection::kExecute});
if (!mapping->IsValid()) {
return nullptr;
}
return mapping;
}
// Data Mapping
DataMapping::DataMapping(std::vector<uint8_t> data) : data_(std::move(data)) {}
DataMapping::DataMapping(const std::string& string)
: data_(string.begin(), string.end()) {}
DataMapping::~DataMapping() = default;
size_t DataMapping::GetSize() const {
return data_.size();
}
const uint8_t* DataMapping::GetMapping() const {
return data_.data();
}
// NonOwnedMapping
NonOwnedMapping::NonOwnedMapping(const uint8_t* data,
size_t size,
const ReleaseProc& release_proc)
: data_(data), size_(size), release_proc_(release_proc) {}
NonOwnedMapping::~NonOwnedMapping() {
if (release_proc_) {
release_proc_(data_, size_);
}
}
size_t NonOwnedMapping::GetSize() const {
return size_;
}
const uint8_t* NonOwnedMapping::GetMapping() const {
return data_;
}
// MallocMapping
MallocMapping::MallocMapping() : data_(nullptr), size_(0) {}
MallocMapping::MallocMapping(uint8_t* data, size_t size)
: data_(data), size_(size) {}
MallocMapping::MallocMapping(fml::MallocMapping&& mapping)
: data_(mapping.data_), size_(mapping.size_) {
mapping.data_ = nullptr;
mapping.size_ = 0;
}
MallocMapping::~MallocMapping() {
free(data_);
data_ = nullptr;
}
MallocMapping MallocMapping::Copy(const void* begin, size_t length) {
auto result =
MallocMapping(reinterpret_cast<uint8_t*>(malloc(length)), length);
FML_CHECK(result.GetMapping() != nullptr);
memcpy(const_cast<uint8_t*>(result.GetMapping()), begin, length);
return result;
}
size_t MallocMapping::GetSize() const {
return size_;
}
const uint8_t* MallocMapping::GetMapping() const {
return data_;
}
uint8_t* MallocMapping::Release() {
uint8_t* result = data_;
data_ = nullptr;
size_ = 0;
return result;
}
// Symbol Mapping
SymbolMapping::SymbolMapping(fml::RefPtr<fml::NativeLibrary> native_library,
const char* symbol_name)
: native_library_(std::move(native_library)) {
if (native_library_ && symbol_name != nullptr) {
mapping_ = native_library_->ResolveSymbol(symbol_name);
if (mapping_ == nullptr) {
// Apparently, dart_bootstrap seems to account for the Mac behavior of
// requiring the underscore prefixed symbol name on non-Mac platforms as
// well. As a fallback, check the underscore prefixed variant of the
// symbol name and allow callers to not have handle this on a per platform
// toolchain quirk basis.
std::stringstream underscore_symbol_name;
underscore_symbol_name << "_" << symbol_name;
mapping_ =
native_library_->ResolveSymbol(underscore_symbol_name.str().c_str());
}
}
}
SymbolMapping::~SymbolMapping() = default;
size_t SymbolMapping::GetSize() const {
return 0;
}
const uint8_t* SymbolMapping::GetMapping() const {
return mapping_;
}
} // namespace fml