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