caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Caml Wrappers
@ 2001-03-07 23:58 Ravi Chamarty
  2001-03-08  2:39 ` Jacques Garrigue
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Ravi Chamarty @ 2001-03-07 23:58 UTC (permalink / raw)
  To: caml-list


Hi ,

 I am trying to write OCaml interfaces around a C library.  I tried
looking at the Unix library and trying to understand it. However, I have
much complex data types to deal with.I was wondering if anyone could help
me out. 

Here is a signature of one of the C functions:

an_flow_t * 
an_flow_create(void *mem, an_cred_t *cred, an_cpuspec_t *cspec,
		an_memspec_t *mspec, an_flowinit_func_t init, 
		void *initarg, an_flowterm_func_t term)


Each of these types are complex structures in C. How do I represent these
in OCaml. Is it useful to use classes and methods? Or do I use abstract
types to represent them ?

An example of one of these structures is given below:

struct an_flow_t {
  struct hnode me; 	  /* flow hier link (must be first) */
  struct ani_cred *cred;  /* credentials */
  struct ani_mempool *mpool;  /* mempool */
  struct ani_chan *chanlist;  /* list of channels */
  struct ani_threadpool tpool;  /* thread pool */
  int flags;  /* various flags */
  an_flowterm_func_t termfunc; /* termination function */
  ani_resource_t res;  /* reserved resources */
  int refs;  /* reference count */
  ani_ilock_t lock;  /* mutual exclusion */
  an_flowstats_t stats;  /* statistics */
};   


I would be grateful for any help.

Thanks
Ravi

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Caml Wrappers
  2001-03-07 23:58 [Caml-list] Caml Wrappers Ravi Chamarty
@ 2001-03-08  2:39 ` Jacques Garrigue
  2001-03-08  8:45 ` Jocelyn Serot
  2001-03-08 15:26 ` John Max Skaller
  2 siblings, 0 replies; 5+ messages in thread
From: Jacques Garrigue @ 2001-03-08  2:39 UTC (permalink / raw)
  To: ravi; +Cc: caml-list

From: Ravi Chamarty <ravi@ittc.ukans.edu>

>  I am trying to write OCaml interfaces around a C library.  I tried
> looking at the Unix library and trying to understand it. However, I have
> much complex data types to deal with.I was wondering if anyone could help
> me out. 
> 
> Here is a signature of one of the C functions:
> 
> an_flow_t * 
> an_flow_create(void *mem, an_cred_t *cred, an_cpuspec_t *cspec,
> 		an_memspec_t *mspec, an_flowinit_func_t init, 
> 		void *initarg, an_flowterm_func_t term)
> 
> 
> Each of these types are complex structures in C. How do I represent these
> in OCaml. Is it useful to use classes and methods? Or do I use abstract
> types to represent them ?

Classes and methods would be no help. You can use them later to wrap up
an abstract datatype, if it is really heavily used, but generally you
won't need them.

The real choice is rather between using an abstract datatype wrapping
a pointer to the C structure, or converting to a caml data structure
(records, and eventually sums).  This essentially depends on whether
you have control on when these structures will be freed: if they can
disappear suddenly, then it means that with abstract datatypes, you
could have a stale pointer on the caml side. Then the only safe
approach is to convert, which Unix does in almost all cases.

If you can decide when to deallocate these structures, then abstract
datatype is a potential approach.  The advantage is that you don't
need to convert back C values to caml, with all the gory GC details.
You just define accessor functions in C, and call them from caml.
Most of lablgtk is done that way, but it is a bit overly complex
because it also uses automatic deallocation (finalized pointers) which
you do not necessarily need. A simpler example is the Dbm library in
the standard distribution.

> An example of one of these structures is given below:
> 
> struct an_flow_t {
>   struct hnode me; 	  /* flow hier link (must be first) */
>   struct ani_cred *cred;  /* credentials */
>   struct ani_mempool *mpool;  /* mempool */
>   struct ani_chan *chanlist;  /* list of channels */
>   struct ani_threadpool tpool;  /* thread pool */
>   int flags;  /* various flags */
>   an_flowterm_func_t termfunc; /* termination function */
>   ani_resource_t res;  /* reserved resources */
>   int refs;  /* reference count */
>   ani_ilock_t lock;  /* mutual exclusion */
>   an_flowstats_t stats;  /* statistics */
> };   
> 
> 
> I would be grateful for any help.
> 
> Thanks
> Ravi
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr
> 
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


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

* [Caml-list] Caml Wrappers
  2001-03-07 23:58 [Caml-list] Caml Wrappers Ravi Chamarty
  2001-03-08  2:39 ` Jacques Garrigue
@ 2001-03-08  8:45 ` Jocelyn Serot
  2001-03-08 18:53   ` Ravi Chamarty
  2001-03-08 15:26 ` John Max Skaller
  2 siblings, 1 reply; 5+ messages in thread
From: Jocelyn Serot @ 2001-03-08  8:45 UTC (permalink / raw)
  To: Ravi Chamarty; +Cc: caml-list

Ravi Chamarty writes:
> 
> Hi ,
> 
>  I am trying to write OCaml interfaces around a C library.  I tried
> looking at the Unix library and trying to understand it. However, I have
> much complex data types to deal with.I was wondering if anyone could help
> me out. 
> 
> Here is a signature of one of the C functions:
> 
> an_flow_t * 
> an_flow_create(void *mem, an_cred_t *cred, an_cpuspec_t *cspec,
> 		an_memspec_t *mspec, an_flowinit_func_t init, 
> 		void *initarg, an_flowterm_func_t term)
> 
> 
> Each of these types are complex structures in C. How do I represent these
> in OCaml. Is it useful to use classes and methods? Or do I use abstract
> types to represent them ?
> 
> An example of one of these structures is given below:
> 
> [...]
> I would be grateful for any help.
> 
> Thanks
> Ravi
> 

