// Write C++ code that defines Snapshot::snapshot_ to contain the snapshot
// to the file given by filename. Only the first size chars are written.
static int WriteInternalSnapshotToFile(const char* filename,
- const char* str,
+ const v8::internal::byte* bytes,
int size) {
FILE* f = i::OS::FOpen(filename, "wb");
if (f == NULL) {
fprintf(f, "#include \"platform.h\"\n\n");
fprintf(f, "#include \"snapshot.h\"\n\n");
fprintf(f, "namespace v8 {\nnamespace internal {\n\n");
- fprintf(f, "const char Snapshot::data_[] = {");
+ fprintf(f, "const byte Snapshot::data_[] = {");
int written = 0;
- written += fprintf(f, "%i", str[0]);
+ written += fprintf(f, "0x%x", bytes[0]);
for (int i = 1; i < size; ++i) {
- written += fprintf(f, ",%i", str[i]);
+ written += fprintf(f, ",0x%x", bytes[i]);
// The following is needed to keep the line length low on Visual C++:
if (i % 512 == 0) fprintf(f, "\n");
}
i::Heap::CollectAllGarbage();
i::Serializer ser;
ser.Serialize();
- char* str;
+ v8::internal::byte* bytes;
int len;
- ser.Finalize(&str, &len);
+ ser.Finalize(&bytes, &len);
- WriteInternalSnapshotToFile(argv[1], str, len);
+ WriteInternalSnapshotToFile(argv[1], bytes, len);
- i::DeleteArray(str);
+ i::DeleteArray(bytes);
return 0;
}
SnapshotWriter() {
len_ = 0;
max_ = 8 << 10; // 8K initial size
- str_ = NewArray<char>(max_);
+ str_ = NewArray<byte>(max_);
}
~SnapshotWriter() {
DeleteArray(str_);
}
- void GetString(char** str, int* len) {
- *str = NewArray<char>(len_);
+ void GetBytes(byte** str, int* len) {
+ *str = NewArray<byte>(len_);
memcpy(*str, str_, len_);
*len = len_;
}
Address position() { return reinterpret_cast<Address>(&str_[len_]); }
private:
- char* str_; // the snapshot
+ byte* str_; // the snapshot
int len_; // the current length of str_
int max_; // the allocated size of str_
};
CHECK(0 <= pos && pos <= len_);
while (len_ + bytes >= max_) {
max_ *= 2;
- char* old = str_;
- str_ = NewArray<char>(max_);
+ byte* old = str_;
+ str_ = NewArray<byte>(max_);
memcpy(str_, old, len_);
DeleteArray(old);
}
if (pos < len_) {
- char* old = str_;
- str_ = NewArray<char>(max_);
+ byte* old = str_;
+ str_ = NewArray<byte>(max_);
memcpy(str_, old, pos);
memcpy(str_ + pos + bytes, old + pos, len_ - pos);
DeleteArray(old);
}
-void Serializer::Finalize(char** str, int* len) {
- writer_->GetString(str, len);
+void Serializer::Finalize(byte** str, int* len) {
+ writer_->GetBytes(str, len);
}
static const int kInitArraySize = 32;
-Deserializer::Deserializer(const char* str, int len)
+Deserializer::Deserializer(const byte* str, int len)
: reader_(str, len),
map_pages_(kInitArraySize),
old_pointer_pages_(kInitArraySize),
// Returns the serialized buffer. Ownership is transferred to the
// caller. Only the destructor and getters may be called after this call.
- void Finalize(char** str, int* len);
+ void Finalize(byte** str, int* len);
int roots() { return roots_; }
int objects() { return objects_; }
class SnapshotReader {
public:
- SnapshotReader(const char* str, int len): str_(str), end_(str + len) {}
+ SnapshotReader(const byte* str, int len): str_(str), end_(str + len) {}
void ExpectC(char expected) {
int c = GetC();
}
private:
- const char* str_;
- const char* end_;
+ const byte* str_;
+ const byte* end_;
};
class Deserializer: public ObjectVisitor {
public:
// Create a deserializer. The snapshot is held in str and has size len.
- Deserializer(const char* str, int len);
+ Deserializer(const byte* str, int len);
virtual ~Deserializer();
namespace v8 { namespace internal {
-bool Snapshot::Deserialize(const char* content, int len) {
+bool Snapshot::Deserialize(const byte* content, int len) {
Deserializer des(content, len);
des.GetFlags();
return V8::Initialize(&des);
bool Snapshot::Initialize(const char* snapshot_file) {
if (snapshot_file) {
int len;
- char* str = ReadChars(snapshot_file, &len);
+ byte* str = ReadBytes(snapshot_file, &len);
if (!str) return false;
bool result = Deserialize(str, len);
DeleteArray(str);
bool Snapshot::WriteToFile(const char* snapshot_file) {
Serializer ser;
ser.Serialize();
- char* str;
+ byte* str;
int len;
ser.Finalize(&str, &len);
- int written = WriteChars(snapshot_file, str, len);
+ int written = WriteBytes(snapshot_file, str, len);
DeleteArray(str);
return written == len;
namespace v8 { namespace internal {
-const char Snapshot::data_[] = { 0 };
+const byte Snapshot::data_[] = { 0 };
int Snapshot::size_ = 0;
} } // namespace v8::internal
static bool WriteToFile(const char* snapshot_file);
private:
- static const char data_[];
+ static const byte data_[];
static int size_;
- static bool Deserialize(const char* content, int len);
+ static bool Deserialize(const byte* content, int len);
DISALLOW_IMPLICIT_CONSTRUCTORS(Snapshot);
};
}
-char* ReadChars(const char* filename, int* size, bool verbose) {
- return ReadCharsFromFile(filename, size, 0, verbose);
+byte* ReadBytes(const char* filename, int* size, bool verbose) {
+ char* chars = ReadCharsFromFile(filename, size, 0, verbose);
+ return reinterpret_cast<byte*>(chars);
}
}
+int WriteBytes(const char* filename,
+ const byte* bytes,
+ int size,
+ bool verbose) {
+ const char* str = reinterpret_cast<const char*>(bytes);
+ return WriteChars(filename, str, size, verbose);
+}
+
+
StringBuilder::StringBuilder(int size) {
buffer_ = Vector<char>::New(size);
position_ = 0;
char* ReadLine(const char* prompt);
-// Read and return the raw chars in a file. the size of the buffer is returned
+// Read and return the raw bytes in a file. the size of the buffer is returned
// in size.
-// The returned buffer is not 0-terminated. It must be freed by the caller.
-char* ReadChars(const char* filename, int* size, bool verbose = true);
+// The returned buffer must be freed by the caller.
+byte* ReadBytes(const char* filename, int* size, bool verbose = true);
// Write size chars from str to the file given by filename.
bool verbose = true);
+// Write size bytes to the file given by filename.
+// The file is overwritten. Returns the number of bytes written.
+int WriteBytes(const char* filename,
+ const byte* bytes,
+ int size,
+ bool verbose = true);
+
+
// Write the C code
// const char* <varname> = "<str>";
// const int <varname>_len = <len>;