forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping.h
150 lines (106 loc) · 3.2 KB
/
mapping.h
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
// 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.
#ifndef FLUTTER_FML_MAPPING_H_
#define FLUTTER_FML_MAPPING_H_
#include <initializer_list>
#include <memory>
#include <string>
#include <vector>
#include "flutter/fml/build_config.h"
#include "flutter/fml/file.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/native_library.h"
#include "flutter/fml/unique_fd.h"
namespace fml {
class Mapping {
public:
Mapping();
virtual ~Mapping();
virtual size_t GetSize() const = 0;
virtual const uint8_t* GetMapping() const = 0;
private:
FML_DISALLOW_COPY_AND_ASSIGN(Mapping);
};
class FileMapping final : public Mapping {
public:
enum class Protection {
kRead,
kWrite,
kExecute,
};
FileMapping(const fml::UniqueFD& fd,
std::initializer_list<Protection> protection = {
Protection::kRead});
~FileMapping() override;
static std::unique_ptr<FileMapping> CreateReadOnly(const std::string& path);
static std::unique_ptr<FileMapping> CreateReadOnly(
const fml::UniqueFD& base_fd,
const std::string& sub_path = "");
static std::unique_ptr<FileMapping> CreateReadExecute(
const std::string& path);
static std::unique_ptr<FileMapping> CreateReadExecute(
const fml::UniqueFD& base_fd,
const std::string& sub_path = "");
// |Mapping|
size_t GetSize() const override;
// |Mapping|
const uint8_t* GetMapping() const override;
uint8_t* GetMutableMapping();
bool IsValid() const;
private:
bool valid_ = false;
size_t size_ = 0;
uint8_t* mapping_ = nullptr;
uint8_t* mutable_mapping_ = nullptr;
#if OS_WIN
fml::UniqueFD mapping_handle_;
#endif
FML_DISALLOW_COPY_AND_ASSIGN(FileMapping);
};
class DataMapping final : public Mapping {
public:
DataMapping(std::vector<uint8_t> data);
DataMapping(const std::string& string);
~DataMapping() override;
// |Mapping|
size_t GetSize() const override;
// |Mapping|
const uint8_t* GetMapping() const override;
private:
std::vector<uint8_t> data_;
FML_DISALLOW_COPY_AND_ASSIGN(DataMapping);
};
class NonOwnedMapping final : public Mapping {
public:
using ReleaseProc = std::function<void(const uint8_t* data, size_t size)>;
NonOwnedMapping(const uint8_t* data,
size_t size,
const ReleaseProc& release_proc = nullptr);
~NonOwnedMapping() override;
// |Mapping|
size_t GetSize() const override;
// |Mapping|
const uint8_t* GetMapping() const override;
private:
const uint8_t* const data_;
const size_t size_;
const ReleaseProc release_proc_;
FML_DISALLOW_COPY_AND_ASSIGN(NonOwnedMapping);
};
class SymbolMapping final : public Mapping {
public:
SymbolMapping(fml::RefPtr<fml::NativeLibrary> native_library,
const char* symbol_name);
~SymbolMapping() override;
// |Mapping|
size_t GetSize() const override;
// |Mapping|
const uint8_t* GetMapping() const override;
private:
fml::RefPtr<fml::NativeLibrary> native_library_;
const uint8_t* mapping_ = nullptr;
FML_DISALLOW_COPY_AND_ASSIGN(SymbolMapping);
};
} // namespace fml
#endif // FLUTTER_FML_MAPPING_H_