Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] camlidl - finalizing cows without COM!
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christopher Quinn <cq@h...>
Subject: Re: [Caml-list] camlidl - finalizing cows without COM!
Jakob Lichtenberg wrote:
 > typedef int cow;
 > cow createAndReferenceCow(void);
 > void freeCow(cow);
 >
 > Now, when the OCaml structures for some reason decide to 
garbage collect my
 > cow the finalized object should call freeCow......  I 
have done this a bunch
 > of times without the idl interface, but have no clue how 
to specify this in
 > an idl file.  Help!!!
 >
 > And I would really *love* not to bring COM interfaces 
into this...

Hi,

You can use the finalisation feature of Caml's C interface,
but you will need to play games with your IDL declarations:

value mkcow(COW c)
{
  value v = alloc_custom(cow_ops,.....);
  *Data_custom_ptr(v) = c;
  return v;
}
#define COW_val(v) (COW)*Data_custom_ptr(v)
#define Val_COW(c) mkcow(c)

#define COW_c2ml(c,ctx)   Val_COW(c)
#define COW_ml2c(v,cp,ctx) COW_val(v)

and finally in idl ...

typedef [ml2c(COW_ml2c), c2ml(COW_c2ml)]
   int cow;

That is it in general, but undoubtedly wrong in detail.
One has to worry about the uniqueness of a cow handle 
because in the normal course of using cow related idl 
functions, the handle may repeatedly be allocated a new 
container value, each of which will have the finalisation 
function you defined for cow_ops invoked on it.

I tied myself in knots trying different schemes and in the 
end I discovered Gc.finalise, which means you can declare 
free_cow() in IDL and use it naturally from within caml!
*BUT* Gc.finalise cannot handle cascades of resource 
handles, ie. if one handle is used to request another, 
dependent resource then even if you arrange for there to be 
gc dependencies between the two, their finalisation may 
still occur out of order.

- Chris Quinn

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