<div dir="ltr">It's not true that the experiment below[1] runs valgrind-clean.  I must have forgotten to compile with -g or something.  Calling a `c-define`d function from a release functions gives me this:<br><div><br>
<a href="https://mercure.iro.umontreal.ca/pipermail/gambit-list/2013-December/007346.html">https://mercure.iro.umontreal.ca/pipermail/gambit-list/2013-December/007346.html</a><br><br></div><div>Anyway, I worked out a proof of concept where the 'table' is in C:<br>
</div><div><br><a href="https://github.com/euccastro/refcount-hacks">https://github.com/euccastro/refcount-hacks</a><br><br></div><div>I think this will do the trick.<br></div><div><br>[1] (I sent the reply below only to Mikael, by mistake.)<br>
<br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Estevo</b> <span dir="ltr"><<a href="mailto:euccastro@gmail.com">euccastro@gmail.com</a>></span><br>Date: Thu, Dec 26, 2013 at 2:53 PM<br>
Subject: Re: [gambit-list] Anyone else working in a schemey wrapper for c structs/unions/types?<br>To: Mikael <<a href="mailto:mikael.rcv@gmail.com">mikael.rcv@gmail.com</a>><br><br><br><div dir="ltr"><br><div class="gmail_extra">
<div class="gmail_quote"><div class="im"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><a href="https://github.com/feeley/gambit/pull/61" style="font-family:arial,sans-serif;font-size:13px" target="_blank">https://github.com/feeley/gambit/pull/61</a> - for a quick proof of concept, that's a very neat use of the ffi type!!!</div>

</div></blockquote><div><br></div></div><div>Thanks!  I tried the approach you suggested and the code is much simpler, at least for this particular purpose.  There are some dangling questions left, that I explain below in more detail.<br>

</div><div class="im"><div> <div><br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">

<div>So you tried to somehow use wills to force GC order? Without having studied this, I guess that was not what they were intended for in the first place and it makes good sense that that did not work out well.</div></div>

</div></div></blockquote><div><br></div></div><div>I completely agree. <br></div><div class="im"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote"><div><div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">

<div><br>Once user code gets hold of A, pure Scheme code can't really know when it's OK to let go of that vector.  It's the same problem.  Of course you can keep a reference forever, but you don't want to hold on to unneeded memory either.<br>



</div></div></div></div></blockquote><div><br></div></div><div>You should be able to work that out and question is just how heavy it would be implementation-wise to do, no??</div><div><br>If not why not?</div><div>
<div></div></div></div></div></div></blockquote><div><br></div></div><div>There is no way in pure Scheme to detect when an object has been reclaimed.  This follows from the unlimited extent semantics mandated in the Scheme Reports.  From the perspective of Scheme code, objects are never deleted.  Gambit doesn't violate this, and that is fine.<br>

  <br></div><div class="im"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">



<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>But these restrictions need not be showstoppers.  A scheme that might work:<br></div><div> - on initalization, create an ___alloc_rc'ed table[1] and assign a global C pointer to it </div>

<div> - whenever you create a dependent foreign pointer F, store in the table a reference to its dependent object D, keyed by the address of the F's C pointer, and<br>


</div><div> - set a release function for F that will clear that entry in the table.<br></div></div></div></div></blockquote><div><br></div></div><div>Why do you need a "table" (by table here you mean resizable vector)?</div>

</div></div></div></blockquote><div><br></div></div><div>No, I mean the type of object that (make-table) returns.  I need<br> - to keep strong references to the original object in some place that the garbage collector will scan,<br>

 - to be able to retrieve these references by key (in particular, by address of the C object to which they point), in order to remove them when the dependent pointer is finalized.<br> <br></div><div class="im"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Nice!</div><div><br></div><div>Gambit's builtin functionality for this is fully sufficient at Gambit's level of abstraction over the underlying system, and indeed there's plenty of space for higher-level abstractions that more in this area.</div>



