Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: runtime/vm/snapshot.cc

Issue 9303031: Add support for lists and backward references when decoding a message to a Dart_CObject object (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased to r3830 Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/snapshot.h ('k') | runtime/vm/snapshot_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 CMessageReader::CMessageReader(const uint8_t* buffer, 440 CMessageReader::CMessageReader(const uint8_t* buffer,
441 intptr_t length, 441 intptr_t length,
442 ReAlloc alloc) 442 ReAlloc alloc)
443 : BaseReader(buffer, length), alloc_(alloc) { 443 : BaseReader(buffer, length),
444 alloc_(alloc),
445 backward_references_(kNumInitialReferences) {
446 // Initialize marker objects used to handle Lists.
447 // TODO(sjesse): Remove this when message serialization format is
448 // updated.
449 memset(&type_arguments_marker, 0, sizeof(type_arguments_marker));
450 memset(&dynamic_type_marker, 0, sizeof(dynamic_type_marker));
451 type_arguments_marker.type =
452 static_cast<Dart_CObject::Type>(Dart_CObject_Internal::kTypeArguments);
453 dynamic_type_marker.type =
454 static_cast<Dart_CObject::Type>(Dart_CObject_Internal::kDynamicType);
444 } 455 }
445 456
446 457
447 intptr_t CMessageReader::LookupInternalClass(intptr_t class_header) { 458 intptr_t CMessageReader::LookupInternalClass(intptr_t class_header) {
448 SerializedHeaderType header_type = SerializedHeaderTag::decode(class_header); 459 SerializedHeaderType header_type = SerializedHeaderTag::decode(class_header);
449 ASSERT(header_type == kObjectId); 460 ASSERT(header_type == kObjectId);
450 intptr_t header_value = SerializedHeaderData::decode(class_header); 461 intptr_t header_value = SerializedHeaderData::decode(class_header);
451 return header_value; 462 return header_value;
452 } 463 }
453 464
454 465
455 Dart_CObject* CMessageReader::AllocateDartValue(Dart_CObject::Type type) { 466 Dart_CObject* CMessageReader::AllocateDartCObject(Dart_CObject::Type type) {
456 Dart_CObject* value = 467 Dart_CObject* value =
457 reinterpret_cast<Dart_CObject*>( 468 reinterpret_cast<Dart_CObject*>(
458 alloc_(NULL, 0, sizeof(Dart_CObject))); 469 alloc_(NULL, 0, sizeof(Dart_CObject)));
459 value->type = type; 470 value->type = type;
460 return value; 471 return value;
461 } 472 }
462 473
463 474
464 Dart_CObject* CMessageReader::AllocateDartValueNull() { 475 Dart_CObject* CMessageReader::AllocateDartCObjectNull() {
465 return AllocateDartValue(Dart_CObject::kNull); 476 return AllocateDartCObject(Dart_CObject::kNull);
466 } 477 }
467 478
468 479
469 Dart_CObject* CMessageReader::AllocateDartValueBool(bool val) { 480 Dart_CObject* CMessageReader::AllocateDartCObjectBool(bool val) {
470 Dart_CObject* value = AllocateDartValue(Dart_CObject::kBool); 481 Dart_CObject* value = AllocateDartCObject(Dart_CObject::kBool);
471 value->value.as_bool = val; 482 value->value.as_bool = val;
472 return value; 483 return value;
473 } 484 }
474 485
475 486
476 Dart_CObject* CMessageReader::AllocateDartValueInt32(int32_t val) { 487 Dart_CObject* CMessageReader::AllocateDartCObjectInt32(int32_t val) {
477 Dart_CObject* value = AllocateDartValue(Dart_CObject::kInt32); 488 Dart_CObject* value = AllocateDartCObject(Dart_CObject::kInt32);
478 value->value.as_int32 = val; 489 value->value.as_int32 = val;
479 return value; 490 return value;
480 } 491 }
481 492
482 493
483 Dart_CObject* CMessageReader::AllocateDartValueDouble(double val) { 494 Dart_CObject* CMessageReader::AllocateDartCObjectDouble(double val) {
484 Dart_CObject* value = AllocateDartValue(Dart_CObject::kDouble); 495 Dart_CObject* value = AllocateDartCObject(Dart_CObject::kDouble);
485 value->value.as_double = val; 496 value->value.as_double = val;
486 return value; 497 return value;
487 } 498 }
488 499
489 500
490 Dart_CObject* CMessageReader::AllocateDartValueString(intptr_t length) { 501 Dart_CObject* CMessageReader::AllocateDartCObjectString(intptr_t length) {
491 // Allocate a Dart_CObject structure followed by an array of chars 502 // Allocate a Dart_CObject structure followed by an array of chars
492 // for the string content. The pointer to the string content is set 503 // for the string content. The pointer to the string content is set
493 // up to this area. 504 // up to this area.
494 Dart_CObject* value = 505 Dart_CObject* value =
495 reinterpret_cast<Dart_CObject*>( 506 reinterpret_cast<Dart_CObject*>(
496 alloc_(NULL, 0, sizeof(Dart_CObject) + length + 1)); 507 alloc_(NULL, 0, sizeof(Dart_CObject) + length + 1));
497 value->value.as_string = reinterpret_cast<char*>(value) + sizeof(*value); 508 value->value.as_string = reinterpret_cast<char*>(value) + sizeof(*value);
498 value->type = Dart_CObject::kString; 509 value->type = Dart_CObject::kString;
499 return value; 510 return value;
500 } 511 }
501 512
502 513
503 Dart_CObject* CMessageReader::AllocateDartValueArray(intptr_t length) { 514 Dart_CObject* CMessageReader::AllocateDartCObjectArray(intptr_t length) {
504 // Allocate a Dart_CObject structure followed by an array of 515 // Allocate a Dart_CObject structure followed by an array of
505 // pointers to Dart_CObject structures. The pointer to the array 516 // pointers to Dart_CObject structures. The pointer to the array
506 // content is set up to this area. 517 // content is set up to this area.
507 Dart_CObject* value = 518 Dart_CObject* value =
508 reinterpret_cast<Dart_CObject*>( 519 reinterpret_cast<Dart_CObject*>(
509 alloc_(NULL, 0, sizeof(Dart_CObject) + length * sizeof(value))); 520 alloc_(NULL, 0, sizeof(Dart_CObject) + length * sizeof(value)));
510 value->type = Dart_CObject::kArray; 521 value->type = Dart_CObject::kArray;
511 value->value.as_array.length = length; 522 value->value.as_array.length = length;
512 if (length > 0) { 523 if (length > 0) {
513 value->value.as_array.values = reinterpret_cast<Dart_CObject**>(value + 1); 524 value->value.as_array.values = reinterpret_cast<Dart_CObject**>(value + 1);
(...skipping 15 matching lines...) Expand all
529 if (SerializedHeaderData::decode(class_header) == kInstanceId) { 540 if (SerializedHeaderData::decode(class_header) == kInstanceId) {
530 return NULL; 541 return NULL;
531 } 542 }
532 543
533 ASSERT((class_header & kSmiTagMask) != 0); 544 ASSERT((class_header & kSmiTagMask) != 0);
534 class_id = LookupInternalClass(class_header); 545 class_id = LookupInternalClass(class_header);
535 switch (class_id) { 546 switch (class_id) {
536 case Object::kClassClass: { 547 case Object::kClassClass: {
537 return NULL; 548 return NULL;
538 } 549 }
550 case Object::kTypeArgumentsClass: {
551 // TODO(sjesse): Remove this when message serialization format is
552 // updated (currently length is leaked).
553 AddBackwardReference(object_id, NULL);
554 Dart_CObject* length = ReadObject();
555 ASSERT(length->type == Dart_CObject::kInt32);
556 for (int i = 0; i < length->value.as_int32; i++) {
557 Dart_CObject* type = ReadObject();
558 if (type != &dynamic_type_marker) return NULL;
559 }
560 return &type_arguments_marker;
561 break;
562 }
539 case ObjectStore::kArrayClass: { 563 case ObjectStore::kArrayClass: {
540 intptr_t len = ReadSmiValue(); 564 intptr_t len = ReadSmiValue();
565 Dart_CObject* value = AllocateDartCObjectArray(len);
566 AddBackwardReference(object_id, value);
541 // Skip type arguments. 567 // Skip type arguments.
568 // TODO(sjesse): Remove this when message serialization format is
569 // updated (currently type_arguments is leaked).
542 Dart_CObject* type_arguments = ReadObject(); 570 Dart_CObject* type_arguments = ReadObject();
543 if (type_arguments == NULL || 571 if (type_arguments != &type_arguments_marker &&
544 type_arguments->type != Dart_CObject::kNull) { 572 type_arguments->type != Dart_CObject::kNull) {
545 return NULL; 573 return NULL;
546 } 574 }
547 Dart_CObject* value = AllocateDartValueArray(len);
548 for (int i = 0; i < len; i++) { 575 for (int i = 0; i < len; i++) {
549 value->value.as_array.values[i] = ReadObject(); 576 value->value.as_array.values[i] = ReadObject();
550 } 577 }
551 return value; 578 return value;
552 break; 579 break;
553 } 580 }
554 case ObjectStore::kDoubleClass: { 581 case ObjectStore::kDoubleClass: {
555 // Read the double value for the object. 582 // Read the double value for the object.
556 return AllocateDartValueDouble(Read<double>()); 583 Dart_CObject* object = AllocateDartCObjectDouble(Read<double>());
584 AddBackwardReference(object_id, object);
585 return object;
557 break; 586 break;
558 } 587 }
559 case ObjectStore::kOneByteStringClass: { 588 case ObjectStore::kOneByteStringClass: {
560 intptr_t len = ReadSmiValue(); 589 intptr_t len = ReadSmiValue();
561 intptr_t hash = ReadSmiValue(); 590 intptr_t hash = ReadSmiValue();
562 USE(hash); 591 USE(hash);
563 Dart_CObject* value = AllocateDartValueString(len); 592 Dart_CObject* object = AllocateDartCObjectString(len);
564 char* p = value->value.as_string; 593 AddBackwardReference(object_id, object);
594 char* p = object->value.as_string;
565 for (intptr_t i = 0; i < len; i++) { 595 for (intptr_t i = 0; i < len; i++) {
566 *p = Read<uint8_t>(); 596 *p = Read<uint8_t>();
567 p++; 597 p++;
568 } 598 }
569 *p = '\0'; 599 *p = '\0';
570 return value; 600 return object;
571 break; 601 break;
572 } 602 }
573 case ObjectStore::kTwoByteStringClass: 603 case ObjectStore::kTwoByteStringClass:
574 // Two byte strings not supported. 604 // Two byte strings not supported.
575 return NULL; 605 return NULL;
576 break; 606 break;
577 case ObjectStore::kFourByteStringClass: 607 case ObjectStore::kFourByteStringClass:
578 // Four byte strings not supported. 608 // Four byte strings not supported.
579 return NULL; 609 return NULL;
580 break; 610 break;
581 default: 611 default:
582 // Everything else not supported. 612 // Everything else not supported.
583 return NULL; 613 return NULL;
584 } 614 }
585 } 615 }
586 616
587 617
588 Dart_CObject* CMessageReader::ReadIndexedObject(intptr_t object_id) { 618 Dart_CObject* CMessageReader::ReadIndexedObject(intptr_t object_id) {
589 if (object_id == Object::kNullObject) { 619 if (object_id == Object::kNullObject) {
590 return AllocateDartValueNull(); 620 return AllocateDartCObjectNull();
591 } else if (object_id == ObjectStore::kTrueValue) { 621 } else if (object_id == ObjectStore::kTrueValue) {
592 return AllocateDartValueBool(true); 622 return AllocateDartCObjectBool(true);
593 } else if (object_id == ObjectStore::kFalseValue) { 623 } else if (object_id == ObjectStore::kFalseValue) {
594 return AllocateDartValueBool(false); 624 return AllocateDartCObjectBool(false);
625 } else if (object_id == ObjectStore::kDynamicType ||
626 object_id == ObjectStore::kDoubleInterface ||
627 object_id == ObjectStore::kIntInterface ||
628 object_id == ObjectStore::kBoolInterface ||
629 object_id == ObjectStore::kStringInterface) {
630 // Always return dynamic type (this is only a marker).
631 return &dynamic_type_marker;
595 } else { 632 } else {
596 // TODO(sgjesse): Handle back-references. 633 intptr_t index = object_id - kMaxPredefinedObjectIds;
597 UNREACHABLE(); 634 ASSERT(index < backward_references_.length());
635 ASSERT(backward_references_[index] != NULL);
636 return backward_references_[index];
598 } 637 }
599 return NULL; 638 return NULL;
600 } 639 }
601 640
602 641
603 Dart_CObject* CMessageReader::ReadObjectImpl(intptr_t header) { 642 Dart_CObject* CMessageReader::ReadObjectImpl(intptr_t header) {
604 SerializedHeaderType header_type = SerializedHeaderTag::decode(header); 643 SerializedHeaderType header_type = SerializedHeaderTag::decode(header);
605 intptr_t header_value = SerializedHeaderData::decode(header); 644 intptr_t header_value = SerializedHeaderData::decode(header);
606 645
607 if (header_type == kObjectId) { 646 if (header_type == kObjectId) {
608 return ReadIndexedObject(header_value); 647 return ReadIndexedObject(header_value);
609 } 648 }
610 ASSERT(header_type == kInlined); 649 ASSERT(header_type == kInlined);
611 return ReadInlinedObject(header_value); 650 return ReadInlinedObject(header_value);
612 } 651 }
613 652
614 653
615 Dart_CObject* CMessageReader::ReadObject() { 654 Dart_CObject* CMessageReader::ReadObject() {
616 int64_t value = Read<int64_t>(); 655 int64_t value = Read<int64_t>();
617 if ((value & kSmiTagMask) == 0) { 656 if ((value & kSmiTagMask) == 0) {
618 Dart_CObject* dart_value = AllocateDartValueInt32(value >> kSmiTagShift); 657 Dart_CObject* dart_value = AllocateDartCObjectInt32(value >> kSmiTagShift);
619 return dart_value; 658 return dart_value;
620 } 659 }
621 ASSERT((value <= kIntptrMax) && (value >= kIntptrMin)); 660 ASSERT((value <= kIntptrMax) && (value >= kIntptrMin));
622 return ReadObjectImpl(value); 661 return ReadObjectImpl(value);
623 } 662 }
624 663
625 664
665 void CMessageReader::AddBackwardReference(intptr_t id, Dart_CObject* obj) {
666 ASSERT((id - kMaxPredefinedObjectIds) == backward_references_.length());
667 backward_references_.Add(obj);
668 }
669
670
626 void MessageWriter::WriteMessage(intptr_t field_count, intptr_t *data) { 671 void MessageWriter::WriteMessage(intptr_t field_count, intptr_t *data) {
627 // Write out the serialization header value for this object. 672 // Write out the serialization header value for this object.
628 WriteSerializationMarker(kInlined, kMaxPredefinedObjectIds); 673 WriteSerializationMarker(kInlined, kMaxPredefinedObjectIds);
629 674
630 // Write out the class and tags information. 675 // Write out the class and tags information.
631 WriteObjectHeader(ObjectStore::kArrayClass, 0); 676 WriteObjectHeader(ObjectStore::kArrayClass, 0);
632 677
633 // Write out the length field. 678 // Write out the length field.
634 Write<RawObject*>(Smi::New(field_count)); 679 Write<RawObject*>(Smi::New(field_count));
635 680
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 893
849 894
850 void SnapshotWriterVisitor::VisitPointers(RawObject** first, RawObject** last) { 895 void SnapshotWriterVisitor::VisitPointers(RawObject** first, RawObject** last) {
851 for (RawObject** current = first; current <= last; current++) { 896 for (RawObject** current = first; current <= last; current++) {
852 RawObject* raw_obj = *current; 897 RawObject* raw_obj = *current;
853 writer_->WriteObject(raw_obj); 898 writer_->WriteObject(raw_obj);
854 } 899 }
855 } 900 }
856 901
857 } // namespace dart 902 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/snapshot.h ('k') | runtime/vm/snapshot_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698