From: Gordon McNutt <gmcnutt@ca...> - 2008-05-23 02:35:17
Hi, noticed a thread about this issue from a few months ago.
I fixed this in a really terrible way for nazghul
(http://sourceforge.net/projects/nazghul/). Might as well share. I added
a function called scm_protect(sc, cell) which can be called from C. My
C code calls this on any cell which it intends to hang on to . The
way I implemented scm_protect is truly awful, but I was still unfamiliar
with the interpreter when I struck upon the solution: I added a
doubly-linked list field to the Scheme cell structure (thus doubling
it's size -- ouch!). scm_protect() puts the cell on the protected list,
and the gc walks it during the mark phase like it would any other
register. The doubly linked list makes it a quick operation to unprotect
Ugly, but it's worked great for years.
 If the C code is just going to call some other functions (including
Scheme procedures, recursively) then I found that it was not necessary
to protect the cells, because the Scheme procedures calling the C code
always had references keeping them alive. But if the C function was
going to keep pointers to the cells after it returned then of course it
had to protect them. Likewise, if it was building a Scheme list from
scratch to pass it into Scheme then it had to protect the head of the
list until it was all built, since cell allocations may invoke the GC.