<div dir="ltr">Aha.<div><br></div><div>Very well, then I guess it's good time to ask Marc how this stuff works really. :))</div><div><br></div><div><br></div><div>Marc, would you feel like enlightening us on this one - how use and reclaim of non-pointered FFI structs works out?</div>

<div><br></div><div>Thanks,</div><div>Mikael</div><div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/8/22 Chris Mueller <span dir="ltr"><<a href="mailto:ruunsmail@gmail.com" target="_blank">ruunsmail@gmail.com</a>></span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Good point.<br>
<br>
But gsc / gcc are reclaiming ___result_voidstar is of type<br>
void* and has no elements x and y therefore.<br>
<br>
Manually casting to struct point* is of course possible, but will result<br>
in an expected segmentation fault during the execution of the script.<br>
<div class="im"><br>
<br>
On 22.08.2013 08:34, Mikael wrote:<br>
> What about you presume that ___result_voidstar is already allocated, and<br>
> you just access/mutate it? I.e.<br>
><br>
><br>
> (define make-point<br>
>     (c-lambda (int int) struct-point<br>
>       "___result_voidstar->.x = ___arg1;<br>
> ___result_voidstar->y = ___arg2;"))<br>
><br>
> ?<br>
><br>
><br>
><br>
</div>> 2013/8/22 Chris Mueller <<a href="mailto:ruunsmail@gmail.com">ruunsmail@gmail.com</a> <mailto:<a href="mailto:ruunsmail@gmail.com">ruunsmail@gmail.com</a>>><br>
<div><div class="h5">><br>
>     I'm opening a new subject for the ffi struct issue to sum up the current<br>
>     discussion:<br>
><br>
>     In 19.1 "The Mapping of types between C and Scheme" of the gambit<br>
>     documentation is mentioned:<br>
><br>
>     "In the case of the struct, union and type types, the default function<br>
>     reclaims the copy on the C heap referenced by the internal pointer (when<br>
>     using a C++ compiler this is done using ‘delete<br>
>     (type*)internal-pointer’, which calls the destructor of type if it is a<br>
>     class) and returns ‘___FIX(___NO_ERR)’. In many situations the default<br>
>     release-function will perform the appropriate cleanup for the foreign<br>
>     type."<br>
><br>
>     If i interpret this correctly, a declared struct type in a ffi provides<br>
>     a default release function that automatically calls a destructor (used<br>
>     with g++) and frees the corresponding memory.<br>
><br>
>     Unfortanetly, i'm little lost to use it correctly and experiment<br>
>     currently heavily to find the right way.<br>
><br>
><br>
>     Assume a type:<br>
><br>
>     (c-declare "struct point { int x; int y; };")<br>
>     (c-define-type struct-point (struct "point"))<br>
><br>
><br>
>     Assume a test programm that allocates a point 1_000_000_000 times.<br>
><br>
>     (define (alloc-point times)<br>
>         (let loop ((i 0))<br>
>            (if (< i times)<br>
>              (let ((a (make-point 0 0)))<br>
>                (loop (+ i 1)))))))<br>
><br>
>     (alloc-point 1000000000)<br>
><br>
><br>
>     How do we implement (make-point x y) to use a ffi struct type with a<br>
>     default release function in Gambit?<br>
><br>
>     1) With a manually allocated pointer it will leak memory.<br>
><br>
>     (define make-point<br>
>         (c-lambda (int int) struct-point<br>
>           "point* a = new Point;<br>
>            a->x = ___arg1;<br>
>            a->y = ___arg2;<br>
>            ___result_voidstar = a;"))<br>
><br>
>     2) With a stack-allocated value it will segfault immediately.<br>
><br>
>     (define make-point<br>
>         (c-lambda (int int) struct-point<br>
>           "point a;<br>
>            a.x = ___arg1;<br>
>            a.y = ___arg2;<br>
>            ___result = a;"))<br>
><br>
>     3) If we give the address of a stack-allocated value to<br>
>     ___result_voidstar the allocation loop is running with constant memory<br>
>     space but the programm will segfault after quiting with CTRL + X.<br>
><br>
>     (define make-point<br>
>         (c-lambda (int int) struct-point<br>
>           "point a;<br>
>            a.x = ___arg1;<br>
>            a.y = ___arg2;<br>
>            ___result_voidstar = &a;"))<br>
><br>
><br>
>     For more details, see discussion:<br>
>     <a href="https://mercure.iro.umontreal.ca/pipermail/gambit-list/2013-August/006950.html" target="_blank">https://mercure.iro.umontreal.ca/pipermail/gambit-list/2013-August/006950.html</a><br>
><br>
><br>
>     Chris<br>
>     _______________________________________________<br>
>     Gambit-list mailing list<br>
</div></div>>     <a href="mailto:Gambit-list@iro.umontreal.ca">Gambit-list@iro.umontreal.ca</a> <mailto:<a href="mailto:Gambit-list@iro.umontreal.ca">Gambit-list@iro.umontreal.ca</a>><br>
>     <a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br>
<div class="HOEnZb"><div class="h5">><br>
><br>
<br>
_______________________________________________<br>
Gambit-list mailing list<br>
<a href="mailto:Gambit-list@iro.umontreal.ca">Gambit-list@iro.umontreal.ca</a><br>
<a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br>
</div></div></blockquote></div><br></div></div>