OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_SNAPSHOT_H_ | 5 #ifndef VM_SNAPSHOT_H_ |
6 #define VM_SNAPSHOT_H_ | 6 #define VM_SNAPSHOT_H_ |
7 | 7 |
8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
9 #include "vm/allocation.h" | 9 #include "vm/allocation.h" |
10 #include "vm/bitfield.h" | 10 #include "vm/bitfield.h" |
11 #include "vm/globals.h" | 11 #include "vm/globals.h" |
12 #include "vm/growable_array.h" | 12 #include "vm/growable_array.h" |
13 #include "vm/isolate.h" | 13 #include "vm/isolate.h" |
14 #include "vm/visitor.h" | 14 #include "vm/visitor.h" |
15 | 15 |
16 namespace dart { | 16 namespace dart { |
17 | 17 |
18 // Forward declarations. | 18 // Forward declarations. |
19 class AbstractType; | 19 class AbstractType; |
20 class AbstractTypeArguments; | 20 class AbstractTypeArguments; |
| 21 class Array; |
21 class Class; | 22 class Class; |
22 class Heap; | 23 class Heap; |
23 class Library; | 24 class Library; |
24 class Object; | 25 class Object; |
25 class ObjectStore; | 26 class ObjectStore; |
| 27 class RawAbstractTypeArguments; |
26 class RawArray; | 28 class RawArray; |
27 class RawBigint; | 29 class RawBigint; |
28 class RawClass; | 30 class RawClass; |
29 class RawContext; | 31 class RawContext; |
30 class RawDouble; | 32 class RawDouble; |
31 class RawField; | 33 class RawField; |
32 class RawFourByteString; | 34 class RawFourByteString; |
33 class RawFunction; | 35 class RawFunction; |
34 class RawGrowableObjectArray; | 36 class RawGrowableObjectArray; |
35 class RawImmutableArray; | 37 class RawImmutableArray; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 kHeaderTagBits> { | 79 kHeaderTagBits> { |
78 }; | 80 }; |
79 | 81 |
80 | 82 |
81 class SerializedHeaderData : public BitField<intptr_t, | 83 class SerializedHeaderData : public BitField<intptr_t, |
82 kHeaderTagBits, | 84 kHeaderTagBits, |
83 kObjectIdTagBits> { | 85 kObjectIdTagBits> { |
84 }; | 86 }; |
85 | 87 |
86 | 88 |
| 89 enum DeserializeState { |
| 90 kIsDeserialized = 0, |
| 91 kIsNotDeserialized = 1, |
| 92 }; |
| 93 |
| 94 |
| 95 enum SerializeState { |
| 96 kIsSerialized = 0, |
| 97 kIsNotSerialized = 1, |
| 98 }; |
| 99 |
| 100 |
87 // Structure capturing the raw snapshot. | 101 // Structure capturing the raw snapshot. |
88 class Snapshot { | 102 class Snapshot { |
89 public: | 103 public: |
90 enum Kind { | 104 enum Kind { |
91 kFull = 0, // Full snapshot of the current dart heap. | 105 kFull = 0, // Full snapshot of the current dart heap. |
92 kScript, // A partial snapshot of only the application script. | 106 kScript, // A partial snapshot of only the application script. |
93 kMessage, // A partial snapshot used only for isolate messaging. | 107 kMessage, // A partial snapshot used only for isolate messaging. |
94 }; | 108 }; |
95 | 109 |
96 static const int kHeaderSize = 2 * sizeof(int32_t); | 110 static const int kHeaderSize = 2 * sizeof(int32_t); |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 Heap* heap() const { return isolate_->heap(); } | 362 Heap* heap() const { return isolate_->heap(); } |
349 ObjectStore* object_store() const { return isolate_->object_store(); } | 363 ObjectStore* object_store() const { return isolate_->object_store(); } |
350 Object* ObjectHandle() { return &obj_; } | 364 Object* ObjectHandle() { return &obj_; } |
351 String* StringHandle() { return &str_; } | 365 String* StringHandle() { return &str_; } |
352 AbstractType* TypeHandle() { return &type_; } | 366 AbstractType* TypeHandle() { return &type_; } |
353 AbstractTypeArguments* TypeArgumentsHandle() { return &type_arguments_; } | 367 AbstractTypeArguments* TypeArgumentsHandle() { return &type_arguments_; } |
354 | 368 |
355 // Reads an object. | 369 // Reads an object. |
356 RawObject* ReadObject(); | 370 RawObject* ReadObject(); |
357 | 371 |
358 RawClass* ReadClassId(intptr_t object_id); | 372 // Add object to backward references. |
| 373 void AddBackRef(intptr_t id, Object* obj, DeserializeState state); |
359 | 374 |
360 // Add object to backward references. | 375 // Get an object from the backward references list. |
361 void AddBackwardReference(intptr_t id, Object* obj); | 376 Object* GetBackRef(intptr_t id); |
362 | 377 |
363 // Read a full snap shot. | 378 // Read a full snap shot. |
364 void ReadFullSnapshot(); | 379 void ReadFullSnapshot(); |
365 | 380 |
366 // Helper functions for creating uninitialized versions | 381 // Helper functions for creating uninitialized versions |
367 // of various object types. These are used when reading a | 382 // of various object types. These are used when reading a |
368 // full snapshot. | 383 // full snapshot. |
369 RawArray* NewArray(intptr_t len); | 384 RawArray* NewArray(intptr_t len); |
370 RawImmutableArray* NewImmutableArray(intptr_t len); | 385 RawImmutableArray* NewImmutableArray(intptr_t len); |
371 RawOneByteString* NewOneByteString(intptr_t len); | 386 RawOneByteString* NewOneByteString(intptr_t len); |
(...skipping 11 matching lines...) Expand all Loading... |
383 RawTypeParameter* NewTypeParameter(); | 398 RawTypeParameter* NewTypeParameter(); |
384 RawFunction* NewFunction(); | 399 RawFunction* NewFunction(); |
385 RawField* NewField(); | 400 RawField* NewField(); |
386 RawLibrary* NewLibrary(); | 401 RawLibrary* NewLibrary(); |
387 RawLibraryPrefix* NewLibraryPrefix(); | 402 RawLibraryPrefix* NewLibraryPrefix(); |
388 RawScript* NewScript(); | 403 RawScript* NewScript(); |
389 RawLiteralToken* NewLiteralToken(); | 404 RawLiteralToken* NewLiteralToken(); |
390 RawGrowableObjectArray* NewGrowableObjectArray(); | 405 RawGrowableObjectArray* NewGrowableObjectArray(); |
391 | 406 |
392 private: | 407 private: |
| 408 class BackRefNode : public ZoneAllocated { |
| 409 public: |
| 410 BackRefNode(Object* reference, DeserializeState state) |
| 411 : reference_(reference), state_(state) {} |
| 412 Object* reference() const { return reference_; } |
| 413 bool is_deserialized() const { return state_ == kIsDeserialized; } |
| 414 void set_state(DeserializeState state) { state_ = state; } |
| 415 |
| 416 private: |
| 417 Object* reference_; |
| 418 DeserializeState state_; |
| 419 |
| 420 DISALLOW_COPY_AND_ASSIGN(BackRefNode); |
| 421 }; |
| 422 |
393 // Allocate uninitialized objects, this is used when reading a full snapshot. | 423 // Allocate uninitialized objects, this is used when reading a full snapshot. |
394 RawObject* AllocateUninitialized(const Class& cls, intptr_t size); | 424 RawObject* AllocateUninitialized(const Class& cls, intptr_t size); |
395 | 425 |
396 // Internal implementation of ReadObject once the header value is read. | 426 RawClass* ReadClassId(intptr_t object_id); |
| 427 RawObject* ReadObjectImpl(); |
397 RawObject* ReadObjectImpl(intptr_t header); | 428 RawObject* ReadObjectImpl(intptr_t header); |
| 429 RawObject* ReadObjectRef(); |
398 | 430 |
399 // Read an object that was serialized as an Id (singleton, object store, | 431 // Read an object that was serialized as an Id (singleton, object store, |
400 // or an object that was already serialized before). | 432 // or an object that was already serialized before). |
401 RawObject* ReadIndexedObject(intptr_t object_id); | 433 RawObject* ReadIndexedObject(intptr_t object_id); |
402 | 434 |
403 // Read an inlined object from the stream. | 435 // Read an inlined object from the stream. |
404 RawObject* ReadInlinedObject(intptr_t object_id); | 436 RawObject* ReadInlinedObject(intptr_t object_id); |
405 | 437 |
406 // Based on header field check to see if it is an internal VM class. | 438 // Based on header field check to see if it is an internal VM class. |
407 RawClass* LookupInternalClass(intptr_t class_header); | 439 RawClass* LookupInternalClass(intptr_t class_header); |
408 | 440 |
| 441 void ArrayReadFrom(const Array& result, intptr_t len, intptr_t tags); |
| 442 |
409 Snapshot::Kind kind_; // Indicates type of snapshot(full, script, message). | 443 Snapshot::Kind kind_; // Indicates type of snapshot(full, script, message). |
410 Isolate* isolate_; // Current isolate. | 444 Isolate* isolate_; // Current isolate. |
411 Class& cls_; // Temporary Class handle. | 445 Class& cls_; // Temporary Class handle. |
412 Object& obj_; // Temporary Object handle. | 446 Object& obj_; // Temporary Object handle. |
413 String& str_; // Temporary String handle. | 447 String& str_; // Temporary String handle. |
414 Library& library_; // Temporary library handle. | 448 Library& library_; // Temporary library handle. |
415 AbstractType& type_; // Temporary type handle. | 449 AbstractType& type_; // Temporary type handle. |
416 AbstractTypeArguments& type_arguments_; // Temporary type argument handle. | 450 AbstractTypeArguments& type_arguments_; // Temporary type argument handle. |
417 GrowableArray<Object*> backward_references_; | 451 GrowableArray<BackRefNode*> backward_references_; |
418 | 452 |
| 453 friend class Array; |
| 454 friend class Class; |
| 455 friend class Context; |
| 456 friend class ContextScope; |
| 457 friend class Field; |
| 458 friend class Function; |
| 459 friend class GrowableObjectArray; |
| 460 friend class ImmutableArray; |
| 461 friend class InstantiatedTypeArguments; |
| 462 friend class JSRegExp; |
| 463 friend class Library; |
| 464 friend class LibraryPrefix; |
| 465 friend class LiteralToken; |
| 466 friend class Script; |
| 467 friend class TokenStream; |
| 468 friend class Type; |
| 469 friend class TypeArguments; |
| 470 friend class TypeParameter; |
| 471 friend class UnresolvedClass; |
419 DISALLOW_COPY_AND_ASSIGN(SnapshotReader); | 472 DISALLOW_COPY_AND_ASSIGN(SnapshotReader); |
420 }; | 473 }; |
421 | 474 |
422 | 475 |
423 class BaseWriter { | 476 class BaseWriter { |
424 public: | 477 public: |
425 // Size of the snapshot. | 478 // Size of the snapshot. |
426 intptr_t BytesWritten() const { return stream_.bytes_written(); } | 479 intptr_t BytesWritten() const { return stream_.bytes_written(); } |
427 | 480 |
428 // Writes raw data to the stream (basic type). | 481 // Writes raw data to the stream (basic type). |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 // which comprises of a flag(full/partial snaphot) and the length of | 554 // which comprises of a flag(full/partial snaphot) and the length of |
502 // serialzed bytes. | 555 // serialzed bytes. |
503 void FinalizeBuffer() { | 556 void FinalizeBuffer() { |
504 BaseWriter::FinalizeBuffer(kind_); | 557 BaseWriter::FinalizeBuffer(kind_); |
505 UnmarkAll(); | 558 UnmarkAll(); |
506 } | 559 } |
507 | 560 |
508 // Serialize an object into the buffer. | 561 // Serialize an object into the buffer. |
509 void WriteObject(RawObject* raw); | 562 void WriteObject(RawObject* raw); |
510 | 563 |
511 void WriteClassId(RawClass* cls); | |
512 | |
513 // Unmark all objects that were marked as forwarded for serializing. | |
514 void UnmarkAll(); | |
515 | |
516 // Writes a full snapshot of the Isolate. | 564 // Writes a full snapshot of the Isolate. |
517 void WriteFullSnapshot(); | 565 void WriteFullSnapshot(); |
518 | 566 |
519 uword GetObjectTags(RawObject* raw); | 567 uword GetObjectTags(RawObject* raw); |
520 | 568 |
521 private: | 569 private: |
522 class ForwardObjectNode : public ZoneAllocated { | 570 class ForwardObjectNode : public ZoneAllocated { |
523 public: | 571 public: |
524 ForwardObjectNode(RawObject* raw, uword tags) : raw_(raw), tags_(tags) {} | 572 ForwardObjectNode(RawObject* raw, uword tags, SerializeState state) |
| 573 : raw_(raw), tags_(tags), state_(state) {} |
525 RawObject* raw() const { return raw_; } | 574 RawObject* raw() const { return raw_; } |
526 uword tags() const { return tags_; } | 575 uword tags() const { return tags_; } |
| 576 bool is_serialized() const { return state_ == kIsSerialized; } |
| 577 void set_state(SerializeState value) { state_ = value; } |
527 | 578 |
528 private: | 579 private: |
529 RawObject* raw_; | 580 RawObject* raw_; |
530 uword tags_; | 581 uword tags_; |
| 582 SerializeState state_; |
531 | 583 |
532 DISALLOW_COPY_AND_ASSIGN(ForwardObjectNode); | 584 DISALLOW_COPY_AND_ASSIGN(ForwardObjectNode); |
533 }; | 585 }; |
534 | 586 |
535 intptr_t MarkObject(RawObject* raw); | 587 intptr_t MarkObject(RawObject* raw, SerializeState state); |
| 588 void UnmarkAll(); |
536 | 589 |
537 bool CheckAndWritePredefinedObject(RawObject* raw); | 590 bool CheckAndWritePredefinedObject(RawObject* raw); |
538 | 591 |
| 592 void WriteObjectRef(RawObject* raw); |
| 593 void WriteClassId(RawClass* cls); |
| 594 void WriteObjectImpl(RawObject* raw); |
539 void WriteInlinedObject(RawObject* raw); | 595 void WriteInlinedObject(RawObject* raw); |
| 596 void WriteForwardedObjects(); |
| 597 void ArrayWriteTo(intptr_t object_id, |
| 598 intptr_t array_kind, |
| 599 intptr_t tags, |
| 600 RawSmi* length, |
| 601 RawAbstractTypeArguments* type_arguments, |
| 602 RawObject* data[]); |
540 | 603 |
541 ObjectStore* object_store() const { return object_store_; } | 604 ObjectStore* object_store() const { return object_store_; } |
542 | 605 |
543 Snapshot::Kind kind_; | 606 Snapshot::Kind kind_; |
544 ObjectStore* object_store_; // Object store for common classes. | 607 ObjectStore* object_store_; // Object store for common classes. |
545 ClassTable* class_table_; // Class table for the class index to class lookup. | 608 ClassTable* class_table_; // Class table for the class index to class lookup. |
546 GrowableArray<ForwardObjectNode*> forward_list_; | 609 GrowableArray<ForwardObjectNode*> forward_list_; |
547 | 610 |
| 611 friend class RawArray; |
| 612 friend class RawClass; |
| 613 friend class RawGrowableObjectArray; |
| 614 friend class RawImmutableArray; |
| 615 friend class RawJSRegExp; |
| 616 friend class RawLibrary; |
| 617 friend class RawLiteralToken; |
| 618 friend class RawScript; |
| 619 friend class RawTokenStream; |
| 620 friend class RawTypeArguments; |
| 621 friend class SnapshotWriterVisitor; |
548 DISALLOW_COPY_AND_ASSIGN(SnapshotWriter); | 622 DISALLOW_COPY_AND_ASSIGN(SnapshotWriter); |
549 }; | 623 }; |
550 | 624 |
551 | 625 |
552 class ScriptSnapshotWriter : public SnapshotWriter { | 626 class ScriptSnapshotWriter : public SnapshotWriter { |
553 public: | 627 public: |
554 ScriptSnapshotWriter(uint8_t** buffer, ReAlloc alloc) | 628 ScriptSnapshotWriter(uint8_t** buffer, ReAlloc alloc) |
555 : SnapshotWriter(Snapshot::kScript, buffer, alloc) { | 629 : SnapshotWriter(Snapshot::kScript, buffer, alloc) { |
556 ASSERT(buffer != NULL); | 630 ASSERT(buffer != NULL); |
557 ASSERT(alloc != NULL); | 631 ASSERT(alloc != NULL); |
558 } | 632 } |
559 ~ScriptSnapshotWriter() { } | 633 ~ScriptSnapshotWriter() { } |
560 | 634 |
561 // Writes a partial snapshot of the script. | 635 // Writes a partial snapshot of the script. |
562 void WriteScriptSnapshot(const Library& lib); | 636 void WriteScriptSnapshot(const Library& lib); |
563 | 637 |
564 private: | 638 private: |
565 DISALLOW_COPY_AND_ASSIGN(ScriptSnapshotWriter); | 639 DISALLOW_COPY_AND_ASSIGN(ScriptSnapshotWriter); |
566 }; | 640 }; |
567 | 641 |
568 | 642 |
569 // An object pointer visitor implementation which writes out | 643 // An object pointer visitor implementation which writes out |
570 // objects to a snap shot. | 644 // objects to a snap shot. |
571 class SnapshotWriterVisitor : public ObjectPointerVisitor { | 645 class SnapshotWriterVisitor : public ObjectPointerVisitor { |
572 public: | 646 public: |
573 explicit SnapshotWriterVisitor(SnapshotWriter* writer) | 647 explicit SnapshotWriterVisitor(SnapshotWriter* writer) |
574 : ObjectPointerVisitor(Isolate::Current()), writer_(writer) {} | 648 : ObjectPointerVisitor(Isolate::Current()), |
| 649 writer_(writer), |
| 650 as_references_(true) {} |
| 651 |
| 652 SnapshotWriterVisitor(SnapshotWriter* writer, bool as_references) |
| 653 : ObjectPointerVisitor(Isolate::Current()), |
| 654 writer_(writer), |
| 655 as_references_(as_references) {} |
575 | 656 |
576 virtual void VisitPointers(RawObject** first, RawObject** last); | 657 virtual void VisitPointers(RawObject** first, RawObject** last); |
577 | 658 |
578 private: | 659 private: |
579 SnapshotWriter* writer_; | 660 SnapshotWriter* writer_; |
| 661 bool as_references_; |
580 | 662 |
581 DISALLOW_COPY_AND_ASSIGN(SnapshotWriterVisitor); | 663 DISALLOW_COPY_AND_ASSIGN(SnapshotWriterVisitor); |
582 }; | 664 }; |
583 | 665 |
584 } // namespace dart | 666 } // namespace dart |
585 | 667 |
586 #endif // VM_SNAPSHOT_H_ | 668 #endif // VM_SNAPSHOT_H_ |
OLD | NEW |