<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/12/222cf89526a65ce589d277efdd2829d4"
  from="Jean-Christophe Filliatre &lt;filliatr@l...&gt;"
  author="Jean-Christophe Filliatre"
  date="2003-12-11T15:29:27"
  subject="Re: [Caml-list] Garbage collection and a reference counting library "
  prev="2003/12/00762880f710a47d94d59f5f9e80641e"
  next="2003/12/aecf296ada293906c355ef91f393e249"
  prev-in-thread="2003/12/23761d3eb8a5f461976c2334cc53cb1c"
  next-in-thread="2003/12/273818949a6b419c61c9cef3c10d64bf"
  prev-thread="2003/12/0a730e95e9a98ca965d70ceb4206faf5"
  next-thread="2003/12/b218a258251a78c1b945b8dfd7846220"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Garbage collection and a reference counting library ">
<msg 
  url="2003/12/23761d3eb8a5f461976c2334cc53cb1c"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2003-12-11T14:36:46"
  subject="[Caml-list] Garbage collection and a reference counting library ">
<msg 
  url="2003/12/222cf89526a65ce589d277efdd2829d4"
  from="Jean-Christophe Filliatre &lt;filliatr@l...&gt;"
  author="Jean-Christophe Filliatre"
  date="2003-12-11T15:29:27"
  subject="Re: [Caml-list] Garbage collection and a reference counting library ">
<msg 
  url="2003/12/273818949a6b419c61c9cef3c10d64bf"
  from="David Brown &lt;caml-list@d...&gt;"
  author="David Brown"
  date="2003-12-11T16:57:44"
  subject="Re: [Caml-list] Garbage collection and a reference counting library">
</msg>
</msg>
<msg 
  url="2003/12/17a764809903dd9118a09590699d8f47"
  from="Damien Doligez &lt;damien.doligez@i...&gt;"
  author="Damien Doligez"
  date="2003-12-16T22:54:22"
  subject="Re: [Caml-list] Garbage collection and a reference counting library ">
</msg>
</msg>
</thread>

<contents>

Richard Jones writes:
 &gt; 
 &gt; I wrote an interface to allow OCaml to call Perl code and libraries.
 &gt; (...)
 &gt; But I'm not really sure how to do this.  At present the code uses the
 &gt; following subroutine to wrap up Perl SVs (the void *ptr in this case)
 &gt; in OCaml objects with an ABSTRACT_TAG.  (Note also the XXX comment,
 &gt; since I'm not actually sure if this code itself is correct).
 &gt; 
 &gt; static value
 &gt; Val_voidptr (void *ptr)
 &gt; {
 &gt;   value rv = alloc (1, Abstract_tag); /* XXX Is this correct? */
 &gt;   Field(rv, 0) = (value) ptr;
 &gt;   return rv;
 &gt; }

A first remark: to be GC-friendly you have to use CAMLlocal1 to
declare rv and CAMLreturn instead of return.

Then, to be able to add a finalization function, you should use custom
blocks and not the Abstract_tag. The code then looks like

======================================================================
#define Perl_val(x) (*((void**)(Data_custom_val(x))))

void ml_perl_finalize(value v) {
  perlFree(Perl_val(v));
}

static struct custom_operations perl_custom_operations = {
  "...",
  ml_perl_finalize,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default
};

#define ALLOC_PERL(v) \
  v = alloc_custom(&amp;perl_custom_operations,sizeof(void*),0,1)

static value
Val_voidptr (void *ptr)
{
  CAMLlocal1(rv);
  ALLOC_PERL(rv);
  Perl_val(rv) = ptr;
  CAMLreturn (rv);
}

======================================================================

Hope this helps,
-- 
Jean-Christophe 

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

</contents>

</message>

