Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | // Returns length. |
| 61 | int GetBlob(const byte** data); |
| 62 | |
| 63 | int position() { return position_; } |
| 64 | |
| 65 | private: |
| 66 | const byte* data_; |
| 67 | int length_; |
| 68 | int position_; |
| 69 | |
| 70 | DISALLOW_COPY_AND_ASSIGN(SnapshotByteSource); |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * Sink to write snapshot files to. |
| 76 | * |
| 77 | * Subclasses must implement actual storage or i/o. |
| 78 | */ |
| 79 | class SnapshotByteSink { |
| 80 | public: |
| 81 | SnapshotByteSink() {} |
| 82 | explicit SnapshotByteSink(int initial_size) : data_(initial_size) {} |
| 83 | |
| 84 | ~SnapshotByteSink() {} |
| 85 | |
| 86 | void Put(byte b, const char* description) { data_.Add(b); } |
| 87 | |
| 88 | void PutSection(int b, const char* description) { |
| 89 | DCHECK_LE(b, kMaxUInt8); |
| 90 | Put(static_cast<byte>(b), description); |
| 91 | } |
| 92 | |
| 93 | void PutInt(uintptr_t integer, const char* description); |
| 94 | void PutRaw(const byte* data, int number_of_bytes, const char* description); |
| 95 | int Position() { return data_.length(); } |
| 96 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 97 | const List<byte>* data() const { return &data_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 98 | |
| 99 | private: |
| 100 | List<byte> data_; |
| 101 | }; |
| 102 | |
| 103 | } // namespace internal |
| 104 | } // namespace v8 |
| 105 | |
| 106 | #endif // V8_SNAPSHOT_SNAPSHOT_SOURCE_SINK_H_ |