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 byte* array, int length);
23 ~SnapshotByteSource();
25 bool HasMore() { return position_ < length_; }
28 DCHECK(position_ < length_);
29 return data_[position_++];
32 int32_t GetUnalignedInt();
34 void Advance(int by) { position_ += by; }
36 void CopyRaw(byte* to, int number_of_bytes);
39 // This way of variable-length encoding integers does not suffer from branch
41 uint32_t answer = GetUnalignedInt();
42 int bytes = answer & 3;
44 uint32_t mask = 0xffffffffu;
45 mask >>= 32 - (bytes << 3);
51 bool GetBlob(const byte** data, int* number_of_bytes);
55 int position() { return position_; }
62 DISALLOW_COPY_AND_ASSIGN(SnapshotByteSource);
67 * Sink to write snapshot files to.
69 * Subclasses must implement actual storage or i/o.
71 class SnapshotByteSink {
73 virtual ~SnapshotByteSink() { }
74 virtual void Put(byte b, const char* description) = 0;
75 virtual void PutSection(int b, const char* description) {
76 DCHECK_LE(b, kMaxUInt8);
77 Put(static_cast<byte>(b), description);
79 void PutInt(uintptr_t integer, const char* description);
80 void PutRaw(byte* data, int number_of_bytes, const char* description);
81 void PutBlob(byte* data, int number_of_bytes, const char* description);
82 virtual int Position() = 0;
86 class DummySnapshotSink : public SnapshotByteSink {
88 DummySnapshotSink() : length_(0) {}
89 virtual ~DummySnapshotSink() {}
90 virtual void Put(byte b, const char* description) { length_++; }
91 virtual int Position() { return length_; }
98 // Wrap a SnapshotByteSink into a DebugSnapshotSink to get debugging output.
99 class DebugSnapshotSink : public SnapshotByteSink {
101 explicit DebugSnapshotSink(SnapshotByteSink* chained) : sink_(chained) {}
102 virtual void Put(byte b, const char* description) OVERRIDE;
103 virtual int Position() OVERRIDE { return sink_->Position(); }
106 SnapshotByteSink* sink_;
110 class ListSnapshotSink : public i::SnapshotByteSink {
112 explicit ListSnapshotSink(i::List<byte>* data) : data_(data) {}
113 virtual void Put(byte b, const char* description) OVERRIDE {
116 virtual int Position() OVERRIDE { return data_->length(); }
119 i::List<byte>* data_;
122 } // namespace v8::internal
125 #endif // V8_SNAPSHOT_SOURCE_SINK_H_