[前][次][番号順一覧][スレッド一覧]

ruby-changes:14821

From: mame <ko1@a...>
Date: Tue, 16 Feb 2010 21:34:29 +0900 (JST)
Subject: [ruby-changes:14821] Ruby:r26684 (trunk): * gc.c (rb_objspace_call_finalizer): always run finalizer at exit,

mame	2010-02-16 21:34:09 +0900 (Tue, 16 Feb 2010)

  New Revision: 26684

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26684

  Log:
    * gc.c (rb_objspace_call_finalizer): always run finalizer at exit,
      before object is free'd.  [ruby-dev:40382]

  Modified files:
    trunk/ChangeLog
    trunk/gc.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 26683)
+++ ChangeLog	(revision 26684)
@@ -1,3 +1,8 @@
+Tue Feb 16 21:32:01 2010  Yusuke Endoh  <mame@t...>
+
+	* 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@f...>
 
 	* file.c (rb_group_member): renamed from group_member.
Index: gc.c
===================================================================
--- gc.c	(revision 26683)
+++ gc.c	(revision 26684)
@@ -2551,11 +2551,29 @@
 }
 
 static void
-run_final(rb_objspace_t *objspace, VALUE obj)
+run_finalizer(rb_objspace_t *objspace, VALUE obj, VALUE objid, VALUE table)
 {
     long i;
     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;
 
     objid = rb_obj_id(obj);	/* make obj into id */
@@ -2573,17 +2591,7 @@
 
     if (finalizer_table &&
 	st_delete(finalizer_table, (st_data_t*)&obj, &table)) {
-	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);
-	}
+	run_finalizer(objspace, obj, objid, table);
     }
 }
 
@@ -2615,20 +2623,35 @@
 chain_finalized_object(st_data_t key, st_data_t val, st_data_t 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) {
 	    p->as.free.flags = FL_MARK | T_ZOMBIE; /* remain marked */
 	    RDATA(p)->dfree = 0;
 	}
 	p->as.free.next = *final_list;
 	*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
 rb_gc_call_finalizer_at_exit(void)
 {
@@ -2644,15 +2667,24 @@
 
     /* run finalizers */
     if (finalizer_table) {
-	finalize_deferred(objspace);
-	while (finalizer_table->num_entries > 0) {
+	do {
+	    /* 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_data_t)&final_list);
-	    if (!(p = final_list)) break;
-	    do {
-		final_list = p->as.free.next;
-		run_final(objspace, (VALUE)p);
-	    } while ((p = final_list) != 0);
+		       (st_data_t)&deferred_final_list);
+	} while (deferred_final_list);
+	/* force to run finalizer */
+	while (finalizer_table->num_entries) {
+	    struct force_finalize_list *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);
 	finalizer_table = 0;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]