This chapter describes SBCL's interface to C programs and libraries (and, since C interfaces are a sort of lingua franca of the Unix world, to other programs and libraries in general.)
Note: In the modern Lisp world, the usual term for this functionality is Foreign Function Interface, or FFI, where despite the mention of "function" in this term, FFI also refers to direct manipulation of C data structures as well as functions. The traditional CMU CL terminology is Alien Interface, and while that older terminology is no longer used much in the system documentation, it still reflected in names in the implementation, notably in the name of the SB-ALIEN package.
Because of Lisp's emphasis on dynamic memory allocation and garbage collection, Lisp implementations use non-C-like memory representations for objects. This representation mismatch creates friction when a Lisp program must share objects with programs which expect C data. There are three common approaches to establishing communication:
The burden can be placed on the foreign program (and programmer) by requiring the knowledge and use of the representations used internally by the Lisp implementation. This can require a considerable amount of "glue" code on the C side, and that code tends to be sensitively dependent on the internal implementation details of the Lisp system.
The Lisp system can automatically convert objects back and forth between the Lisp and foreign representations. This is convenient, but translation becomes prohibitively slow when large or complex data structures must be shared. This approach is supported by the SBCL FFI, and used automatically by the when passing integers and strings.
The Lisp program can directly manipulate foreign objects through the use of extensions to the Lisp language.
SBCL, like CMU CL before it, relies primarily on the automatic conversion and direct manipulation approaches. Foreign values of simple scalar types are automatically converted, complex types are directly manipulated in their foreign representation. Furthermore, Lisp strings are represented internally with null termination bytes so that they can be passed directly to C interfaces without allocating new zero-terminated copies.
Any foreign objects that can't automatically be converted into Lisp values are represented by objects of type alien-value. Since Lisp is a dynamically typed language, even foreign objects must have a run-time type; this type information is provided by encapsulating the raw pointer to the foreign data within an alien-value object.
The type language and operations on foreign types are intentionally similar to those of the C language.