Re: [Haskell-cafe] Could FFI support pass-by-value of structs?
On Tue, Jun 16, 2009 at 10:48:19AM -0300, Maurício wrote: It's not usual, but it is allowed to have values of structs passed between functions directly instead of using pointers: /*/ struct ex { int x; int y; int z; }; ex example_functions (ex p) { (...) } /*/ Would it be possible to allow that in Haskell FFI by, say, allowing any instance of Storable to be used in a 'foreign' declaration? Like: There are a couple problems with this. First, the storage layout for a given C struct may be radically different depending on the back end, even sometimes depending on the operating system. So you can't write a portable Storable instance. The other problem is that the storage layout isn't enough to use structs as arguments as the calling conventions also come into play. Some calling conventions want structs expanded and places on the stack, others want a pointer to some stack allocated data, returning a struct may or may not create a hidden first argument. Now, Haskell compilers already have to tackle calling conventions for basic types, so it is entirely plausable to solve this, the question is, is it worth the effort? anything you write will end up not being portable anyway because it depends on the structure layout. The end result being, structure passing is complicated, so if you need to interface to a library that has pre-defined structeres, use hsc2hs to manually peek and poke into the correct offsets, it will make things easier and be more portable. John -- John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Could FFI support pass-by-value of structs?
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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Could FFI support pass-by-value of structs?
It's not usual, but it is allowed to have values of structs passed between functions directly instead of using pointers: /*/ struct ex { int x; int y; int z; }; ex example_functions (ex p) { (...) } /*/ Would it be possible to allow that in Haskell FFI by, say, allowing any instance of Storable to be used in a 'foreign' declaration? Like: -- data Ex = (...) instance Storable Ex where sizeOf _ = ... alignment = sizeOf (...) foreign import ccall example_functions exampleFunction :: Ex - IO Ex -- Thanks, Maurício ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe