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
Re: [Caml-list] finalizing bigarrays
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-07-09 (04:48)
From: Fernando Alegre <fernando@c...>
Subject: Re: [Caml-list] finalizing bigarrays

We need to use the stock Caml 3.04, and will not update until the next
official Caml release, so unfortunately, the suggested use
of Gc.finalise is out of the way until then. 

We opted for the cloning of bigarray_stubs.c. Our modified
bigarray_custom_stubs.c is compiled into the application. It does not
replace the original one, but supplements it. After dealing with  some
garbage collection weirdness that I'll comment in a separate message, we
managed to make our custom bigarrays work. So everything is fine for us now.

I'd still suggest that a "bigarray_alloc_with_finalizer" function would be
a great addition to the standard bigarrays. It would make it so much easier
for us users to interface Caml with C libraries... without having to reinvent
the wheel each time.

And while we are at it, I would also beg for a no-bounds-checked option for
bigarrays. Then, the world will be perfect! :-)


On Mon, Jul 08, 2002 at 05:48:13PM +0200, Xavier Leroy wrote:
> > Can Gc.finalise be used with externally allocated bigarrays?
> > I thought it worked only for Caml-allocated types.
> It does.  However, a Caml bigarray value is actually a small block
> inside the Caml heap, containing pointers to the actual bigarray data.
> So, you can attach a finalization function to the Caml bigarray value.
> Presumably, this (Caml) function would call a C function that recovers
> the pointer to the actual bigarray data, then call your deallocation
> function on this pointer.  Something along the lines below:
> value my_bigarray_finalize(value v)
> {
>   struct caml_bigarray * b = Bigarray_val(v);
>   if (b->proxy == NULL) {
>     my_finalization_function(b->data);
>   } else {
>     if (-- b->proxy->refcount == 0) {
>       my_finalization_function(b->proxy->data);
>       stat_free(b->proxy);
>     }
>   }
>   return Val_unit;
> }
> - Xavier Leroy
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: