From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 785577EE25 for ; Fri, 7 Jun 2013 03:28:07 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of berenger@riken.jp) identity=pra; client-ip=134.160.33.175; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="berenger@riken.jp"; x-sender="berenger@riken.jp"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of berenger@riken.jp designates 134.160.33.175 as permitted sender) identity=mailfrom; client-ip=134.160.33.175; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="berenger@riken.jp"; x-sender="berenger@riken.jp"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of postmaster@postman.riken.jp designates 134.160.33.175 as permitted sender) identity=helo; client-ip=134.160.33.175; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="berenger@riken.jp"; x-sender="postmaster@postman.riken.jp"; x-conformance=sidf_compatible; x-record-type="v=spf1" X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AngAADs2sVGGoCGvnGdsb2JhbABZgzmDPLs1gREOAQEBAQEGFgk8giMBAQQBIwQRQBELGAICBRYLAgIJAwIBAgFFEwgBAYgDBgyqBJFQgSaNWTqCR4EUA4kejiGBKYR1jHqBRg X-IPAS-Result: AngAADs2sVGGoCGvnGdsb2JhbABZgzmDPLs1gREOAQEBAQEGFgk8giMBAQQBIwQRQBELGAICBRYLAgIJAwIBAgFFEwgBAYgDBgyqBJFQgSaNWTqCR4EUA4kejiGBKYR1jHqBRg X-IronPort-AV: E=Sophos;i="4.87,818,1363129200"; d="scan'208";a="20762715" Received: from postman3.riken.jp (HELO postman.riken.jp) ([134.160.33.175]) by mail2-smtp-roc.national.inria.fr with ESMTP; 07 Jun 2013 03:28:05 +0200 Received: from postman.riken.jp (postman3.riken.jp [127.0.0.1]) by postman.riken.jp (Postfix) with SMTP id 5C58238380FC for ; Fri, 7 Jun 2013 10:28:02 +0900 (JST) Received: from [172.27.98.103] (rikad98.riken.jp [134.160.214.98]) by postman.riken.jp (Postfix) with ESMTPA id D0A9B38200DD for ; Fri, 7 Jun 2013 10:28:01 +0900 (JST) Message-ID: <51B13721.1070101@riken.jp> Date: Fri, 07 Jun 2013 10:28:01 +0900 From: Francois Berenger User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: caml-list@inria.fr References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-PMX-Version: 6.0.0.2142326, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2013.6.7.12118 Subject: Re: [Caml-list] ANN: ocaml-ctypes, a library for calling C functions directly from OCaml That looks very interesting!!! How about the cost of exchanging values between C and OCaml? Is there a trick in ocaml-ctypes like there is for bigarrays? Regards, F. On 06/07/2013 08:17 AM, Jeremy Yallop wrote: > I'm happy to announce the initial release of ocaml-ctypes. > > The ocaml-ctypes library makes it possible to call C functions > directly from OCaml without writing or generating C code. The core of > the library is a set of combinators for describing C types -- scalars, > functions, structs, unions, arrays, and pointers to values and > functions. Type descriptions can then be used to bind native > functions and values. Here's a simple example: > > # let puts = foreign "puts" (string @-> returning int);; > val puts : string -> int = > # puts "Hello, world!";; > Hello, world! > > Here's a more substantial example that shows how to describe a C > structure type, map the type to an OCaml record, and call a function > that returns the structure. > > (* Describe the C struct. There are two fields, both ints. *) > let div_t = structure "div_t";; > let q = div_t *:* int > let r = div_t *:* int > let () = seal div_t > > (* Define the OCaml record that we'll use to view the C structure. *) > type div_result = { quot : int; rem: int } > > (* Define the conversions between the C struct and the OCaml record. *) > let div_result_of_div_t d = { quot = getf d q; rem = getf d r } > let div_t_of_div_result {quot; rem} = > let d = make div_t in (setf d q quot; setf d r rem; d) > > (* Create a "view type" for that looks like div_result and behaves > like div_t *) > let div_result = view ~read:div_result_of_div_t > ~write:div_t_of_div_result div_t > > (* Bind to the standard C `div' function *) > let div = foreign "div" (int @-> int @-> returning div_result) > > (* Try it out *) > # div 17 2;; > - : div_result = {quot = 8; rem = 1} > > The distribution contains larger examples and a fairly extensive test > suite, showing how to use other features of the library, such as > binding to functions that accept callback arguments. Among the > examples is Anil Madhavapeddy's port of the `curses' example from the > OCaml documentation; it's instructive to compare the two > implementations: > > OCaml manual curses example > http://caml.inria.fr/pub/docs/manual-ocaml/manual033.html#toc147 > > ocaml-ctypes curses example > https://github.com/ocamllabs/ocaml-ctypes/blob/master/examples/ncurses/ncurses.ml > > Detailed installation instructions for ocaml-ctypes can be found in > the tutorial. (Briefly: ensure libffi is installed, then 'opam > install ctypes'.) > > Comments, bug reports, and other feedback are most welcome. > > Tutorial: > https://github.com/ocamllabs/ocaml-ctypes/wiki/ctypes-tutorial > Examples: > https://github.com/ocamllabs/ocaml-ctypes/tree/master/examples > API documentation: http://ocamllabs.github.io/ocaml-ctypes/ > Github repository: https://github.com/ocamllabs/ocaml-ctypes > Direct download: > https://github.com/ocamllabs/ocaml-ctypes/archive/ocaml-ctypes-0.1.tar.gz >