forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dart_snapshot_buffer.cc
102 lines (77 loc) · 2.91 KB
/
dart_snapshot_buffer.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
// 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/runtime/dart_snapshot_buffer.h"
#include <utility>
#include "flutter/fml/mapping.h"
namespace blink {
class NativeLibrarySnapshotBuffer final : public DartSnapshotBuffer {
public:
NativeLibrarySnapshotBuffer(fml::RefPtr<fml::NativeLibrary> library,
const char* symbol_name)
: library_(std::move(library)) {
if (library_) {
symbol_ = library_->ResolveSymbol(symbol_name);
}
}
const uint8_t* GetSnapshotPointer() const override { return symbol_; }
size_t GetSnapshotSize() const override { return 0; }
private:
fml::RefPtr<fml::NativeLibrary> library_;
const uint8_t* symbol_ = nullptr;
FML_DISALLOW_COPY_AND_ASSIGN(NativeLibrarySnapshotBuffer);
};
class MappingBuffer final : public DartSnapshotBuffer {
public:
MappingBuffer(std::unique_ptr<fml::Mapping> mapping)
: mapping_(std::move(mapping)) {
FML_DCHECK(mapping_);
}
const uint8_t* GetSnapshotPointer() const override {
return mapping_->GetMapping();
}
size_t GetSnapshotSize() const override { return mapping_->GetSize(); }
private:
std::unique_ptr<fml::Mapping> mapping_;
FML_DISALLOW_COPY_AND_ASSIGN(MappingBuffer);
};
class UnmanagedAllocation final : public DartSnapshotBuffer {
public:
UnmanagedAllocation(const uint8_t* allocation) : allocation_(allocation) {}
const uint8_t* GetSnapshotPointer() const override { return allocation_; }
size_t GetSnapshotSize() const override { return 0; }
private:
const uint8_t* allocation_;
FML_DISALLOW_COPY_AND_ASSIGN(UnmanagedAllocation);
};
std::unique_ptr<DartSnapshotBuffer>
DartSnapshotBuffer::CreateWithSymbolInLibrary(
fml::RefPtr<fml::NativeLibrary> library,
const char* symbol_name) {
auto source = std::make_unique<NativeLibrarySnapshotBuffer>(
std::move(library), symbol_name);
return source->GetSnapshotPointer() == nullptr ? nullptr : std::move(source);
}
std::unique_ptr<DartSnapshotBuffer>
DartSnapshotBuffer::CreateWithContentsOfFile(
const fml::UniqueFD& fd,
std::initializer_list<fml::FileMapping::Protection> protection) {
return CreateWithMapping(std::make_unique<fml::FileMapping>(fd, protection));
}
std::unique_ptr<DartSnapshotBuffer> DartSnapshotBuffer::CreateWithMapping(
std::unique_ptr<fml::Mapping> mapping) {
if (mapping == nullptr || mapping->GetSize() == 0 ||
mapping->GetMapping() == nullptr) {
return nullptr;
}
return std::make_unique<MappingBuffer>(std::move(mapping));
}
std::unique_ptr<DartSnapshotBuffer>
DartSnapshotBuffer::CreateWithUnmanagedAllocation(const uint8_t* allocation) {
if (allocation == nullptr) {
return nullptr;
}
return std::make_unique<UnmanagedAllocation>(allocation);
}
DartSnapshotBuffer::~DartSnapshotBuffer() = default;
} // namespace blink