caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] finalizing bigarrays
@ 2002-07-08 14:48 Damien Doligez
  2002-07-08 14:52 ` Fernando Alegre
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Doligez @ 2002-07-08 14:48 UTC (permalink / raw)
  To: andrieu, fernando; +Cc: caml-list

>From: Olivier Andrieu <andrieu@ijm.jussieu.fr>

>How about defining
>  external finalise_my_bigarray : ('a, 'b, 'c) Bigarray.Genarray.t -> unit
>and then using
>  Gc.finalise ?

Watch out !  Gc.finalise is buggy in 3.04.  Before you use it, wait
for 3.05, or use the CVS version of O'Caml, or ask me for a patch.

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] finalizing bigarrays
  2002-07-08 14:48 [Caml-list] finalizing bigarrays Damien Doligez
@ 2002-07-08 14:52 ` Fernando Alegre
  2002-07-08 15:48   ` Xavier Leroy
  0 siblings, 1 reply; 6+ messages in thread
From: Fernando Alegre @ 2002-07-08 14:52 UTC (permalink / raw)
  To: Damien Doligez; +Cc: andrieu, fernando, caml-list


Can Gc.finalise be used with externally allocated bigarrays?

I thought it worked only for Caml-allocated types.

Am I wrong?

Fernando

On Mon, Jul 08, 2002 at 04:48:19PM +0200, Damien Doligez wrote:
> >From: Olivier Andrieu <andrieu@ijm.jussieu.fr>
> 
> >How about defining
> >  external finalise_my_bigarray : ('a, 'b, 'c) Bigarray.Genarray.t -> unit
> >and then using
> >  Gc.finalise ?
> 
> Watch out !  Gc.finalise is buggy in 3.04.  Before you use it, wait
> for 3.05, or use the CVS version of O'Caml, or ask me for a patch.
> 
> -- Damien

-- 
					
			-Fernando Alegre (fernando@cc.gatech.edu)
-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] finalizing bigarrays
  2002-07-08 14:52 ` Fernando Alegre
@ 2002-07-08 15:48   ` Xavier Leroy
  2002-07-09  4:49     ` Fernando Alegre
  0 siblings, 1 reply; 6+ messages in thread
From: Xavier Leroy @ 2002-07-08 15:48 UTC (permalink / raw)
  To: Fernando Alegre; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] finalizing bigarrays
  2002-07-08 15:48   ` Xavier Leroy
@ 2002-07-09  4:49     ` Fernando Alegre
  0 siblings, 0 replies; 6+ messages in thread
From: Fernando Alegre @ 2002-07-09  4:49 UTC (permalink / raw)
  To: Xavier Leroy; +Cc: Fernando Alegre, caml-list


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! :-)

Thanks,

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] finalizing bigarrays
  2002-07-08 13:28 Fernando Alegre
@ 2002-07-08 14:21 ` Olivier Andrieu
  0 siblings, 0 replies; 6+ messages in thread
From: Olivier Andrieu @ 2002-07-08 14:21 UTC (permalink / raw)
  To: Fernando Alegre; +Cc: caml-list

 Fernando Alegre [Monday 8 July 2002] :
 > 
 > Are there any plans to add a "finalize" function to externally
 > managed bigarrays?
 > 
 > We have a C library that allocates bigarrays in a certain complex
 > way and needs to deallocate them when the bigarrays are no longer
 > in use. We have thought of several ways to do this, but neither is
 > optimal, except the last one:
 > 
 > 1) Wrap the bigarray in a custom type that we need to drag along
 >    with it all the time. The finalizer in the custom type will then
 >    deallocate the bigarray.

How about defining

  external finalise_my_bigarray : ('a, 'b, 'c) Bigarray.Genarray.t -> unit

and then using

  Gc.finalise ?


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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Caml-list] finalizing bigarrays
@ 2002-07-08 13:28 Fernando Alegre
  2002-07-08 14:21 ` Olivier Andrieu
  0 siblings, 1 reply; 6+ messages in thread
From: Fernando Alegre @ 2002-07-08 13:28 UTC (permalink / raw)
  To: caml-list


Are there any plans to add a "finalize" function to externally managed
bigarrays?

We have a C library that allocates bigarrays in a certain complex way and
needs to deallocate them when the bigarrays are no longer in use. We have
thought of several ways to do this, but neither is optimal, except the last
one:

1) Wrap the bigarray in a custom type that we need to drag along with it
   all the time. The finalizer in the custom type will then deallocate the
   bigarray.
   
2) Do without bigarrays, and move some code from Caml to C.

3) Copy the bigarray source code (bigarray_stubs.c) and modify it to accept
   an external finalizer. We would still use bigarrays, so that compiler
   optimizations are performed.
   
4) Ask the OCaml developers to add a way to finalize bigarrays... :-)

So... any chance this will be added in the next release?

Thank you,

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2002-07-09  4:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-08 14:48 [Caml-list] finalizing bigarrays Damien Doligez
2002-07-08 14:52 ` Fernando Alegre
2002-07-08 15:48   ` Xavier Leroy
2002-07-09  4:49     ` Fernando Alegre
  -- strict thread matches above, loose matches on Subject: below --
2002-07-08 13:28 Fernando Alegre
2002-07-08 14:21 ` Olivier Andrieu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).