[Gc] C++ and exceptions continued: GC_throw & GC_remove_roots_containing

Filip Pizlo pizlo at mac.com
Tue Feb 7 20:11:07 PST 2006


Hans,

As far as I can tell, the GC_throw() definition discussed previously  
works just fine.  I've modified the 6.5 version of the gc to include  
the supporting function GC_remove_roots_containing().  Other than  
that, the only thing needed is the following definition of GC_throw():

template< typename T >
void GC_throw(const T &exn) {
     try {
	throw exn;
     } catch (const T &exn) {
	char *ptr=(char*)&exn;
	GC_add_roots(ptr,ptr+sizeof(T));
	throw;
     }
}

Any exception thrown using GC_throw() needs to have a destructor that  
looks like:

     ~myexc() {
	GC_remove_roots_containing((char*)this);
     }

I include the patch for mark_rts.c.  I would have modified the  
version of the GC that is latest in cvs, but I've been having  
authorization problems accessing sourceforge.

-Filip



Patch for mark_rts.c:

***************
*** 314,319 ****
--- 314,333 ----
   }

   #if !defined(MSWIN32) && !defined(MSWINCE)
+ int GC_remove_roots_containing(a)
+ char * a;
+ {
+     int res;
+     DCL_LOCK_STATE;
+
+     DISABLE_SIGNALS();
+     LOCK();
+     res=GC_remove_roots_containing_inner(a);
+     UNLOCK();
+     ENABLE_SIGNALS();
+     return res;
+ }
+
   void GC_remove_roots(b, e)
   char * b; char * e;
   {
***************
*** 324,329 ****
--- 338,360 ----
       GC_remove_roots_inner(b, e);
       UNLOCK();
       ENABLE_SIGNALS();
+ }
+
+ int GC_remove_roots_containing_inner(a)
+ char * a;
+ {
+     int i;
+     int res=0;
+     for (i = 0; i < n_root_sets; ) {
+       if (GC_static_roots[i].r_start <= (ptr_t)a && GC_static_roots 
[i].r_end > (ptr_t)a) {
+             GC_remove_root_at_pos(i);
+           res++;
+       } else {
+           i++;
+       }
+     }
+     GC_rebuild_root_index();
+     return res;
   }

   /* Should only be called when the lock is held */



More information about the Gc mailing list