<div dir="ltr">What about you presume that ___result_voidstar is already allocated, and you just access/mutate it? I.e.<div><br></div><div><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">(define make-point</span><br style="font-family:arial,sans-serif;font-size:13px">

<span style="font-family:arial,sans-serif;font-size:13px">   (c-lambda (int int) struct-point</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">     "</span><span style="font-size:13px;font-family:arial,sans-serif">___result_voidstar-</span><font face="arial, sans-serif">></font><span style="font-family:arial,sans-serif;font-size:13px">.x = ___arg1;</span><br style="font-family:arial,sans-serif;font-size:13px">

<span style="font-family:arial,sans-serif;font-size:13px">      </span><span style="font-family:arial,sans-serif;font-size:13px">___result_voidstar-</span><font face="arial, sans-serif">></font><span style="font-family:arial,sans-serif;font-size:13px">y = ___arg2;</span><span style="font-family:arial,sans-serif;font-size:13px">"))</span></div>

<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">?</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br>

</span></div></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">

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>
<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>
</blockquote></div><br></div>