Skip to content

Commit

Permalink
Merge pull request alexgutteridge#10 from aaronjg/1cf57d989190b325552…
Browse files Browse the repository at this point in the history
…b7e24c21edf034eada1e6

Add Hooks for Garbage Collection
  • Loading branch information
alexgutteridge committed Nov 30, 2011
2 parents 66b9c0f + 1cf57d9 commit 6a51ee7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
12 changes: 5 additions & 7 deletions ext/Converters.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
18 changes: 9 additions & 9 deletions ext/rsruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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)
Expand All @@ -50,9 +54,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);
Expand All @@ -61,13 +65,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
Expand All @@ -90,7 +90,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);

Expand Down
1 change: 1 addition & 0 deletions ext/rsruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 6a51ee7

Please sign in to comment.