1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* gc.c (rb_objspace_call_finalizer): always run finalizer at exit,

before object is free'd.  [ruby-dev:40382]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26684 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2010-02-16 12:34:09 +00:00
parent 792b3efa62
commit 2236f6a682
2 changed files with 63 additions and 26 deletions

View file

@ -1,3 +1,8 @@
Tue Feb 16 21:32:01 2010 Yusuke Endoh <mame@tsg.ne.jp>
* gc.c (rb_objspace_call_finalizer): always run finalizer at exit,
before object is free'd. [ruby-dev:40382]
Tue Feb 16 20:56:39 2010 Tanaka Akira <akr@fsij.org> Tue Feb 16 20:56:39 2010 Tanaka Akira <akr@fsij.org>
* file.c (rb_group_member): renamed from group_member. * file.c (rb_group_member): renamed from group_member.

84
gc.c
View file

@ -2551,11 +2551,29 @@ run_single_final(VALUE arg)
} }
static void static void
run_final(rb_objspace_t *objspace, VALUE obj) run_finalizer(rb_objspace_t *objspace, VALUE obj, VALUE objid, VALUE table)
{ {
long i; long i;
int status; int status;
VALUE args[3], table, objid; VALUE args[3];
args[1] = 0;
args[2] = (VALUE)rb_safe_level();
if (!args[1] && RARRAY_LEN(table) > 0) {
args[1] = rb_obj_freeze(rb_ary_new3(1, objid));
}
for (i=0; i<RARRAY_LEN(table); i++) {
VALUE final = RARRAY_PTR(table)[i];
args[0] = RARRAY_PTR(final)[1];
args[2] = FIX2INT(RARRAY_PTR(final)[0]);
rb_protect(run_single_final, (VALUE)args, &status);
}
}
static void
run_final(rb_objspace_t *objspace, VALUE obj)
{
VALUE table, objid;
RUBY_DATA_FUNC free_func = 0; RUBY_DATA_FUNC free_func = 0;
objid = rb_obj_id(obj); /* make obj into id */ objid = rb_obj_id(obj); /* make obj into id */
@ -2573,17 +2591,7 @@ run_final(rb_objspace_t *objspace, VALUE obj)
if (finalizer_table && if (finalizer_table &&
st_delete(finalizer_table, (st_data_t*)&obj, &table)) { st_delete(finalizer_table, (st_data_t*)&obj, &table)) {
args[1] = 0; run_finalizer(objspace, obj, objid, table);
args[2] = (VALUE)rb_safe_level();
if (!args[1] && RARRAY_LEN(table) > 0) {
args[1] = rb_obj_freeze(rb_ary_new3(1, objid));
}
for (i=0; i<RARRAY_LEN(table); i++) {
VALUE final = RARRAY_PTR(table)[i];
args[0] = RARRAY_PTR(final)[1];
args[2] = FIX2INT(RARRAY_PTR(final)[0]);
rb_protect(run_single_final, (VALUE)args, &status);
}
} }
} }
@ -2615,18 +2623,33 @@ static int
chain_finalized_object(st_data_t key, st_data_t val, st_data_t arg) chain_finalized_object(st_data_t key, st_data_t val, st_data_t arg)
{ {
RVALUE *p = (RVALUE *)key, **final_list = (RVALUE **)arg; RVALUE *p = (RVALUE *)key, **final_list = (RVALUE **)arg;
if (p->as.basic.flags & FL_FINALIZE) { if (p->as.basic.flags & (FL_FINALIZE|FL_MARK) == FL_FINALIZE) {
if (BUILTIN_TYPE(p) != T_ZOMBIE) { if (BUILTIN_TYPE(p) != T_ZOMBIE) {
p->as.free.flags = FL_MARK | T_ZOMBIE; /* remain marked */ p->as.free.flags = FL_MARK | T_ZOMBIE; /* remain marked */
RDATA(p)->dfree = 0; RDATA(p)->dfree = 0;
} }
p->as.free.next = *final_list; p->as.free.next = *final_list;
*final_list = p; *final_list = p;
return ST_CONTINUE;
}
else {
return ST_DELETE;
} }
return ST_CONTINUE;
}
struct force_finalize_list {
VALUE obj;
VALUE table;
struct force_finalize_list *next;
};
static int
force_chain_object(st_data_t key, st_data_t val, st_data_t arg)
{
struct force_finalize_list **prev = (struct force_finalize_list **)arg;
struct force_finalize_list *curr = ALLOC(struct force_finalize_list);
curr->obj = key;
curr->table = val;
curr->next = *prev;
*prev = curr;
return ST_DELETE;
} }
void void
@ -2644,15 +2667,24 @@ rb_objspace_call_finalizer(rb_objspace_t *objspace)
/* run finalizers */ /* run finalizers */
if (finalizer_table) { if (finalizer_table) {
finalize_deferred(objspace); do {
while (finalizer_table->num_entries > 0) { /* XXX: this loop will make no sense */
/* because mark will not be removed */
finalize_deferred(objspace);
mark_tbl(objspace, finalizer_table, 0);
st_foreach(finalizer_table, chain_finalized_object, st_foreach(finalizer_table, chain_finalized_object,
(st_data_t)&final_list); (st_data_t)&deferred_final_list);
if (!(p = final_list)) break; } while (deferred_final_list);
do { /* force to run finalizer */
final_list = p->as.free.next; while (finalizer_table->num_entries) {
run_final(objspace, (VALUE)p); struct force_finalize_list *list = 0;
} while ((p = final_list) != 0); st_foreach(finalizer_table, force_chain_object, (st_data_t)&list);
while (list) {
struct force_finalize_list *curr = list;
run_finalizer(objspace, curr->obj, rb_obj_id(curr->obj), curr->table);
list = curr->next;
xfree(curr);
}
} }
st_free_table(finalizer_table); st_free_table(finalizer_table);
finalizer_table = 0; finalizer_table = 0;