guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is my procedure getting GCed?


From: Dirk Herrmann
Subject: Re: Is my procedure getting GCed?
Date: Mon, 7 May 2001 22:59:58 +0200 (MEST)

On 1 May 2001, Michael Livshin wrote:

> perhaps what is needed is a simple interface to add/remove GC root
> locations, a-la Boehm.
> 
> so you would say `scm_gc_add_root (&fProc)' and not worry about it.
> 
> comments?

This would be very good, IMO.  Even in current guile there are a lot of
places which could be simplified if there was the possibility to add
root locations for the gc.  The trick that is currently used is, to store 
a cell's address in a global C variable, make the cell a permanently
protected object, and then use the car or cdr to store the value of
interest:

  SCM anchor_cell;  /* Points to a permanent cell. */

  SCM
  get_value () {
    return SCM_CELL_WORD_0 (anchor_cell);
  }

  void
  set_value (SCM new_value) {
    SCM_SET_CELL_WORD_0 (anchor_cell, new_value);
  }

  void
  init () {
    anchor_cell = scm_cons (initial_value, SCM_BOOL_F);
    scm_permanent_object (anchor_cell);
  }

There are some disadvantages to this approach:  First, you need to
allocate a cell that itself is not of any use except for forming the root
point for the gc.  Second, whenever you want to access the value of
interest, you have to access it indirectly via that cell.  Third, it is a
source of errors since it is tempting for programmers to change the global
variable itself instead of its car or cdr.

With your proposed scm_gc_add_root procedure, the above code becomes:

  SCM value;

  SCM
  get_value () {
    return value;
  }

  void
  set_value (SCM new_value) {
    value = new_value;
  }

  void
  init () {
    value = initial_value;
    scm_gc_add_root (&value);
  }

Now, as the example procedures get_value and set_value demonstrate, you
can handle the global C variable in the same way as a local SCM variable.
I have not checked it, but it may even be that scm_permanent_object
becomes unnecessary if scm_gc_add_root is added.

I don't know about how this works with shared libraries, though:  What
happens if you unlink a shared lib, but still have pointers into global
variables belonging to that lib?

Best regards,
Dirk Herrmann




reply via email to

[Prev in Thread] Current Thread [Next in Thread]