Flutter Engine
The Flutter Engine
mapping.h
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#ifndef FLUTTER_FML_MAPPING_H_
6#define FLUTTER_FML_MAPPING_H_
7
8#include <initializer_list>
9#include <memory>
10#include <string>
11#include <vector>
12
13#include "flutter/fml/build_config.h"
14#include "flutter/fml/file.h"
15#include "flutter/fml/macros.h"
16#include "flutter/fml/native_library.h"
17#include "flutter/fml/unique_fd.h"
18
19namespace fml {
20
21class Mapping {
22 public:
24
25 virtual ~Mapping();
26
27 virtual size_t GetSize() const = 0;
28
29 virtual const uint8_t* GetMapping() const = 0;
30
31 // Whether calling madvise(DONTNEED) on the mapping is non-destructive.
32 // Generally true for file-mapped memory and false for anonymous memory.
33 virtual bool IsDontNeedSafe() const = 0;
34
35 private:
36 FML_DISALLOW_COPY_AND_ASSIGN(Mapping);
37};
38
39class FileMapping final : public Mapping {
40 public:
41 enum class Protection {
42 kRead,
43 kWrite,
44 kExecute,
45 };
46
47 explicit FileMapping(const fml::UniqueFD& fd,
48 std::initializer_list<Protection> protection = {
50
51 ~FileMapping() override;
52
53 static std::unique_ptr<FileMapping> CreateReadOnly(const std::string& path);
54
55 static std::unique_ptr<FileMapping> CreateReadOnly(
56 const fml::UniqueFD& base_fd,
57 const std::string& sub_path = "");
58
59 static std::unique_ptr<FileMapping> CreateReadExecute(
60 const std::string& path);
61
62 static std::unique_ptr<FileMapping> CreateReadExecute(
63 const fml::UniqueFD& base_fd,
64 const std::string& sub_path = "");
65
66 // |Mapping|
67 size_t GetSize() const override;
68
69 // |Mapping|
70 const uint8_t* GetMapping() const override;
71
72 // |Mapping|
73 bool IsDontNeedSafe() const override;
74
75 uint8_t* GetMutableMapping();
76
77 bool IsValid() const;
78
79 private:
80 bool valid_ = false;
81 size_t size_ = 0;
82 uint8_t* mapping_ = nullptr;
83 uint8_t* mutable_mapping_ = nullptr;
84
85#if FML_OS_WIN
86 fml::UniqueFD mapping_handle_;
87#endif
88
89 FML_DISALLOW_COPY_AND_ASSIGN(FileMapping);
90};
91
92class DataMapping final : public Mapping {
93 public:
94 explicit DataMapping(std::vector<uint8_t> data);
95
96 explicit DataMapping(const std::string& string);
97
98 ~DataMapping() override;
99
100 // |Mapping|
101 size_t GetSize() const override;
102
103 // |Mapping|
104 const uint8_t* GetMapping() const override;
105
106 // |Mapping|
107 bool IsDontNeedSafe() const override;
108
109 private:
110 std::vector<uint8_t> data_;
111
112 FML_DISALLOW_COPY_AND_ASSIGN(DataMapping);
113};
114
115class NonOwnedMapping final : public Mapping {
116 public:
117 using ReleaseProc = std::function<void(const uint8_t* data, size_t size)>;
118 NonOwnedMapping(const uint8_t* data,
119 size_t size,
120 const ReleaseProc& release_proc = nullptr,
121 bool dontneed_safe = false);
122
123 ~NonOwnedMapping() override;
124
125 // |Mapping|
126 size_t GetSize() const override;
127
128 // |Mapping|
129 const uint8_t* GetMapping() const override;
130
131 // |Mapping|
132 bool IsDontNeedSafe() const override;
133
134 private:
135 const uint8_t* const data_;
136 const size_t size_;
137 const ReleaseProc release_proc_;
138 const bool dontneed_safe_;
139
140 FML_DISALLOW_COPY_AND_ASSIGN(NonOwnedMapping);
141};
142
143/// A Mapping like NonOwnedMapping, but uses Free as its release proc.
144class MallocMapping final : public Mapping {
145 public:
147
148 /// Creates a MallocMapping for a region of memory (without copying it).
149 /// The function will `abort()` if the malloc fails.
150 /// @param data The starting address of the mapping.
151 /// @param size The size of the mapping in bytes.
152 MallocMapping(uint8_t* data, size_t size);
153
155
156 ~MallocMapping() override;
157
158 /// Copies the data from `begin` to `end`.
159 /// It's templated since void* arithemetic isn't allowed and we want support
160 /// for `uint8_t` and `char`.
161 template <typename T>
162 static MallocMapping Copy(const T* begin, const T* end) {
163 FML_DCHECK(end >= begin);
164 size_t length = end - begin;
165 return Copy(begin, length);
166 }
167
168 /// Copies a region of memory into a MallocMapping.
169 /// The function will `abort()` if the malloc fails.
170 /// @param begin The starting address of where we will copy.
171 /// @param length The length of the region to copy in bytes.
172 static MallocMapping Copy(const void* begin, size_t length);
173
174 // |Mapping|
175 size_t GetSize() const override;
176
177 // |Mapping|
178 const uint8_t* GetMapping() const override;
179
180 // |Mapping|
181 bool IsDontNeedSafe() const override;
182
183 /// Removes ownership of the data buffer.
184 /// After this is called; the mapping will point to nullptr.
185 [[nodiscard]] uint8_t* Release();
186
187 private:
188 uint8_t* data_;
189 size_t size_;
190
191 FML_DISALLOW_COPY_AND_ASSIGN(MallocMapping);
192};
193
194class SymbolMapping final : public Mapping {
195 public:
197 const char* symbol_name);
198
199 ~SymbolMapping() override;
200
201 // |Mapping|
202 size_t GetSize() const override;
203
204 // |Mapping|
205 const uint8_t* GetMapping() const override;
206
207 // |Mapping|
208 bool IsDontNeedSafe() const override;
209
210 private:
211 fml::RefPtr<fml::NativeLibrary> native_library_;
212 const uint8_t* mapping_ = nullptr;
213
214 FML_DISALLOW_COPY_AND_ASSIGN(SymbolMapping);
215};
216
217} // namespace fml
218
219#endif // FLUTTER_FML_MAPPING_H_
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
FileMapping(const fml::UniqueFD &fd, std::initializer_list< Protection > protection={ Protection::kRead})
static std::unique_ptr< FileMapping > CreateReadExecute(const std::string &path)
Definition: mapping.cc:44
~FileMapping() override
size_t GetSize() const override
bool IsDontNeedSafe() const override
uint8_t * GetMutableMapping()
Definition: mapping.cc:16
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition: mapping.cc:20
bool IsValid() const
const uint8_t * GetMapping() const override
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
virtual bool IsDontNeedSafe() const =0
virtual const uint8_t * GetMapping() const =0
virtual ~Mapping()
virtual size_t GetSize() const =0
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
#define FML_DCHECK(condition)
Definition: logging.h:103
Dart_NativeFunction function
Definition: fuchsia.cc:51
size_t length
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
#define T
Definition: precompiler.cc:65
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63