| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 #include "src/snapshot-source-sink.h" | 6 #include "src/snapshot-source-sink.h" |
| 7 | 7 |
| 8 #include "src/base/logging.h" | 8 #include "src/base/logging.h" |
| 9 #include "src/handles-inl.h" | 9 #include "src/handles-inl.h" |
| 10 #include "src/serialize.h" // for SerializerDeserializer::nop() in AtEOF() | 10 #include "src/serialize.h" // for SerializerDeserializer::nop() in AtEOF() |
| 11 | 11 |
| 12 | 12 |
| 13 namespace v8 { | 13 namespace v8 { |
| 14 namespace internal { | 14 namespace internal { |
| 15 | 15 |
| 16 | 16 |
| 17 SnapshotByteSource::SnapshotByteSource(const byte* array, int length) | 17 SnapshotByteSource::SnapshotByteSource(Vector<const byte> payload) |
| 18 : data_(array), length_(length), position_(0) { | 18 : data_(payload.start()), length_(payload.length()), position_(0) {} |
| 19 } | |
| 20 | 19 |
| 21 | 20 |
| 22 SnapshotByteSource::~SnapshotByteSource() { } | 21 SnapshotByteSource::~SnapshotByteSource() { } |
| 23 | 22 |
| 24 | 23 |
| 25 int32_t SnapshotByteSource::GetUnalignedInt() { | 24 int32_t SnapshotByteSource::GetUnalignedInt() { |
| 26 DCHECK(position_ < length_); // Require at least one byte left. | 25 DCHECK(position_ < length_); // Require at least one byte left. |
| 27 int32_t answer = data_[position_]; | 26 int32_t answer = data_[position_]; |
| 28 answer |= data_[position_ + 1] << 8; | 27 answer |= data_[position_ + 1] << 8; |
| 29 answer |= data_[position_ + 2] << 16; | 28 answer |= data_[position_ + 2] << 16; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 45 if (integer > 0xff) bytes = 2; | 44 if (integer > 0xff) bytes = 2; |
| 46 if (integer > 0xffff) bytes = 3; | 45 if (integer > 0xffff) bytes = 3; |
| 47 if (integer > 0xffffff) bytes = 4; | 46 if (integer > 0xffffff) bytes = 4; |
| 48 integer |= (bytes - 1); | 47 integer |= (bytes - 1); |
| 49 Put(static_cast<int>(integer & 0xff), "IntPart1"); | 48 Put(static_cast<int>(integer & 0xff), "IntPart1"); |
| 50 if (bytes > 1) Put(static_cast<int>((integer >> 8) & 0xff), "IntPart2"); | 49 if (bytes > 1) Put(static_cast<int>((integer >> 8) & 0xff), "IntPart2"); |
| 51 if (bytes > 2) Put(static_cast<int>((integer >> 16) & 0xff), "IntPart3"); | 50 if (bytes > 2) Put(static_cast<int>((integer >> 16) & 0xff), "IntPart3"); |
| 52 if (bytes > 3) Put(static_cast<int>((integer >> 24) & 0xff), "IntPart4"); | 51 if (bytes > 3) Put(static_cast<int>((integer >> 24) & 0xff), "IntPart4"); |
| 53 } | 52 } |
| 54 | 53 |
| 55 void SnapshotByteSink::PutRaw(byte* data, int number_of_bytes, | 54 |
| 55 void SnapshotByteSink::PutRaw(const byte* data, int number_of_bytes, |
| 56 const char* description) { | 56 const char* description) { |
| 57 data_.AddAll(Vector<byte>(data, number_of_bytes)); | 57 data_.AddAll(Vector<byte>(const_cast<byte*>(data), number_of_bytes)); |
| 58 } | |
| 59 | |
| 60 void SnapshotByteSink::PutBlob(byte* data, int number_of_bytes, | |
| 61 const char* description) { | |
| 62 PutInt(number_of_bytes, description); | |
| 63 PutRaw(data, number_of_bytes, description); | |
| 64 } | 58 } |
| 65 | 59 |
| 66 | 60 |
| 61 void SnapshotByteSink::PutBlob(Vector<const byte> blob, |
| 62 const char* description) { |
| 63 PutInt(blob.length(), description); |
| 64 PutRaw(blob.start(), blob.length(), description); |
| 65 } |
| 66 |
| 67 |
| 67 bool SnapshotByteSource::AtEOF() { | 68 bool SnapshotByteSource::AtEOF() { |
| 68 if (0u + length_ - position_ > 2 * sizeof(uint32_t)) return false; | 69 if (0u + length_ - position_ > 2 * sizeof(uint32_t)) return false; |
| 69 for (int x = position_; x < length_; x++) { | 70 for (int x = position_; x < length_; x++) { |
| 70 if (data_[x] != SerializerDeserializer::nop()) return false; | 71 if (data_[x] != SerializerDeserializer::nop()) return false; |
| 71 } | 72 } |
| 72 return true; | 73 return true; |
| 73 } | 74 } |
| 74 | 75 |
| 75 | 76 |
| 76 bool SnapshotByteSource::GetBlob(const byte** data, int* number_of_bytes) { | 77 bool SnapshotByteSource::GetBlob(const byte** data, int* number_of_bytes) { |
| 77 int size = GetInt(); | 78 int size = GetInt(); |
| 78 *number_of_bytes = size; | 79 *number_of_bytes = size; |
| 79 | 80 |
| 80 if (position_ + size < length_) { | 81 if (position_ + size < length_) { |
| 81 *data = &data_[position_]; | 82 *data = &data_[position_]; |
| 82 Advance(size); | 83 Advance(size); |
| 83 return true; | 84 return true; |
| 84 } else { | 85 } else { |
| 85 Advance(length_ - position_); // proceed until end. | 86 Advance(length_ - position_); // proceed until end. |
| 86 return false; | 87 return false; |
| 87 } | 88 } |
| 88 } | 89 } |
| 89 | 90 |
| 90 } // namespace v8::internal | 91 } // namespace v8::internal |
| 91 } // namespace v8 | 92 } // namespace v8 |
| OLD | NEW |