v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
snapshot-source-sink.h
Go to the documentation of this file.
1// Copyright 2012 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_SNAPSHOT_SOURCE_SINK_H_
6#define V8_SNAPSHOT_SNAPSHOT_SOURCE_SINK_H_
7
8#include <utility>
9#include <vector>
10
11#include "src/base/atomicops.h"
12#include "src/base/logging.h"
13#include "src/common/globals.h"
14#include "src/utils/utils.h"
15
16namespace v8 {
17namespace internal {
18
19
25class SnapshotByteSource final {
26 public:
27 SnapshotByteSource(const char* data, int length)
28 : data_(reinterpret_cast<const uint8_t*>(data)),
29 length_(length),
30 position_(0) {}
31
33 : data_(payload.begin()), length_(payload.length()), position_(0) {}
34
38
39 bool HasMore() { return position_ < length_; }
40
41 uint8_t Get() {
43 return data_[position_++];
44 }
45
46 uint8_t Peek() const {
48 return data_[position_];
49 }
50
51 void Advance(int by) { position_ += by; }
52
53 void CopyRaw(void* to, int number_of_bytes) {
54 DCHECK_LE(position_ + number_of_bytes, length_);
55 memcpy(to, data_ + position_, number_of_bytes);
56 position_ += number_of_bytes;
57 }
58
59 void CopySlots(Address* dest, int number_of_slots) {
60 base::AtomicWord* start = reinterpret_cast<base::AtomicWord*>(dest);
61 base::AtomicWord* end = start + number_of_slots;
62 for (base::AtomicWord* p = start; p < end;
63 ++p, position_ += sizeof(base::AtomicWord)) {
65 memcpy(&val, data_ + position_, sizeof(base::AtomicWord));
66 base::Relaxed_Store(p, val);
67 }
68 }
69
70#ifdef V8_COMPRESS_POINTERS
71 void CopySlots(Tagged_t* dest, int number_of_slots) {
72 AtomicTagged_t* start = reinterpret_cast<AtomicTagged_t*>(dest);
73 AtomicTagged_t* end = start + number_of_slots;
74 for (AtomicTagged_t* p = start; p < end;
75 ++p, position_ += sizeof(AtomicTagged_t)) {
77 memcpy(&val, data_ + position_, sizeof(AtomicTagged_t));
78 base::Relaxed_Store(p, val);
79 }
80 }
81#endif
82
83 // Decode a uint30 with run-length encoding. Must have been encoded with
84 // PutUint30.
85 inline uint32_t GetUint30() {
86 // This way of decoding variable-length encoded integers does not
87 // suffer from branch mispredictions.
89 uint32_t answer = data_[position_];
90 answer |= data_[position_ + 1] << 8;
91 answer |= data_[position_ + 2] << 16;
92 answer |= data_[position_ + 3] << 24;
93 int bytes = (answer & 3) + 1;
94 Advance(bytes);
95 uint32_t mask = 0xffffffffu;
96 mask >>= 32 - (bytes << 3);
97 answer &= mask;
98 answer >>= 2;
99 return answer;
100 }
101
102 uint32_t GetUint32() {
103 uint32_t integer;
104 CopyRaw(reinterpret_cast<uint8_t*>(&integer), sizeof(integer));
105 return integer;
106 }
107
108 // Returns length.
109 int GetBlob(const uint8_t** data);
110
111 int position() const { return position_; }
113
114 const uint8_t* data() const { return data_; }
115 int length() const { return length_; }
116
117 private:
118 const uint8_t* data_;
121};
122
129 public:
130 SnapshotByteSink() = default;
131 explicit SnapshotByteSink(int initial_size) : data_(initial_size) {}
132
133 ~SnapshotByteSink() = default;
134
135 void Put(uint8_t b, const char* description) { data_.push_back(b); }
136
137 void PutN(int number_of_bytes, const uint8_t v, const char* description);
138 // Append a uint30 with run-length encoding. Must be decoded with GetUint30.
139 void PutUint30(uint32_t integer, const char* description);
140 void PutUint32(uint32_t integer, const char* description) {
141 PutRaw(reinterpret_cast<uint8_t*>(&integer), sizeof(integer), description);
142 }
143 void PutRaw(const uint8_t* data, int number_of_bytes,
144 const char* description);
145
146 void Append(const SnapshotByteSink& other);
147 int Position() const { return static_cast<int>(data_.size()); }
148
149 const std::vector<uint8_t>* data() const { return &data_; }
150
151 private:
152 std::vector<uint8_t> data_;
153};
154
155} // namespace internal
156} // namespace v8
157
158#endif // V8_SNAPSHOT_SNAPSHOT_SOURCE_SINK_H_
void PutN(int number_of_bytes, const uint8_t v, const char *description)
void PutUint32(uint32_t integer, const char *description)
void Append(const SnapshotByteSink &other)
void PutUint30(uint32_t integer, const char *description)
void PutRaw(const uint8_t *data, int number_of_bytes, const char *description)
void Put(uint8_t b, const char *description)
const std::vector< uint8_t > * data() const
void CopySlots(Address *dest, int number_of_slots)
SnapshotByteSource(const SnapshotByteSource &)=delete
SnapshotByteSource & operator=(const SnapshotByteSource &)=delete
SnapshotByteSource(const char *data, int length)
SnapshotByteSource(base::Vector< const uint8_t > payload)
void CopyRaw(void *to, int number_of_bytes)
int start
int end
uint32_t const mask
void Relaxed_Store(volatile Atomic8 *ptr, Atomic8 value)
Definition atomicops.h:189
Atomic32 AtomicWord
Definition atomicops.h:76
Address Tagged_t
Definition globals.h:547
base::AtomicWord AtomicTagged_t
Definition globals.h:548
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_LT(v1, v2)
Definition logging.h:489