Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Garbage collection and a reference counting library
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-12-11 (15:29)
From: Jean-Christophe Filliatre <filliatr@l...>
Subject: Re: [Caml-list] Garbage collection and a reference counting library

Richard Jones writes:
 > I wrote an interface to allow OCaml to call Perl code and libraries.
 > (...)
 > 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;
 > }

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

static struct custom_operations perl_custom_operations = {

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

static value
Val_voidptr (void *ptr)
  Perl_val(rv) = ptr;
  CAMLreturn (rv);


Hope this helps,

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: