https://wiki.haskell.org/index.php?title=Library/libffi&feed=atom&action=historyLibrary/libffi - Revision history2024-03-19T11:29:25ZRevision history for this page on the wikiMediaWiki 1.35.5https://wiki.haskell.org/index.php?title=Library/libffi&diff=28501&oldid=prevRemi: create libffi "homepage"2009-06-04T19:38:55Z<p>create libffi "homepage"</p>
<p><b>New page</b></p><div>libffi is a binding to the C library [http://sourceware.org/libffi/ libffi], allowing C functions to be called whose types are not known before run-time.<br />
<br />
It was designed to easily enable the addition of an foreign function interface to a (not-yet-released) new interpreter.<br />
<br />
==How to use it==<br />
<br />
The main function it exports is:<br />
<br />
callFFI :: FunPtr a -> RetType b -> [Arg] -> IO b<br />
<br />
In addition, many values like <hask>retWord64 :: RetType Word64</hask> and <hask>argCULong :: CULong -> Arg</hask> are exported.<br />
<br />
The main module to import is [http://hackage.haskell.org/packages/archive/libffi/0.1/doc/html/Foreign-LibFFI.html Foreign.LibFFI]. Importing the other modules should only be necessary when one wants to extend the API to support passing/returning new types to C.<br />
<br />
===Example===<br />
<br />
And because code is worth a thousand words, here a small program<br />
that uses C calls to write a 1Gb buffer of uninitialized garbage to a file:<br />
<br />
import System.Posix.DynamicLinker<br />
import Foreign.LibFFI<br />
<br />
main = do<br />
malloc <- dlsym Default "malloc"<br />
creat <- dlsym Default "creat"<br />
write <- dlsym Default "write"<br />
let sz = 2 ^ 30<br />
buf <- callFFI malloc (retPtr retVoid) [argCSize sz]<br />
fd <- callFFI creat retCInt [argString "/tmp/test", argCUInt 0o644]<br />
n <- callFFI write retCSize [argCInt fd, argPtr buf, argCSize sz]<br />
putStrLn $ show n ++ " bytes written"<br />
<br />
More interesting examples are included in examples/ in the<br />
package.<br />
<br />
==Does it work?==<br />
<br />
It should work on any 32/64bits machine on which libffi works,<br />
but has been primarily tested on linux x86_64.<br />
The current libffi is not exception-safe (exception = memory leak)<br />
and callFFI has quite some overhead that would be unnecessary<br />
with another api.<br />
It is, however, very easy to use.<br />
<br />
In the long term, I am not sure whether it will survive in its current form: Although easy, the current API makes it impossible to be really efficient (with the lists, decoding and initialization for each call). In addition, libffi doesn't work on windows, and I am not sure whether e.g. a [http://www.nongnu.org/cinvoke/ C/Invoke] binding would use the same API.<br />
<br />
==How to get and install it==<br />
A cabal package is available [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/libffi here].<br />
<br />
If you have cabal-install installed, this should also work:<br />
<br />
cabal install libffi<br />
<br />
[[Category:Libraries]]</div>Remi