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 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 // updated. | 448 // updated. |
449 memset(&type_arguments_marker, 0, sizeof(type_arguments_marker)); | 449 memset(&type_arguments_marker, 0, sizeof(type_arguments_marker)); |
450 memset(&dynamic_type_marker, 0, sizeof(dynamic_type_marker)); | 450 memset(&dynamic_type_marker, 0, sizeof(dynamic_type_marker)); |
451 type_arguments_marker.type = | 451 type_arguments_marker.type = |
452 static_cast<Dart_CObject::Type>(Dart_CObject_Internal::kTypeArguments); | 452 static_cast<Dart_CObject::Type>(Dart_CObject_Internal::kTypeArguments); |
453 dynamic_type_marker.type = | 453 dynamic_type_marker.type = |
454 static_cast<Dart_CObject::Type>(Dart_CObject_Internal::kDynamicType); | 454 static_cast<Dart_CObject::Type>(Dart_CObject_Internal::kDynamicType); |
455 } | 455 } |
456 | 456 |
457 | 457 |
458 Dart_CMessage* CMessageReader::ReadMessage() { | 458 Dart_CObject* CMessageReader::ReadMessage() { |
459 // Read the object out of the message. | 459 // Read the object out of the message. |
460 Dart_CObject* object = ReadObject(); | 460 return ReadObject(); |
461 | |
462 Dart_CMessage* message = | |
463 reinterpret_cast<Dart_CMessage*>(alloc_(NULL, 0, sizeof(Dart_CMessage))); | |
464 if (message == NULL) return NULL; | |
465 message->root = object; | |
466 return message; | |
467 } | 461 } |
468 | 462 |
469 intptr_t CMessageReader::LookupInternalClass(intptr_t class_header) { | 463 intptr_t CMessageReader::LookupInternalClass(intptr_t class_header) { |
470 SerializedHeaderType header_type = SerializedHeaderTag::decode(class_header); | 464 SerializedHeaderType header_type = SerializedHeaderTag::decode(class_header); |
471 ASSERT(header_type == kObjectId); | 465 ASSERT(header_type == kObjectId); |
472 intptr_t header_value = SerializedHeaderData::decode(class_header); | 466 intptr_t header_value = SerializedHeaderData::decode(class_header); |
473 return header_value; | 467 return header_value; |
474 } | 468 } |
475 | 469 |
476 | 470 |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 | 688 |
695 // Write out the individual Smis. | 689 // Write out the individual Smis. |
696 for (int i = 0; i < field_count; i++) { | 690 for (int i = 0; i < field_count; i++) { |
697 Write<RawObject*>(Integer::New(data[i])); | 691 Write<RawObject*>(Integer::New(data[i])); |
698 } | 692 } |
699 | 693 |
700 FinalizeBuffer(); | 694 FinalizeBuffer(); |
701 } | 695 } |
702 | 696 |
703 | 697 |
| 698 void MessageWriter::MarkCObject(Dart_CObject* object, intptr_t object_id) { |
| 699 // Mark the object as serialized by adding the object id to the |
| 700 // upper bits of the type field in the Dart_CObject structure. Add |
| 701 // an offset for making marking of object id 0 possible. |
| 702 ASSERT(!IsCObjectMarked(object)); |
| 703 intptr_t mark_value = object_id + kDartCObjectMarkOffset; |
| 704 object->type = static_cast<Dart_CObject::Type>( |
| 705 ((mark_value) << kDartCObjectTypeBits) | object->type); |
| 706 } |
| 707 |
| 708 |
| 709 void MessageWriter::UnmarkCObject(Dart_CObject* object) { |
| 710 ASSERT(IsCObjectMarked(object)); |
| 711 object->type = static_cast<Dart_CObject::Type>( |
| 712 object->type & kDartCObjectTypeMask); |
| 713 } |
| 714 |
| 715 |
| 716 bool MessageWriter::IsCObjectMarked(Dart_CObject* object) { |
| 717 return (object->type & kDartCObjectMarkMask) != 0; |
| 718 } |
| 719 |
| 720 |
| 721 intptr_t MessageWriter::GetMarkedCObjectMark(Dart_CObject* object) { |
| 722 ASSERT(IsCObjectMarked(object)); |
| 723 intptr_t mark_value = ((object->type & kDartCObjectMarkMask) >> 3); |
| 724 // An offset was added to object id for making marking object id 0 possible. |
| 725 return mark_value - kDartCObjectMarkOffset; |
| 726 } |
| 727 |
| 728 |
| 729 void MessageWriter::UnmarkAllCObjects(Dart_CObject* object) { |
| 730 if (!IsCObjectMarked(object)) return; |
| 731 UnmarkCObject(object); |
| 732 if (object->type == Dart_CObject::kArray) { |
| 733 for (int i = 0; i < object->value.as_array.length; i++) { |
| 734 Dart_CObject* element = object->value.as_array.values[i]; |
| 735 UnmarkAllCObjects(element); |
| 736 } |
| 737 } |
| 738 } |
| 739 |
| 740 |
| 741 void MessageWriter::WriteSmi(int32_t value) { |
| 742 Write<RawObject*>(Smi::New(value)); |
| 743 } |
| 744 |
| 745 |
| 746 void MessageWriter::WriteInlinedHeader(Dart_CObject* object) { |
| 747 // Write out the serialization header value for this object. |
| 748 WriteSerializationMarker(kInlined, kMaxPredefinedObjectIds + object_id_); |
| 749 // Mark object with its object id. |
| 750 MarkCObject(object, object_id_); |
| 751 // Advance object id. |
| 752 object_id_++; |
| 753 } |
| 754 |
| 755 |
| 756 void MessageWriter::WriteCObject(Dart_CObject* object) { |
| 757 if (IsCObjectMarked(object)) { |
| 758 intptr_t object_id = GetMarkedCObjectMark(object); |
| 759 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); |
| 760 return; |
| 761 } |
| 762 |
| 763 switch (object->type) { |
| 764 case Dart_CObject::kNull: |
| 765 WriteIndexedObject(Object::kNullObject); |
| 766 break; |
| 767 case Dart_CObject::kBool: |
| 768 if (object->value.as_bool) { |
| 769 WriteIndexedObject(ObjectStore::kTrueValue); |
| 770 } else { |
| 771 WriteIndexedObject(ObjectStore::kFalseValue); |
| 772 } |
| 773 break; |
| 774 case Dart_CObject::kInt32: { |
| 775 WriteSmi(object->value.as_int32); |
| 776 break; |
| 777 } |
| 778 case Dart_CObject::kDouble: |
| 779 // Write out the serialization header value for this object. |
| 780 WriteInlinedHeader(object); |
| 781 // Write out the class and tags information. |
| 782 WriteObjectHeader(ObjectStore::kDoubleClass, 0); |
| 783 // Write double value. |
| 784 Write<double>(object->value.as_double); |
| 785 break; |
| 786 case Dart_CObject::kString: { |
| 787 // Write out the serialization header value for this object. |
| 788 WriteInlinedHeader(object); |
| 789 // Write out the class and tags information. |
| 790 WriteObjectHeader(ObjectStore::kOneByteStringClass, 0); |
| 791 // Write string length, hash and content |
| 792 char* str = object->value.as_string; |
| 793 intptr_t len = strlen(str); |
| 794 WriteSmi(len); |
| 795 WriteSmi(0); // TODO(sgjesse): Hash - not written. |
| 796 for (intptr_t i = 0; i < len; i++) { |
| 797 Write<uint8_t>(str[i]); |
| 798 } |
| 799 break; |
| 800 } |
| 801 case Dart_CObject::kArray: { |
| 802 // Write out the serialization header value for this object. |
| 803 WriteInlinedHeader(object); |
| 804 // Write out the class and tags information. |
| 805 WriteObjectHeader(ObjectStore::kArrayClass, 0); |
| 806 WriteSmi(object->value.as_array.length); |
| 807 // Write out the type arguments. |
| 808 WriteIndexedObject(Object::kNullObject); |
| 809 // Write out array elements. |
| 810 for (int i = 0; i < object->value.as_array.length; i++) { |
| 811 WriteCObject(object->value.as_array.values[i]); |
| 812 } |
| 813 break; |
| 814 } |
| 815 default: |
| 816 UNREACHABLE(); |
| 817 } |
| 818 } |
| 819 |
| 820 |
| 821 void MessageWriter::WriteCMessage(Dart_CObject* object) { |
| 822 WriteCObject(object); |
| 823 UnmarkAllCObjects(object); |
| 824 FinalizeBuffer(); |
| 825 } |
| 826 |
| 827 |
704 void SnapshotWriter::WriteObject(RawObject* rawobj) { | 828 void SnapshotWriter::WriteObject(RawObject* rawobj) { |
705 // An object is written in one of the following ways: | 829 // An object is written in one of the following ways: |
706 // - Smi: the Smi value is written as is (last bit is not tagged). | 830 // - Smi: the Smi value is written as is (last bit is not tagged). |
707 // - VM internal class (from VM isolate): (index of class in vm isolate | 0x3) | 831 // - VM internal class (from VM isolate): (index of class in vm isolate | 0x3) |
708 // - Object that has already been written: (negative id in stream | 0x3) | 832 // - Object that has already been written: (negative id in stream | 0x3) |
709 // - Object that is seen for the first time (inlined as follows): | 833 // - Object that is seen for the first time (inlined as follows): |
710 // (object size in multiples of kObjectAlignment | 0x1) | 834 // (object size in multiples of kObjectAlignment | 0x1) |
711 // serialized fields of the object | 835 // serialized fields of the object |
712 // ...... | 836 // ...... |
713 | 837 |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 | 1028 |
905 | 1029 |
906 void SnapshotWriterVisitor::VisitPointers(RawObject** first, RawObject** last) { | 1030 void SnapshotWriterVisitor::VisitPointers(RawObject** first, RawObject** last) { |
907 for (RawObject** current = first; current <= last; current++) { | 1031 for (RawObject** current = first; current <= last; current++) { |
908 RawObject* raw_obj = *current; | 1032 RawObject* raw_obj = *current; |
909 writer_->WriteObject(raw_obj); | 1033 writer_->WriteObject(raw_obj); |
910 } | 1034 } |
911 } | 1035 } |
912 | 1036 |
913 } // namespace dart | 1037 } // namespace dart |
OLD | NEW |