Chromium Code Reviews| Index: runtime/vm/raw_object_snapshot.cc |
| diff --git a/runtime/vm/raw_object_snapshot.cc b/runtime/vm/raw_object_snapshot.cc |
| index 9a1be618eab884ab160b2025a087f5029beea9a7..562131242bdb0dd7d840c67c63a427c4ca725ae5 100644 |
| --- a/runtime/vm/raw_object_snapshot.cc |
| +++ b/runtime/vm/raw_object_snapshot.cc |
| @@ -990,147 +990,137 @@ void RawString::WriteTo(SnapshotWriter* writer, |
| } |
| -RawOneByteString* OneByteString::ReadFrom(SnapshotReader* reader, |
| - intptr_t object_id, |
| - bool classes_serialized) { |
| +template<typename CharType, |
| + typename StringType, |
| + typename RawStringType, |
| + typename CharAddrType, |
| + typename SetHashType> |
| +static RawStringType* ReadStringFrom(SnapshotReader* reader, |
| + intptr_t object_id, |
| + bool classes_serialized, |
| + CharAddrType char_addr, |
| + SetHashType set_hash) { |
| ASSERT(reader != NULL); |
| // Read the length so that we can determine instance size to allocate. |
| RawSmi* smi_len = GetSmi(reader->Read<intptr_t>()); |
| intptr_t len = Smi::Value(smi_len); |
| RawSmi* smi_hash = GetSmi(reader->Read<intptr_t>()); |
| - // Set up the one byte string object. |
| - OneByteString& str_obj = OneByteString::ZoneHandle( |
| - OneByteString::New(len, classes_serialized ? Heap::kOld : Heap::kNew)); |
| + // Set up the string object. |
| + StringType& str_obj = StringType::ZoneHandle( |
| + StringType::New(len, classes_serialized ? Heap::kOld : Heap::kNew)); |
| for (intptr_t i = 0; i < len; i++) { |
| - *str_obj.CharAddr(i) = reader->Read<uint8_t>(); |
| + *((str_obj).*(char_addr))(i) = reader->Read<CharType>(); |
| } |
| reader->AddBackwardReference(object_id, &str_obj); |
| - RawOneByteString* raw_str = str_obj.raw(); |
| - raw_str->ptr()->hash_ = smi_hash; |
| + ((str_obj).*(set_hash))(Smi::Value(smi_hash)); |
|
turnidge
2011/10/27 17:28:35
Thinking out loud... Optional...
If you wanted t
|
| return str_obj.raw(); |
| } |
| -void RawOneByteString::WriteTo(SnapshotWriter* writer, |
| - intptr_t object_id, |
| - bool serialize_classes) { |
| - ASSERT(writer != NULL); |
| - intptr_t len = Smi::Value(ptr()->length_); |
| - |
| - // Write out the serialization header value for this object. |
| - writer->WriteObjectHeader(kInlined, object_id); |
| - |
| - // Write out the class information. |
| - writer->WriteObjectHeader(kObjectId, ObjectStore::kOneByteStringClass); |
| - |
| - // Write out the length field. |
| - writer->Write<RawObject*>(ptr()->length_); |
| - |
| - // Write out the hash field. |
| - writer->Write<RawObject*>(ptr()->hash_); |
| - |
| - // Write out the string. |
| - for (intptr_t i = 0; i < len; i++) { |
| - writer->Write<uint8_t>(ptr()->data_[i]); |
| - } |
| +RawOneByteString* OneByteString::ReadFrom(SnapshotReader* reader, |
| + intptr_t object_id, |
| + bool classes_serialized) { |
| + return ReadStringFrom<uint8_t, OneByteString, RawOneByteString>( |
| + reader, |
| + object_id, |
| + classes_serialized, |
| + &OneByteString::CharAddr, |
| + &OneByteString::SetHash); |
| } |
| RawTwoByteString* TwoByteString::ReadFrom(SnapshotReader* reader, |
| intptr_t object_id, |
| bool classes_serialized) { |
| - ASSERT(reader != NULL); |
| - // Read the length so that we can determine instance size to allocate. |
| - RawSmi* smi_len = GetSmi(reader->Read<intptr_t>()); |
| - intptr_t len = Smi::Value(smi_len); |
| - RawSmi* smi_hash = GetSmi(reader->Read<intptr_t>()); |
| + return ReadStringFrom<uint16_t, TwoByteString, RawTwoByteString>( |
| + reader, |
| + object_id, |
| + classes_serialized, |
| + &TwoByteString::CharAddr, |
| + &TwoByteString::SetHash); |
| +} |
| - // Set up the two byte string object. |
| - TwoByteString& str_obj = TwoByteString::ZoneHandle( |
| - TwoByteString::New(len, classes_serialized ? Heap::kOld : Heap::kNew)); |
| - for (intptr_t i = 0; i < len; i++) { |
| - *str_obj.CharAddr(i) = reader->Read<uint16_t>(); |
| - } |
| - reader->AddBackwardReference(object_id, &str_obj); |
| - RawTwoByteString* raw_str = str_obj.raw(); |
| - raw_str->ptr()->hash_ = smi_hash; |
| - return str_obj.raw(); |
| +RawFourByteString* FourByteString::ReadFrom(SnapshotReader* reader, |
| + intptr_t object_id, |
| + bool classes_serialized) { |
| + return ReadStringFrom<uint32_t, FourByteString, RawFourByteString>( |
| + reader, |
| + object_id, |
| + classes_serialized, |
| + &FourByteString::CharAddr, |
| + &FourByteString::SetHash); |
| } |
| -void RawTwoByteString::WriteTo(SnapshotWriter* writer, |
| - intptr_t object_id, |
| - bool serialize_classes) { |
| +template<typename CharType> |
| +static void StringWriteTo(SnapshotWriter* writer, |
| + intptr_t object_id, |
| + intptr_t class_id, |
| + bool serialize_classes, |
| + RawSmi* length, |
| + RawSmi* hash, |
| + CharType* data) { |
| ASSERT(writer != NULL); |
| - intptr_t len = Smi::Value(ptr()->length_); |
| + intptr_t len = Smi::Value(length); |
| // Write out the serialization header value for this object. |
| writer->WriteObjectHeader(kInlined, object_id); |
| // Write out the class information. |
| - writer->WriteObjectHeader(kObjectId, ObjectStore::kTwoByteStringClass); |
| + writer->WriteObjectHeader(kObjectId, class_id); |
| // Write out the length field. |
| - writer->Write<RawObject*>(ptr()->length_); |
| + writer->Write<RawObject*>(length); |
| // Write out the hash field. |
| - writer->Write<RawObject*>(ptr()->hash_); |
| + writer->Write<RawObject*>(hash); |
| // Write out the string. |
| for (intptr_t i = 0; i < len; i++) { |
| - writer->Write<uint16_t>(ptr()->data_[i]); |
| + writer->Write<CharType>(data[i]); |
| } |
| } |
| -RawFourByteString* FourByteString::ReadFrom(SnapshotReader* reader, |
| - intptr_t object_id, |
| - bool classes_serialized) { |
| - ASSERT(reader != NULL); |
| - // Read the length so that we can determine instance size to allocate. |
| - RawSmi* smi_len = GetSmi(reader->Read<intptr_t>()); |
| - intptr_t len = Smi::Value(smi_len); |
| - RawSmi* smi_hash = GetSmi(reader->Read<intptr_t>()); |
| +void RawOneByteString::WriteTo(SnapshotWriter* writer, |
| + intptr_t object_id, |
| + bool serialize_classes) { |
| + StringWriteTo<uint8_t>(writer, |
| + object_id, |
| + ObjectStore::kOneByteStringClass, |
| + serialize_classes, |
| + ptr()->length_, |
| + ptr()->hash_, |
| + ptr()->data_); |
| +} |
| - // Set up the four byte string object. |
| - FourByteString& str_obj = FourByteString::ZoneHandle( |
| - FourByteString::New(len, classes_serialized ? Heap::kOld : Heap::kNew)); |
| - for (intptr_t i = 0; i < len; i++) { |
| - *str_obj.CharAddr(i) = reader->Read<uint32_t>(); |
| - } |
| - reader->AddBackwardReference(object_id, &str_obj); |
| - RawFourByteString* raw_str = str_obj.raw(); |
| - raw_str->ptr()->hash_ = smi_hash; |
| - return str_obj.raw(); |
| +void RawTwoByteString::WriteTo(SnapshotWriter* writer, |
| + intptr_t object_id, |
| + bool serialize_classes) { |
| + StringWriteTo<uint16_t>(writer, |
| + object_id, |
| + ObjectStore::kTwoByteStringClass, |
| + serialize_classes, |
| + ptr()->length_, |
| + ptr()->hash_, |
| + ptr()->data_); |
| } |
| void RawFourByteString::WriteTo(SnapshotWriter* writer, |
| intptr_t object_id, |
| bool serialize_classes) { |
| - ASSERT(writer != NULL); |
| - intptr_t len = Smi::Value(ptr()->length_); |
| - |
| - // Write out the serialization header value for this object. |
| - writer->WriteObjectHeader(kInlined, object_id); |
| - |
| - // Write out the class information. |
| - writer->WriteObjectHeader(kObjectId, ObjectStore::kFourByteStringClass); |
| - |
| - // Write out the length field. |
| - writer->Write<RawObject*>(ptr()->length_); |
| - |
| - // Write out the hash field. |
| - writer->Write<RawObject*>(ptr()->hash_); |
| - |
| - // Write out the string. |
| - for (intptr_t i = 0; i < len; i++) { |
| - writer->Write<uint32_t>(ptr()->data_[i]); |
| - } |
| + StringWriteTo<uint32_t>(writer, |
| + object_id, |
| + ObjectStore::kFourByteStringClass, |
| + serialize_classes, |
| + ptr()->length_, |
| + ptr()->hash_, |
| + ptr()->data_); |
| } |