guile-user
[Top][All Lists]
Advanced

[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: Fri, 06 Sep 2013 14:19:31 +0200
User-agent: Gnus/5.130007 (Ma Gnus v0.7) Emacs/24.3 (gnu/linux)

Hi,

Andrew Gaylard <address@hidden> skribis:

> On 09/04/13 23:13, Ludovic Courtès wrote:
>> Hi Doug,
>>
>> Doug Evans <address@hidden> skribis:
>>> 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’.
> Hi Ludo',
>
> Thanks for this information -- it helps answer my guile-dbi question
> (http://lists.gnu.org/archive/html/guile-user/2013-08/msg00133.html).
>
> You say "chances are" -- so when does one need a free() function?

When some of the resources associated with a SMOB are not under GC’s
control, and thus need to be freed explicitly by some other means.

Examples of such resources include malloc’d memory, file descriptors,
and opaque objects managed by an external library.

> I suspect that guile-dbi does require one, because it has to close
> the DB handle; right?

In the case of GDBM, for instance, ‘gdbm_open’ returns an opaque
GDBM_FILE, and that has to be explicitly freed via ‘gdbm_close’.  So
yes, in that case a SMOB ‘free’ function is required.

> Since the SMOB was allocated with scm_gc_malloc, and since the
> things it mark()s are all SCM types, does it follow that it's safe not to
> mark() it at all?

Yes.  Quoting the manual (info "(guile) Smobs"):

   Defining a marking procedure may sometimes be unnecessary because
   large parts of the process’ memory (with the exception of
   ‘scm_gc_malloc_pointerless’ regions, and ‘malloc’- or
   ‘scm_malloc’-allocated memory) are scanned for live pointers(1).

> Where can I find more information about how guile and its GC work?
> In particular, is there a FAQ guide to debugging GC problems?

The manual has some info under “Smobs” and “Memory Blocks”.  Guile’s GC
is the BDW-GC, whose documentation is available from
<http://www.hpl.hp.com/personal/Hans_Boehm/gc/>.

HTH,
Ludo’.




reply via email to

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