[gambit-list] gambit on the iphone
Marc Feeley
feeley at iro.umontreal.ca
Fri Apr 24 15:20:31 EDT 2009
On 24-Apr-09, at 2:09 PM, Francois Magnan wrote:
> Hi Mikael,
>
> Thank you for your answer, I will look into this. It seems I am
> missing some basic info on the FFI interface because I don't
> understand why a c-define cannot return a char-string.
>
There is no problem in passing a string from Scheme to C or C to
Scheme. That's because strings are copied when they cross between
Scheme and C.
Note however that when a Scheme string is converted to a C string, the
C string will be a reference counted object whose reference count is
equal to 1. That's because the reference count indicates how many
references to this C string exist from the "C world". The C string is
only deallocated when the reference count is decremented to 0 (using a
call to ___release_string).
So take this code as an example:
(c-define (int2str n) (int) char-string "int2str" ""
(number->string n))
(define go (c-lambda (int) void #<<END
int i;
char *str;
for (i=0; i<___arg1; i++)
{
str = int2str (i);
#if 0
printf ("i=%d str=%s\n", i, str);
#endif
___EXT(___release_string) (str);
}
END
))
(go 1000000)
If you compile and run it *without* the call to ___release_string then
you have a memory leak and the virtual memory will go up gradually.
With the call to ___release_string the memory usage will stay constant.
Note that the reference count can be incremented with a call to
___addref_string . This could be useful for implementing a reference
counting garbage collection of strings on the C side (for example if
multiple modules are sharing ownership of these strings).
Marc
More information about the Gambit-list
mailing list