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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 enum Kind { | 104 enum Kind { |
105 kFull = 0, // Full snapshot of the current dart heap. | 105 kFull = 0, // Full snapshot of the current dart heap. |
106 kScript, // A partial snapshot of only the application script. | 106 kScript, // A partial snapshot of only the application script. |
107 kMessage, // A partial snapshot used only for isolate messaging. | 107 kMessage, // A partial snapshot used only for isolate messaging. |
108 }; | 108 }; |
109 | 109 |
110 static const int kHeaderSize = 2 * sizeof(int32_t); | 110 static const int kHeaderSize = 2 * sizeof(int32_t); |
111 static const int kLengthIndex = 0; | 111 static const int kLengthIndex = 0; |
112 static const int kSnapshotFlagIndex = 1; | 112 static const int kSnapshotFlagIndex = 1; |
113 | 113 |
114 static const Snapshot* SetupFromBuffer(const void* raw_memory); | 114 static const int kTrustedLength = -1; |
| 115 |
| 116 // Convert a (buffer, buffer_len) to a Snapshot. |
| 117 // |
| 118 // This function will return NULL if buffer_len is less than the |
| 119 // minimum legal size or if buffer_len does not match the length |
| 120 // encoded internally in the buffer. |
| 121 // |
| 122 // If the buffer is coming from a trusted source, you can pass |
| 123 // Snapshot::kTrustedLength to trust the length encoded internally |
| 124 // in the buffer. |
| 125 static const Snapshot* SetupFromBuffer(const void* buffer, |
| 126 intptr_t buffer_len); |
115 | 127 |
116 // Getters. | 128 // Getters. |
117 const uint8_t* content() const { return content_; } | 129 const uint8_t* content() const { return content_; } |
118 int32_t length() const { return length_; } | 130 int32_t length() const { return length_; } |
119 Kind kind() const { return static_cast<Kind>(kind_); } | 131 Kind kind() const { return static_cast<Kind>(kind_); } |
120 | 132 |
121 bool IsMessageSnapshot() const { return kind_ == kMessage; } | 133 bool IsMessageSnapshot() const { return kind_ == kMessage; } |
122 bool IsScriptSnapshot() const { return kind_ == kScript; } | 134 bool IsScriptSnapshot() const { return kind_ == kScript; } |
123 bool IsFullSnapshot() const { return kind_ == kFull; } | 135 bool IsFullSnapshot() const { return kind_ == kFull; } |
124 int32_t Size() const { return length_ + sizeof(Snapshot); } | 136 int32_t Size() const { return length_ + sizeof(Snapshot); } |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 WriteIntptrValue(value); | 365 WriteIntptrValue(value); |
354 } | 366 } |
355 | 367 |
356 // Write out a buffer of bytes. | 368 // Write out a buffer of bytes. |
357 void WriteBytes(const uint8_t* addr, intptr_t len) { | 369 void WriteBytes(const uint8_t* addr, intptr_t len) { |
358 stream_.WriteBytes(addr, len); | 370 stream_.WriteBytes(addr, len); |
359 } | 371 } |
360 | 372 |
361 // Finalize the serialized buffer by filling in the header information | 373 // Finalize the serialized buffer by filling in the header information |
362 // which comprises of a flag(snaphot kind) and the length of | 374 // which comprises of a flag(snaphot kind) and the length of |
363 // serialzed bytes. | 375 // serialzed bytes. Returns the total length of the buffer in bytes. |
364 void FinalizeBuffer(Snapshot::Kind kind) { | 376 intptr_t FinalizeBuffer(Snapshot::Kind kind) { |
365 int32_t* data = reinterpret_cast<int32_t*>(stream_.buffer()); | 377 int32_t* data = reinterpret_cast<int32_t*>(stream_.buffer()); |
366 data[Snapshot::kLengthIndex] = stream_.bytes_written(); | 378 data[Snapshot::kLengthIndex] = stream_.bytes_written(); |
367 data[Snapshot::kSnapshotFlagIndex] = kind; | 379 data[Snapshot::kSnapshotFlagIndex] = kind; |
| 380 return stream_.bytes_written(); |
368 } | 381 } |
369 | 382 |
370 protected: | 383 protected: |
371 BaseWriter(uint8_t** buffer, ReAlloc alloc) : stream_(buffer, alloc) { | 384 BaseWriter(uint8_t** buffer, ReAlloc alloc) : stream_(buffer, alloc) { |
372 ASSERT(buffer != NULL); | 385 ASSERT(buffer != NULL); |
373 ASSERT(alloc != NULL); | 386 ASSERT(alloc != NULL); |
374 // Make room for recording snapshot buffer size. | 387 // Make room for recording snapshot buffer size. |
375 stream_.set_current(*buffer + Snapshot::kHeaderSize); | 388 stream_.set_current(*buffer + Snapshot::kHeaderSize); |
376 } | 389 } |
377 ~BaseWriter() { } | 390 ~BaseWriter() { } |
(...skipping 14 matching lines...) Expand all Loading... |
392 class_table_(Isolate::Current()->class_table()), | 405 class_table_(Isolate::Current()->class_table()), |
393 forward_list_() { | 406 forward_list_() { |
394 } | 407 } |
395 ~SnapshotWriter() { } | 408 ~SnapshotWriter() { } |
396 | 409 |
397 // Snapshot kind. | 410 // Snapshot kind. |
398 Snapshot::Kind kind() const { return kind_; } | 411 Snapshot::Kind kind() const { return kind_; } |
399 | 412 |
400 // Finalize the serialized buffer by filling in the header information | 413 // Finalize the serialized buffer by filling in the header information |
401 // which comprises of a flag(full/partial snaphot) and the length of | 414 // which comprises of a flag(full/partial snaphot) and the length of |
402 // serialzed bytes. | 415 // serialzed bytes. Returns the total bytes written. |
403 void FinalizeBuffer() { | 416 intptr_t FinalizeBuffer() { |
404 BaseWriter::FinalizeBuffer(kind_); | 417 intptr_t bytes_written = BaseWriter::FinalizeBuffer(kind_); |
405 UnmarkAll(); | 418 UnmarkAll(); |
| 419 return bytes_written; |
406 } | 420 } |
407 | 421 |
408 // Serialize an object into the buffer. | 422 // Serialize an object into the buffer. |
409 void WriteObject(RawObject* raw); | 423 void WriteObject(RawObject* raw); |
410 | 424 |
411 // Writes a full snapshot of the Isolate. | 425 // Writes a full snapshot of the Isolate. |
412 void WriteFullSnapshot(); | 426 intptr_t WriteFullSnapshot(); |
413 | 427 |
414 uword GetObjectTags(RawObject* raw); | 428 uword GetObjectTags(RawObject* raw); |
415 | 429 |
416 private: | 430 private: |
417 class ForwardObjectNode : public ZoneAllocated { | 431 class ForwardObjectNode : public ZoneAllocated { |
418 public: | 432 public: |
419 ForwardObjectNode(RawObject* raw, uword tags, SerializeState state) | 433 ForwardObjectNode(RawObject* raw, uword tags, SerializeState state) |
420 : raw_(raw), tags_(tags), state_(state) {} | 434 : raw_(raw), tags_(tags), state_(state) {} |
421 RawObject* raw() const { return raw_; } | 435 RawObject* raw() const { return raw_; } |
422 uword tags() const { return tags_; } | 436 uword tags() const { return tags_; } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 class ScriptSnapshotWriter : public SnapshotWriter { | 488 class ScriptSnapshotWriter : public SnapshotWriter { |
475 public: | 489 public: |
476 ScriptSnapshotWriter(uint8_t** buffer, ReAlloc alloc) | 490 ScriptSnapshotWriter(uint8_t** buffer, ReAlloc alloc) |
477 : SnapshotWriter(Snapshot::kScript, buffer, alloc) { | 491 : SnapshotWriter(Snapshot::kScript, buffer, alloc) { |
478 ASSERT(buffer != NULL); | 492 ASSERT(buffer != NULL); |
479 ASSERT(alloc != NULL); | 493 ASSERT(alloc != NULL); |
480 } | 494 } |
481 ~ScriptSnapshotWriter() { } | 495 ~ScriptSnapshotWriter() { } |
482 | 496 |
483 // Writes a partial snapshot of the script. | 497 // Writes a partial snapshot of the script. |
484 void WriteScriptSnapshot(const Library& lib); | 498 intptr_t WriteScriptSnapshot(const Library& lib); |
485 | 499 |
486 private: | 500 private: |
487 DISALLOW_COPY_AND_ASSIGN(ScriptSnapshotWriter); | 501 DISALLOW_COPY_AND_ASSIGN(ScriptSnapshotWriter); |
488 }; | 502 }; |
489 | 503 |
490 | 504 |
491 // An object pointer visitor implementation which writes out | 505 // An object pointer visitor implementation which writes out |
492 // objects to a snap shot. | 506 // objects to a snap shot. |
493 class SnapshotWriterVisitor : public ObjectPointerVisitor { | 507 class SnapshotWriterVisitor : public ObjectPointerVisitor { |
494 public: | 508 public: |
(...skipping 12 matching lines...) Expand all Loading... |
507 private: | 521 private: |
508 SnapshotWriter* writer_; | 522 SnapshotWriter* writer_; |
509 bool as_references_; | 523 bool as_references_; |
510 | 524 |
511 DISALLOW_COPY_AND_ASSIGN(SnapshotWriterVisitor); | 525 DISALLOW_COPY_AND_ASSIGN(SnapshotWriterVisitor); |
512 }; | 526 }; |
513 | 527 |
514 } // namespace dart | 528 } // namespace dart |
515 | 529 |
516 #endif // VM_SNAPSHOT_H_ | 530 #endif // VM_SNAPSHOT_H_ |
OLD | NEW |