Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: caml-list@inria.fr
Subject: [Caml-list] Garbage collection and a reference counting library
Date: Thu, 11 Dec 2003 14:36:40 +0000	[thread overview]
Message-ID: <20031211143640.GA21082@redhat.com> (raw)

Dear OCamlers,

I wrote an interface to allow OCaml to call Perl code and libraries.
This interface doesn't pay much attention to collecting memory
correctly.  In particular when a Perl SV (scalar value) is allocated
and passed to us, it will have a Perl internal link count of, say, 1,
and we never decrement that link count, so Perl will never free it.

This wasn't originally an issue because all the programs I was writing
were shortlived (and Perl has enough memory leaks of its own anyway).
But inevitably perhaps, I'm now dealing with a long-running OCaml /
Perl hybrid program, and the lack of proper memory handling has bitten
me on the backside.  So now it looks like I'll need to spend some time
fixing this.

But I'm not really sure how to do this.  At present the code uses the
following subroutine to wrap up Perl SVs (the void *ptr in this case)
in OCaml objects with an ABSTRACT_TAG.  (Note also the XXX comment,
since I'm not actually sure if this code itself is correct).

static value
Val_voidptr (void *ptr)
{
  value rv = alloc (1, Abstract_tag); /* XXX Is this correct? */
  Field(rv, 0) = (value) ptr;
  return rv;
}

#define Voidptr_val(type,rv) ((type *) Field ((rv), 0))

#define Val_sv(sv) (Val_voidptr ((sv)))
#define Sv_val(svv) (Voidptr_val (SV, (svv)))

I imagine that what I should do is to add a finalization function, so
that when 'rv' is garbage collected, I should decrement the Perl link
count, thus forcing Perl to free the associated SV.

Is this safe though?  Can OCaml copy this object, and collect one of
them, thus causing the SV to be deallocated early?  Do I need another
layer of indirection between 'rv' and the SV?

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
C2LIB is a library of basic Perl/STL-like types for C. Vectors, hashes,
trees, string funcs, pool allocator: http://www.annexia.org/freeware/c2lib/

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


             reply	other threads:[~2003-12-11 14:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-11 14:36 Richard Jones [this message]
2003-12-11 15:14 ` Jean-Christophe Filliatre
2003-12-11 16:57   ` David Brown
2003-12-16 22:55 ` Damien Doligez

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

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

  git send-email \
    --in-reply-to=20031211143640.GA21082@redhat.com \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    /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