[gambit-list] Allocating structures from C
christian at pflanze.mine.nu
Mon Nov 6 01:43:32 EST 2006
At 0:12 Uhr +0000 06.11.2006, Taylor R Campbell wrote:
>Out of curiosity, why are you doing so much work on the C side, rather
>than letting Scheme control everything? You could return an integer
>to Scheme here, which Scheme code could then wrap in the structure; it
>seems awfully indirect and a great deal more work than it's worth to
>do it from C, but perhaps I'm missing a better reason here.
The reason is returning errors.
One can only return one value from C (except that c-lambda offers an
error reporting mechanism, see below), and if the normal value is to
be the whole range of a fixnum, there is no space to return an error
number (just returning e.g. #f cannot tell which error happened). Now
I could allocate a data structure in scheme and pass this to C to be
mutatet and returned from there in the case of an error, but that
would cost a memory allocation also in the non-error case.
c-lambda offers ___err to return errors, but it doesn't seem
extensible (it's based on globally predefined fixnum values; there is
the possibility to return arbitrary string messages, but I think then
the exception values are not typed anymore, which would be necessary
to be able to catch them selectively).
##c-code is faster (no overhead at all) than c-lambda (about 50
cycles), probably mostly because c-lambda is setting up the scheme
environment so that C code can call back to scheme(*), which is not
possible from ##c-code. That's why I've been asking for a solution
which doesn't need a callback to scheme to allocate a structure.
(*BTW Marc: would it be possible to do this setup step only when
needed, kind of lazily?)
More information about the Gambit-list