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.
5 #ifndef V8_SNAPSHOT_SOURCE_SINK_H_
6 #define V8_SNAPSHOT_SOURCE_SINK_H_
8 #include "src/base/logging.h"
16 * Source to read snapshot and builtins files from.
18 * Note: Memory ownership remains with callee.
20 class SnapshotByteSource FINAL {
22 SnapshotByteSource(const char* data, int length)
23 : data_(reinterpret_cast<const byte*>(data)),
27 explicit SnapshotByteSource(Vector<const byte> payload)
28 : data_(payload.start()), length_(payload.length()), position_(0) {}
30 ~SnapshotByteSource() {}
32 bool HasMore() { return position_ < length_; }
35 DCHECK(position_ < length_);
36 return data_[position_++];
39 void Advance(int by) { position_ += by; }
41 void CopyRaw(byte* to, int number_of_bytes);
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;
53 uint32_t mask = 0xffffffffu;
54 mask >>= 32 - (bytes << 3);
60 bool GetBlob(const byte** data, int* number_of_bytes);
64 int position() { return position_; }
71 DISALLOW_COPY_AND_ASSIGN(SnapshotByteSource);
76 * Sink to write snapshot files to.
78 * Subclasses must implement actual storage or i/o.
80 class SnapshotByteSink {
83 explicit SnapshotByteSink(int initial_size) : data_(initial_size) {}
85 ~SnapshotByteSink() {}
87 void Put(byte b, const char* description) { data_.Add(b); }
89 void PutSection(int b, const char* description) {
90 DCHECK_LE(b, kMaxUInt8);
91 Put(static_cast<byte>(b), description);
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(); }
98 const List<byte>& data() const { return data_; }
104 } // namespace v8::internal
107 #endif // V8_SNAPSHOT_SOURCE_SINK_H_