[Haskell-cafe] FFI and returning structs

Donn Cave donn at avvanta.com
Wed Nov 19 04:23:02 EST 2008


Quoth Maurcio <briqueabraque at 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


More information about the Haskell-Cafe mailing list