e0290c9415b01274a1d4e22670e46b3bcf13a270
[platform/upstream/v8.git] / src / snapshot / 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
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         position_(0) {}
26
27   explicit SnapshotByteSource(Vector<const byte> payload)
28       : data_(payload.start()), length_(payload.length()), position_(0) {}
29
30   ~SnapshotByteSource() {}
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
43   inline int GetInt() {
44     // This way of decoding variable-length encoded integers does not
45     // suffer from branch mispredictions.
46     DCHECK(position_ + 3 < length_);
47     uint32_t answer = data_[position_];
48     answer |= data_[position_ + 1] << 8;
49     answer |= data_[position_ + 2] << 16;
50     answer |= data_[position_ + 3] << 24;
51     int bytes = (answer & 3) + 1;
52     Advance(bytes);
53     uint32_t mask = 0xffffffffu;
54     mask >>= 32 - (bytes << 3);
55     answer &= mask;
56     answer >>= 2;
57     return answer;
58   }
59
60   bool GetBlob(const byte** data, int* number_of_bytes);
61
62   bool AtEOF();
63
64   int position() { return position_; }
65
66  private:
67   const byte* data_;
68   int length_;
69   int position_;
70
71   DISALLOW_COPY_AND_ASSIGN(SnapshotByteSource);
72 };
73
74
75 /**
76  * Sink to write snapshot files to.
77  *
78  * Subclasses must implement actual storage or i/o.
79  */
80 class SnapshotByteSink {
81  public:
82   SnapshotByteSink() {}
83   explicit SnapshotByteSink(int initial_size) : data_(initial_size) {}
84
85   ~SnapshotByteSink() {}
86
87   void Put(byte b, const char* description) { data_.Add(b); }
88
89   void PutSection(int b, const char* description) {
90     DCHECK_LE(b, kMaxUInt8);
91     Put(static_cast<byte>(b), description);
92   }
93
94   void PutInt(uintptr_t integer, const char* description);
95   void PutRaw(const byte* data, int number_of_bytes, const char* description);
96   int Position() { return data_.length(); }
97
98   const List<byte>& data() const { return data_; }
99
100  private:
101   List<byte> data_;
102 };
103
104 }  // namespace v8::internal
105 }  // namespace v8
106
107 #endif  // V8_SNAPSHOT_SNAPSHOT_SOURCE_SINK_H_