Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
snapshot.h
Go to the documentation of this file.
1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_SNAPSHOT_H_
6#define RUNTIME_VM_SNAPSHOT_H_
7
8#include <memory>
9#include <utility>
10
11#include "platform/assert.h"
12#include "platform/unaligned.h"
13#include "vm/allocation.h"
14#include "vm/globals.h"
15#include "vm/message.h"
16#include "vm/thread.h"
17
18namespace dart {
19
20// Structure capturing the raw snapshot.
21//
22class Snapshot {
23 public:
24 enum Kind {
25 kFull, // Full snapshot of an application.
26 kFullCore, // Full snapshot of core libraries.
27 kFullJIT, // Full + JIT code
28 kFullAOT, // Full + AOT code
29 kNone, // gen_snapshot
31 };
32 static const char* KindToCString(Kind kind);
33
34 static const Snapshot* SetupFromBuffer(const void* raw_memory);
35
36 static constexpr int32_t kMagicValue = 0xdcdcf5f5;
37 static constexpr intptr_t kMagicOffset = 0;
38 static constexpr intptr_t kMagicSize = sizeof(int32_t);
39 static constexpr intptr_t kLengthOffset = kMagicOffset + kMagicSize;
40 static constexpr intptr_t kLengthSize = sizeof(int64_t);
41 static constexpr intptr_t kKindOffset = kLengthOffset + kLengthSize;
42 static constexpr intptr_t kKindSize = sizeof(int64_t);
43 static constexpr intptr_t kHeaderSize = kKindOffset + kKindSize;
44
45 // Accessors.
46 bool check_magic() const {
47 return Read<int32_t>(kMagicOffset) == kMagicValue;
48 }
49 void set_magic() { return Write<int32_t>(kMagicOffset, kMagicValue); }
50 // Excluding the magic value from the size written in the buffer is needed
51 // so we give a proper version mismatch error for snapshots create before
52 // magic value was written by the VM instead of the embedder.
53 int64_t large_length() const {
54 return Read<int64_t>(kLengthOffset) + kMagicSize;
55 }
56 intptr_t length() const { return static_cast<intptr_t>(large_length()); }
57 void set_length(intptr_t value) {
58 return Write<int64_t>(kLengthOffset, value - kMagicSize);
59 }
60 Kind kind() const { return static_cast<Kind>(Read<int64_t>(kKindOffset)); }
61 void set_kind(Kind value) { return Write<int64_t>(kKindOffset, value); }
62
63 static bool IsFull(Kind kind) {
64 return (kind == kFull) || (kind == kFullCore) || (kind == kFullJIT) ||
65 (kind == kFullAOT);
66 }
67 static bool IncludesCode(Kind kind) {
68 return (kind == kFullJIT) || (kind == kFullAOT);
69 }
70
72#if !defined(DART_COMPRESSED_POINTERS)
73 return IncludesCode(kind);
74#else
75 return false;
76#endif
77 }
78
79 const uint8_t* Addr() const { return reinterpret_cast<const uint8_t*>(this); }
80
81 const uint8_t* DataImage() const {
82 if (!IncludesCode(kind())) {
83 return nullptr;
84 }
86 return Addr() + offset;
87 }
88
89 private:
90 // Prevent Snapshot from ever being allocated directly.
91 Snapshot();
92
93 template <typename T>
94 T Read(intptr_t offset) const {
95 return LoadUnaligned(
96 reinterpret_cast<const T*>(reinterpret_cast<uword>(this) + offset));
97 }
98
99 template <typename T>
100 void Write(intptr_t offset, T value) {
101 return StoreUnaligned(
102 reinterpret_cast<T*>(reinterpret_cast<uword>(this) + offset), value);
103 }
104
105 DISALLOW_COPY_AND_ASSIGN(Snapshot);
106};
107
108inline static bool IsSnapshotCompatible(Snapshot::Kind vm_kind,
109 Snapshot::Kind isolate_kind) {
110 if (vm_kind == isolate_kind) return true;
111 if (((vm_kind == Snapshot::kFull) || (vm_kind == Snapshot::kFullCore)) &&
112 isolate_kind == Snapshot::kFullJIT)
113 return true;
114 return Snapshot::IsFull(isolate_kind);
115}
116
118 public:
120 : StackResource(Thread::Current()), message_(nullptr) {}
121
122 void set_message(std::unique_ptr<Message> message) {
123 ASSERT(message_ == nullptr);
124 message_ = std::move(message);
125 }
126 std::unique_ptr<Message> StealMessage() { return std::move(message_); }
127
128 private:
129 std::unique_ptr<Message> message_;
130};
131
132} // namespace dart
133
134#endif // RUNTIME_VM_SNAPSHOT_H_
void set_message(std::unique_ptr< Message > message)
Definition snapshot.h:122
std::unique_ptr< Message > StealMessage()
Definition snapshot.h:126
Kind kind() const
Definition snapshot.h:60
static bool IsFull(Kind kind)
Definition snapshot.h:63
void set_magic()
Definition snapshot.h:49
bool check_magic() const
Definition snapshot.h:46
static const Snapshot * SetupFromBuffer(const void *raw_memory)
Definition snapshot.cc:30
const uint8_t * Addr() const
Definition snapshot.h:79
static constexpr int32_t kMagicValue
Definition snapshot.h:36
static const char * KindToCString(Kind kind)
Definition snapshot.cc:12
static constexpr intptr_t kMagicSize
Definition snapshot.h:38
static bool IncludesStringsInROData(Kind kind)
Definition snapshot.h:71
intptr_t length() const
Definition snapshot.h:56
static constexpr intptr_t kLengthOffset
Definition snapshot.h:39
static bool IncludesCode(Kind kind)
Definition snapshot.h:67
static constexpr intptr_t kMagicOffset
Definition snapshot.h:37
static constexpr intptr_t kKindOffset
Definition snapshot.h:41
int64_t large_length() const
Definition snapshot.h:53
const uint8_t * DataImage() const
Definition snapshot.h:81
static constexpr intptr_t kLengthSize
Definition snapshot.h:40
void set_length(intptr_t value)
Definition snapshot.h:57
static constexpr intptr_t kHeaderSize
Definition snapshot.h:43
static constexpr intptr_t kKindSize
Definition snapshot.h:42
void set_kind(Kind value)
Definition snapshot.h:61
static constexpr T RoundUp(T x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:105
#define ASSERT(E)
uint8_t value
Win32Message message
static constexpr intptr_t kObjectStartAlignment
uintptr_t uword
Definition globals.h:501
static T LoadUnaligned(const T *ptr)
Definition unaligned.h:14
static void StoreUnaligned(T *ptr, T value)
Definition unaligned.h:22
static bool IsSnapshotCompatible(Snapshot::Kind vm_kind, Snapshot::Kind isolate_kind)
Definition snapshot.h:108
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581
#define T
Point offset