summaryrefslogtreecommitdiff
path: root/deps/v8/src/snapshot-source-sink.h
blob: c1a31b5645fec748a9d562538b611601dddf41ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_SNAPSHOT_SOURCE_SINK_H_
#define V8_SNAPSHOT_SOURCE_SINK_H_

#include "src/base/logging.h"
#include "src/utils.h"

namespace v8 {
namespace internal {


/**
 * Source to read snapshot and builtins files from.
 *
 * Note: Memory ownership remains with callee.
 */
class SnapshotByteSource FINAL {
 public:
  SnapshotByteSource(const byte* array, int length);
  ~SnapshotByteSource();

  bool HasMore() { return position_ < length_; }

  int Get() {
    DCHECK(position_ < length_);
    return data_[position_++];
  }

  int32_t GetUnalignedInt();

  void Advance(int by) { position_ += by; }

  void CopyRaw(byte* to, int number_of_bytes);

  inline int GetInt() {
    // This way of variable-length encoding integers does not suffer from branch
    // mispredictions.
    uint32_t answer = GetUnalignedInt();
    int bytes = (answer & 3) + 1;
    Advance(bytes);
    uint32_t mask = 0xffffffffu;
    mask >>= 32 - (bytes << 3);
    answer &= mask;
    answer >>= 2;
    return answer;
  }

  bool GetBlob(const byte** data, int* number_of_bytes);

  bool AtEOF();

  int position() { return position_; }

 private:
  const byte* data_;
  int length_;
  int position_;

  DISALLOW_COPY_AND_ASSIGN(SnapshotByteSource);
};


/**
 * Sink to write snapshot files to.
 *
 * Subclasses must implement actual storage or i/o.
 */
class SnapshotByteSink {
 public:
  virtual ~SnapshotByteSink() { }
  virtual void Put(byte b, const char* description) = 0;
  virtual void PutSection(int b, const char* description) {
    DCHECK_LE(b, kMaxUInt8);
    Put(static_cast<byte>(b), description);
  }
  void PutInt(uintptr_t integer, const char* description);
  void PutRaw(byte* data, int number_of_bytes, const char* description);
  void PutBlob(byte* data, int number_of_bytes, const char* description);
  virtual int Position() = 0;
};


class DummySnapshotSink : public SnapshotByteSink {
 public:
  DummySnapshotSink() : length_(0) {}
  virtual ~DummySnapshotSink() {}
  virtual void Put(byte b, const char* description) { length_++; }
  virtual int Position() { return length_; }

 private:
  int length_;
};


// Wrap a SnapshotByteSink into a DebugSnapshotSink to get debugging output.
class DebugSnapshotSink : public SnapshotByteSink {
 public:
  explicit DebugSnapshotSink(SnapshotByteSink* chained) : sink_(chained) {}
  virtual void Put(byte b, const char* description) OVERRIDE;
  virtual int Position() OVERRIDE { return sink_->Position(); }

 private:
  SnapshotByteSink* sink_;
};


class ListSnapshotSink : public i::SnapshotByteSink {
 public:
  explicit ListSnapshotSink(i::List<byte>* data) : data_(data) {}
  virtual void Put(byte b, const char* description) OVERRIDE {
    data_->Add(b);
  }
  virtual int Position() OVERRIDE { return data_->length(); }

 private:
  i::List<byte>* data_;
};

}  // namespace v8::internal
}  // namespace v8

#endif  // V8_SNAPSHOT_SOURCE_SINK_H_