Change type of snapshot from char array to byte array to avoid portability problems between different compilers.
Review URL: http://codereview.chromium.org/18583 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1145 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
8faf0da7b6
commit
dd5cda5dde
@ -114,7 +114,7 @@ static int* counter_callback(const char* name) {
|
||||
// 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) {
|
||||
@ -126,11 +126,11 @@ static int WriteInternalSnapshotToFile(const char* filename,
|
||||
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");
|
||||
}
|
||||
@ -174,13 +174,13 @@ int main(int argc, char** argv) {
|
||||
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;
|
||||
}
|
||||
|
@ -686,15 +686,15 @@ class SnapshotWriter {
|
||||
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_;
|
||||
}
|
||||
@ -742,7 +742,7 @@ class SnapshotWriter {
|
||||
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_
|
||||
};
|
||||
@ -752,14 +752,14 @@ void SnapshotWriter::Reserve(int bytes, int pos) {
|
||||
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);
|
||||
@ -929,8 +929,8 @@ void Serializer::Serialize() {
|
||||
}
|
||||
|
||||
|
||||
void Serializer::Finalize(char** str, int* len) {
|
||||
writer_->GetString(str, len);
|
||||
void Serializer::Finalize(byte** str, int* len) {
|
||||
writer_->GetBytes(str, len);
|
||||
}
|
||||
|
||||
|
||||
@ -1180,7 +1180,7 @@ RelativeAddress Serializer::Allocate(HeapObject* obj) {
|
||||
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),
|
||||
|
@ -135,7 +135,7 @@ class Serializer: public ObjectVisitor {
|
||||
|
||||
// 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_; }
|
||||
@ -211,7 +211,7 @@ class Serializer: public ObjectVisitor {
|
||||
|
||||
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();
|
||||
@ -247,8 +247,8 @@ class SnapshotReader {
|
||||
}
|
||||
|
||||
private:
|
||||
const char* str_;
|
||||
const char* end_;
|
||||
const byte* str_;
|
||||
const byte* end_;
|
||||
};
|
||||
|
||||
|
||||
@ -257,7 +257,7 @@ class SnapshotReader {
|
||||
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();
|
||||
|
||||
|
@ -35,7 +35,7 @@
|
||||
|
||||
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);
|
||||
@ -45,7 +45,7 @@ bool Snapshot::Deserialize(const char* content, int len) {
|
||||
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);
|
||||
@ -60,11 +60,11 @@ bool Snapshot::Initialize(const char* snapshot_file) {
|
||||
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;
|
||||
|
@ -33,7 +33,7 @@
|
||||
|
||||
namespace v8 { namespace internal {
|
||||
|
||||
const char Snapshot::data_[] = { 0 };
|
||||
const byte Snapshot::data_[] = { 0 };
|
||||
int Snapshot::size_ = 0;
|
||||
|
||||
} } // namespace v8::internal
|
||||
|
@ -45,10 +45,10 @@ class Snapshot {
|
||||
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);
|
||||
};
|
||||
|
14
src/utils.cc
14
src/utils.cc
@ -182,8 +182,9 @@ char* ReadCharsFromFile(const char* filename,
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -233,6 +234,15 @@ int WriteChars(const char* filename,
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
|
14
src/utils.h
14
src/utils.h
@ -224,10 +224,10 @@ void Flush();
|
||||
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.
|
||||
@ -238,6 +238,14 @@ int WriteChars(const char* 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>;
|
||||
|
Loading…
Reference in New Issue
Block a user