snapshot-source-sink.h 2.78 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_SNAPSHOT_SNAPSHOT_SOURCE_SINK_H_
#define V8_SNAPSHOT_SNAPSHOT_SOURCE_SINK_H_
7

8
#include "src/base/logging.h"
9 10 11 12 13 14 15 16 17 18 19
#include "src/utils.h"

namespace v8 {
namespace internal {


/**
 * Source to read snapshot and builtins files from.
 *
 * Note: Memory ownership remains with callee.
 */
20
class SnapshotByteSource final {
21
 public:
22 23 24 25
  SnapshotByteSource(const char* data, int length)
      : data_(reinterpret_cast<const byte*>(data)),
        length_(length),
        position_(0) {}
26 27 28 29

  explicit SnapshotByteSource(Vector<const byte> payload)
      : data_(payload.start()), length_(payload.length()), position_(0) {}

30
  ~SnapshotByteSource() = default;
31 32 33

  bool HasMore() { return position_ < length_; }

34
  byte Get() {
35
    DCHECK(position_ < length_);
36 37 38 39 40
    return data_[position_++];
  }

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

41
  void CopyRaw(void* to, int number_of_bytes) {
42 43 44
    memcpy(to, data_ + position_, number_of_bytes);
    position_ += number_of_bytes;
  }
45 46

  inline int GetInt() {
47 48 49 50 51 52 53
    // This way of decoding variable-length encoded integers does not
    // suffer from branch mispredictions.
    DCHECK(position_ + 3 < length_);
    uint32_t answer = data_[position_];
    answer |= data_[position_ + 1] << 8;
    answer |= data_[position_ + 2] << 16;
    answer |= data_[position_ + 3] << 24;
54
    int bytes = (answer & 3) + 1;
55 56 57 58 59 60 61 62
    Advance(bytes);
    uint32_t mask = 0xffffffffu;
    mask >>= 32 - (bytes << 3);
    answer &= mask;
    answer >>= 2;
    return answer;
  }

63 64
  // Returns length.
  int GetBlob(const byte** data);
65 66

  int position() { return position_; }
67
  void set_position(int position) { position_ = position; }
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

 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:
85
  SnapshotByteSink() = default;
86 87
  explicit SnapshotByteSink(int initial_size) : data_(initial_size) {}

88
  ~SnapshotByteSink() = default;
89

90
  void Put(byte b, const char* description) { data_.push_back(b); }
91 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);
99 100

  void Append(const SnapshotByteSink& other);
101
  int Position() const { return static_cast<int>(data_.size()); }
102

103
  const std::vector<byte>* data() const { return &data_; }
104 105

 private:
106
  std::vector<byte> data_;
107 108
};

109
}  // namespace internal
110 111
}  // namespace v8

112
#endif  // V8_SNAPSHOT_SNAPSHOT_SOURCE_SINK_H_