V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
snapshot-source-sink.h
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 "src/base/logging.h"
9 #include "src/utils.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 
20 class SnapshotByteSource final {
21  public:
22  SnapshotByteSource(const char* data, int length)
23  : data_(reinterpret_cast<const byte*>(data)),
24  length_(length),
25  position_(0) {}
26 
27  explicit SnapshotByteSource(Vector<const byte> payload)
28  : data_(payload.start()), length_(payload.length()), position_(0) {}
29 
30  ~SnapshotByteSource() = default;
31 
32  bool HasMore() { return position_ < length_; }
33 
34  byte Get() {
35  DCHECK(position_ < length_);
36  return data_[position_++];
37  }
38 
39  void Advance(int by) { position_ += by; }
40 
41  void CopyRaw(byte* to, int number_of_bytes) {
42  memcpy(to, data_ + position_, number_of_bytes);
43  position_ += number_of_bytes;
44  }
45 
46  inline int GetInt() {
47  // This way of decoding variable-length encoded integers does not
48  // suffer from branch mispredictions.
49  DCHECK(position_ + 3 < length_);
50  uint32_t answer = data_[position_];
51  answer |= data_[position_ + 1] << 8;
52  answer |= data_[position_ + 2] << 16;
53  answer |= data_[position_ + 3] << 24;
54  int bytes = (answer & 3) + 1;
55  Advance(bytes);
56  uint32_t mask = 0xffffffffu;
57  mask >>= 32 - (bytes << 3);
58  answer &= mask;
59  answer >>= 2;
60  return answer;
61  }
62 
63  // Returns length.
64  int GetBlob(const byte** data);
65 
66  int position() { return position_; }
67  void set_position(int position) { position_ = position; }
68 
69  private:
70  const byte* data_;
71  int length_;
72  int position_;
73 
74  DISALLOW_COPY_AND_ASSIGN(SnapshotByteSource);
75 };
76 
77 
84  public:
85  SnapshotByteSink() = default;
86  explicit SnapshotByteSink(int initial_size) : data_(initial_size) {}
87 
88  ~SnapshotByteSink() = default;
89 
90  void Put(byte b, const char* description) { data_.push_back(b); }
91 
92  void PutSection(int b, const char* description) {
93  DCHECK_LE(b, kMaxUInt8);
94  Put(static_cast<byte>(b), description);
95  }
96 
97  void PutInt(uintptr_t integer, const char* description);
98  void PutRaw(const byte* data, int number_of_bytes, const char* description);
99 
100  void Append(const SnapshotByteSink& other);
101  int Position() const { return static_cast<int>(data_.size()); }
102 
103  const std::vector<byte>* data() const { return &data_; }
104 
105  private:
106  std::vector<byte> data_;
107 };
108 
109 } // namespace internal
110 } // namespace v8
111 
112 #endif // V8_SNAPSHOT_SNAPSHOT_SOURCE_SINK_H_
Definition: libplatform.h:13