[gambit-list] os_dyn

Michele Zaffalon michele.zaffalon at gmail.com
Thu Oct 8 09:13:03 EDT 2009

>> Is this the only way to have a Scheme defined function passed on the
>> fly to C? Short of using global variables as in here
>> https://mercure.iro.umontreal.ca/pipermail/gambit-list/2009-January/002939.html
> Well, the only solutions I find acceptable.  There might be some (more or
> less portable) library to implement C closures that could be used (libffi?).
>  You have to understand what the fundamental problem is.  In C, functions
> pointers are implemented (usually) as the address of the code which
> implements the function.  The C compiler generates a "call" instruction to
> that address.  Scheme closures carry with them an environment... that is
> what makes them more useful than plain functions.  So to convert a Scheme
> closure to a C function pointer, in general it is necessary to allocate a
> fresh code address that will correspond to the Scheme closure.  The
> technique shown in the message linked above is based on the idea of
> allocating at compile time a pool of C functions, and then to assign them on
> demand to Scheme closures.  It is 100% portable, but it places a static
> limit on the number of Scheme closures that can be converted (the code could
> be improved to un-assign the Scheme closure when it is no longer live using
> wills for example).  The other solution, based on dynamic code generation,
> does not place a limit on the number of converted Scheme closures, but it is
> not portable (it is CPU and OS dependent).  There is no portable solution I
> know of that solves both issues (portable here means it respects the C
> standard and only relies on the C standard).
> Marc

I don't understand: in

(c-define (helper-func x)
	  (int) int "helper_func" ""
	  (*f* x))

(define gsl-func
  (c-lambda (int) int "gsl_func"))

and helper_func is called by the gsl_func routine, *f* can be any
Scheme closure.
Then why can't the C interface accept

(define gsl-func2
  (c-lambda ( (function (int) int) int) int "gsl_func"))

and generate the correct helper-func with *f* replaced by whatever
function is passed to gsl-func2?

I am sure this is a silly question so apologies in advance.


More information about the Gambit-list mailing list