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" |
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 data[Snapshot::kSnapshotFlagIndex] = kind; | 480 data[Snapshot::kSnapshotFlagIndex] = kind; |
481 } | 481 } |
482 | 482 |
483 private: | 483 private: |
484 WriteStream stream_; | 484 WriteStream stream_; |
485 | 485 |
486 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseWriter); | 486 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseWriter); |
487 }; | 487 }; |
488 | 488 |
489 | 489 |
| 490 class ForwardList { |
| 491 public: |
| 492 explicit ForwardList(intptr_t first_object_id) |
| 493 : first_object_id_(first_object_id), |
| 494 nodes_(), |
| 495 first_unprocessed_object_id_(first_object_id) {} |
| 496 |
| 497 class Node : public ZoneAllocated { |
| 498 public: |
| 499 Node(RawObject* raw, uword tags, SerializeState state) |
| 500 : raw_(raw), tags_(tags), state_(state) {} |
| 501 RawObject* raw() const { return raw_; } |
| 502 uword tags() const { return tags_; } |
| 503 bool is_serialized() const { return state_ == kIsSerialized; } |
| 504 |
| 505 private: |
| 506 // Private to ensure the invariant of first_unprocessed_object_id_. |
| 507 void set_state(SerializeState value) { state_ = value; } |
| 508 |
| 509 RawObject* raw_; |
| 510 uword tags_; |
| 511 SerializeState state_; |
| 512 |
| 513 friend class ForwardList; |
| 514 DISALLOW_COPY_AND_ASSIGN(Node); |
| 515 }; |
| 516 |
| 517 Node* NodeForObjectId(intptr_t object_id) const { |
| 518 return nodes_[object_id - first_object_id_]; |
| 519 } |
| 520 |
| 521 // Returns the id for the added object. |
| 522 intptr_t MarkAndAddObject(RawObject* raw, SerializeState state); |
| 523 |
| 524 // Exhaustively processes all unserialized objects in this list. 'writer' may |
| 525 // concurrently add more objects. |
| 526 void SerializeAll(ObjectVisitor* writer); |
| 527 |
| 528 // Restores the tags of all objects in this list. |
| 529 void UnmarkAll() const; |
| 530 |
| 531 private: |
| 532 intptr_t first_object_id() const { return first_object_id_; } |
| 533 intptr_t next_object_id() const { return nodes_.length() + first_object_id_; } |
| 534 |
| 535 const intptr_t first_object_id_; |
| 536 GrowableArray<Node*> nodes_; |
| 537 intptr_t first_unprocessed_object_id_; |
| 538 |
| 539 DISALLOW_COPY_AND_ASSIGN(ForwardList); |
| 540 }; |
| 541 |
| 542 |
490 class SnapshotWriter : public BaseWriter { | 543 class SnapshotWriter : public BaseWriter { |
491 protected: | 544 protected: |
492 SnapshotWriter(Snapshot::Kind kind, | 545 SnapshotWriter(Snapshot::Kind kind, |
493 uint8_t** buffer, | 546 uint8_t** buffer, |
494 ReAlloc alloc, | 547 ReAlloc alloc, |
495 intptr_t initial_size); | 548 intptr_t initial_size); |
496 | 549 |
497 public: | 550 public: |
498 // Snapshot kind. | 551 // Snapshot kind. |
499 Snapshot::Kind kind() const { return kind_; } | 552 Snapshot::Kind kind() const { return kind_; } |
500 | 553 |
501 // Serialize an object into the buffer. | 554 // Serialize an object into the buffer. |
502 void WriteObject(RawObject* raw); | 555 void WriteObject(RawObject* raw); |
503 | 556 |
504 uword GetObjectTags(RawObject* raw); | 557 uword GetObjectTags(RawObject* raw); |
505 | 558 |
506 Exceptions::ExceptionType exception_type() const { | 559 Exceptions::ExceptionType exception_type() const { |
507 return exception_type_; | 560 return exception_type_; |
508 } | 561 } |
509 void set_exception_type(Exceptions::ExceptionType type) { | 562 void set_exception_type(Exceptions::ExceptionType type) { |
510 exception_type_ = type; | 563 exception_type_ = type; |
511 } | 564 } |
512 const char* exception_msg() const { return exception_msg_; } | 565 const char* exception_msg() const { return exception_msg_; } |
513 void set_exception_msg(const char* msg) { | 566 void set_exception_msg(const char* msg) { |
514 exception_msg_ = msg; | 567 exception_msg_ = msg; |
515 } | 568 } |
516 void ThrowException(Exceptions::ExceptionType type, const char* msg); | 569 void ThrowException(Exceptions::ExceptionType type, const char* msg); |
517 | 570 |
518 protected: | 571 protected: |
519 class ForwardObjectNode : public ZoneAllocated { | 572 void UnmarkAll() { |
520 public: | 573 forward_list_.UnmarkAll(); |
521 ForwardObjectNode(RawObject* raw, uword tags, SerializeState state) | 574 } |
522 : raw_(raw), tags_(tags), state_(state) {} | |
523 RawObject* raw() const { return raw_; } | |
524 uword tags() const { return tags_; } | |
525 bool is_serialized() const { return state_ == kIsSerialized; } | |
526 void set_state(SerializeState value) { state_ = value; } | |
527 | |
528 private: | |
529 RawObject* raw_; | |
530 uword tags_; | |
531 SerializeState state_; | |
532 | |
533 DISALLOW_COPY_AND_ASSIGN(ForwardObjectNode); | |
534 }; | |
535 | |
536 intptr_t MarkObject(RawObject* raw, SerializeState state); | |
537 void UnmarkAll(); | |
538 | 575 |
539 bool CheckAndWritePredefinedObject(RawObject* raw); | 576 bool CheckAndWritePredefinedObject(RawObject* raw); |
540 void HandleVMIsolateObject(RawObject* raw); | 577 void HandleVMIsolateObject(RawObject* raw); |
541 | 578 |
542 void WriteObjectRef(RawObject* raw); | 579 void WriteObjectRef(RawObject* raw); |
543 void WriteClassId(RawClass* cls); | 580 void WriteClassId(RawClass* cls); |
544 void WriteObjectImpl(RawObject* raw); | 581 void WriteObjectImpl(RawObject* raw); |
545 void WriteInlinedObject(RawObject* raw); | 582 void WriteInlinedObject(RawObject* raw); |
546 void WriteForwardedObjects(); | 583 void WriteForwardedObjects(); |
547 void ArrayWriteTo(intptr_t object_id, | 584 void ArrayWriteTo(intptr_t object_id, |
548 intptr_t array_kind, | 585 intptr_t array_kind, |
549 intptr_t tags, | 586 intptr_t tags, |
550 RawSmi* length, | 587 RawSmi* length, |
551 RawTypeArguments* type_arguments, | 588 RawTypeArguments* type_arguments, |
552 RawObject* data[]); | 589 RawObject* data[]); |
553 void CheckIfSerializable(RawClass* cls); | 590 void CheckIfSerializable(RawClass* cls); |
554 void SetWriteException(Exceptions::ExceptionType type, const char* msg); | 591 void SetWriteException(Exceptions::ExceptionType type, const char* msg); |
555 void WriteInstance(intptr_t object_id, | 592 void WriteInstance(intptr_t object_id, |
556 RawObject* raw, | 593 RawObject* raw, |
557 RawClass* cls, | 594 RawClass* cls, |
558 intptr_t tags); | 595 intptr_t tags); |
559 void WriteInstanceRef(RawObject* raw, RawClass* cls); | 596 void WriteInstanceRef(RawObject* raw, RawClass* cls); |
560 | 597 |
561 ObjectStore* object_store() const { return object_store_; } | 598 ObjectStore* object_store() const { return object_store_; } |
562 | 599 |
563 private: | 600 private: |
564 Snapshot::Kind kind_; | 601 Snapshot::Kind kind_; |
565 ObjectStore* object_store_; // Object store for common classes. | 602 ObjectStore* object_store_; // Object store for common classes. |
566 ClassTable* class_table_; // Class table for the class index to class lookup. | 603 ClassTable* class_table_; // Class table for the class index to class lookup. |
567 GrowableArray<ForwardObjectNode*> forward_list_; | 604 ForwardList forward_list_; |
568 Exceptions::ExceptionType exception_type_; // Exception type. | 605 Exceptions::ExceptionType exception_type_; // Exception type. |
569 const char* exception_msg_; // Message associated with exception. | 606 const char* exception_msg_; // Message associated with exception. |
570 | 607 |
571 friend class RawArray; | 608 friend class RawArray; |
572 friend class RawClass; | 609 friend class RawClass; |
573 friend class RawClosureData; | 610 friend class RawClosureData; |
574 friend class RawGrowableObjectArray; | 611 friend class RawGrowableObjectArray; |
575 friend class RawImmutableArray; | 612 friend class RawImmutableArray; |
576 friend class RawJSRegExp; | 613 friend class RawJSRegExp; |
577 friend class RawLibrary; | 614 friend class RawLibrary; |
578 friend class RawLiteralToken; | 615 friend class RawLiteralToken; |
| 616 friend class RawMirrorReference; |
579 friend class RawReceivePort; | 617 friend class RawReceivePort; |
580 friend class RawScript; | 618 friend class RawScript; |
581 friend class RawStacktrace; | 619 friend class RawStacktrace; |
582 friend class RawTokenStream; | 620 friend class RawTokenStream; |
583 friend class RawTypeArguments; | 621 friend class RawTypeArguments; |
584 friend class RawMirrorReference; | 622 friend class RawUserTag; |
585 friend class SnapshotWriterVisitor; | 623 friend class SnapshotWriterVisitor; |
586 friend class RawUserTag; | 624 friend class WriteInlinedObjectVisitor; |
587 DISALLOW_COPY_AND_ASSIGN(SnapshotWriter); | 625 DISALLOW_COPY_AND_ASSIGN(SnapshotWriter); |
588 }; | 626 }; |
589 | 627 |
590 | 628 |
591 class FullSnapshotWriter : public SnapshotWriter { | 629 class FullSnapshotWriter : public SnapshotWriter { |
592 public: | 630 public: |
593 static const intptr_t kInitialSize = 64 * KB; | 631 static const intptr_t kInitialSize = 64 * KB; |
594 FullSnapshotWriter(uint8_t** buffer, ReAlloc alloc) | 632 FullSnapshotWriter(uint8_t** buffer, ReAlloc alloc) |
595 : SnapshotWriter(Snapshot::kFull, buffer, alloc, kInitialSize) { | 633 : SnapshotWriter(Snapshot::kFull, buffer, alloc, kInitialSize) { |
596 ASSERT(buffer != NULL); | 634 ASSERT(buffer != NULL); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 private: | 698 private: |
661 SnapshotWriter* writer_; | 699 SnapshotWriter* writer_; |
662 bool as_references_; | 700 bool as_references_; |
663 | 701 |
664 DISALLOW_COPY_AND_ASSIGN(SnapshotWriterVisitor); | 702 DISALLOW_COPY_AND_ASSIGN(SnapshotWriterVisitor); |
665 }; | 703 }; |
666 | 704 |
667 } // namespace dart | 705 } // namespace dart |
668 | 706 |
669 #endif // VM_SNAPSHOT_H_ | 707 #endif // VM_SNAPSHOT_H_ |
OLD | NEW |