[Gc] Using GC as a pointer registry

Alec Orr Alec.Orr@wbemsolutions.com
Mon, 08 Dec 2003 16:25:56 -0500


Andreas:

There was a post about debugging bad pointers a while ago (included 
below) - you may be able to use to your advantage:

http://www.hpl.hp.com/hosted/linux/mail-archives/gc/2003-December/000259.html

"Suppose you have a pointer p with address p.  If GC_find_header(p) is 
not NULL, then the object is in the garbage collected heap.  (GC_base(p) 
can also be used for this test.  For GC heap objects you should get 
GC_base(p)==p; for everything outside the GC heap, GC_base(p) == 0).  If 
the object at the point of failure looks like a pointer followed by 
zeros, it was probably
reclaimed.  You should also check that GC_is_marked(p) == 0."

SO, you can manufacture a function like:

/* Prototype the GC functions WITHOUT using their native GC datatype - 
under the covers, the parameters and returns are pointers which are 
always 4 bytes (see gc.h), so as long as we compare them (but NOT 
dereference them) we should be ok */


extern "C" {
	void* GC_find_header(void*);
	void* GC_base(void*);	
}

/** A function which returns true (1) or false (0) depending on some 
integrity tests */

int GCpointerCheck(void* ptr) {
	int isvalid = 1; /* assume true to start */

	/* Run some checks to see if this pointer is valid */
	if (NULL = ptr) {
		isvalid = 0; /* NULL pointers are never good */

	if (NULL == GC_find_header(ptr)) {
		isvalid = 0;

	} else if (GC_base(ptr) != ptr) {
		ivalid = 0;

	};
	return isvalid;
}

The function above will only work for pointers which you know SHOULD be 
managed using the GC_malloc family of functions (which I assume you're 
doing).  I have't compiled this, just saw your post and wanted to get 
back to you ASAP.

Hope this helps!
Alec Orr



Andreas Saebjornsen wrote:

> 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
> 
> 
>