Chromium Code Reviews| Index: runtime/vm/object.cc |
| diff --git a/runtime/vm/object.cc b/runtime/vm/object.cc |
| index 711186f5fe4a78c093baea636267d53df2d74fd3..87862a003d4e6f06de53cd41a9661a07e54a7d59 100644 |
| --- a/runtime/vm/object.cc |
| +++ b/runtime/vm/object.cc |
| @@ -15,6 +15,7 @@ |
| #include "vm/compiler_stats.h" |
| #include "vm/class_finalizer.h" |
| #include "vm/dart.h" |
| +#include "vm/dart_api_state.h" |
| #include "vm/dart_entry.h" |
| #include "vm/debuginfo.h" |
| #include "vm/exceptions.h" |
| @@ -6546,7 +6547,7 @@ RawString* String::New(const String& str, Heap::Space space) { |
| RawString* String::NewExternal(const uint8_t* characters, |
| intptr_t len, |
| void* peer, |
| - PeerFinalizer callback, |
| + Dart_PeerFinalizer callback, |
| Heap::Space space) { |
| return ExternalOneByteString::New(characters, len, peer, callback, space); |
| } |
| @@ -6555,7 +6556,7 @@ RawString* String::NewExternal(const uint8_t* characters, |
| RawString* String::NewExternal(const uint16_t* characters, |
| intptr_t len, |
| void* peer, |
| - PeerFinalizer callback, |
| + Dart_PeerFinalizer callback, |
| Heap::Space space) { |
| return ExternalTwoByteString::New(characters, len, peer, callback, space); |
| } |
| @@ -6564,7 +6565,7 @@ RawString* String::NewExternal(const uint16_t* characters, |
| RawString* String::NewExternal(const uint32_t* characters, |
| intptr_t len, |
| void* peer, |
| - PeerFinalizer callback, |
| + Dart_PeerFinalizer callback, |
| Heap::Space space) { |
| return ExternalFourByteString::New(characters, len, peer, callback, space); |
| } |
| @@ -7331,19 +7332,33 @@ const char* FourByteString::ToCString() const { |
| } |
| -RawExternalOneByteString* ExternalOneByteString::New(const uint8_t* data, |
| - intptr_t len, |
| - void* peer, |
| - PeerFinalizer callback, |
| - Heap::Space space) { |
| +static void AddFinalizer(const Object& referent, |
| + void* peer, |
| + Dart_PeerFinalizer callback) { |
| + ApiState* state = Isolate::Current()->api_state(); |
| + ASSERT(state != NULL); |
| + WeakPersistentHandle* weak_ref = |
| + state->weak_persistent_handles().AllocateHandle(); |
| + weak_ref->set_raw(referent); |
| + weak_ref->set_peer(peer); |
| + weak_ref->set_callback(callback); |
|
siva
2012/02/11 03:00:58
As discussed offline we need a strategy to delete
cshapiro
2012/02/11 04:11:50
Yes, done. The strategy I chose was to add a new
|
| +} |
| + |
| + |
| +RawExternalOneByteString* ExternalOneByteString::New( |
| + const uint8_t* data, |
| + intptr_t len, |
| + void* peer, |
| + Dart_PeerFinalizer callback, |
| + Heap::Space space) { |
| Isolate* isolate = Isolate::Current(); |
| const Class& cls = |
| Class::Handle(isolate->object_store()->external_one_byte_string_class()); |
| ExternalOneByteString& result = ExternalOneByteString::Handle(); |
| + ExternalStringData<uint8_t>* external_data = |
| + new ExternalStringData<uint8_t>(data, peer, callback); |
| { |
| - ExternalStringData<uint8_t>* external_data = |
| - new ExternalStringData<uint8_t>(data, peer, callback); |
| RawObject* raw = Object::Allocate(cls, |
| ExternalOneByteString::InstanceSize(), |
| space); |
| @@ -7353,28 +7368,38 @@ RawExternalOneByteString* ExternalOneByteString::New(const uint8_t* data, |
| result.SetHash(0); |
| result.SetExternalData(external_data); |
| } |
| + AddFinalizer(result, external_data, ExternalOneByteString::Finalize); |
| return result.raw(); |
| } |
| +void ExternalOneByteString::Finalize(Dart_Handle handle, void* peer) { |
| + ExternalStringData<uint8_t>* external_data = |
| + reinterpret_cast<ExternalStringData<uint8_t>*>(peer); |
| + external_data->Finalize(handle); |
| + delete external_data; |
| +} |
| + |
| + |
| const char* ExternalOneByteString::ToCString() const { |
| return String::ToCString(); |
| } |
| -RawExternalTwoByteString* ExternalTwoByteString::New(const uint16_t* data, |
| - intptr_t len, |
| - void* peer, |
| - PeerFinalizer callback, |
| - Heap::Space space) { |
| +RawExternalTwoByteString* ExternalTwoByteString::New( |
| + const uint16_t* data, |
| + intptr_t len, |
| + void* peer, |
| + Dart_PeerFinalizer callback, |
| + Heap::Space space) { |
| Isolate* isolate = Isolate::Current(); |
| const Class& cls = |
| Class::Handle(isolate->object_store()->external_two_byte_string_class()); |
| ExternalTwoByteString& result = ExternalTwoByteString::Handle(); |
| + ExternalStringData<uint16_t>* external_data = |
| + new ExternalStringData<uint16_t>(data, peer, callback); |
| { |
| - ExternalStringData<uint16_t>* external_data = |
| - new ExternalStringData<uint16_t>(data, peer, callback); |
| RawObject* raw = Object::Allocate(cls, |
| ExternalTwoByteString::InstanceSize(), |
| space); |
| @@ -7384,28 +7409,38 @@ RawExternalTwoByteString* ExternalTwoByteString::New(const uint16_t* data, |
| result.SetHash(0); |
| result.SetExternalData(external_data); |
| } |
| + AddFinalizer(result, external_data, ExternalTwoByteString::Finalize); |
| return result.raw(); |
| } |
| +void ExternalTwoByteString::Finalize(Dart_Handle handle, void* peer) { |
| + ExternalStringData<uint16_t>* external_data = |
| + reinterpret_cast<ExternalStringData<uint16_t>*>(peer); |
| + external_data->Finalize(handle); |
| + delete external_data; |
| +} |
| + |
| + |
| const char* ExternalTwoByteString::ToCString() const { |
| return String::ToCString(); |
| } |
| -RawExternalFourByteString* ExternalFourByteString::New(const uint32_t* data, |
| - intptr_t len, |
| - void* peer, |
| - PeerFinalizer callback, |
| - Heap::Space space) { |
| +RawExternalFourByteString* ExternalFourByteString::New( |
| + const uint32_t* data, |
| + intptr_t len, |
| + void* peer, |
| + Dart_PeerFinalizer callback, |
| + Heap::Space space) { |
| Isolate* isolate = Isolate::Current(); |
| const Class& cls = |
| Class::Handle(isolate->object_store()->external_four_byte_string_class()); |
| ExternalFourByteString& result = ExternalFourByteString::Handle(); |
| + ExternalStringData<uint32_t>* external_data = |
| + new ExternalStringData<uint32_t>(data, peer, callback); |
| { |
| - ExternalStringData<uint32_t>* external_data = |
| - new ExternalStringData<uint32_t>(data, peer, callback); |
| RawObject* raw = Object::Allocate(cls, |
| ExternalFourByteString::InstanceSize(), |
| space); |
| @@ -7415,10 +7450,19 @@ RawExternalFourByteString* ExternalFourByteString::New(const uint32_t* data, |
| result.SetHash(0); |
| result.SetExternalData(external_data); |
| } |
| + AddFinalizer(result, external_data, ExternalFourByteString::Finalize); |
| return result.raw(); |
| } |
| +void ExternalFourByteString::Finalize(Dart_Handle handle, void* peer) { |
| + ExternalStringData<uint32_t>* external_data = |
| + reinterpret_cast<ExternalStringData<uint32_t>*>(peer); |
| + external_data->Finalize(handle); |
| + delete external_data; |
| +} |
| + |
| + |
| const char* ExternalFourByteString::ToCString() const { |
| return String::ToCString(); |
| } |
| @@ -7659,13 +7703,25 @@ const char* InternalByteArray::ToCString() const { |
| } |
| +void ExternalByteArray::Finalize(Dart_Handle handle, void* peer) { |
| + ExternalByteArrayData* external_data = |
| + reinterpret_cast<ExternalByteArrayData*>(peer); |
| + external_data->Finalize(handle); |
| + delete external_data; |
| +} |
| + |
| + |
| RawExternalByteArray* ExternalByteArray::New(uint8_t* data, |
| intptr_t len, |
| + void* peer, |
| + Dart_PeerFinalizer callback, |
| Heap::Space space) { |
| Isolate* isolate = Isolate::Current(); |
| const Class& external_byte_array_class = |
| Class::Handle(isolate->object_store()->external_byte_array_class()); |
| ExternalByteArray& result = ExternalByteArray::Handle(); |
| + ExternalByteArrayData* external_data = |
| + new ExternalByteArrayData(data, peer, callback); |
| { |
| RawObject* raw = Object::Allocate(external_byte_array_class, |
| ExternalByteArray::InstanceSize(), |
| @@ -7673,8 +7729,9 @@ RawExternalByteArray* ExternalByteArray::New(uint8_t* data, |
| NoGCScope no_gc; |
| result ^= raw; |
| result.SetLength(len); |
| - result.SetData(data); |
| + result.SetExternalData(external_data); |
| } |
| + AddFinalizer(result, external_data, ExternalByteArray::Finalize); |
| return result.raw(); |
| } |