2014-06-23 13:52:17 +00:00
|
|
|
// 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_
|
|
|
|
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/logging.h"
|
2014-06-23 13:52:17 +00:00
|
|
|
#include "src/utils.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Source to read snapshot and builtins files from.
|
|
|
|
*
|
|
|
|
* Note: Memory ownership remains with callee.
|
|
|
|
*/
|
2014-09-02 07:07:52 +00:00
|
|
|
class SnapshotByteSource FINAL {
|
2014-06-23 13:52:17 +00:00
|
|
|
public:
|
|
|
|
SnapshotByteSource(const byte* array, int length);
|
|
|
|
~SnapshotByteSource();
|
|
|
|
|
|
|
|
bool HasMore() { return position_ < length_; }
|
|
|
|
|
|
|
|
int Get() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(position_ < length_);
|
2014-06-23 13:52:17 +00:00
|
|
|
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();
|
2014-09-25 07:32:13 +00:00
|
|
|
int bytes = (answer & 3) + 1;
|
2014-06-23 13:52:17 +00:00
|
|
|
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() { }
|
2014-07-10 10:28:05 +00:00
|
|
|
virtual void Put(byte b, const char* description) = 0;
|
|
|
|
virtual void PutSection(int b, const char* description) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_LE(b, kMaxUInt8);
|
2014-07-10 10:28:05 +00:00
|
|
|
Put(static_cast<byte>(b), description);
|
2014-06-23 13:52:17 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-08 09:04:08 +00:00
|
|
|
class DummySnapshotSink : public SnapshotByteSink {
|
|
|
|
public:
|
|
|
|
DummySnapshotSink() : length_(0) {}
|
|
|
|
virtual ~DummySnapshotSink() {}
|
2014-07-10 10:28:05 +00:00
|
|
|
virtual void Put(byte b, const char* description) { length_++; }
|
2014-07-08 09:04:08 +00:00
|
|
|
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) {}
|
2014-09-02 07:07:52 +00:00
|
|
|
virtual void Put(byte b, const char* description) OVERRIDE;
|
|
|
|
virtual int Position() OVERRIDE { return sink_->Position(); }
|
2014-07-08 09:04:08 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
SnapshotByteSink* sink_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ListSnapshotSink : public i::SnapshotByteSink {
|
|
|
|
public:
|
2014-07-10 10:28:05 +00:00
|
|
|
explicit ListSnapshotSink(i::List<byte>* data) : data_(data) {}
|
2014-09-02 07:07:52 +00:00
|
|
|
virtual void Put(byte b, const char* description) OVERRIDE {
|
2014-07-10 10:28:05 +00:00
|
|
|
data_->Add(b);
|
2014-07-08 09:04:08 +00:00
|
|
|
}
|
2014-09-02 07:07:52 +00:00
|
|
|
virtual int Position() OVERRIDE { return data_->length(); }
|
2014-07-08 09:04:08 +00:00
|
|
|
|
|
|
|
private:
|
2014-07-10 10:28:05 +00:00
|
|
|
i::List<byte>* data_;
|
2014-07-08 09:04:08 +00:00
|
|
|
};
|
|
|
|
|
2014-06-23 13:52:17 +00:00
|
|
|
} // namespace v8::internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // V8_SNAPSHOT_SOURCE_SINK_H_
|