forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dart_snapshot_buffer.cc
94 lines (70 loc) · 2.73 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
// Copyright 2017 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 FileSnapshotBuffer final : public DartSnapshotBuffer {
public:
FileSnapshotBuffer(const char* path, bool executable)
: mapping_(path, executable) {
if (mapping_.GetSize() > 0) {
symbol_ = mapping_.GetMapping();
}
}
const uint8_t* GetSnapshotPointer() const override { return symbol_; }
size_t GetSnapshotSize() const override { return mapping_.GetSize(); }
private:
fml::FileMapping mapping_;
const uint8_t* symbol_ = nullptr;
FML_DISALLOW_COPY_AND_ASSIGN(FileSnapshotBuffer);
};
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 char* file_path,
bool executable) {
auto source = std::make_unique<FileSnapshotBuffer>(file_path, executable);
return source->GetSnapshotPointer() == nullptr ? nullptr : std::move(source);
}
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