| 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 #include "vm/snapshot.h" | 5 #include "vm/snapshot.h" |
| 6 | 6 |
| 7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
| 8 #include "vm/bootstrap.h" | 8 #include "vm/bootstrap.h" |
| 9 #include "vm/exceptions.h" | 9 #include "vm/exceptions.h" |
| 10 #include "vm/heap.h" | 10 #include "vm/heap.h" |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 #undef SNAPSHOT_READ | 430 #undef SNAPSHOT_READ |
| 431 default: UNREACHABLE(); break; | 431 default: UNREACHABLE(); break; |
| 432 } | 432 } |
| 433 if (kind_ == Snapshot::kFull) { | 433 if (kind_ == Snapshot::kFull) { |
| 434 obj_.SetCreatedFromSnapshot(); | 434 obj_.SetCreatedFromSnapshot(); |
| 435 } | 435 } |
| 436 return obj_.raw(); | 436 return obj_.raw(); |
| 437 } | 437 } |
| 438 | 438 |
| 439 | 439 |
| 440 void MessageWriter::WriteMessage(intptr_t field_count, intptr_t *data) { | |
| 441 // Write out the serialization header value for this object. | |
| 442 WriteSerializationMarker(kInlined, kMaxPredefinedObjectIds); | |
| 443 | |
| 444 // Write out the class and tags information. | |
| 445 WriteObjectHeader(ObjectStore::kArrayClass, 0); | |
| 446 | |
| 447 // Write out the length field. | |
| 448 Write<RawObject*>(Smi::New(field_count)); | |
| 449 | |
| 450 // Write out the type arguments. | |
| 451 WriteIndexedObject(Object::kNullObject); | |
| 452 | |
| 453 // Write out the individual Smis. | |
| 454 for (int i = 0; i < field_count; i++) { | |
| 455 Write<RawObject*>(Integer::New(data[i])); | |
| 456 } | |
| 457 | |
| 458 FinalizeBuffer(); | |
| 459 } | |
| 460 | |
| 461 | |
| 462 void MessageWriter::MarkCObject(Dart_CObject* object, intptr_t object_id) { | |
| 463 // Mark the object as serialized by adding the object id to the | |
| 464 // upper bits of the type field in the Dart_CObject structure. Add | |
| 465 // an offset for making marking of object id 0 possible. | |
| 466 ASSERT(!IsCObjectMarked(object)); | |
| 467 intptr_t mark_value = object_id + kDartCObjectMarkOffset; | |
| 468 object->type = static_cast<Dart_CObject::Type>( | |
| 469 ((mark_value) << kDartCObjectTypeBits) | object->type); | |
| 470 } | |
| 471 | |
| 472 | |
| 473 void MessageWriter::UnmarkCObject(Dart_CObject* object) { | |
| 474 ASSERT(IsCObjectMarked(object)); | |
| 475 object->type = static_cast<Dart_CObject::Type>( | |
| 476 object->type & kDartCObjectTypeMask); | |
| 477 } | |
| 478 | |
| 479 | |
| 480 bool MessageWriter::IsCObjectMarked(Dart_CObject* object) { | |
| 481 return (object->type & kDartCObjectMarkMask) != 0; | |
| 482 } | |
| 483 | |
| 484 | |
| 485 intptr_t MessageWriter::GetMarkedCObjectMark(Dart_CObject* object) { | |
| 486 ASSERT(IsCObjectMarked(object)); | |
| 487 intptr_t mark_value = ((object->type & kDartCObjectMarkMask) >> 3); | |
| 488 // An offset was added to object id for making marking object id 0 possible. | |
| 489 return mark_value - kDartCObjectMarkOffset; | |
| 490 } | |
| 491 | |
| 492 | |
| 493 void MessageWriter::UnmarkAllCObjects(Dart_CObject* object) { | |
| 494 if (!IsCObjectMarked(object)) return; | |
| 495 UnmarkCObject(object); | |
| 496 if (object->type == Dart_CObject::kArray) { | |
| 497 for (int i = 0; i < object->value.as_array.length; i++) { | |
| 498 Dart_CObject* element = object->value.as_array.values[i]; | |
| 499 UnmarkAllCObjects(element); | |
| 500 } | |
| 501 } | |
| 502 } | |
| 503 | |
| 504 | |
| 505 void MessageWriter::WriteSmi(int64_t value) { | |
| 506 ASSERT(Smi::IsValid64(value)); | |
| 507 Write<RawObject*>(Smi::New(value)); | |
| 508 } | |
| 509 | |
| 510 | |
| 511 void MessageWriter::WriteMint(Dart_CObject* object, int64_t value) { | |
| 512 ASSERT(!Smi::IsValid64(value)); | |
| 513 // Write out the serialization header value for mint object. | |
| 514 WriteInlinedHeader(object); | |
| 515 // Write out the class and tags information. | |
| 516 WriteObjectHeader(ObjectStore::kMintClass, 0); | |
| 517 // Write the 64-bit value. | |
| 518 Write<int64_t>(value); | |
| 519 } | |
| 520 | |
| 521 | |
| 522 void MessageWriter::WriteInt32(Dart_CObject* object) { | |
| 523 int64_t value = object->value.as_int32; | |
| 524 if (Smi::IsValid64(value)) { | |
| 525 WriteSmi(value); | |
| 526 } else { | |
| 527 WriteMint(object, value); | |
| 528 } | |
| 529 } | |
| 530 | |
| 531 | |
| 532 void MessageWriter::WriteInt64(Dart_CObject* object) { | |
| 533 int64_t value = object->value.as_int64; | |
| 534 if (Smi::IsValid64(value)) { | |
| 535 WriteSmi(value); | |
| 536 } else { | |
| 537 WriteMint(object, value); | |
| 538 } | |
| 539 } | |
| 540 | |
| 541 | |
| 542 void MessageWriter::WriteInlinedHeader(Dart_CObject* object) { | |
| 543 // Write out the serialization header value for this object. | |
| 544 WriteSerializationMarker(kInlined, kMaxPredefinedObjectIds + object_id_); | |
| 545 // Mark object with its object id. | |
| 546 MarkCObject(object, object_id_); | |
| 547 // Advance object id. | |
| 548 object_id_++; | |
| 549 } | |
| 550 | |
| 551 | |
| 552 void MessageWriter::WriteCObject(Dart_CObject* object) { | |
| 553 if (IsCObjectMarked(object)) { | |
| 554 intptr_t object_id = GetMarkedCObjectMark(object); | |
| 555 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); | |
| 556 return; | |
| 557 } | |
| 558 | |
| 559 switch (object->type) { | |
| 560 case Dart_CObject::kNull: | |
| 561 WriteIndexedObject(Object::kNullObject); | |
| 562 break; | |
| 563 case Dart_CObject::kBool: | |
| 564 if (object->value.as_bool) { | |
| 565 WriteIndexedObject(ObjectStore::kTrueValue); | |
| 566 } else { | |
| 567 WriteIndexedObject(ObjectStore::kFalseValue); | |
| 568 } | |
| 569 break; | |
| 570 case Dart_CObject::kInt32: | |
| 571 WriteInt32(object); | |
| 572 break; | |
| 573 case Dart_CObject::kInt64: | |
| 574 WriteInt64(object); | |
| 575 break; | |
| 576 case Dart_CObject::kBigint: { | |
| 577 // Write out the serialization header value for this object. | |
| 578 WriteInlinedHeader(object); | |
| 579 // Write out the class and tags information. | |
| 580 WriteObjectHeader(ObjectStore::kBigintClass, 0); | |
| 581 // Write hex string length and content | |
| 582 char* hex_string = object->value.as_bigint; | |
| 583 intptr_t len = strlen(hex_string); | |
| 584 WriteIntptrValue(len); | |
| 585 for (intptr_t i = 0; i < len; i++) { | |
| 586 Write<uint8_t>(hex_string[i]); | |
| 587 } | |
| 588 break; | |
| 589 } | |
| 590 case Dart_CObject::kDouble: | |
| 591 // Write out the serialization header value for this object. | |
| 592 WriteInlinedHeader(object); | |
| 593 // Write out the class and tags information. | |
| 594 WriteObjectHeader(ObjectStore::kDoubleClass, 0); | |
| 595 // Write double value. | |
| 596 Write<double>(object->value.as_double); | |
| 597 break; | |
| 598 case Dart_CObject::kString: { | |
| 599 // Write out the serialization header value for this object. | |
| 600 WriteInlinedHeader(object); | |
| 601 // Write out the class and tags information. | |
| 602 WriteObjectHeader(ObjectStore::kOneByteStringClass, 0); | |
| 603 // Write string length, hash and content | |
| 604 char* str = object->value.as_string; | |
| 605 intptr_t len = strlen(str); | |
| 606 WriteSmi(len); | |
| 607 WriteSmi(0); // TODO(sgjesse): Hash - not written. | |
| 608 for (intptr_t i = 0; i < len; i++) { | |
| 609 Write<uint8_t>(str[i]); | |
| 610 } | |
| 611 break; | |
| 612 } | |
| 613 case Dart_CObject::kArray: { | |
| 614 // Write out the serialization header value for this object. | |
| 615 WriteInlinedHeader(object); | |
| 616 // Write out the class and tags information. | |
| 617 WriteObjectHeader(ObjectStore::kArrayClass, 0); | |
| 618 WriteSmi(object->value.as_array.length); | |
| 619 // Write out the type arguments. | |
| 620 WriteIndexedObject(Object::kNullObject); | |
| 621 // Write out array elements. | |
| 622 for (int i = 0; i < object->value.as_array.length; i++) { | |
| 623 WriteCObject(object->value.as_array.values[i]); | |
| 624 } | |
| 625 break; | |
| 626 } | |
| 627 case Dart_CObject::kByteArray: { | |
| 628 // Write out the serialization header value for this object. | |
| 629 WriteInlinedHeader(object); | |
| 630 // Write out the class and tags information. | |
| 631 WriteObjectHeader(ObjectStore::kInternalByteArrayClass, 0); | |
| 632 uint8_t* bytes = object->value.as_byte_array.values; | |
| 633 intptr_t len = object->value.as_byte_array.length; | |
| 634 WriteSmi(len); | |
| 635 for (intptr_t i = 0; i < len; i++) { | |
| 636 Write<uint8_t>(bytes[i]); | |
| 637 } | |
| 638 break; | |
| 639 } | |
| 640 default: | |
| 641 UNREACHABLE(); | |
| 642 } | |
| 643 } | |
| 644 | |
| 645 | |
| 646 void MessageWriter::WriteCMessage(Dart_CObject* object) { | |
| 647 WriteCObject(object); | |
| 648 UnmarkAllCObjects(object); | |
| 649 FinalizeBuffer(); | |
| 650 } | |
| 651 | |
| 652 | |
| 653 void SnapshotWriter::WriteObject(RawObject* rawobj) { | 440 void SnapshotWriter::WriteObject(RawObject* rawobj) { |
| 654 // An object is written in one of the following ways: | 441 // An object is written in one of the following ways: |
| 655 // - Smi: the Smi value is written as is (last bit is not tagged). | 442 // - Smi: the Smi value is written as is (last bit is not tagged). |
| 656 // - VM internal class (from VM isolate): (index of class in vm isolate | 0x3) | 443 // - VM internal class (from VM isolate): (index of class in vm isolate | 0x3) |
| 657 // - Object that has already been written: (negative id in stream | 0x3) | 444 // - Object that has already been written: (negative id in stream | 0x3) |
| 658 // - Object that is seen for the first time (inlined as follows): | 445 // - Object that is seen for the first time (inlined as follows): |
| 659 // (object size in multiples of kObjectAlignment | 0x1) | 446 // (object size in multiples of kObjectAlignment | 0x1) |
| 660 // serialized fields of the object | 447 // serialized fields of the object |
| 661 // ...... | 448 // ...... |
| 662 | 449 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 | 640 |
| 854 | 641 |
| 855 void SnapshotWriterVisitor::VisitPointers(RawObject** first, RawObject** last) { | 642 void SnapshotWriterVisitor::VisitPointers(RawObject** first, RawObject** last) { |
| 856 for (RawObject** current = first; current <= last; current++) { | 643 for (RawObject** current = first; current <= last; current++) { |
| 857 RawObject* raw_obj = *current; | 644 RawObject* raw_obj = *current; |
| 858 writer_->WriteObject(raw_obj); | 645 writer_->WriteObject(raw_obj); |
| 859 } | 646 } |
| 860 } | 647 } |
| 861 | 648 |
| 862 } // namespace dart | 649 } // namespace dart |
| OLD | NEW |