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