unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* The GC and Guardians
@ 2003-07-07 21:50 Thamer Al-Harbash
  2003-07-07 23:21 ` Han-Wen Nienhuys
  0 siblings, 1 reply; 3+ messages in thread
From: Thamer Al-Harbash @ 2003-07-07 21:50 UTC (permalink / 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* The GC and Guardians
  2003-07-07 21:50 The GC and Guardians Thamer Al-Harbash
@ 2003-07-07 23:21 ` Han-Wen Nienhuys
  2003-07-08  0:03   ` Thamer Al-Harbash
  0 siblings, 1 reply; 3+ messages in thread
From: Han-Wen Nienhuys @ 2003-07-07 23:21 UTC (permalink / raw)
  Cc: guile-devel

tmh@whitefang.com writes:
> 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.

Yup, this looks ok.

> 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 don't know anything about guardians, but why don't you simply use
scm_gc_(un)protect_object()?

--
Han-Wen Nienhuys   |   hanwen@cs.uu.nl   |   http://www.xs4all.nl/~hanwen 


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: The GC and Guardians
  2003-07-07 23:21 ` Han-Wen Nienhuys
@ 2003-07-08  0:03   ` Thamer Al-Harbash
  0 siblings, 0 replies; 3+ messages in thread
From: Thamer Al-Harbash @ 2003-07-08  0:03 UTC (permalink / raw)
  Cc: guile-devel

On Tue, 8 Jul 2003, Han-Wen Nienhuys wrote:

> I don't know anything about guardians, but why don't you simply use
> scm_gc_(un)protect_object()?

Thanks. I'll look into that.

My apologies for posting this to the devel list. This should have
been posted to the user list.

-- 
Thamer Al-Harbash


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-07-08  0:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-07-07 21:50 The GC and Guardians Thamer Al-Harbash
2003-07-07 23:21 ` Han-Wen Nienhuys
2003-07-08  0:03   ` Thamer Al-Harbash

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).