From 6a43f6b13acc3db6ae76a1ea0ecf4187968aa005 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Tue, 21 Jun 2011 10:24:46 -0500 Subject: [PATCH] allocate a handle's payload wrapper beforehand, that way it is not allocated during GC. --- ext/v8/v8_handle.cpp | 24 +++++++++++------------- ext/v8/v8_handle.h | 2 ++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ext/v8/v8_handle.cpp b/ext/v8/v8_handle.cpp index a9ec76d..d8e5719 100644 --- a/ext/v8/v8_handle.cpp +++ b/ext/v8/v8_handle.cpp @@ -18,15 +18,24 @@ v8_handle::v8_handle(Handle object) { v8_handle::~v8_handle() {} v8_handle::Payload::Payload(Handle object) { + rb_gc_register_address(&wrapper); handle = Persistent::New(object); + wrapper = Data_Wrap_Struct(rb_cObject, 0, destroy, this); +} + +v8_handle::Payload::~Payload() { + rb_gc_unregister_address(&wrapper); } -v8_handle::Payload::~Payload() {} void v8_handle::Payload::release() { handle.Dispose(); handle.Clear(); } +void v8_handle::Payload::destroy(v8_handle::Payload* payload) { + delete payload; +} + namespace { /** * Holds dead references, that are no longer being held in Ruby, so that they can be garbage collected @@ -44,14 +53,6 @@ namespace { rb_gc_mark(handle->weakref_callback_parameters); } - /** - * Deallocates this handle. This function is invoked on Zombie handles after they have - * been released from V8 and finally - */ - void v8_handle_free(v8_handle::Payload* payload) { - delete payload; - } - /** * Whenver a V8::C::Handle becomes garbage collected, we do not free it immediately. * instead, we put them into a "zombie" queue, where its corresponding V8 storage cell @@ -60,8 +61,7 @@ namespace { */ void v8_handle_enqueue(v8_handle* handle) { handle->dead = true; - VALUE payload = Data_Wrap_Struct(rb_cObject, 0, v8_handle_free, handle->payload); - rb_ary_unshift(handle_queue, payload); + rb_ary_unshift(handle_queue, handle->payload->wrapper); } /** @@ -113,8 +113,6 @@ namespace { } value.Dispose(); handle->payload->release(); - // handle->handle.Dispose(); - // handle->handle.Clear(); handle->dead = true; } diff --git a/ext/v8/v8_handle.h b/ext/v8/v8_handle.h index 8c34b7e..0c93610 100644 --- a/ext/v8/v8_handle.h +++ b/ext/v8/v8_handle.h @@ -14,7 +14,9 @@ struct v8_handle { Payload(v8::Handle object); virtual ~Payload(); void release(); + static void destroy(v8_handle::Payload* payload); v8::Persistent handle; + VALUE wrapper; }; v8_handle(v8::Handle object);