[Top][All Lists]

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

Re: smob gc protection, and inheritance

From: Ludovic Courtès
Subject: Re: smob gc protection, and inheritance
Date: Wed, 04 Sep 2013 23:13:27 +0200
User-agent: Gnus/5.130007 (Ma Gnus v0.7) Emacs/24.3 (gnu/linux)

Hi Doug,

Doug Evans <address@hidden> skribis:

> I have a few questions about smobs:

I’m assuming Guile 2.x here.

> 1) Suppose I have some C code that creates a smob and its containing
> SCM, but does not always expose the SCM to Scheme.
> E.g.
> struct foo_object
> {
>   int bar;
>   SCM baz;
> }
> static SCM
> make_foo_smob (void)
> {
>   struct foo_object *foo_smob = (struct foo_object *)
>     scm_gc_malloc (sizeof (struct foo_object), "foo");
>   SCM foo_scm;
>   foo_smob->bar = -1;
>   foo_smob->baz = SCM_BOOL_F;
>   foo_scm = scm_new_smob (foo_smob_tag, (scm_t_bits) foo_smob);
>   return foo_scm;  
> }
> If the caller stores foo_smob in the heap somewhere, and not foo_scm,
> is that enough to prevent the object from being garbage collected?

Yes, because the region returned by ‘scm_gc_malloc’ is scanned by the GC.

> 2) Is it possible to inherit, e.g., with goops, a smob?
> IOW, can I extend a smob through inheritance?
> Or must I store the smob in a class, and provide accessors?
> [kinda like the "is a" vs "has a" relationship]

Presumably, at least to some extent:

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (use-modules (gnutls))
scheme@(guile-user)> (make-session connection-end/client)
$1 = #<session 2dcefe0>
scheme@(guile-user)> (use-modules (oop goops))
scheme@(guile-user)> (class-of $1)
$2 = #<<class> <session> 2e26000>
scheme@(guile-user)> (define-class <sub-session> (<session>))
scheme@(guile-user)> (change-class $1 <sub-session>)
ERROR: In procedure scm-error:
ERROR: No applicable method for #<<generic> change-class (1)> in call 
(change-class #<session 2dcefe0> #<<class> <sub-session> 2f33000>)

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
--8<---------------cut here---------------end--------------->8---

Andy can say more, I think.  :-)

> 3) The docs aren't as clear as they could be on whether the "smob"
> free function needs to scm_gc_free all results of calls to scm_gc_malloc
> made when constructing the smob.  IIUC, this is not necessary.

The ‘scm_gc_free’ function doesn’t need to be called nowadays, because
the GC automatically frees ‘scm_gc_malloc’ regions when they are no
longer referenced.

So chances are you don’t even need a SMOB ‘free’ function.

> However, why does the image example do this?

Indeed, the ‘mark’ and ‘free’ functions in that example could be removed
altogether, since the only resources associated with the SMOB is memory
returned by ‘scm_gc_malloc’.


reply via email to

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