Line data Source code
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 :
15 : /**
16 : * Source to read snapshot and builtins files from.
17 : *
18 : * Note: Memory ownership remains with callee.
19 : */
20 : class SnapshotByteSource final {
21 : public:
22 : SnapshotByteSource(const char* data, int length)
23 : : data_(reinterpret_cast<const byte*>(data)),
24 : length_(length),
25 59602 : position_(0) {}
26 :
27 : explicit SnapshotByteSource(Vector<const byte> payload)
28 423790 : : 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 2902153681 : return data_[position_++];
37 : }
38 :
39 1358859620 : void Advance(int by) { position_ += by; }
40 :
41 : void CopyRaw(void* to, int number_of_bytes) {
42 570686780 : memcpy(to, data_ + position_, number_of_bytes);
43 570686780 : position_ += number_of_bytes;
44 : }
45 :
46 1358740416 : 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 1358740416 : uint32_t answer = data_[position_];
51 1358740416 : answer |= data_[position_ + 1] << 8;
52 1358740416 : answer |= data_[position_ + 2] << 16;
53 1358740416 : answer |= data_[position_ + 3] << 24;
54 1358740416 : int bytes = (answer & 3) + 1;
55 : Advance(bytes);
56 : uint32_t mask = 0xffffffffu;
57 1358740416 : mask >>= 32 - (bytes << 3);
58 1358740416 : answer &= mask;
59 1358740416 : answer >>= 2;
60 1358740416 : 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 :
78 : /**
79 : * Sink to write snapshot files to.
80 : *
81 : * Subclasses must implement actual storage or i/o.
82 : */
83 : class SnapshotByteSink {
84 : public:
85 : SnapshotByteSink() = default;
86 : explicit SnapshotByteSink(int initial_size) : data_(initial_size) {}
87 :
88 : ~SnapshotByteSink() = default;
89 :
90 19677321 : 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 1954244 : 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 7560 : 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_
|