Re: [Haskell-cafe] FFI and returning structs
Quoth Maurcio <briqueabraque@yahoo.com>: | I have not found examples of this in documentation | or hackage packages: how can I deal with funcions | returning not pointers to structs but structs | thenselves? | | struct example {... | | example function_name (... | | My intuition said I could try a data declaration | that is a properly done instance of Storable, and | then use that. | | data Example = ... | | instance Storable Example where ... | | But it seems I was wrong. Eh! probably not wrong - the problem is not how the data looks, but where it is. If it were my problem, I would try a wrapper function that returns the same value as an argument - e.g., if you have struct X f(), then void f_(struct X *x) { *x = f(); } ... so your wrapper will look like ccall "f_" f :: Ptr X -> IO () instead of ccall "f" f :: IO X Ironically, that's actually just what the original function is doing - cf. "struct return convention", where the caller allocates space and passes a pointer as a hidden parameter. But you can't just declare it that way, because the original f bumps the stack pointer on return (you may see "ret $4" in the assembly code, on Intel hardware) Try to compile the wrapper with the same compiler that compiled the original function. I'm really just guessing that your Haskell compiler doesn't understand or perfectly handle this particular C wart. If my suggestion doesn't work, I may have guessed wrong. Donn
I have not found examples of this in documentation or hackage packages: how can I deal with funcions returning not pointers to structs but structs thenselves?
struct example {...
example function_name (...
(...)
(...)
If it were my problem, I would try a wrapper function that returns the same value as an argument - e.g., if you have struct X f(), then
void f_(struct X *x) { *x = f(); }
(...) Ironically, that's actually just what the original function is doing - cf. "struct return convention", where the caller allocates space and passes a pointer as a hidden parameter. (...)
Can I be sure things always goes like that? I remember reading somewhere that if, for instance, a struct is smaller in size than a pointer (like a struct made of three chars), the struct could be returned by itself. Also, why Haskell FFI do not automate that? As long as it is Storable data, can't it get the pointer and 'peek' it for me? Thanks, Maurício
participants (2)
-
Donn Cave -
Maurício