[Haskell-cafe] Could FFI support pass-by-value of structs?
Felipe Lessa
felipe.lessa at gmail.com
Tue Jun 23 19:20:35 EDT 2009
On Tue, Jun 16, 2009 at 10:48:19AM -0300, Maurício wrote:
> /*****/
> struct ex {
> int x;
> int y;
> int z;
> };
>
> ex example_functions (ex p)
> {
> (...)
> }
> /*****/
You may adopt the approach I used with Hipmunk[1] where there is
a wrapper library written in C. For your example you could do
something like
> void wr_example_functions(ex *p) {
> *p = example_functions(*p);
> }
and in you Haskell code you write something like
> foreign import ccall unsafe "wrapper.h"
> wr_example_functions :: Ptr Ex -> IO ()
>
> exampleFunctions :: Ex -> IO Ex
> exampleFunctions input =
> with input $ \ptr -> do
> wr_example_functions ptr
> peek ptr
The structure is allocated on the stack, so the drawbacks are
only another function call and two structure copies, and probably
that shouldn't hurt a lot. Note that depending on the C compiler
those costs may even get somewhat optimized (e.g. by inlining).
[1] http://hackage.haskell.org/package/Hipmunk
--
Felipe.
More information about the Haskell-Cafe
mailing list