[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Chicken-users] Constructing parameter lists in C: can I use FFI?
From: |
Daniel B. Faken |
Subject: |
[Chicken-users] Constructing parameter lists in C: can I use FFI? |
Date: |
Thu, 21 Jul 2005 10:39:50 -0400 (EDT) |
Hello all,
I'm wondering how I can get access to Chicken's FFI for building
function calls in C.
The problem I have is that I need *entry points* as well as callbacks,
because I am using Chicken as a (scripted) library which C programs will
call out into (see http://chromium.sourceforge.net).
Thus I cannot necessarily enter the Chicken Toplevel() before doing
these callbacks - and I cannot guarantee I will NOT have entered the
toplevel.
This general issue was discussed about a year ago:
http://lists.gnu.org/archive/html/chicken-users/2004-06/msg00065.html
(I haven't found any more recent remarks)
My solution thus far has been to create two versions for each 'library
function' (there are about 600) I need to implement:
* a normal callback
e.g. scm_Lightiv(C_word scmfn, int light, int pname, int *params)
* an IndirectCallback() Entry-Point function that takes four params:
+ the scheme callback fn ('scmfn' above)
+ a pointer to an array of pointers to the parameters
e.g. void *args[] = {&light, &pname, ¶ms, NULL};
+ a pointer to a return value (if any)
e.g. GLint *rval;
+ an indirect version of the specific function which takes the
param-pointer-list and return-pointer, and then calls the
*callback*
e.g. void scm_indirectLightiv(C_word scmfn, void **args, void *rval)
{
*rval = scm_Lightiv(scmfn, *(int *)(args+0),
*(int *)(args+1), *(int **)(args+2));
}
Note that the second version requires three levels (function calls) of
indirection, not to mention all the &s and dereferences -- in addition to
the overhead of calling an (define-embedded) entry-point!
I.e. this is not likely to be terribly efficient.. (I haven't benchmarked
the difference yet, but note that this is a performance-critical library
(OpenGL)).
Now a preferable solution would be to construct the parameter list,
as a C_word (scheme-object) via C_list(...), and either
* use C_callback(scmfn, arglist) for callbacks or
* use a single entry-point 'scm_applyfn' that takes two
scheme-object args and just calls (apply scmfn arglist).
(if you've read this far, thanks for bearing with me :)
The "Problem": its not easy to construct these arglists; or, at least, its
much harder than using the Chicken-FFI's ability to define & translate
types (e.g. using define-foreign-type). [an aside: I'm using a
code-generator, since there are so many functions, but there are about
200 different C-function signatures in all]
So the Question is: is there a way to access these definitions from the
C side of things? (of course, any other solutions are welcome)
Thanks for any input!
cheers,
Daniel Faken