Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 } | 255 } |
| 256 | 256 |
| 257 private: | 257 private: |
| 258 const byte* str_; | 258 const byte* str_; |
| 259 const byte* end_; | 259 const byte* end_; |
| 260 }; | 260 }; |
| 261 | 261 |
| 262 | 262 |
| 263 // A Deserializer reads a snapshot and reconstructs the Object graph it defines. | 263 // A Deserializer reads a snapshot and reconstructs the Object graph it defines. |
| 264 | 264 |
| 265 class Deserializer: public ObjectVisitor { | 265 |
| 266 // TODO(erikcorry): Get rid of this superclass when we are using the new | |
| 267 // snapshot code exclusively. | |
| 268 class GenericDeserializer: public ObjectVisitor { | |
| 269 public: | |
| 270 virtual void GetLog() = 0; | |
| 271 virtual void Deserialize() = 0; | |
| 272 }; | |
| 273 | |
| 274 | |
| 275 // TODO(erikcorry): Get rid of this class. | |
| 276 class Deserializer: public GenericDeserializer { | |
| 266 public: | 277 public: |
| 267 // Create a deserializer. The snapshot is held in str and has size len. | 278 // Create a deserializer. The snapshot is held in str and has size len. |
| 268 Deserializer(const byte* str, int len); | 279 Deserializer(const byte* str, int len); |
| 269 | 280 |
| 270 virtual ~Deserializer(); | 281 virtual ~Deserializer(); |
| 271 | 282 |
| 272 // Read the flags from the header of the file, and set those that | 283 // Read the flags from the header of the file, and set those that |
| 273 // should be inherited from the snapshot. | 284 // should be inherited from the snapshot. |
| 274 void GetFlags(); | 285 void GetFlags(); |
| 275 | 286 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 | 343 |
| 333 ExternalReferenceDecoder* reference_decoder_; | 344 ExternalReferenceDecoder* reference_decoder_; |
| 334 | 345 |
| 335 #ifdef DEBUG | 346 #ifdef DEBUG |
| 336 bool expect_debug_information_; | 347 bool expect_debug_information_; |
| 337 #endif | 348 #endif |
| 338 | 349 |
| 339 DISALLOW_COPY_AND_ASSIGN(Deserializer); | 350 DISALLOW_COPY_AND_ASSIGN(Deserializer); |
| 340 }; | 351 }; |
| 341 | 352 |
| 353 | |
| 354 class SnapshotByteSource { | |
| 355 public: | |
| 356 SnapshotByteSource(const byte* array, int length) | |
|
Mads Ager (chromium)
2009/10/26 11:14:05
Please add spaces between the methods in this clas
| |
| 357 : data_(array), length_(length), position_(0) { } | |
| 358 bool HasMore() { return position_ < length_; } | |
| 359 int Get() { | |
| 360 ASSERT(position_ < length_); | |
| 361 return data_[position_++]; | |
| 362 } | |
| 363 int GetInt() { | |
| 364 // A little unwind to catch the really small ints. | |
| 365 int snapshot_byte = Get(); | |
| 366 if ((snapshot_byte & 0x80) == 0) { | |
| 367 return snapshot_byte; | |
| 368 } | |
| 369 uintptr_t accumulator = (snapshot_byte & 0x7f) << 7; | |
| 370 while (true) { | |
| 371 snapshot_byte = Get(); | |
| 372 if ((snapshot_byte & 0x80) == 0) { | |
| 373 return accumulator | snapshot_byte; | |
| 374 } | |
| 375 accumulator = (accumulator | (snapshot_byte & 0x7f)) << 7; | |
| 376 } | |
| 377 return accumulator; | |
|
Mads Ager (chromium)
2009/10/26 11:14:05
Put an UNREACHABLE() in front of this return as do
| |
| 378 } | |
| 379 bool AtEOF() { | |
| 380 return position_ == length_; | |
| 381 } | |
| 382 | |
| 383 private: | |
| 384 const byte* data_; | |
| 385 int length_; | |
| 386 int position_; | |
| 387 }; | |
| 388 | |
| 389 | |
| 390 // The SerDes class is a common superclass for Serializer2 and Deserializer2 | |
| 391 // which is used to store common constants and methods used by both. | |
| 392 // TODO(erikcorry): This should inherit from ObjectVisitor. | |
| 393 class SerDes: public GenericDeserializer { | |
| 394 protected: | |
| 395 enum DataType { | |
| 396 SMI_SERIALIZATION, | |
| 397 RAW_DATA_SERIALIZATION, | |
| 398 OBJECT_SERIALIZATION, | |
| 399 CODE_OBJECT_SERIALIZATION, | |
| 400 BACKREF_SERIALIZATION, | |
| 401 CODE_BACKREF_SERIALIZATION, | |
| 402 EXTERNAL_REFERENCE_SERIALIZATION, | |
| 403 SYNCHRONIZE | |
| 404 }; | |
| 405 static const int kSmiBias = 16; | |
|
Mads Ager (chromium)
2009/10/26 11:14:05
Could you add a comment about the bias?
| |
| 406 static const int kLargeData = LAST_SPACE; | |
| 407 static const int kLargeCode = kLargeData + 1; | |
| 408 static const int kLargeFixedArray = kLargeCode + 1; | |
| 409 static const int kNumberOfSpaces = kLargeFixedArray + 1; | |
| 410 | |
| 411 static inline bool SpaceIsLarge(int space) { return space >= kLargeData; } | |
| 412 static inline bool SpaceIsPaged(int space) { | |
| 413 return space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE; | |
| 414 } | |
| 415 }; | |
| 416 | |
| 417 | |
| 418 | |
| 419 // A Deserializer reads a snapshot and reconstructs the Object graph it defines. | |
| 420 class Deserializer2: public SerDes { | |
| 421 public: | |
| 422 // Create a deserializer. The snapshot is held in str and has size len. | |
|
Mads Ager (chromium)
2009/10/26 11:14:05
Create a deserializer from a byte source? The res
| |
| 423 explicit Deserializer2(SnapshotByteSource* source); | |
| 424 | |
| 425 virtual ~Deserializer2() { } | |
| 426 | |
| 427 // Deserialize the snapshot into an empty heap. | |
| 428 void Deserialize(); | |
| 429 void GetLog() { } // TODO(erikcorry): Get rid of this. | |
| 430 #ifdef DEBUG | |
| 431 virtual void Synchronize(const char* tag); | |
| 432 #endif | |
| 433 | |
| 434 private: | |
| 435 virtual void VisitPointers(Object** start, Object** end); | |
| 436 virtual void VisitExternalReferences(Address* start, Address* end) { | |
|
Mads Ager (chromium)
2009/10/26 11:14:05
I would add space before and after the methods tha
| |
| 437 UNREACHABLE(); | |
| 438 } | |
| 439 virtual void VisitRuntimeEntry(RelocInfo* rinfo) { | |
| 440 UNREACHABLE(); | |
| 441 } | |
| 442 int CurrentAllocationAddress(int space) { | |
| 443 // The three different kinds of large objects have different tags in the | |
| 444 // snapshot so the deserializer knows which kind of object to allocate, | |
| 445 // but they share a fullness_ entry. | |
| 446 if (SpaceIsLarge(space)) space = LO_SPACE; | |
| 447 return fullness_[space]; | |
| 448 } | |
| 449 | |
| 450 HeapObject* GetAddress(int space); | |
| 451 Address Allocate(int space, int size); | |
| 452 bool ReadObject(Object** write_back); | |
| 453 | |
| 454 // Keep track of the pages in the paged spaces. | |
| 455 // (In large object space we are keeping track of individual objects | |
| 456 // rather than pages.) In new space we just need the address of the | |
| 457 // first object and the others will flow from that. | |
| 458 List<Address> pages_[SerDes::kNumberOfSpaces]; | |
| 459 | |
| 460 SnapshotByteSource* source_; | |
| 461 ExternalReferenceDecoder* external_reference_decoder_; | |
| 462 // Keep track of the fullness of each space in order to generate | |
| 463 // relative addresses for back references. Large objects are | |
| 464 // just numbered sequentially since relative addresses make no | |
| 465 // sense in large object space. | |
| 466 int fullness_[LAST_SPACE + 1]; | |
| 467 | |
| 468 DISALLOW_COPY_AND_ASSIGN(Deserializer2); | |
| 469 }; | |
| 470 | |
| 471 | |
| 472 class SnapshotByteSink { | |
| 473 public: | |
| 474 virtual ~SnapshotByteSink() { } | |
| 475 virtual void Put(int byte, const char* description) = 0; | |
| 476 void PutInt(uintptr_t integer, const char* description) { | |
| 477 const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7; | |
|
Mads Ager (chromium)
2009/10/26 11:14:05
Move to implementation file?
| |
| 478 for (int shift = max_shift; shift > 0; shift -= 7) { | |
| 479 if (integer >= 1u << shift) { | |
| 480 Put(((integer >> shift) & 0x7f) | 0x80, "intpart"); | |
| 481 } | |
| 482 } | |
| 483 Put(integer & 0x7f, "intlastpart"); | |
| 484 } | |
| 485 }; | |
| 486 | |
| 487 | |
| 488 class Serializer2 : public SerDes { | |
| 489 public: | |
| 490 explicit Serializer2(SnapshotByteSink* sink); | |
| 491 // Serialize the current state of the heap. This operation destroys the | |
| 492 // heap contents. | |
| 493 void Serialize(); | |
| 494 void VisitPointers(Object** start, Object** end); | |
| 495 void Finalize(int* y) {} | |
|
Mads Ager (chromium)
2009/10/26 11:14:05
Add a comment? What does Finalize do? What is y?
| |
| 496 void GetLog() { } // TODO(erikcorry): Get rid of this. | |
| 497 void Deserialize() { } // TODO(erikcorry): Get rid of this. | |
| 498 #ifdef DEBUG | |
| 499 virtual void Synchronize(const char* tag); | |
| 500 #endif | |
| 501 | |
| 502 private: | |
| 503 enum ReferenceRepresentation { | |
| 504 TAGGED_REPRESENTATION, // Deserialize as a tagged object reference. | |
|
Mads Ager (chromium)
2009/10/26 11:14:05
How about just "Tagged object reference."?
| |
| 505 CODE_TARGET_REPRESENTATION // Deserialize a reference to first instruction. | |
|
Mads Ager (chromium)
2009/10/26 11:14:05
How about just "Reference to first instruction in
| |
| 506 }; | |
| 507 class ObjectSerializer : public ObjectVisitor { | |
| 508 public: | |
| 509 ObjectSerializer(Serializer2* serializer, | |
| 510 Object* o, | |
| 511 SnapshotByteSink* sink, | |
| 512 ReferenceRepresentation representation) | |
| 513 : serializer_(serializer), | |
| 514 object_(HeapObject::cast(o)), | |
| 515 sink_(sink), | |
| 516 reference_representation_(representation), | |
| 517 bytes_processed_so_far_(0) { } | |
| 518 void Serialize(); | |
| 519 void VisitPointers(Object** start, Object** end); | |
| 520 void VisitExternalReferences(Address* start, Address* end); | |
| 521 void VisitCodeTarget(RelocInfo* target); | |
| 522 | |
| 523 private: | |
| 524 void OutputRawData(Address up_to); | |
| 525 | |
| 526 Serializer2* serializer_; | |
| 527 HeapObject* object_; | |
| 528 SnapshotByteSink* sink_; | |
| 529 ReferenceRepresentation reference_representation_; | |
| 530 int bytes_processed_so_far_; | |
| 531 }; | |
| 532 | |
| 533 void SerializeObject(Object* o, ReferenceRepresentation representation); | |
| 534 void InitializeAllocators(); | |
| 535 // This will return the space for an object. If the object is in large | |
| 536 // object space it may return kLargeCode or kLargeFixedArray in order | |
| 537 // to indicate to the deserializer what kind of large object allocation | |
| 538 // to make. | |
| 539 static int SpaceOfObject(HeapObject* object); | |
| 540 // This just returns the space of the object. It will return LO_SPACE | |
| 541 // for all large objects since you can't check the type of the object | |
| 542 // once the map has been used for the serialization address. | |
| 543 static int SpaceOfAlreadySerializedObject(HeapObject* object); | |
| 544 int Allocate(int space, int size); | |
| 545 int CurrentAllocationAddress(int space) { | |
| 546 if (SpaceIsLarge(space)) space = LO_SPACE; | |
| 547 return fullness_[space]; | |
| 548 } | |
| 549 int EncodeExternalReference(Address addr) { | |
| 550 return external_reference_encoder_->Encode(addr); | |
| 551 } | |
| 552 | |
| 553 // Keep track of the fullness of each space in order to generate | |
| 554 // relative addresses for back references. Large objects are | |
| 555 // just numbered sequentially since relative addresses make no | |
| 556 // sense in large object space. | |
| 557 int fullness_[LAST_SPACE + 1]; | |
| 558 SnapshotByteSink* sink_; | |
| 559 int current_root_index_; | |
| 560 ExternalReferenceEncoder* external_reference_encoder_; | |
| 561 | |
| 562 friend class ObjectSerializer; | |
| 563 friend class Deserializer2; | |
| 564 | |
| 565 DISALLOW_COPY_AND_ASSIGN(Serializer2); | |
| 566 }; | |
| 567 | |
| 342 } } // namespace v8::internal | 568 } } // namespace v8::internal |
| 343 | 569 |
| 344 #endif // V8_SERIALIZE_H_ | 570 #endif // V8_SERIALIZE_H_ |
| OLD | NEW |