The layers of Larceny's foreign function interface

Felix S Klock II

The Foreign Function Interface (FFI) in Larceny supports interacting with dynamically loaded C libraries via glue code written in Scheme. The programmer does not need to develop any C code to interface with C libraries. The FFI is divided into layers of Larceny Scheme code; the lowest layers implement kernel functionality, and the higher layers support portable glue code development.
The lower level has two main features of interest. First, the FFI supports callbacks: objects that look like simple function pointers to C but actually invoke Scheme closures when called. This requires generating specialized machine code, and is further complicated by the potential movement of the closures during garbage collections. Second, Larceny heaps can be dumped to disk and reloaded during a fresh runtime execution. The FFI automatically relinks the foreign procedures in such heaps.
The higher level layers provide macros and procedures for extracting information from header files and dictating how C values translate to Scheme values and vice versa. Using the provided tools reduces the amount of human effort needed to develop portable glue code. The macros have simple implementations and do not require much Larceny-specific functionality; they may be useful for writing FFI code on other Scheme systems.

Last updated 23 July 2008.


Valid XHTML 1.0!