| OLD | NEW | 
|    1 // Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file |    1 // Copyright (c) 2011, 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 "vm/allocation.h" |    8 #include "vm/allocation.h" | 
|    9 #include "vm/assert.h" |    9 #include "vm/assert.h" | 
|   10 #include "vm/bitfield.h" |   10 #include "vm/bitfield.h" | 
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  323   ReadStream stream_;  // input stream. |  323   ReadStream stream_;  // input stream. | 
|  324   Snapshot::Kind kind_;  // Indicates type of snapshot(full, script, message). |  324   Snapshot::Kind kind_;  // Indicates type of snapshot(full, script, message). | 
|  325   Heap* heap_;  // Heap into which the objects are deserialized into. |  325   Heap* heap_;  // Heap into which the objects are deserialized into. | 
|  326   ObjectStore* object_store_;  // Object store for common classes. |  326   ObjectStore* object_store_;  // Object store for common classes. | 
|  327   GrowableArray<Object*> backward_references_; |  327   GrowableArray<Object*> backward_references_; | 
|  328  |  328  | 
|  329   DISALLOW_COPY_AND_ASSIGN(SnapshotReader); |  329   DISALLOW_COPY_AND_ASSIGN(SnapshotReader); | 
|  330 }; |  330 }; | 
|  331  |  331  | 
|  332  |  332  | 
|  333 class MessageWriter { |  333 class BaseWriter { | 
|  334  public: |  334  public: | 
|  335   MessageWriter(uint8_t** buffer, ReAlloc alloc) : stream_(buffer, alloc) { |  335   // Size of the snapshot. | 
|  336     ASSERT(buffer != NULL); |  336   intptr_t BytesWritten() const { return stream_.bytes_written(); } | 
|  337     ASSERT(alloc != NULL); |  | 
|  338   } |  | 
|  339   ~MessageWriter() { } |  | 
|  340  |  337  | 
|  341   // Writes a message of integers. |  | 
|  342   void WriteMessage(intptr_t field_count, intptr_t *data); |  | 
|  343  |  | 
|  344  private: |  | 
|  345   // Writes raw data to the stream (basic type). |  338   // Writes raw data to the stream (basic type). | 
|  346   // sizeof(T) must be in {1,2,4,8}. |  339   // sizeof(T) must be in {1,2,4,8}. | 
|  347   template <typename T> |  340   template <typename T> | 
|  348   void Write(T value) { |  341   void Write(T value) { | 
|  349     WriteStream::Raw<sizeof(T), T>::Write(&stream_, value); |  342     WriteStream::Raw<sizeof(T), T>::Write(&stream_, value); | 
|  350   } |  343   } | 
|  351  |  344  | 
|  352   // Write an object that is serialized as an Id (singleton, object store, |  345   // Write an object that is serialized as an Id (singleton, object store, | 
|  353   // or an object that was already serialized before). |  346   // or an object that was already serialized before). | 
|  354   void WriteIndexedObject(intptr_t object_id) { |  347   void WriteIndexedObject(intptr_t object_id) { | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
|  365  |  358  | 
|  366   // Write serialization header information for an object. |  359   // Write serialization header information for an object. | 
|  367   void WriteSerializationMarker(SerializedHeaderType type, intptr_t id) { |  360   void WriteSerializationMarker(SerializedHeaderType type, intptr_t id) { | 
|  368     uword value = 0; |  361     uword value = 0; | 
|  369     value = SerializedHeaderTag::update(type, value); |  362     value = SerializedHeaderTag::update(type, value); | 
|  370     value = SerializedHeaderData::update(id, value); |  363     value = SerializedHeaderData::update(id, value); | 
|  371     Write<uword>(value); |  364     Write<uword>(value); | 
|  372   } |  365   } | 
|  373  |  366  | 
|  374   // Finalize the serialized buffer by filling in the header information |  367   // Finalize the serialized buffer by filling in the header information | 
|  375   // which comprises of a flag(full/partial snaphot) and the length of |  368   // which comprises of a flag(snaphot kind) and the length of | 
|  376   // serialzed bytes. |  369   // serialzed bytes. | 
|  377   void FinalizeBuffer() { |  370   void FinalizeBuffer(Snapshot::Kind kind) { | 
|  378     int32_t* data = reinterpret_cast<int32_t*>(stream_.buffer()); |  371     int32_t* data = reinterpret_cast<int32_t*>(stream_.buffer()); | 
|  379     data[Snapshot::kLengthIndex] = stream_.bytes_written(); |  372     data[Snapshot::kLengthIndex] = stream_.bytes_written(); | 
|  380     data[Snapshot::kSnapshotFlagIndex] = Snapshot::kMessage; |  373     data[Snapshot::kSnapshotFlagIndex] = kind; | 
|  381   } |  374   } | 
|  382  |  375  | 
 |  376  protected: | 
 |  377   BaseWriter(uint8_t** buffer, ReAlloc alloc) : stream_(buffer, alloc) { | 
 |  378     ASSERT(buffer != NULL); | 
 |  379     ASSERT(alloc != NULL); | 
 |  380   } | 
 |  381   ~BaseWriter() { } | 
 |  382  | 
 |  383  private: | 
|  383   WriteStream stream_; |  384   WriteStream stream_; | 
|  384  |  385  | 
 |  386   DISALLOW_IMPLICIT_CONSTRUCTORS(BaseWriter); | 
 |  387 }; | 
 |  388  | 
 |  389  | 
 |  390 class MessageWriter : public BaseWriter { | 
 |  391  public: | 
 |  392   MessageWriter(uint8_t** buffer, ReAlloc alloc) : BaseWriter(buffer, alloc) { | 
 |  393   } | 
 |  394   ~MessageWriter() { } | 
 |  395  | 
 |  396   // Writes a message of integers. | 
 |  397   void WriteMessage(intptr_t field_count, intptr_t *data); | 
 |  398  | 
 |  399   void FinalizeBuffer() { | 
 |  400     BaseWriter::FinalizeBuffer(Snapshot::kMessage); | 
 |  401   } | 
 |  402  | 
 |  403  private: | 
|  385   DISALLOW_COPY_AND_ASSIGN(MessageWriter); |  404   DISALLOW_COPY_AND_ASSIGN(MessageWriter); | 
|  386 }; |  405 }; | 
|  387  |  406  | 
|  388  |  407  | 
|  389 class SnapshotWriter { |  408 class SnapshotWriter : public BaseWriter { | 
|  390  public: |  409  public: | 
|  391   SnapshotWriter(Snapshot::Kind kind, uint8_t** buffer, ReAlloc alloc) |  410   SnapshotWriter(Snapshot::Kind kind, uint8_t** buffer, ReAlloc alloc) | 
|  392       : stream_(buffer, alloc), |  411       : BaseWriter(buffer, alloc), | 
|  393         kind_(kind), |  412         kind_(kind), | 
|  394         object_store_(Isolate::Current()->object_store()), |  413         object_store_(Isolate::Current()->object_store()), | 
|  395         forward_list_() { |  414         forward_list_() { | 
|  396     ASSERT(buffer != NULL); |  | 
|  397     ASSERT(alloc != NULL); |  | 
|  398   } |  415   } | 
|  399   ~SnapshotWriter() { } |  416   ~SnapshotWriter() { } | 
|  400  |  417  | 
|  401   // Snapshot kind. |  418   // Snapshot kind. | 
|  402   Snapshot::Kind kind() const { return kind_; } |  419   Snapshot::Kind kind() const { return kind_; } | 
|  403  |  420  | 
|  404   // Size of the snapshot. |  | 
|  405   intptr_t Size() const { return stream_.bytes_written(); } |  | 
|  406  |  | 
|  407   // Finalize the serialized buffer by filling in the header information |  421   // Finalize the serialized buffer by filling in the header information | 
|  408   // which comprises of a flag(full/partial snaphot) and the length of |  422   // which comprises of a flag(full/partial snaphot) and the length of | 
|  409   // serialzed bytes. |  423   // serialzed bytes. | 
|  410   void FinalizeBuffer() { |  424   void FinalizeBuffer() { | 
|  411     int32_t* data = reinterpret_cast<int32_t*>(stream_.buffer()); |  425     BaseWriter::FinalizeBuffer(kind_); | 
|  412     data[Snapshot::kLengthIndex] = stream_.bytes_written(); |  | 
|  413     data[Snapshot::kSnapshotFlagIndex] = kind_; |  | 
|  414     UnmarkAll(); |  426     UnmarkAll(); | 
|  415   } |  427   } | 
|  416  |  428  | 
|  417   // Writes raw data to the stream (basic type). |  | 
|  418   // sizeof(T) must be in {1,2,4,8}. |  | 
|  419   template <typename T> |  | 
|  420   void Write(T value) { |  | 
|  421     WriteStream::Raw<sizeof(T), T>::Write(&stream_, value); |  | 
|  422   } |  | 
|  423  |  | 
|  424   // Serialize an object into the buffer. |  429   // Serialize an object into the buffer. | 
|  425   void WriteObject(RawObject* raw); |  430   void WriteObject(RawObject* raw); | 
|  426  |  431  | 
|  427   void WriteClassId(RawClass* cls); |  432   void WriteClassId(RawClass* cls); | 
|  428  |  433  | 
|  429   // Write an object that is serialized as an Id (singleton, object store, |  | 
|  430   // or an object that was already serialized before). |  | 
|  431   void WriteIndexedObject(intptr_t object_id) { |  | 
|  432     WriteSerializationMarker(kObjectId, object_id); |  | 
|  433   } |  | 
|  434  |  | 
|  435   // Write out object header value. |  | 
|  436   void WriteObjectHeader(intptr_t class_id, intptr_t tags) { |  | 
|  437     // Write out the class information. |  | 
|  438     WriteIndexedObject(class_id); |  | 
|  439     // Write out the tags information. |  | 
|  440     Write<intptr_t>(tags); |  | 
|  441   } |  | 
|  442  |  | 
|  443   // Write serialization header information for an object. |  | 
|  444   void WriteSerializationMarker(SerializedHeaderType type, intptr_t id) { |  | 
|  445     uword value = 0; |  | 
|  446     value = SerializedHeaderTag::update(type, value); |  | 
|  447     value = SerializedHeaderData::update(id, value); |  | 
|  448     Write<uword>(value); |  | 
|  449   } |  | 
|  450  |  | 
|  451   // Unmark all objects that were marked as forwarded for serializing. |  434   // Unmark all objects that were marked as forwarded for serializing. | 
|  452   void UnmarkAll(); |  435   void UnmarkAll(); | 
|  453  |  436  | 
|  454   // Writes a full snapshot of the Isolate. |  437   // Writes a full snapshot of the Isolate. | 
|  455   void WriteFullSnapshot(); |  438   void WriteFullSnapshot(); | 
|  456  |  439  | 
|  457  private: |  440  private: | 
|  458   class ForwardObjectNode : public ZoneAllocated { |  441   class ForwardObjectNode : public ZoneAllocated { | 
|  459    public: |  442    public: | 
|  460     ForwardObjectNode(RawObject* raw, RawClass* cls) : raw_(raw), cls_(cls) {} |  443     ForwardObjectNode(RawObject* raw, RawClass* cls) : raw_(raw), cls_(cls) {} | 
|  461     RawObject* raw() const { return raw_; } |  444     RawObject* raw() const { return raw_; } | 
|  462     RawClass* cls() const { return cls_; } |  445     RawClass* cls() const { return cls_; } | 
|  463  |  446  | 
|  464    private: |  447    private: | 
|  465     RawObject* raw_; |  448     RawObject* raw_; | 
|  466     RawClass* cls_; |  449     RawClass* cls_; | 
|  467  |  450  | 
|  468     DISALLOW_COPY_AND_ASSIGN(ForwardObjectNode); |  451     DISALLOW_COPY_AND_ASSIGN(ForwardObjectNode); | 
|  469   }; |  452   }; | 
|  470  |  453  | 
|  471   intptr_t MarkObject(RawObject* raw, RawClass* cls); |  454   intptr_t MarkObject(RawObject* raw, RawClass* cls); | 
|  472  |  455  | 
|  473   void WriteInlinedObject(RawObject* raw); |  456   void WriteInlinedObject(RawObject* raw); | 
|  474  |  457  | 
|  475   ObjectStore* object_store() const { return object_store_; } |  458   ObjectStore* object_store() const { return object_store_; } | 
|  476  |  459  | 
|  477   WriteStream stream_; |  | 
|  478   Snapshot::Kind kind_; |  460   Snapshot::Kind kind_; | 
|  479   ObjectStore* object_store_;  // Object store for common classes. |  461   ObjectStore* object_store_;  // Object store for common classes. | 
|  480   GrowableArray<ForwardObjectNode*> forward_list_; |  462   GrowableArray<ForwardObjectNode*> forward_list_; | 
|  481  |  463  | 
|  482   DISALLOW_COPY_AND_ASSIGN(SnapshotWriter); |  464   DISALLOW_COPY_AND_ASSIGN(SnapshotWriter); | 
|  483 }; |  465 }; | 
|  484  |  466  | 
|  485  |  467  | 
|  486 class ScriptSnapshotWriter : public SnapshotWriter { |  468 class ScriptSnapshotWriter : public SnapshotWriter { | 
|  487  public: |  469  public: | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
|  510  |  492  | 
|  511  private: |  493  private: | 
|  512   SnapshotWriter* writer_; |  494   SnapshotWriter* writer_; | 
|  513  |  495  | 
|  514   DISALLOW_COPY_AND_ASSIGN(SnapshotWriterVisitor); |  496   DISALLOW_COPY_AND_ASSIGN(SnapshotWriterVisitor); | 
|  515 }; |  497 }; | 
|  516  |  498  | 
|  517 }  // namespace dart |  499 }  // namespace dart | 
|  518  |  500  | 
|  519 #endif  // VM_SNAPSHOT_H_ |  501 #endif  // VM_SNAPSHOT_H_ | 
| OLD | NEW |