|
|
Because of Lisp's emphasis on dynamic memory allocation and garbage collection, Lisp implementations use unconventional memory representations for objects. This representation mismatch creates problems when a Lisp program must share objects with programs written in another language. There are three different approaches to establishing communication:
The Alien type language and operations are most similar to those of the C language, but Aliens can also be used when communicating with most other languages that can be linked with C.
|
Alien types have a description language based on nested list structure. For example:
has the corresponding Alien type:struct foo { int a; struct foo *b[100]; };
(struct foo (a int) (b (array (* (struct foo)) 100)))
|
Types may be either named or anonymous. With structure and union types, the name is part of the type specifier, allowing recursively defined types such as:
An anonymous structure or union type is specified by using the name nil. The with-alien macro defines a local scope which ``captures'' any named type definitions. Other types are not inherently named, but can be given named abbreviations using def-alien-type.(struct foo (a (* (struct foo))))
This macro globally defines name as a shorthand for the Alien type type. When introducing global structure and union type definitions, name may be nil, in which case the name to define is taken from the type's name.
|
The Alien types form a subsystem of the CMUCL type system. An alien type specifier provides a way to use any Alien type as a Lisp type specifier. For example
can be used to determine whether foo is a pointer to an int. alien type specifiers can be used in the same ways as ordinary type specifiers (like string.) Alien type declarations are subject to the same precise type checking as any other declaration (section See section 4.5.2.)(typep foo '(alien (* int)))
Note that the Alien type system overlaps with normal Lisp type specifiers in some cases. For example, the type specifier (alien single-float) is identical to single-float, since Alien floats are automatically converted to Lisp floats. When type-of is called on an Alien value that is not automatically converted to a Lisp value, then it will return an alien type specifier.
|
Some Alien type names are Common Lispsymbols, but the names are still exported from the alien package, so it is legal to say alien:single-float. These are the basic Alien type specifiers:
A pointer to an object of the specified type. If type is t, then it means a pointer to anything, similar to ``void *'' in ANSI C. Currently, the only way to detect a null pointer is:
See section 6.5(zerop (sap-int (alien-sap ptr)))
An array of the specified dimensions, holding elements of type type. Note that (* int) and (array int) are considered to be different types when type checking is done; pointer and array types must be explicitly coerced using cast.
Arrays are accessed using deref, passing the indices as additional arguments. Elements are stored in column-major order (as in C), so the first dimension determines only the size of the memory block, and not the layout of the higher dimensions. An array whose first dimension is variable may be specified by using nil as the first dimension. Fixed-size arrays can be allocated as array elements, structure slots or with-alien variables. Dynamic arrays can only be allocated using make-alien.
A structure type with the specified name and fields. Fields are allocated at the same positions used by the implementation's C compiler. bits is intended for C-like bit field support, but is currently unused. If name is nil, then the type is anonymous.
If a named Alien struct specifier is passed to def-alien-type or with-alien, then this defines, respectively, a new global or local Alien structure type. If no fields are specified, then the fields are taken from the current (local or global) Alien structure type definition of name.
Similar to struct, but defines a union type. All fields are allocated at the same offset, and the size of the union is the size of the largest field. The programmer must determine which field is active from context.
An enumeration type that maps between integer values and keywords. If name is nil, then the type is anonymous. Each spec is either a keyword, or a list (keyword value). If integer is not supplied, then it defaults to one greater than the value for the preceding spec (or to zero if it is the first spec.)
A signed integer with the specified number of bits precision. The upper limit on integer precision is determined by the machine's word size. If no size is specified, the maximum size will be used.
Identical to signed---the distinction between signed and integer is purely stylistic.
Like signed, but specifies an unsigned integer.
Similar to an enumeration type that maps 0 to nil and all other values to t. bits determines the amount of storage allocated to hold the truth value.
A floating-point number in IEEE single format.
A floating-point number in IEEE double format.
A Alien function that takes arguments of the specified arg-types and returns a result of type result-type. Note that the only context where a function type is directly specified is in the argument to alien-funcall (see section alien-funcall.) In all other contexts, functions are represented by function pointer types: (* (function ...)).
A pointer which is represented in Lisp as a system-area-pointer object (see section 6.5.)
|
The c-call package exports these type-equivalents to the C type of the same name: char, short, int, long, unsigned-char, unsigned-short, unsigned-int, unsigned-long, float, double. c-call also exports these types:
This type is used in function types to declare that no useful value is returned. Evaluation of an alien-funcall form will return zero values.
This type is similar to (* char), but is interpreted as a null-terminated string, and is automatically converted into a Lisp string when accessed. If the pointer is C NULL (or 0), then accessing gives Lisp nil.Assigning a Lisp string to a c-string structure field or variable stores the contents of the string to the memory already pointed to by that variable. When an Alien of type (* char) is assigned to a c-string, then the c-string pointer is assigned to. This allows c-string pointers to be initialized. For example:
Storing Lisp nil writes C NULL to the c-string pointer.(def-alien-type nil (struct foo (str c-string))) (defun make-foo (str) (let ((my-foo (make-alien (struct foo)))) (setf (slot my-foo 'str) (make-alien char (length str))) (setf (slot my-foo 'str) str) my-foo))
|
This section describes the basic operations on Alien values.
|
This function returns the value pointed to by an Alien pointer or the value of an Alien array element. If a pointer, an optional single index can be specified to give the equivalent of C pointer arithmetic; this index is scaled by the size of the type pointed to. If an array, the number of indices must be the same as the number of dimensions in the array type. deref can be set with setf to assign a new value.
This function extracts the value of slot slot-name from the an Alien struct or union. If struct-or-union is a pointer to a structure or union, then it is automatically dereferenced. This can be set with setf to assign a new value. Note that slot-name is evaluated, and need not be a compile-time constant (but only constant slot accesses are efficiently compiled.)
|
This macro returns a pointer to the location specified by alien-expr, which must be either an Alien variable, a use of deref, a use of slot, or a use of extern-alien.
This macro converts alien to a new Alien with the specified new-type. Both types must be an Alien pointer, array or function type. Note that the result is not eq to the argument, but does refer to the same data bits.
[Function]
alien:alien-sap alien-valuesap-alien converts sap (a system area pointer see section 6.5) to an Alien value with the specified type. type is not evaluated.
alien-sap returns the SAP which points to alien-value's data.
The type to sap-alien and the type of the alien-value to alien-sap must some Alien pointer, array or record type.
|
Dynamic Aliens are allocated using the malloc library, so foreign code can call free on the result of make-alien, and Lisp code can call free-alien on objects allocated by foreign code.
This macro returns a dynamically allocated Alien of the specified type (which is not evaluated.) The allocated memory is not initialized, and may contain arbitrary junk. If supplied, size is an expression to evaluate to compute the size of the allocated object. There are two major cases:
- When type is an array type, an array of that type is allocated and a pointer to it is returned. Note that you must use deref to change the result to an array before you can use deref to read or write elements:
If supplied, size is used as the first dimension for the array.(defvar *foo* (make-alien (array char 10))) (type-of *foo*) ==> (alien (* (array (signed 8) 10))) (setf (deref (deref foo) 0) 10) ==> 10- When type is any other type, then then an object for that type is allocated, and a pointer to it is returned. So (make-alien int) returns a (* int). If size is specified, then a block of that many objects is allocated, with the result pointing to the first one.
See also with-alien, which stack-allocates Aliens.This function frees the storage for alien (which must have been allocated with make-alien or malloc.)
|
Both local (stack allocated) and external (C global) Alien variables are supported.
|
This macro establishes local alien variables with the specified Alien types and names for dynamic extent of the body. The variable names are established as symbol-macros; the bindings have lexical scope, and may be assigned with setq or setf. This form is analogous to defining a local variable in C: additional storage is allocated, and the initial value is copied.
with-alien also establishes a new scope for named structures and unions. Any type specified for a variable may contain name structure or union types with the slots specified. Within the lexical scope of the binding specifiers and body, a locally defined structure type foo can be referenced by its name using:
(struct foo)
|
External Alien names are strings, and Lisp names are symbols. When an external Alien is represented using a Lisp variable, there must be a way to convert from one name syntax into the other. The macros extern-alien, def-alien-variable and def-alien-routine use this conversion heuristic:
(alien-string lisp-symbol)
This macro defines name as an external Alien variable of the specified Alien type. name and type are not evaluated. The Lisp name of the variable (see above) becomes a global Alien variable in the Lisp namespace. Global Alien variables are effectively ``global symbol macros''; a reference to the variable fetches the contents of the external variable. Similarly, setting the variable stores new contents---the new contents must be of the declared type.
For example, it is often necessary to read the global C variable errno to determine why a particular function call failed. It is possible to define errno and make it accessible from Lisp by the following:
(def-alien-variable "errno" int) ;; Now it is possible to get the value of the C variable errno simply by ;; referencing that Lisp variable: ;; (print errno)
This macro returns an Alien with the specified type which points to an externally defined value. name is not evaluated, and may be specified either as a string or a symbol. type is an unevaluated Alien type specifier.
|
Now that we have Alien types, operations and variables, we can manipulate foreign data structures. This C declaration can be translated into the following Alien type:
With this definition, the following C expression can be translated in this way:struct foo { int a; struct foo *b[100]; }; <==> (def-alien-type nil (struct foo (a int) (b (array (* (struct foo)) 100))))
Or consider this example of an external C variable and some accesses:struct foo f; f.b[7].a <==> (with-alien ((f (struct foo))) (slot (deref (slot f 'b) 7) 'a) ;; ;; Do something with f... )
which can be made be manipulated in Lisp like this:struct c_struct { short x, y; char a, b; int z; c_struct *n; }; extern struct c_struct *my_struct; my_struct->x++; my_struct->a = 5; my_struct = my_struct->n;
(def-alien-type nil (struct c-struct (x short) (y short) (a char) (b char) (z int) (n (* c-struct)))) (def-alien-variable "my_struct" (* c-struct)) (incf (slot my-struct 'x)) (setf (slot my-struct 'a) 5) (setq my-struct (slot my-struct 'n))
|
Foreign object files are loaded into the running Lisp process by load-foreign. First, it runs the linker on the files and libraries, creating an absolute Unix object file. This object file is then loaded into into the currently running Lisp. The external symbols defining routines and variables are made available for future external references (e.g. by extern-alien.) load-foreign must be run before any of the defined symbols are referenced.
Note that if a Lisp core image is saved (using save-lisp), all loaded foreign code is lost when the image is restarted.
files is a simple-string or list of simple-strings specifying the names of the object files. libraries is a list of simple-strings specifying libraries in a format that ld, the Unix linker, expects. The default value for libraries is ("-lc") (i.e., the standard C library). base-file is the file to use for the initial symbol table information. The default is the Lisp start up code: path:lisp. env should be a list of simple strings in the format of Unix environment variables (i.e., A=B, where A is an environment variable and B is its value). The default value for env is the environment information available at the time Lisp was invoked. Unless you are certain that you want to change this, you should just use the default.
|
The foreign function call interface allows a Lisp program to call functions written in other languages. The current implementation of the foreign function call interface assumes a C calling convention and thus routines written in any language that adheres to this convention may be called from Lisp.
Lisp sets up various interrupt handling routines and other environment information when it first starts up, and expects these to be in place at all times. The C functions called by Lisp should either not change the environment, especially the interrupt entry points, or should make sure that these entry points are restored when the C function returns to Lisp. If a C function makes changes without restoring things to the way they were when the C function was entered, there is no telling what will happen.
|
Here is an example which allocates a (struct foo), calls a foreign function to initialize it, then returns a Lisp vector of all the (* (struct foo)) objects filled in by the foreign call:This function is the foreign function call primitive: alien-function is called with the supplied arguments and its value is returned. The alien-function is an arbitrary run-time expression; to call a constant function, use extern-alien or def-alien-routine.
The type of alien-function must be (alien (function ...)) or (alien (* (function ...))), See section 8.2.3. The function type is used to determine how to call the function (as though it was declared with a prototype.) The type need not be known at compile time, but only known-type calls are efficiently compiled. Limitations:
- Structure type return values are not implemented.
- Passing of structures by value is not implemented.
;; Allocate a foo on the stack. (with-alien ((f (struct foo))) ;; ;; Call some C function to fill in foo fields. (alien-funcall (extern-alien "mangle_foo" (function void (* foo))) (addr f)) ;; ;; Find how many foos to use by getting the A field. (let* ((num (slot f 'a)) (result (make-array num))) ;; ;; Get a pointer to the array so that we don't have to keep extracting it: (with-alien ((a (* (array (* (struct foo)) 100)) (addr (slot f 'b)))) ;; ;; Loop over the first N elements and stash them in the result vector. (dotimes (i num) (setf (svref result i) (deref (deref a) i))) result)))
|
This macro is a convenience for automatically generating Lisp interfaces to simple foreign functions. The primary feature is the parameter style specification, which translates the C pass-by-reference idiom into additional return values.
name is usually a string external symbol, but may also be a symbol Lisp name or a list of the foreign name and the Lisp name. If only one name is specified, the other is automatically derived, (see section 8.4.2.)
result-type is the Alien type of the return value. Each remaining subform specifies an argument to the foreign function. aname is the symbol name of the argument to the constructed function (for documentation) and atype is the Alien type of corresponding foreign argument. The semantics of the actual call are the same as for alien-funcall. style should be one of the following:
Any efficiency-critical foreign interface function should be inline expanded by preceding def-alien-routine with:
- :in
- specifies that the argument is passed by value. This is the default. :in arguments have no corresponding return value from the Lisp function.
- :out
- specifies a pass-by-reference output value. The type of the argument must be a pointer to a fixed sized object (such as an integer or pointer). :out and :in-out cannot be used with pointers to arrays, records or functions. An object of the correct size is allocated, and its address is passed to the foreign function. When the function returns, the contents of this location are returned as one of the values of the Lisp function.
- :copy
- is similar to :in, but the argument is copied to a pre-allocated object and a pointer to this object is passed to the foreign routine.
- :in-out
- is a combination of :copy and :out. The argument is copied to a pre-allocated object and a pointer to this object is passed to the foreign routine. On return, the contents of this location is returned as an additional value.
In addition to avoiding the Lisp call overhead, this allows pointers, word-integers and floats to be passed using non-descriptor representations, avoiding consing (see section 5.11.2.)(declaim (inline lisp-name))
|
Consider the C function cfoo with the following calling convention:
which can be described by the following call to def-alien-routine:/* a for update * i out */ void cfoo (char *str, char *a, int *i);
The Lisp function cfoo will have two arguments (str and a) and two return values (a and i).(def-alien-routine "cfoo" void (str c-string) (a char :in-out) (i int :out))
|
Calling Lisp functions from C is sometimes possible, but is rather hackish. See funcall0 ... funcall3 in the lisp/arch.h. The arguments must be valid CMUCL object descriptors (e.g. fixnums must be left-shifted by 2.) See compiler/generic/objdef.lisp or the derived file lisp/internals.h for details of the object representation. lisp/internals.h is mechanically generated, and is not part of the source distribution. It is distributed in the docs/ directory of the binary distribution.
Note that the garbage collector moves objects, and won't be able to fix up any references in C variables, so either turn GC off or don't keep Lisp pointers in C data unless they are to statically allocated objects. You can use purify to place live data structures in static space so that they won't move during GC.
|
Due to the way CMUCL manages memory, the amount of memory that can be dynamically allocated by malloc or make-alien is limited1.
To overcome this limitation, it is possible to access the content of Lisp arrays which are limited only by the amount of physical memory and swap space available. However, this technique is only useful if the foreign function takes pointers to memory instead of allocating memory for itself. In latter case, you will have to modify the foreign functions.
This technique takes advantage of the fact that CMUCL has specialized array types (see section 5.11.8) that match a typical C array. For example, a (simple-array double-float (100)) is stored in memory in essentially the same way as the C array double x[100] would be. The following function allows us to get the physical address of such a Lisp array:
Assume we have the C function below that we wish to use:(defun array-data-address (array) "Return the physical address of where the actual data of an array is stored. ARRAY must be a specialized array type in CMUCL. This means ARRAY must be an array of one of the following types: double-float single-float (unsigned-byte 32) (unsigned-byte 16) (unsigned-byte 8) (signed-byte 32) (signed-byte 16) (signed-byte 8) " (declare (type (or #+signed-array (array (signed-byte 8)) #+signed-array (array (signed-byte 16)) #+signed-array (array (signed-byte 32)) (array (unsigned-byte 8)) (array (unsigned-byte 16)) (array (unsigned-byte 32)) (array single-float) (array double-float)) array) (optimize (speed 3) (safety 0)) (ext:optimize-interface (safety 3))) ;; with-array-data will get us to the actual data. However, because ;; the array could have been displaced, we need to know where the ;; data starts. (lisp::with-array-data ((data array) (start) (end)) (declare (ignore end)) ;; DATA is a specialized simple-array. Memory is laid out like this: ;; ;; byte offset Value ;; 0 type code (should be 70 for double-float vector) ;; 4 4 * number of elements in vector ;; 8 1st element of vector ;; ... ... ;; (let ((addr (+ 8 (logandc1 7 (kernel:get-lisp-obj-address data)))) (type-size (let ((type (array-element-type data))) (cond ((or (equal type '(signed-byte 8)) (equal type '(unsigned-byte 8))) 1) ((or (equal type '(signed-byte 16)) (equal type '(unsigned-byte 16))) 2) ((or (equal type '(signed-byte 32)) (equal type '(unsigned-byte 32))) 4) ((equal type 'single-float) 4) ((equal type 'double-float) 8) (t (error "Unknown specialized array element type")))))) (declare (type (unsigned-byte 32) addr) (optimize (speed 3) (safety 0) (ext:inhibit-warnings 3))) (system:int-sap (the (unsigned-byte 32) (+ addr (* type-size start)))))))
The following example generates two large arrays in Lisp, and calls the C function to do the desired computation. This would not have been possible using malloc or make-alien since we need about 16 MB of memory to hold the two arrays.double dotprod(double* x, double* y, int n) { int k; double sum = 0; for (k = 0; k < n; ++k) { sum += x[k] * y[k]; } }
In this example, it may be useful to wrap the inner let expression in an unwind-protect that first turns off garbage collection and then turns garbage collection on afterwards. This will prevent garbage collection from moving x and y after we have obtained the (now erroneous) addresses but before the call to dotprod is made.(def-alien-routine "dotprod" double (x (* double-float) :in) (y (* double-float) :in) (n int :in)) (let ((x (make-array 1000000 :element-type 'double-float)) (y (make-array 1000000 :element-type 'double-float))) ;; Initialize X and Y somehow (let ((x-addr (system:int-sap (array-data-address x))) (y-addr (system:int-sap (array-data-address y)))) (dotprod x-addr y-addr 1000000)))
|
This section presents a complete example of an interface to a somewhat complicated C function. This example should give a fairly good idea of how to get the effect you want for almost any kind of C function. Suppose you have the following C function which you want to be able to call from Lisp in the file test.c:
struct c_struct { int x; char *s; }; struct c_struct *c_function (i, s, r, a) int i; char *s; struct c_struct *r; int a[10]; { int j; struct c_struct *r2; printf("i = %d\n", i); printf("s = %s\n", s); printf("r->x = %d\n", r->x); printf("r->s = %s\n", r->s); for (j = 0; j < 10; j++) printf("a[%d] = %d.\n", j, a[j]); r2 = (struct c_struct *) malloc (sizeof(struct c_struct)); r2->x = i + 5; r2->s = "A C string"; return(r2); };It is possible to call this function from Lisp using the file test.lisp whose contents is:
To execute the above example, it is necessary to compile the C routine as follows:;;; -*- Package: test-c-call -*- (in-package "TEST-C-CALL") (use-package "ALIEN") (use-package "C-CALL") ;;; Define the record c-struct in Lisp. (def-alien-type nil (struct c-struct (x int) (s c-string))) ;;; Define the Lisp function interface to the C routine. It returns a ;;; pointer to a record of type c-struct. It accepts four parameters: ;;; i, an int; s, a pointer to a string; r, a pointer to a c-struct ;;; record; and a, a pointer to the array of 10 ints. ;;; ;;; The INLINE declaration eliminates some efficiency notes about heap ;;; allocation of Alien values. (declaim (inline c-function)) (def-alien-routine c-function (* (struct c-struct)) (i int) (s c-string) (r (* (struct c-struct))) (a (array int 10))) ;;; A function which sets up the parameters to the C function and ;;; actually calls it. (defun call-cfun () (with-alien ((ar (array int 10)) (c-struct (struct c-struct))) (dotimes (i 10) ; Fill array. (setf (deref ar i) i)) (setf (slot c-struct 'x) 20) (setf (slot c-struct 's) "A Lisp String") (with-alien ((res (* (struct c-struct)) (c-function 5 "Another Lisp String" (addr c-struct) ar))) (format t "Returned from C function.~%") (multiple-value-prog1 (values (slot res 'x) (slot res 's)) ;; ;; Deallocate result after we are done using it. (free-alien res)))))
In order to enable incremental loading with some linkers, you may need to say:cc -c test.c
Once the C code has been compiled, you can start up Lisp and load it in:cc -G 0 -c test.c
If any of the foreign functions do output, they should not be called from within Hemlock. Depending on the situation, various strange behavior occurs. Under X, the output goes to the window in which Lisp was started; on a terminal, the output will overwrite the Hemlock screen image; in a Hemlock slave, standard output is /dev/null by default, so any output is discarded.% lisp ;;; Lisp should start up with its normal prompt. ;;; Compile the Lisp file. This step can be done separately. You don't have ;;; to recompile every time. * (compile-file "test.lisp") ;;; Load the foreign object file to define the necessary symbols. This must ;;; be done before loading any code that refers to these symbols. next block ;;; of comments are actually the output of LOAD-FOREIGN. Different linkers ;;; will give different warnings, but some warning about redefining the code ;;; size is typical. * (load-foreign "test.o") ;;; Running library:load-foreign.csh... ;;; Loading object file... ;;; Parsing symbol table... Warning: "_gp" moved from #x00C082C0 to #x00C08460. Warning: "end" moved from #x00C00340 to #x00C004E0. ;;; o.k. now load the compiled Lisp object file. * (load "test") ;;; Now we can call the routine that sets up the parameters and calls the C ;;; function. * (test-c-call::call-cfun) ;;; The C routine prints the following information to standard output. i = 5 s = Another Lisp string r->x = 20 r->s = A Lisp string a[0] = 0. a[1] = 1. a[2] = 2. a[3] = 3. a[4] = 4. a[5] = 5. a[6] = 6. a[7] = 7. a[8] = 8. a[9] = 9. ;;; Lisp prints out the following information. Returned from C function. ;;; Return values from the call to test-c-call::call-cfun. 10 "A C string" *