Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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) {
  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(&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