[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Returning a scheme object inside another

From: Zbigniew
Subject: [Chicken-users] Returning a scheme object inside another
Date: Tue, 22 Nov 2005 22:39:22 -0600

I have a C function which calls another C function that creates and
returns a scheme-object.  Because it returns (rather than continuing)
I cannot allocate this on the stack, rather I use malloc.  The
original caller itself returns this object, along with a c-pointer
that can later be freed.  As the return value is copied, this works

The problem comes when I want to return a scheme-object inside another
container (such as a list or a structure).  Unfortunately, a slot not
pointing to the nursery (such as a pointer to a malloced area) is not
copied into GC-able memory, even when using C_mutate.  [I only tried
this with a structure, but I think the same will happen with a list.] 
So, I can never free this pointer, although maybe some trickery with
finalization would work.

So far, I only have had to return one scheme-object inside one
structure.  I got around the malloc problem by not using
malloc---instead allocating the (small) object on the heap.

I anticipate though that I might have to return several objects at the
same time, say in a list.  I would hate to have to allocate them all
on the heap, but malloc or C_alloc won't work.  I can only think of
two other solutions: 1) invoke a callback to cons the objects into a
list, magically copying them into the heap; or 2) rewrite a lot of
this code into Scheme and have the allocation taken care of for me.

As for #1, the overhead of a foreign-safe-lambda may be annoying in
this application (I will have to see) and as for #2, I have been
avoiding it as there are a bunch of nested C functions, and I may not
be able to wrap some due to idiosyncrasies in the host API.  However,
this option is growing on me.

I welcome any thoughts.  Undoubtedly I will find some solution when I
need it, anyway.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]