Permalink
Browse files

add hooks on garbage collection

  • Loading branch information...
Aarong93 committed Oct 13, 2011
1 parent c1247f4 commit 1cf57d989190b325552b7e24c21edf034eada1e6
Showing with 15 additions and 16 deletions.
  1. +5 −7 ext/Converters.c
  2. +9 −9 ext/rsruby.c
  3. +1 −0 ext/rsruby.h
View
@@ -272,11 +272,9 @@ VALUE to_ruby_with_mode(SEXP robj, int mode)
if (i<0) return Qnil;
if (i==1) break;
default:
R_References = CONS(robj, R_References);
SET_SYMVALUE(install("R.References"), R_References);
protect_robj(robj);
obj = Data_Wrap_Struct(rb_const_get(rb_cObject,
rb_intern("RObj")), 0, 0, robj);
rb_intern("RObj")), 0, &Robj_dealloc, robj);
rb_iv_set(obj,"@conversion",INT2FIX(TOP_MODE));
rb_iv_set(obj,"@wrap",Qfalse);
}
@@ -443,7 +441,7 @@ from_proc_table(SEXP robj, VALUE *fun)
l = FIX2INT(rb_funcall(proc_table,rb_intern("size"),0));
obj = Data_Wrap_Struct(rb_const_get(rb_cObject,
rb_intern("RObj")), 0, 0, robj);
rb_intern("RObj")), 0, &Robj_dealloc, robj);
rb_iv_set(obj,"@conversion",INT2FIX(TOP_MODE));
rb_iv_set(obj,"@wrap",Qfalse);
@@ -506,7 +504,7 @@ to_ruby_proc(SEXP robj, VALUE *obj)
//Create new object based on robj and call the function
//found above with it as argument
tmp = Data_Wrap_Struct(rb_const_get(rb_cObject,
rb_intern("RObj")), 0, 0, robj);
rb_intern("RObj")), 0, &Robj_dealloc, robj);
rb_iv_set(tmp,"@conversion",INT2FIX(TOP_MODE));
rb_iv_set(tmp,"@wrap",Qfalse);
@@ -572,7 +570,7 @@ to_ruby_class(SEXP robj, VALUE *obj)
return 0; /* conversion failed */
tmp = Data_Wrap_Struct(rb_const_get(rb_cObject,
rb_intern("RObj")), 0, 0, robj);
rb_intern("RObj")), 0, &Robj_dealloc, robj);
rb_iv_set(tmp,"@conversion",INT2FIX(TOP_MODE));
rb_iv_set(tmp,"@wrap",Qfalse);
View
@@ -34,6 +34,10 @@
/* Global list to protect R objects from garbage collection */
/* This is inspired in $R_SRC/src/main/memory.c */
static SEXP R_References;
void protect_robj(SEXP robj){
R_References = CONS(robj, R_References);
SET_SYMVALUE(install("R.References"), R_References);
}
SEXP
RecursiveRelease(SEXP obj, SEXP list)
@@ -48,9 +52,9 @@ RecursiveRelease(SEXP obj, SEXP list)
}
/* TODO: This needs implementing as a Ruby destructor for each RObj */
/*static void
Robj_dealloc(VALUE self)
{
void
Robj_dealloc(VALUE self)
{
SEXP robj;
Data_Get_Struct(self, struct SEXPREC, robj);
@@ -59,13 +63,9 @@ RecursiveRelease(SEXP obj, SEXP list)
SET_SYMVALUE(install("R.References"), R_References);
return;
}*/
void protect_robj(){
R_References = CONS(robj, R_References);
SET_SYMVALUE(install("R.References"), R_References);
}
/* Obtain an R object via its name.
* This is only used to get the 'get' function.
* All subsequent calls go via the 'get' function itself
@@ -88,7 +88,7 @@ VALUE get_fun(VALUE self, VALUE name){
/* Wrap the returned R object as a ruby Object */
rubyobj = Data_Wrap_Struct(rb_const_get(rb_cObject,
rb_intern("RObj")), 0, 0, robj);
rb_intern("RObj")), 0, &Robj_dealloc , robj);
rb_iv_set(rubyobj,"@conversion",INT2FIX(conversion));
rb_iv_set(rubyobj,"@wrap",Qfalse);
View
@@ -84,5 +84,6 @@ VALUE RObj_init_lcall(VALUE self, VALUE args);
VALUE RObj_to_ruby(VALUE self, VALUE args);
int make_argl(VALUE args, SEXP *e);
void protect_robj(SEXP robj);
void Robj_dealloc(VALUE self);
#endif

0 comments on commit 1cf57d9

Please sign in to comment.