Did you have a look a Camlidl (http://pauillac.inria.fr/caml/camlidl) ?
It can generate rather complex stub code for you.

Jocelyn

-- 
E-mail: Jocelyn.Serot@l_a_s_m_e_a.u_n_i_v-bpclermont.fr .....................
S-mail: LASMEA - UMR 6602 CNRS, Universite Blaise Pascal, 63177 Aubiere cedex
Tel: (33) 04 73.40.73.30 - Fax: (33) 04 73.40.72.62 .........................
Valid e-mail: remove underscores (sorry, this is prevention against junk mail
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Caml Wrappers
  2001-03-07 23:58 [Caml-list] Caml Wrappers Ravi Chamarty
  2001-03-08  2:39 ` Jacques Garrigue
  2001-03-08  8:45 ` Jocelyn Serot
@ 2001-03-08 15:26 ` John Max Skaller
  2 siblings, 0 replies; 5+ messages in thread
From: John Max Skaller @ 2001-03-08 15:26 UTC (permalink / raw)
  To: Ravi Chamarty; +Cc: caml-list

Ravi Chamarty wrote:
> 
> Hi ,
> 
>  I am trying to write OCaml interfaces around a C library.  I tried
> looking at the Unix library and trying to understand it. However, I have
> much complex data types to deal with.I was wondering if anyone could help
> me out.
> 
> Here is a signature of one of the C functions:
> 
> an_flow_t *
> an_flow_create(void *mem, an_cred_t *cred, an_cpuspec_t *cspec,
>                 an_memspec_t *mspec, an_flowinit_func_t init,
>                 void *initarg, an_flowterm_func_t term)
> 
> Each of these types are complex structures in C. How do I represent these
> in OCaml. Is it useful to use classes and methods? Or do I use abstract
> types to represent them ?

In the first instance, you should map these to tuples. Forget
abstraction
at the Ocaml/C interface. Keep the mapping(s) as close to an isomorphism 
as possible.

If you then wish to provide Ocaml with a more abstract view, you can
then
provide an 'in Ocaml' abstraction layer, using modules or classes.


Take a careful look at which of your C structures requires modification
from the Ocaml side. This cannot be so easily done by a simple
pair of mappings. If you require extensive modification, you might
consider
representing the C structure as a handle (abstract value), and using
functions to modify the underlying C structure directly.

For an extensive example of mappings, have a look at the mlgtk package.
Note that creating a binding to a C library with a large set of data
types
is a LOT of work.

-- 
John (Max) Skaller, mailto:skaller@maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
checkout Vyper http://Vyper.sourceforge.net
download Interscript http://Interscript.sourceforge.net
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Caml Wrappers
  2001-03-08  8:45 ` Jocelyn Serot
@ 2001-03-08 18:53   ` Ravi Chamarty
  0 siblings, 0 replies; 5+ messages in thread
From: Ravi Chamarty @ 2001-03-08 18:53 UTC (permalink / raw)
  To: Jocelyn Serot; +Cc: caml-list


Hi,

 Thanks for all the help. I looked up at both the CamlIDL and the dbm
stuff . Both of them helped a lot. I even tried a few wrappers similar to
the dbm library. However, since my C library is quite extensive, using
CamlIDL seems to be a good idea.

 Can anyone please spare a few IDL samples to help me get a good idea of
it.

Thanks again for all the help
Ravi
 



On Thu, 8 Mar 2001, Jocelyn Serot wrote:

> Ravi Chamarty writes:
> > 
> > Hi ,
> > 
> >  I am trying to write OCaml interfaces around a C library.  I tried
> > looking at the Unix library and trying to understand it. However, I have
> > much complex data types to deal with.I was wondering if anyone could help
> > me out. 
> > 
> > Here is a signature of one of the C functions:
> > 
> > an_flow_t * 
> > an_flow_create(void *mem, an_cred_t *cred, an_cpuspec_t *cspec,
> > 		an_memspec_t *mspec, an_flowinit_func_t init, 
> > 		void *initarg, an_flowterm_func_t term)
> > 
> > 
> > Each of these types are complex structures in C. How do I represent these
> > in OCaml. Is it useful to use classes and methods? Or do I use abstract
> > types to represent them ?
> > 
> > An example of one of these structures is given below:
> > 
> > [...]
> > I would be grateful for any help.
> > 
> > Thanks
> > Ravi
> > 
> 
> Did you have a look a Camlidl (http://pauillac.inria.fr/caml/camlidl) ?
> It can generate rather complex stub code for you.
> 
> Jocelyn
> 
> -- 
> E-mail: Jocelyn.Serot@l_a_s_m_e_a.u_n_i_v-bpclermont.fr .....................
> S-mail: LASMEA - UMR 6602 CNRS, Universite Blaise Pascal, 63177 Aubiere cedex
> Tel: (33) 04 73.40.73.30 - Fax: (33) 04 73.40.72.62 .........................
> Valid e-mail: remove underscores (sorry, this is prevention against junk mail
> 


-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2001-03-09 18:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-07 23:58 [Caml-list] Caml Wrappers Ravi Chamarty
2001-03-08  2:39 ` Jacques Garrigue
2001-03-08  8:45 ` Jocelyn Serot
2001-03-08 18:53   ` Ravi Chamarty
2001-03-08 15:26 ` John Max Skaller

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