Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Christopher Quinn <cq@htec.demon.co.uk>
To: Jakob Lichtenberg <jl@itu.dk>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] camlidl - finalizing cows without COM!
Date: Thu, 18 Apr 2002 16:30:09 +0100	[thread overview]
Message-ID: <3CBEE681.9090404@htec.demon.co.uk> (raw)
In-Reply-To: <000901c1e5c1$f47cc130$5ba4389d@redmond.corp.microsoft.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


  reply	other threads:[~2002-04-18 15:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-17  3:42 Jakob Lichtenberg
2002-04-18 15:30 ` Christopher Quinn [this message]
2002-04-19  3:13   ` Jakob Lichtenberg
2002-04-26  8:20 ` Xavier Leroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3CBEE681.9090404@htec.demon.co.uk \
    --to=cq@htec.demon.co.uk \
    --cc=caml-list@inria.fr \
    --cc=jl@itu.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox