Suppose I have the following C code:
typedef struct Foo {
char* p1; /* Some data on the heap. */
size_t s1; /* Size of data. */
char* p2; /* More data on the heap. */
size_t s2;
} Foo;
/* Allocates and writes two pieces of data on the heap and returns them in a Foo. */
Foo makeFoo(size_t x);
However, I believe I can have a ccall if I change makeFoo() to either of the following:
Foo* makeFoo(size_t x);
void makeFoo(Foo* out, size_t x);
The first involves the C code allocating a Foo and returning a pointer to it (so now there's one more pointer for the C code to deallocate later in another function). The second involves the C code writing a Foo value to a piece of memory allocated in Haskell (possibly using Foreign.Marshall.Alloc.alloca). Both signatures work because Foo* is marshallable but are clumsier to use than the original signature. Is there just no way to return a struct by value on the stack? Is there a cleaner way than the above two?
Josh