caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] camlidl - finalizing cows without COM!
@ 2002-04-17  3:42 Jakob Lichtenberg
  2002-04-18 15:30 ` Christopher Quinn
  2002-04-26  8:20 ` Xavier Leroy
  0 siblings, 2 replies; 4+ messages in thread
From: Jakob Lichtenberg @ 2002-04-17  3:42 UTC (permalink / raw)
  To: caml-list

Hi,

I have just started hacking in camlidl and for now had a really pleasant
experience.

However I really can't find out how to create a finalized object without
going into COM interfaces.

Say my underlying C library for manipulating ..... hmmm.. cows  has a
function that creates and references a new cow, and a function that is used
to de-reference the cows when they no longer are live:

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

Thanks,

- Jakob Lichtenberg
-------------------
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] 4+ messages in thread

* Re: [Caml-list] camlidl - finalizing cows without COM!
  2002-04-17  3:42 [Caml-list] camlidl - finalizing cows without COM! Jakob Lichtenberg
@ 2002-04-18 15:30 ` Christopher Quinn
  2002-04-19  3:13   ` Jakob Lichtenberg
  2002-04-26  8:20 ` Xavier Leroy
  1 sibling, 1 reply; 4+ messages in thread
From: Christopher Quinn @ 2002-04-18 15:30 UTC (permalink / raw)
  To: Jakob Lichtenberg; +Cc: caml-list

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


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

* Re: [Caml-list] camlidl - finalizing cows without COM!
  2002-04-18 15:30 ` Christopher Quinn
@ 2002-04-19  3:13   ` Jakob Lichtenberg
  0 siblings, 0 replies; 4+ messages in thread
From: Jakob Lichtenberg @ 2002-04-19  3:13 UTC (permalink / raw)
  To: Christopher Quinn; +Cc: caml-list

Dear Christopher,

Thanks a lot for your help, I can now produce and garbage collect all the
cows I would ever dream of!

There are still a couple of open issues.  My current favourite would be:


Who declares what?!?


The CamlIDL User's manual Sec. 3.8 tells me what prototypes to implement: a
c2ml and an ml2c function.  And then I can use them using the following IDL
declaration (in cow.idl):
  typedef [abstract,c2ml(cow_c2ml), ml2c(cow_ml2c)] int cow;

However I need to declare and implement cow_c2ml and cow_ml2c, right?  I
cannot do that directly in the IDL file since the IDL types does NOT contain
the 'value' type.  I needed to put them in a separate module and then
include them using a quote declaration:

  quote(h,"#include \"cow_marshalling.h\"")

That opened up for a bunch of new entertaining stuff since the c2ml and ml2c
declarations uses an (according to the manual 'internal'?!?) type
'camlidl_ctx' which is declared in the camlidlruntime.h header file.  A file
that I as a 'user' to the best of my knowledge should not include
manually...

Anyway, having made a bunch of strange declarations and includes everything
now basically ROCKS.  CamlIDL have cut 50% of my source code and made
everything so much cooler, thanks Xavier Leroy and Christopher!!!

- Jakob


-------------------
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] 4+ messages in thread

* Re: [Caml-list] camlidl - finalizing cows without COM!
  2002-04-17  3:42 [Caml-list] camlidl - finalizing cows without COM! Jakob Lichtenberg
  2002-04-18 15:30 ` Christopher Quinn
@ 2002-04-26  8:20 ` Xavier Leroy
  1 sibling, 0 replies; 4+ messages in thread
From: Xavier Leroy @ 2002-04-26  8:20 UTC (permalink / raw)
  To: Jakob Lichtenberg; +Cc: caml-list

> Hi,
> 
> I have just started hacking in camlidl and for now had a really pleasant
> experience.
> 
> However I really can't find out how to create a finalized object without
> going into COM interfaces.
> 
> Say my underlying C library for manipulating ..... hmmm.. cows  has a
> function that creates and references a new cow, and a function that is used
> to de-reference the cows when they no longer are live:
> 
> 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!!!

There's a new release of CamlIDL at http://caml.inria.fr/camlidl/
that supports a "finalize" attribute to do just this, e.g.

        typedef [abstract,finalize(freeCow)] int cow;

The release also fixes some minor bugs with error reporting in
presence of import files, and with "const" qualifiers.  (Thanks for
Dmitry Bely for his extensive testing and bug hunting!)

- 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] 4+ messages in thread

end of thread, other threads:[~2002-04-26  8:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-17  3:42 [Caml-list] camlidl - finalizing cows without COM! Jakob Lichtenberg
2002-04-18 15:30 ` Christopher Quinn
2002-04-19  3:13   ` Jakob Lichtenberg
2002-04-26  8:20 ` Xavier Leroy

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