From 6692436b9fe206c130fbfa3fa7b41e18c1048f4a Mon Sep 17 00:00:00 2001 From: tmm1 Date: Thu, 5 Dec 2013 09:24:02 +0000 Subject: ext/objspace: remove OS.after_gc_start_hook= and move internal test * ext/objspace/gc_hook.c: remove this file * ext/-test-/tracepoint/gc_hook.c: new filename for above * ext/objspace/objspace.c: remove ObjectSpace.after_gc_start_hook= * test/objspace/test_objspace.rb: remove test * test/-ext-/tracepoint/test_tracepoint.rb: add above test for tracepoint re-entry git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44007 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/-test-/tracepoint/gc_hook.c | 80 ++++++++++++++++++++++++++++++++ ext/-test-/tracepoint/tracepoint.c | 3 ++ ext/objspace/gc_hook.c | 95 -------------------------------------- ext/objspace/objspace.c | 2 - 4 files changed, 83 insertions(+), 97 deletions(-) create mode 100644 ext/-test-/tracepoint/gc_hook.c delete mode 100644 ext/objspace/gc_hook.c (limited to 'ext') diff --git a/ext/-test-/tracepoint/gc_hook.c b/ext/-test-/tracepoint/gc_hook.c new file mode 100644 index 0000000000..6d8485ecb1 --- /dev/null +++ b/ext/-test-/tracepoint/gc_hook.c @@ -0,0 +1,80 @@ +#include "ruby/ruby.h" +#include "ruby/debug.h" + +static int invoking; /* TODO: should not be global variable */ + +static VALUE +invoke_proc_ensure(void *dmy) +{ + invoking = 0; + return Qnil; +} + +static VALUE +invoke_proc_begin(VALUE proc) +{ + return rb_proc_call(proc, rb_ary_new()); +} + +static void +invoke_proc(void *data) +{ + VALUE proc = (VALUE)data; + invoking += 1; + rb_ensure(invoke_proc_begin, proc, invoke_proc_ensure, 0); +} + +static void +gc_start_end_i(VALUE tpval, void *data) +{ + if (0) { + rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval); + fprintf(stderr, "trace: %s\n", rb_tracearg_event_flag(tparg) == RUBY_INTERNAL_EVENT_GC_START ? "gc_start" : "gc_end"); + } + + if (invoking == 0) { + rb_postponed_job_register(0, invoke_proc, data); + } +} + +static VALUE +set_gc_hook(VALUE module, VALUE proc, rb_event_flag_t event, const char *tp_str, const char *proc_str) +{ + VALUE tpval; + ID tp_key = rb_intern(tp_str); + ID proc_key = rb_intern(proc_str); + + /* disable previous keys */ + if (rb_ivar_defined(module, tp_key) != 0 && + RTEST(tpval = rb_ivar_get(module, tp_key))) { + rb_tracepoint_disable(tpval); + rb_ivar_set(module, tp_key, Qnil); + rb_ivar_set(module, proc_key, Qnil); + } + + if (RTEST(proc)) { + if (!rb_obj_is_proc(proc)) { + rb_raise(rb_eTypeError, "trace_func needs to be Proc"); + } + + tpval = rb_tracepoint_new(0, event, gc_start_end_i, (void *)proc); + rb_ivar_set(module, tp_key, tpval); + rb_ivar_set(module, proc_key, proc); /* GC guard */ + rb_tracepoint_enable(tpval); + } + + return proc; +} + +static VALUE +set_after_gc_start(VALUE module, VALUE proc) +{ + return set_gc_hook(module, proc, RUBY_INTERNAL_EVENT_GC_START, + "__set_after_gc_start_tpval__", "__set_after_gc_start_proc__"); +} + +void +Init_gc_hook(VALUE module) +{ + rb_define_module_function(module, "after_gc_start_hook=", set_after_gc_start, 1); +} diff --git a/ext/-test-/tracepoint/tracepoint.c b/ext/-test-/tracepoint/tracepoint.c index 208bf57a47..245dbd6191 100644 --- a/ext/-test-/tracepoint/tracepoint.c +++ b/ext/-test-/tracepoint/tracepoint.c @@ -85,10 +85,13 @@ tracepoint_specify_normal_and_internal_events(VALUE self) return Qnil; /* should not be reached */ } +void Init_gc_hook(VALUE); + void Init_tracepoint(void) { VALUE mBug = rb_define_module("Bug"); + Init_gc_hook(mBug); rb_define_module_function(mBug, "tracepoint_track_objspace_events", tracepoint_track_objspace_events, 0); rb_define_module_function(mBug, "tracepoint_specify_normal_and_internal_events", tracepoint_specify_normal_and_internal_events, 0); } diff --git a/ext/objspace/gc_hook.c b/ext/objspace/gc_hook.c deleted file mode 100644 index eb6ac7401a..0000000000 --- a/ext/objspace/gc_hook.c +++ /dev/null @@ -1,95 +0,0 @@ -/********************************************************************** - - gc_hook.c - GC hook mechanism/ObjectSpace extender for MRI. - - $Author$ - created at: Tue May 28 01:34:25 2013 - - NOTE: This extension library is not expected to exist except C Ruby. - NOTE: This feature is an example usage of internal event tracing APIs. - - All the files in this distribution are covered under the Ruby's - license (see the file COPYING). - -**********************************************************************/ - -#include "ruby/ruby.h" -#include "ruby/debug.h" - -static int invoking; /* TODO: should not be global variable */ - -static VALUE -invoke_proc_ensure(void *dmy) -{ - invoking = 0; - return Qnil; -} - -static VALUE -invoke_proc_begin(VALUE proc) -{ - return rb_proc_call(proc, rb_ary_new()); -} - -static void -invoke_proc(void *data) -{ - VALUE proc = (VALUE)data; - invoking += 1; - rb_ensure(invoke_proc_begin, proc, invoke_proc_ensure, 0); -} - -static void -gc_start_end_i(VALUE tpval, void *data) -{ - if (0) { - rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval); - fprintf(stderr, "trace: %s\n", rb_tracearg_event_flag(tparg) == RUBY_INTERNAL_EVENT_GC_START ? "gc_start" : "gc_end"); - } - - if (invoking == 0) { - rb_postponed_job_register(0, invoke_proc, data); - } -} - -static VALUE -set_gc_hook(VALUE rb_mObjSpace, VALUE proc, rb_event_flag_t event, const char *tp_str, const char *proc_str) -{ - VALUE tpval; - ID tp_key = rb_intern(tp_str); - ID proc_key = rb_intern(proc_str); - - /* disable previous keys */ - if (rb_ivar_defined(rb_mObjSpace, tp_key) != 0 && - RTEST(tpval = rb_ivar_get(rb_mObjSpace, tp_key))) { - rb_tracepoint_disable(tpval); - rb_ivar_set(rb_mObjSpace, tp_key, Qnil); - rb_ivar_set(rb_mObjSpace, proc_key, Qnil); - } - - if (RTEST(proc)) { - if (!rb_obj_is_proc(proc)) { - rb_raise(rb_eTypeError, "trace_func needs to be Proc"); - } - - tpval = rb_tracepoint_new(0, event, gc_start_end_i, (void *)proc); - rb_ivar_set(rb_mObjSpace, tp_key, tpval); - rb_ivar_set(rb_mObjSpace, proc_key, proc); /* GC guard */ - rb_tracepoint_enable(tpval); - } - - return proc; -} - -static VALUE -set_after_gc_start(VALUE rb_mObjSpace, VALUE proc) -{ - return set_gc_hook(rb_mObjSpace, proc, RUBY_INTERNAL_EVENT_GC_START, - "__set_after_gc_start_tpval__", "__set_after_gc_start_proc__"); -} - -void -Init_gc_hook(VALUE rb_mObjSpace) -{ - rb_define_module_function(rb_mObjSpace, "after_gc_start_hook=", set_after_gc_start, 1); -} diff --git a/ext/objspace/objspace.c b/ext/objspace/objspace.c index f9fa833c99..6dce515bf6 100644 --- a/ext/objspace/objspace.c +++ b/ext/objspace/objspace.c @@ -716,7 +716,6 @@ reachable_objects_from_root(VALUE self) } void Init_object_tracing(VALUE rb_mObjSpace); -void Init_gc_hook(VALUE rb_mObjSpace); void Init_objspace_dump(VALUE rb_mObjSpace); /* @@ -768,6 +767,5 @@ Init_objspace(void) rb_define_method(rb_mInternalObjectWrapper, "internal_object_id", iow_internal_object_id, 0); Init_object_tracing(rb_mObjSpace); - Init_gc_hook(rb_mObjSpace); Init_objspace_dump(rb_mObjSpace); } -- cgit v1.2.3