I'm translating some C functions originally designed for use with Scheme-48.
In that system Scheme objects can be allocated by C. This is done by calling, for instance, s48_make_string(). As I understand it, Chicken's design means that structures are allocated on the C stack frame, so for instance I could use C_alloc() and various C macros such as C_vector() to allocate a Scheme object on the C stack and it would be subsequently usable in Scheme code, but this would only be useful for callbacks, because the data is reclaimed by the normal stack unwind when the foreign (C) function returns. Now I could write my c code to write to static data and have the Scheme code use object-copy, but that's a rather unattractive kludge and not, for instance, thread-safe. It has the advantage of being simple and efficient. A cleaner way is to rewrite the code so that the foreign data structure is declared as a foreign-record and allocated (on the C heap) in Scheme code prior to the C call. Appropriate use of set-finalizer! ensures that the allocated heap memory is freed when the associated (enclosing) Scheme object is about to be reclaimed by garbage collection. This is also pretty efficient because the data structure is controlled by malloc() and free() and there is no run-time impact on the C code, which simply works on the allocated C structure. Out of curiosity, is there another way? How about this. I write (and register with my C code) a simple Scheme callback function which performs object-copy and returns the new copy as a return value. To produce a returnable object I first allocate it on the C stack and then I pass a pointer to the callback function. The callback function copies the data to Scheme garbage-collected storage. A pointer to the copied structure is returned to the calling function which can then pass it back to the caller. Two possible problems occur to me. My knowledge of the mechanics of the design is too murky for me to be sure if they're real problems. The first problem is that a garbage collection may make the callback's return value invalid once it reaches the C code. The second problem is that possibly the mechanics of the allocation process will impact the size of the nursery. _______________________________________________ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users