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] CamlDL/Abstract pointers problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-01-29 (15:52)
From: ronniec95@l...
Subject: Re: [Caml-list] CamlDL/Abstract pointers problem
I understand that bit ok. But what I wanted to know is why CAMLIDL was not
doing this for me... as I said in my original email, I'm quite happy handwriting
all this stuff (Abstract_tag/Custom_tag all ok for me )- it works fine,
but is quite dull!

I specifically wanted to know what it is about camlidl which stops it generating
the correct code for this.


>-- Original Message --
>Date: Thu, 29 Jan 2004 15:02:09 +0000
>Subject: Re: [Caml-list] CamlDL/Abstract pointers problem
>From: Richard Jones <>
>On Thu, Jan 29, 2004 at 01:06:39PM +0000, wrote:
> All I want to do is just give Ocaml a pointer to something I've created
> in C and pass it back to other C functions later; don't want it to do
> with it (including mo
>ing it around the ocaml heap).\

It's my understanding that you can't just pass a pointer back to OCaml
and cast it to a value.  OCaml will think that the pointer points to
an OCaml heap object, and hence try to examine / move / delete it in
the g
>rbage collector.  Instead you need to do one of two things:
either set the bottom bit to 1, in which case OCaml will treat it as a
plain integer, or else wrap it up in a box, as in the code below.  The
Abstract_tag tells the garbage collector not to
>look inside the box.

static value
Val_voidptr (void *ptr)
  CAMLparam0 ();
  CAMLlocal1 (rv);
  rv = alloc (1, Abstract_tag);
  Field(rv, 0) = (value) ptr;
  CAMLreturn (rv);

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

>ou need to call Val_voidptr to wrap up your C void* pointer into an
OCaml box, and Voidptr_val to unwrap and get your pointer back again.

In any case you need to also add lots of calls to Gc.full_major () to
your code, which will trigger an early 
>ore dump if there is some
memory corruption.


Richard Jones.
Merjis Ltd. - improving website return on investment
If I have not seen as far as others, it is beca
>se I have been
standing in the footprints of giants.  -- from Usenet

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