</div></div></div></blockquote><div><br></div></div><div>Indeed!  I should add that I don't want to hide Gambit's standard ffi facilities.  I want to hide my own hacks so what the user gets works seamlessly with those facilities, should they need more control than what my library provides.  That's why I want to give the user bare foreign objects and no scheme wrappers over them.<br>

</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">
<div></div><div>So the totality of what you are looking to provide is</div>
<div><br></div><div> a) the c-struct form which is analogous to define-type with</div><div><br></div><div> b) Scheme-like automatic dependency GC-reference handling between c structure instances as discussed here</div>

<div><br></div><div>?</div></div></div></div></blockquote><div><br></div></div><div>Yes, at least in a first step.  After that is working I'm thinking of doing something similar for arrays of structs/unions/types, perhaps with a vector-like interface.  That is, if you allocate an array of structures the array will stick around for exactly as long as you hold a pointer to any of its members.<div>

<br></div></div>Here's a proof of concept that runs valgrind-clean and prints the expected output in an unmodified Gambit:<br><br>    (c-declare "typedef struct { int x; int y; } point;")<br><br>    (define no-err ((c-lambda () scheme-object "___result = ___FIX(___NO_ERR);")))<br>

<br>    (define dependencies (make-table))<br><br>    (c-define (release-dependent p) ((pointer void)) scheme-object "release_dependent" ""<br>      (if p<br>        (begin<br>          (println "resetting " (foreign-address p))<br>

          (table-set! dependencies (foreign-address p)))<br>        (println "Null pointer?"))<br>      no-err)<br><br>    (c-define-type point (type "point" (point point*)))<br>    (c-define-type dependent-point (type "point" (point* point) "release_dependent"))<br>

<br>    (define (make-dependent-point dependency)<br>      (let ((p<br>              ((c-lambda ()<br>                         dependent-point<br>                     "___result_voidstar = ___EXT(___alloc_rc)(sizeof(point));<br>

                     /* I would really set these using accessors. */<br>                     ((point*)___result_voidstar)->x = 1;<br>                     ((point*)___result_voidstar)->y = 2;"))))<br>        (println "setting " (foreign-address p))<br>

        (table-set! dependencies (foreign-address p) dependency)<br>        p))<br><br>    (define point-x<br>      (c-lambda (point)<br>                int<br>        "___result = ((point*)___arg1_voidstar)->x;"))<br>

    (define point-y<br>      (c-lambda (point)<br>                int<br>        "___result = ((point*)___arg1_voidstar)->y;"))<div class="im"><br><br>    (define (point->string p)<br>      (string-append<br>
</div>        "(point "<br>
        (number->string (point-x p))<div class="im"><br>        " "<br>        (number->string (point-y p))<br>        ")"))<br><br></div>    (define d (cons 'depended 'upon))<br>    (make-will d (lambda (x) (write x) (println " checking out")))<br>

    (define p (make-dependent-point d))<br>    (println "p is " (point->string p))<br>    (println "forgetting global d; its will should not get executed yet")<br>    (set! d #f)<br>    (##gc)<br>    (##gc)<br>

    (##gc)<br>    (println "letting go of p; this should kill d in two garbage collections at most")<br>    (set! p #f)<br>    (println "first garbage collection")<br>    ; The first collection triggers the finalization of p, which deletes it from<br>

    ; the table.  But by the time that happens, the table has already been<br>    ; scanned, and d has been found there.<br>    (##gc)<br>    (println "second garbage collection")<br>    ; In the second collection d will not be reached anymore, so it's reclaimed.<br>

    (##gc)<br>    (println "done.")<br><br></div><div class="gmail_quote">This is not the end of the story, though.  Some consideration must be added for transitive dependencies and for several foreign objects depending on the same root.  I don't think there will be any blockers here.  I'll give it a go this afternoon, and if I get something solid I'll withdraw my pull request.<br>

<br></div><div class="gmail_quote">Thanks again for bouncing ideas!<br></div><div class="gmail_quote"><br></div></div></div>
</div><br></div></div>