[Gc] Basic questions

Fergus Henderson fjh@cs.mu.oz.au
Sat, 10 May 2003 14:57:31 +1000


On 09-May-2003, Emmanuel Stapf [ES] <manus@eiffel.com> wrote:
> I'm trying to understand `GC_register_displacement' but the comment in
> `gc.h' is rather confusing.
> 
> /* Add a displacement to the set of those considered valid by the	*/
> /* collector.  GC_register_displacement(n) means that if p was returned */
> /* by GC_malloc, then (char *)p + n will be considered to be a valid	*/
> /* pointer to n. */
> 
> What does a pointer to `n' means?

It looks like a mistake in the comment.  It should say "... will be
considered to be a valid pointer to the object allocated by GC_malloc
(the one whose first byte is at *(char *)p)", or something along those lines.

> My understanding is that if you have allocated p as:
> 
> p ------>/-------\  Offset 0
>          |       |
>          |       |
>          |       |
>          |       |
> p + n -->|-------|  Offset n
>          |       |
>          |       |
>          |       |
>          |       |
>          \-------/
> 
> Then the GC will consider that p is alive if it can find a pointer that points
> into the first n bytes of `p'. Am I correct?

I don't think so.  If you register offsets "2" and "4" using
GC_register_displacement(), and "p" is a pointer returned from
GC_malloc(), then "p", "(char *)p + 2", and "(char *)p + 4" would
all be considered valid pointers, but "(char *)p + 1" and "(char *)p + 3"
would not.  If you want to register all offsets up to and including "n",
then have to do so explicitly, e.g. using a loop:

	int i;
	for (i = 1; i < n; i++) {
	    GC_register_displacement(i);
	}

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.