<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>I've done this for some code, in both directions. (<a
href="https://github.com/SimulaVR/godot-haskell/blob/simula/src/Godot/Gdnative/Internal/Gdnative.chs#L378">https://github.com/SimulaVR/godot-haskell/blob/simula/src/Godot/Gdnative/Internal/Gdnative.chs#L378</a>)</p>
<p>`Foo makeFoo(size_t x)` is internally equivalent (ABI-dependent,
but at least on x64 Linux and WIndows) to `Foo* makeFoo(Foo*
result, size_t x)`. Alloc the data in Haskell and use that
definition.<br>
</p>
<div class="moz-cite-prefix">On 2020-06-16 16:44, ☂Josh Chia (謝任中)
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CALxtSbQWS4XFLcP6yx_NjYkx04gF-Zyix7JU0TLEEcbdKZriDw@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">Suppose I have the following C code:
<div><br>
</div>
<div>typedef struct Foo {</div>
<div> char* p1; /* Some data on the heap. */</div>
<div> size_t s1; /* Size of data. */</div>
<div> char* p2; /* More data on the heap. */</div>
<div> size_t s2;</div>
<div>} Foo;</div>
<div><br>
</div>
<div>/* Allocates and writes two pieces of data on the heap and
returns them in a Foo. */<br>
</div>
<div>Foo makeFoo(size_t x);</div>
<div><br>
</div>
<div>Based on my limited understanding of Haskell FFI according
to the Haskell 2010 Language Report (<a
href="https://www.haskell.org/onlinereport/haskell2010/haskellch8.html"
target="_blank" moz-do-not-send="true">https://www.haskell.org/onlinereport/haskell2010/haskellch8.html</a>),
it is not possible to have a ccall for makeFoo() because Foo
is not a marshallable foreign result type. Is my understanding
correct?</div>
<div><br>
</div>
<div>However, I believe I can have a ccall if I change makeFoo()
to either of the following:</div>
<div>Foo* makeFoo(size_t x);</div>
<div>void makeFoo(Foo* out, size_t x);</div>
<div><br>
</div>
<div>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?</div>
<div><br>
</div>
<div>Josh</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
<a class="moz-txt-link-freetext" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a>
Only members subscribed via the mailman list are allowed to post.</pre>
</blockquote>
</body>
</html>