[Gc] Using GC as a pointer registry

Boehm, Hans hans_boehm@hp.com
Mon, 8 Dec 2003 13:29:01 -0800


You can use 

GC_base(p) != 0

to detect whether a pointer points to anywhere inside a GC_-allocated
object.  If you are not using the debugging allocator,

GC_base(p) == p

tests whether you have a pointer to the beginning of an object.  If performance
is critical, and you only want to distinguish GC_-pointers from other valid
pointers, GC_find_header(p) != 0 will do, but is much more of a hack.

Hans

> -----Original Message-----
> From: gc-admin@napali.hpl.hp.com [mailto:gc-admin@napali.hpl.hp.com]On
> Behalf Of Andreas Saebjornsen
> Sent: Monday, December 08, 2003 11:45 AM
> To: gc@napali.hpl.hp.com
> Subject: [Gc] Using GC as a pointer registry
> 
> 
> I'am doing some work where I need to verify if a pointer is 
> valid or not. I 
> wonder if it is possible, when GC memory allocators are used, 
> to see if
> a pointer is pointing to a valid object or not. I want that 
> to be a query I 
> can make to GC. The prototype of  such a function could be 
> 'bool GCpointerCheck(void*)' where void* is a pointer and 
> does a lookup
> in the GC "registry". Here is a trivial example demonstrating 
> what I'am looking for:
> 
> #include "gc_cpp.h"
> 
> class foo : public	gc {
>    public:
>       foo(){};
>       ~foo(){};
> };
> 
> int main(int argc, char *argv[])
> {
>     int i = 0;
>     foo* fooObj = new foo();
>     foo* fooPointer;
>     while(true){
>           
>           if(i == 10){
>               i = 0;
>               delete fooObj;
>               fooObj = new foo();
>           }
> 
>           if(  GCpointerCheck(fooPointer) != true )
>                fooPointer = fooObj;
> 
>           i++;
>     }
> };
> 
> 
> Thank you,
> Andreas Saebjoernsen
>        
> _______________________________________________
> Gc mailing list
> Gc@linux.hpl.hp.com
> http://linux.hpl.hp.com/cgi-bin/mailman/listinfo/gc
>