From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id DAA18333; Thu, 8 Mar 2001 03:38:17 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id DAA18294 for ; Thu, 8 Mar 2001 03:38:16 +0100 (MET) Received: from tet.kurims.kyoto-u.ac.jp (L026086.ppp.dion.ne.jp [211.126.26.86]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f282cCb04824 for ; Thu, 8 Mar 2001 03:38:13 +0100 (MET) Received: from localhost (localhost [127.0.0.1]) by tet.kurims.kyoto-u.ac.jp (8.11.1/8.11.1) with ESMTP id f282diL06857; Thu, 8 Mar 2001 11:39:45 +0900 (JST) (envelope-from garrigue@kurims.kyoto-u.ac.jp) To: ravi@ittc.ukans.edu Cc: caml-list@inria.fr Subject: Re: [Caml-list] Caml Wrappers In-Reply-To: References: X-Mailer: Mew version 1.94.2 on Emacs 20.7 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20010308113944U.garrigue@kurims.kyoto-u.ac.jp> Date: Thu, 08 Mar 2001 11:39:44 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Ravi Chamarty > 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