Permalink
Browse files

honor RData dfree() callback

  • Loading branch information...
1 parent c6eaf27 commit 2ba4bd4686b0f517b728216c9f8997da45112f7d Laurent Sansonetti committed Jun 29, 2011
Showing with 21 additions and 4 deletions.
  1. +1 −1 enumerator.c
  2. +1 −1 file.c
  3. +19 −2 gc.c
View
@@ -195,7 +195,7 @@ enumerator_allocate(VALUE klass, SEL sel)
{
struct enumerator *ptr;
return Data_Make_Struct(klass, struct enumerator,
- NULL, -1, ptr);
+ NULL, NULL, ptr);
}
static VALUE
View
2 file.c
@@ -170,7 +170,7 @@ stat_new_0(VALUE klass, struct stat *st)
nst = ALLOC(struct stat);
*nst = *st;
}
- return Data_Wrap_Struct(klass, NULL, xfree, nst);
+ return Data_Wrap_Struct(klass, NULL, NULL, nst);
}
static VALUE
View
21 gc.c
@@ -424,16 +424,33 @@ rb_node_release(NODE *node)
}
}
+static void
+rdata_finalize(void *rcv, SEL sel)
+{
+ if (RDATA(rcv)->dfree != NULL && RDATA(rcv)->data != NULL) {
+ RDATA(rcv)->dfree(RDATA(rcv)->data);
+ RDATA(rcv)->data = NULL;
+ }
+}
+
VALUE
-rb_data_object_alloc(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
+rb_data_object_alloc(VALUE klass, void *datap, RUBY_DATA_FUNC dmark,
+ RUBY_DATA_FUNC dfree)
{
+ if (klass) {
+ Check_Type(klass, T_CLASS);
+ }
+
NEWOBJ(data, struct RData);
- if (klass) Check_Type(klass, T_CLASS);
OBJSETUP(data, klass, T_DATA);
GC_WB(&data->data, datap);
data->dfree = dfree;
data->dmark = dmark;
+ if (dfree != NULL) {
+ rb_objc_install_method2((Class)klass, "finalize", (IMP)rdata_finalize);
+ }
+
return (VALUE)data;
}

0 comments on commit 2ba4bd4

Please sign in to comment.