From: Richard Jones <rich@annexia.org>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Easier FFI
Date: Fri, 21 Dec 2007 11:20:06 +0000 [thread overview]
Message-ID: <20071221112006.GA10879@furbychan.cocan.org> (raw)
In-Reply-To: <200712201639.15513.jon@ffconsultancy.com>
I've got flu at the moment so apologies about this slightly unfocused
laundry-list ...
Lessons from ocaml-libvirt:
[Line numbers refer to this file:
http://hg.et.redhat.com/virt/applications/virt-top--devel?f=c29881e5aa70;file=libvirt/libvirt_c.c ]
(1) You need to be able to link to multiple versions of a dynamic
library. Programs written in OCaml, using ocaml-libvirt, can link to
any libvirt.so >= 0.2.1, even to *future* versions of libvirt.so.
At the very least you need to be able to find out if a function exists
in the dynamic library, and turn non-existance into an OCaml exception
(instead of a dynamic linker segfault). [lines 65-143, example on
lines 560-578]
(2) In this library and in ODE, C objects contain implicit
relationships with each other, and use reference counting to free
themselves. This had to be modelled by making the implicit references
explicit in OCaml (so the GC knows about them). [lines 168-208]
(3) Some libvirt API calls are long-running and need
enter_blocking_section / leave_blocking_section around them. Others
are short-running and so don't. [lines 47-57]
(4) You can't assume that a library won't add more values to the end
of an enum, particularly if you allow yourself to link to a later
version of the library than you compile against. [lines 1826-1889]
On the subject of 'ctypes':
perl4caml offers two 'levels' of access to Perl code. At the low
level you can directly load / instantiate Perl packages and objects.
This low level is unsafe -- it can throw runtime type exceptions --
but requires no explicit binding code at all. If you see a Perl
library which you like, as long as it is installed on your system an
OCaml program can use it. There's also a higher level where you can
write OCaml code which uses Perl objects in a type-safe way, but that
requires someone to write bindings for the Perl library.
However does a 'ctypes for OCaml' need to be type unsafe. PG'OCaml
(my PostgreSQL bindings for OCaml) does this with a camlp4
preprocessor which processes the SQL statements embedded in the OCaml
code, checks the types of the arguments / return values with the
database, and generates inline type-safe code. If there was a way to
take a block of C code, work out what types it requires (using
something like gcc? CIL?), then you could generate compile-time
type-safe bindings for inline C code. That would be a marvellous
thing to have ...
Rich.
--
Richard Jones
Red Hat
next prev parent reply other threads:[~2007-12-21 11:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-20 16:39 Jon Harrop
2007-12-21 3:37 ` [Caml-list] " Dave Benjamin
2007-12-21 3:54 ` Jon Harrop
2007-12-21 11:20 ` Richard Jones [this message]
2008-02-01 17:13 ` Alan Falloon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071221112006.GA10879@furbychan.cocan.org \
--to=rich@annexia.org \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox