[gambit-list] References between 'foreign' objects.

Estevo euccastro at yahoo.com
Sat Mar 24 12:31:13 EDT 2012


I'm trying to write a macro to wrap C structures and unions [1] in an interface similar to that of Scheme records.  It defines an appropriate foreign type, creates a constructor, utility functions to take and dereference pointers, and member accessors and mutators.


Here is the definition [2]:

https://github.com/euccastro/gambit-SDL2/blob/master/ffi.scm

and here is a sample of how you are supposed to use it:

https://github.com/euccastro/gambit-SDL2/blob/master/test-macro-struct.scm

So far I've been letting Gambit take care of cleaning up structs, except for those that were obtained via a non-pointer accessor to an existing struct.  So disposing of your last Scheme reference to a "child" struct will not try to clean up memory in the "parent" struct.  I did this in an unclean way (I'm wrapping each struct twice, with different Scheme names, one of them with a bogus cleanup function; I'm also assuming that every "voidstar" member is a struct or union that has been wrapped using this macro) and I'd be thankful for any suggestions on how to improve this, but this is not my main question.


I still have the somewhat converse problem: when Gambit loses sight of your last Scheme reference to a "parent" struct, it will clean it up even if there are Scheme references to "child" structs.

I would like to somehow make a Scheme reference to the "parent" struct from the "child" structs, so that children will keep the parent alive as long as they are around.  This reference needs not be visible from Scheme; it's only to help the garbage collector do the right thing.

I can think of a couple ways to do this, but they either complicate my macro considerably, or make things more cumbersome for the user -- or both.  I'm asking the list just in case I'm missing an easy way to create these links.


[1] I say "structs" in the rest of this message to refer to both C `struct` and `union`s.

[2] All my code in github is under the WTFPL license (profanity warning):


http://sam.zoy.org/wtfpl/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20120324/2f06c623/attachment.htm>


More information about the Gambit-list mailing list