guile-devel
[Top][All Lists]
Advanced

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

The GC and Guardians


From: Thamer Al-Harbash
Subject: The GC and Guardians
Date: Mon, 7 Jul 2003 17:50:47 -0400 (EDT)

I've read through the explanation of how the garbage collector
works. I understand that the gc uses the stack as a list of
pointers to the heap, and if a two word header is found on the
heap via the pointers then the garbage collector assumes that
that is a valid non-immediate datum which should not be released.

(Well it's a tad more complicated than that -- the gc checks a known
heap segment and alignment)

This works fine insofar as I always return the SCM data from my C
functions, and once I exit the C function which placed the scheme
data pointer on the stack, I no longer reference that data
anymore.

Unfortunately this isn't good enough due to the way a certain
program is designed.

I used this kludge to get around it, and would like to know if
I'm getting myself in trouble:

int foo(...)
{
  SCM mysmob;

  mysmob = create_mysmob(...);
  scm_define(scm_str2symbol(...), mysmob);

  return;
}

My understanding is that since the SMOB bound to the top level
environment, the garbage collector should not destroy it since
its referenced by the top root. I do later undefine it when I no
longer care about it.

Also, on an unrelated problem, I want to be able to create a C
structure and have it reference non-immediate SCM data, but I
cannot turn this C structure into a SMOB. Unfortunately the folks
using my code may not be happy dealing with guile
directly. Should I use a guardian on the SCM data like so?

foo_t *foo_create(void)
{
  foo_t *foo = create_foo();
  SCM guardian;

  foo->mylist = SCM_EOL;
  guardian = scm_make_guardian(...);

  scm_apply(....); /* protect foo->mylist with the guardian procedure */

  return foo;
}

I understand that this will work because the guardian can be
referenced by the gc from a list of guardians until it is
explicitly removed.  Should I define a top level guardian and
just use it to protect non-immediate SCM data? How are guardians
intended to be used from C code when the functions creating them
return and no longer reference them.

If I'm way off could someone point me to C source code that uses
guardians to get around the above problem.

-- 
Thamer Al-Harbash




reply via email to

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