| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 #ifndef V8_SERIALIZE_H_ | 5 #ifndef V8_SERIALIZE_H_ |
| 6 #define V8_SERIALIZE_H_ | 6 #define V8_SERIALIZE_H_ |
| 7 | 7 |
| 8 #include "src/hashmap.h" | 8 #include "src/hashmap.h" |
| 9 #include "src/isolate.h" |
| 10 #include "src/snapshot-source-sink.h" |
| 11 #include "src/heap-profiler.h" |
| 9 | 12 |
| 10 namespace v8 { | 13 namespace v8 { |
| 11 namespace internal { | 14 namespace internal { |
| 12 | 15 |
| 13 // A TypeCode is used to distinguish different kinds of external reference. | 16 // A TypeCode is used to distinguish different kinds of external reference. |
| 14 // It is a single bit to make testing for types easy. | 17 // It is a single bit to make testing for types easy. |
| 15 enum TypeCode { | 18 enum TypeCode { |
| 16 UNCLASSIFIED, // One-of-a-kind references. | 19 UNCLASSIFIED, // One-of-a-kind references. |
| 17 BUILTIN, | 20 BUILTIN, |
| 18 RUNTIME_FUNCTION, | 21 RUNTIME_FUNCTION, |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 } | 128 } |
| 126 | 129 |
| 127 void Put(uint32_t key, Address value) { | 130 void Put(uint32_t key, Address value) { |
| 128 *Lookup(key) = value; | 131 *Lookup(key) = value; |
| 129 } | 132 } |
| 130 | 133 |
| 131 Isolate* isolate_; | 134 Isolate* isolate_; |
| 132 }; | 135 }; |
| 133 | 136 |
| 134 | 137 |
| 135 class SnapshotByteSource { | |
| 136 public: | |
| 137 SnapshotByteSource(const byte* array, int length) | |
| 138 : data_(array), length_(length), position_(0) { } | |
| 139 | |
| 140 bool HasMore() { return position_ < length_; } | |
| 141 | |
| 142 int Get() { | |
| 143 ASSERT(position_ < length_); | |
| 144 return data_[position_++]; | |
| 145 } | |
| 146 | |
| 147 int32_t GetUnalignedInt() { | |
| 148 #if defined(V8_HOST_CAN_READ_UNALIGNED) && __BYTE_ORDER == __LITTLE_ENDIAN | |
| 149 int32_t answer; | |
| 150 ASSERT(position_ + sizeof(answer) <= length_ + 0u); | |
| 151 answer = *reinterpret_cast<const int32_t*>(data_ + position_); | |
| 152 #else | |
| 153 int32_t answer = data_[position_]; | |
| 154 answer |= data_[position_ + 1] << 8; | |
| 155 answer |= data_[position_ + 2] << 16; | |
| 156 answer |= data_[position_ + 3] << 24; | |
| 157 #endif | |
| 158 return answer; | |
| 159 } | |
| 160 | |
| 161 void Advance(int by) { position_ += by; } | |
| 162 | |
| 163 inline void CopyRaw(byte* to, int number_of_bytes); | |
| 164 | |
| 165 inline int GetInt(); | |
| 166 | |
| 167 bool AtEOF(); | |
| 168 | |
| 169 int position() { return position_; } | |
| 170 | |
| 171 private: | |
| 172 const byte* data_; | |
| 173 int length_; | |
| 174 int position_; | |
| 175 }; | |
| 176 | |
| 177 | |
| 178 // The Serializer/Deserializer class is a common superclass for Serializer and | 138 // The Serializer/Deserializer class is a common superclass for Serializer and |
| 179 // Deserializer which is used to store common constants and methods used by | 139 // Deserializer which is used to store common constants and methods used by |
| 180 // both. | 140 // both. |
| 181 class SerializerDeserializer: public ObjectVisitor { | 141 class SerializerDeserializer: public ObjectVisitor { |
| 182 public: | 142 public: |
| 183 static void Iterate(Isolate* isolate, ObjectVisitor* visitor); | 143 static void Iterate(Isolate* isolate, ObjectVisitor* visitor); |
| 184 | 144 |
| 185 static int nop() { return kNop; } | 145 static int nop() { return kNop; } |
| 186 | 146 |
| 187 protected: | 147 protected: |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 } | 221 } |
| 262 | 222 |
| 263 static const int kNumberOfSpaces = LO_SPACE; | 223 static const int kNumberOfSpaces = LO_SPACE; |
| 264 static const int kAnyOldSpace = -1; | 224 static const int kAnyOldSpace = -1; |
| 265 | 225 |
| 266 // A bitmask for getting the space out of an instruction. | 226 // A bitmask for getting the space out of an instruction. |
| 267 static const int kSpaceMask = 7; | 227 static const int kSpaceMask = 7; |
| 268 }; | 228 }; |
| 269 | 229 |
| 270 | 230 |
| 271 int SnapshotByteSource::GetInt() { | |
| 272 // This way of variable-length encoding integers does not suffer from branch | |
| 273 // mispredictions. | |
| 274 uint32_t answer = GetUnalignedInt(); | |
| 275 int bytes = answer & 3; | |
| 276 Advance(bytes); | |
| 277 uint32_t mask = 0xffffffffu; | |
| 278 mask >>= 32 - (bytes << 3); | |
| 279 answer &= mask; | |
| 280 answer >>= 2; | |
| 281 return answer; | |
| 282 } | |
| 283 | |
| 284 | |
| 285 void SnapshotByteSource::CopyRaw(byte* to, int number_of_bytes) { | |
| 286 MemCopy(to, data_ + position_, number_of_bytes); | |
| 287 position_ += number_of_bytes; | |
| 288 } | |
| 289 | |
| 290 | |
| 291 // A Deserializer reads a snapshot and reconstructs the Object graph it defines. | 231 // A Deserializer reads a snapshot and reconstructs the Object graph it defines. |
| 292 class Deserializer: public SerializerDeserializer { | 232 class Deserializer: public SerializerDeserializer { |
| 293 public: | 233 public: |
| 294 // Create a deserializer from a snapshot byte source. | 234 // Create a deserializer from a snapshot byte source. |
| 295 explicit Deserializer(SnapshotByteSource* source); | 235 explicit Deserializer(SnapshotByteSource* source); |
| 296 | 236 |
| 297 virtual ~Deserializer(); | 237 virtual ~Deserializer(); |
| 298 | 238 |
| 299 // Deserialize the snapshot into an empty heap. | 239 // Deserialize the snapshot into an empty heap. |
| 300 void Deserialize(Isolate* isolate); | 240 void Deserialize(Isolate* isolate); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 | 301 |
| 362 int reservations_[LAST_SPACE + 1]; | 302 int reservations_[LAST_SPACE + 1]; |
| 363 static const intptr_t kUninitializedReservation = -1; | 303 static const intptr_t kUninitializedReservation = -1; |
| 364 | 304 |
| 365 ExternalReferenceDecoder* external_reference_decoder_; | 305 ExternalReferenceDecoder* external_reference_decoder_; |
| 366 | 306 |
| 367 DISALLOW_COPY_AND_ASSIGN(Deserializer); | 307 DISALLOW_COPY_AND_ASSIGN(Deserializer); |
| 368 }; | 308 }; |
| 369 | 309 |
| 370 | 310 |
| 371 class SnapshotByteSink { | |
| 372 public: | |
| 373 virtual ~SnapshotByteSink() { } | |
| 374 virtual void Put(int byte, const char* description) = 0; | |
| 375 virtual void PutSection(int byte, const char* description) { | |
| 376 Put(byte, description); | |
| 377 } | |
| 378 void PutInt(uintptr_t integer, const char* description); | |
| 379 virtual int Position() = 0; | |
| 380 }; | |
| 381 | |
| 382 | |
| 383 // Mapping objects to their location after deserialization. | 311 // Mapping objects to their location after deserialization. |
| 384 // This is used during building, but not at runtime by V8. | 312 // This is used during building, but not at runtime by V8. |
| 385 class SerializationAddressMapper { | 313 class SerializationAddressMapper { |
| 386 public: | 314 public: |
| 387 SerializationAddressMapper() | 315 SerializationAddressMapper() |
| 388 : no_allocation_(), | 316 : no_allocation_(), |
| 389 serialization_map_(new HashMap(HashMap::PointersMatch)) { } | 317 serialization_map_(new HashMap(HashMap::PointersMatch)) { } |
| 390 | 318 |
| 391 ~SerializationAddressMapper() { | 319 ~SerializationAddressMapper() { |
| 392 delete serialization_map_; | 320 delete serialization_map_; |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 private: | 557 private: |
| 630 virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) { | 558 virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) { |
| 631 return false; | 559 return false; |
| 632 } | 560 } |
| 633 }; | 561 }; |
| 634 | 562 |
| 635 | 563 |
| 636 } } // namespace v8::internal | 564 } } // namespace v8::internal |
| 637 | 565 |
| 638 #endif // V8_SERIALIZE_H_ | 566 #endif // V8_SERIALIZE_H_ |
| OLD | NEW |