<html lang="en"> <head> <title>Introduction to the Foreign Function Interface - SBCL 1.0.31 User Manual</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="SBCL 1.0.31 User Manual"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Foreign-Function-Interface.html#Foreign-Function-Interface" title="Foreign Function Interface"> <link rel="next" href="Foreign-Types.html#Foreign-Types" title="Foreign Types"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <!-- This manual is part of the SBCL software system. See the `README' file for more information. This manual is largely derived from the manual for the CMUCL system, which was produced at Carnegie Mellon University and later released into the public domain. This manual is in the public domain and is provided with absolutely no warranty. See the `COPYING' and `CREDITS' files for more information. --> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Introduction-to-the-Foreign-Function-Interface"></a> <p> Next: <a rel="next" accesskey="n" href="Foreign-Types.html#Foreign-Types">Foreign Types</a>, Up: <a rel="up" accesskey="u" href="Foreign-Function-Interface.html#Foreign-Function-Interface">Foreign Function Interface</a> <hr> </div> <!-- node-name, next, previous, up --> <h3 class="section">8.1 Introduction to the Foreign Function Interface</h3> <!-- AKA "Introduction to Aliens" in the CMU CL manual --> <p>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: <ul> <li>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. <li>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 <acronym>FFI</acronym>, and used automatically by the when passing integers and strings. <li>The Lisp program can directly manipulate foreign objects through the use of extensions to the Lisp language. </ul> <p>SBCL, like CMUCL before it, relies primarily on the automatic conversion and direct manipulation approaches. The <code>SB-ALIEN</code> package provides a facility wherein foreign values of simple scalar types are automatically converted and complex types are directly manipulated in their foreign representation. Additionally the lower-level System Area Pointers (or <acronym>SAP</acronym>s) can be used where necessary to provide untyped access to foreign memory. <p>Any foreign objects that can't automatically be converted into Lisp values are represented by objects of type <code>alien-value</code>. 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 <code>alien-value</code> object. <p>The type language and operations on foreign types are intentionally similar to those of the C language. </body></html>