unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Thamer Al-Harbash <tmh@whitefang.com>
Subject: The GC and Guardians
Date: Mon, 7 Jul 2003 17:50:47 -0400 (EDT)	[thread overview]
Message-ID: <Pine.BSF.4.51.0307071717530.84296@helena.whitefang.com> (raw)

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


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


             reply	other threads:[~2003-07-07 21:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-07 21:50 Thamer Al-Harbash [this message]
2003-07-07 23:21 ` The GC and Guardians Han-Wen Nienhuys
2003-07-08  0:03   ` Thamer Al-Harbash

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.BSF.4.51.0307071717530.84296@helena.whitefang.com \
    --to=tmh@whitefang.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).