v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
read-only-serializer-deserializer.h
Go to the documentation of this file.
1// Copyright 2023 the V8 project 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 V8_SNAPSHOT_READ_ONLY_SERIALIZER_DESERIALIZER_H_
6#define V8_SNAPSHOT_READ_ONLY_SERIALIZER_DESERIALIZER_H_
7
9
10namespace v8 {
11namespace internal {
12namespace ro {
13
14// Common functionality for RO serialization and deserialization.
15
17 // kAllocatePage parameters:
18 // Uint30 page_index
19 // Uint30 area_size_in_bytes
21 // kAllocatePageAt parameters:
22 // Uint30 page_index
23 // Uint30 area_size_in_bytes
24 // Uint32 compressed_page_address
26 //
27 // kSegment parameters:
28 // Uint30 page_index
29 // Uint30 offset
30 // Uint30 size_in_bytes
31 // ... segment byte stream
33 //
34 // kRelocateSegment parameters:
35 // ... relocation byte stream
37 //
38 // kReadOnlyRootsTable parameters:
39 // IF_STATIC_ROOTS(... ro roots table slots)
41 //
43};
44static constexpr int kNumberOfBytecodes =
45 static_cast<int>(kFinalizeReadOnlySpace) + 1;
46
47// Like std::vector<bool> but with a known underlying encoding.
48class BitSet final {
49 public:
50 explicit BitSet(size_t size_in_bits)
52 data_(new uint8_t[size_in_bytes()]()),
53 owns_data_(true) {}
54
55 explicit BitSet(uint8_t* data, size_t size_in_bits)
56 : size_in_bits_(size_in_bits), data_(data), owns_data_(false) {}
57
59 if (owns_data_) delete[] data_;
60 }
61
62 bool contains(int i) const {
63 DCHECK(0 <= i && i < static_cast<int>(size_in_bits_));
64 return (data_[chunk_index(i)] & bit_mask(i)) != 0;
65 }
66
67 void set(int i) {
68 DCHECK(0 <= i && i < static_cast<int>(size_in_bits_));
70 }
71
72 size_t size_in_bits() const { return size_in_bits_; }
76
77 const uint8_t* data() const { return data_; }
78
79 private:
80 static constexpr int kBitsPerChunk = kUInt8Size * kBitsPerByte;
81 static constexpr int chunk_index(int i) { return i / kBitsPerChunk; }
82 static constexpr int bit_index(int i) { return i % kBitsPerChunk; }
83 static constexpr uint32_t bit_mask(int i) { return 1 << bit_index(i); }
84
85 const size_t size_in_bits_;
86 uint8_t* const data_;
87 const bool owns_data_;
88};
89
90// Tagged slots need relocation after deserialization when V8_STATIC_ROOTS is
91// disabled.
92//
93// Note this encoding works for all remaining build configs, in particular for
94// all supported kTaggedSize values.
96 static constexpr int kOffsetBits = kPageSizeBits;
97 static constexpr int kSize = kUInt32Size;
98 static constexpr int kPageIndexBits =
99 kSize * 8 - kOffsetBits; // Determines max number of RO pages.
100
101 explicit EncodedTagged(unsigned int page_index, unsigned int offset)
105 }
106
107 uint32_t ToUint32() const {
108 static_assert(kSize == kUInt32Size);
109 return *reinterpret_cast<const uint32_t*>(this);
110 }
111 static EncodedTagged FromUint32(uint32_t v) {
112 return FromAddress(reinterpret_cast<Address>(&v));
113 }
115 return *reinterpret_cast<EncodedTagged*>(address);
116 }
117
118 const unsigned int page_index : kPageIndexBits;
119 const unsigned int offset : kOffsetBits; // Shifted by kTaggedSizeLog2.
120};
121static_assert(EncodedTagged::kSize == sizeof(EncodedTagged));
122
124 static constexpr int kIsApiReferenceBits = 1;
125 static constexpr int kIndexBits = 31;
126 static constexpr int kSize = kUInt32Size;
127
128 uint32_t ToUint32() const {
129 static_assert(kSize == kUInt32Size);
130 return *reinterpret_cast<const uint32_t*>(this);
131 }
133 return *reinterpret_cast<EncodedExternalReference*>(&v);
134 }
135
136 // This ctor is needed to convert parameter types. We can't use bool/uint32_t
137 // as underlying member types since that messes with field packing on
138 // windows.
140 : is_api_reference(is_api_reference), index(index) {}
141
144};
145static_assert(EncodedExternalReference::kSize ==
147
148} // namespace ro
149} // namespace internal
150} // namespace v8
151
152#endif // V8_SNAPSHOT_READ_ONLY_SERIALIZER_DESERIALIZER_H_
constexpr int kPageSizeBits
BitSet(uint8_t *data, size_t size_in_bits)
static constexpr int chunk_index(int i)
static constexpr uint32_t bit_mask(int i)
static constexpr int kNumberOfBytecodes
constexpr int kUInt8Size
Definition globals.h:394
constexpr int kBitsPerByte
Definition globals.h:682
constexpr int kUInt32Size
Definition globals.h:403
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_LT(v1, v2)
Definition logging.h:489
constexpr T RoundUp(T x, intptr_t m)
Definition macros.h:387
static EncodedExternalReference FromUint32(uint32_t v)
EncodedExternalReference(bool is_api_reference, uint32_t index)
EncodedTagged(unsigned int page_index, unsigned int offset)
static EncodedTagged FromAddress(Address address)