<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/07/dab979091d49d8282ce0ef1bf019d413"
  from="Mary Fernandez &lt;mff@r...&gt;"
  author="Mary Fernandez"
  date="2003-07-18T16:49:44"
  subject="Re: [Caml-list] Two questions about using the CamlIDL"
  prev="2003/07/c32365fec5b601a5a4639c859d9eea0f"
  next="2003/07/5d2fdf4a723ea61c443961a0ab49053d"
  prev-in-thread="2003/07/c3abb130a34e05c89945e77f613ee37a"
  prev-thread="2003/07/ccd92eb3783074eb7e12bf2b325ce13c"
  next-thread="2003/07/342f857b4e404c7db28559ad8cefffb0"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Two questions about using the CamlIDL">
<msg 
  url="2003/07/348514c77483a81afd734d23858d5f77"
  from="Mary Fernandez &lt;mff@r...&gt;"
  author="Mary Fernandez"
  date="2003-07-17T15:25:28"
  subject="[Caml-list] Two questions about using the CamlIDL">
<msg 
  url="2003/07/c3abb130a34e05c89945e77f613ee37a"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-07-17T20:27:43"
  subject="Re: [Caml-list] Two questions about using the CamlIDL">
<msg 
  url="2003/07/dab979091d49d8282ce0ef1bf019d413"
  from="Mary Fernandez &lt;mff@r...&gt;"
  author="Mary Fernandez"
  date="2003-07-18T16:49:44"
  subject="Re: [Caml-list] Two questions about using the CamlIDL">
</msg>
</msg>
</msg>
</thread>

<contents>
Xavier,
Thanks very much.  That is extremely helpful
and answers all our questions.
Mary
On Thu, 2003-07-17 at 16:27, Xavier Leroy wrote:
&gt; &gt; Our Caml application is unusual in that the Caml
&gt; &gt; app calls C functions, which in turn may call
&gt; &gt; Caml functions that return Caml objects to C, 
&gt; &gt; which in turn return those Caml objects back to the 
&gt; &gt; Caml app.  
&gt; &gt; 
&gt; &gt; 1. It is not clear how to use the CamlIDL type syntax 
&gt; &gt; to define the type of a Caml value that will be returned from C.
&gt; 
&gt; Here is a simplistic solution:
&gt; 
&gt;   quote(c, "#include "camlobj.h")
&gt; 
&gt;   typedef [mltype(string), c2ml(long_to_camlobj), ml2c(camlobj_to_long)]
&gt;     long camlobj;
&gt; 
&gt;   camlobj f(camlobj v);
&gt; 
&gt; where camlobj.h contains:
&gt; 
&gt;   typedef long camlobj;
&gt;   #define long_to_camlobj(c) (*(c))
&gt;   #define camlobj_to_long(v,c) (*(c) = (v))
&gt; 
&gt; Basically, a "camlobj" is a long integer whose coercions to and from
&gt; the Caml "value" type are the identity function.  
&gt; 
&gt; You didn't say what Caml type the Caml objects should have.  I put
&gt; "string" here, but any closed type will do.  
&gt; 
&gt; The problem with this solution is that the Caml values that transit
&gt; through the C code under the type "camlobj" are not known to the GC.
&gt; Hence, if a GC occurs (e.g. because your C functions call several Caml
&gt; functions in turn), the "camlobj" values will become wrong.
&gt; 
&gt; A way to avoid this is to wrap the Caml values in a malloced block that
&gt; is registered with the Caml GC:
&gt; 
&gt;   quote(c, "#include &lt;camlobj.h&gt;)
&gt; 
&gt;   typedef [mltype(string), c2ml(unpack_camlobj), ml2c(pack_camlobj)]
&gt;     struct packed_camlobj * camlobj;
&gt; 
&gt;   camlobj f(camlobj v);
&gt; 
&gt; where camlobj.h is
&gt; 
&gt;   typedef struct packed_camlobj { value v; } * camlobj;
&gt;   extern void pack_camlobj(value v, camlobj * c);
&gt;   extern value unpack_camlobj(camlobj * c);
&gt; 
&gt; and camlobj.c contains
&gt; 
&gt;   void pack_camlobj(value v, camlobj * c)
&gt;   {
&gt;     camlobj p = malloc(sizeof(struct packed_camlobj));
&gt;     p-&gt;v = v;
&gt;     register_global_root(&amp;(p-&gt;v));
&gt;     *c = p;
&gt;   }
&gt; 
&gt;   value unpack_camlobj(camlobj * c)
&gt;   {
&gt;     camlobj p = *c;
&gt;     value v = p-&gt;v;
&gt;     remove_global_root(&amp;(p-&gt;v));
&gt;     free(p);
&gt;     return v;
&gt;   }
&gt; 
&gt; Notice that unpack_camlobj removes the GC root and destroys the
&gt; block allocated by malloc().  This is adequate (I hope :-) if your C
&gt; code never stores a camlobj in a global data structure, but simply
&gt; passes them around.  In more complex situations, you'd need to add a
&gt; reference count to the struct packed_camlobj and make sure that the C
&gt; code maintains this refcount properly.
&gt; 
&gt; &gt; 2. Assuming we can specify the above type, the c_function 
&gt; &gt; that calls back into Caml will look something like this:
&gt; &gt; 
&gt; &gt;   CamlObj c_function() { 
&gt; &gt;     CAMLparam0();
&gt; &gt;     CAMLlocal2(caml_obj, args);
&gt; &gt; 
&gt; &gt;     ... Usual set up to get pointer to Caml function 
&gt; &gt;     and allocate space for args ...
&gt; &gt;     
&gt; &gt;     caml_obj = callbackN(*caml_function_closure, 0, args)
&gt; &gt; 
&gt; &gt;     CAMLreturn(caml_obj);
&gt; &gt;   }
&gt; &gt; 
&gt; &gt; Because c_function will be called from the IDL stub functions,
&gt; &gt; do we have to modify the stub functions to follow the same
&gt; &gt; function-call protocol as above?
&gt; 
&gt; I'm not sure I completely understand your question.  If you're asking
&gt; about GC registration of memory roots, I think the "packed_camlobj"
&gt; approach above addresses the issue in a way that does not need
&gt; modifying the stub functions nor the intermediate C functions themselves.
&gt; 
&gt; One last word: in cases of complex C/Caml interactions, as in your
&gt; example, it's often easier to work out the (GC) issues first by
&gt; writing by hand the stubs for a few functions.  Using CamlIDL from the
&gt; beginning makes things even more obscure :-)
&gt; 
&gt; Hope this helps,
&gt; 
&gt; - Xavier Leroy
-- 
Mary Fernandez &lt;mff@research.att.com&gt;
AT&amp;T Labs - Research

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

</contents>

</message>

