Previous: Coercing Foreign Values, Up: Operations On Foreign Values


8.3.3 Foreign Dynamic Allocation

Lisp code can call the C standard library functions malloc and free to dynamically allocate and deallocate foreign variables. The Lisp code shares the same allocator with foreign C code, so it's OK for foreign code to call free on the result of Lisp sb-alien:make-alien, or for Lisp code to call sb-alien:free-alien on foreign objects allocated by C code.

— Macro: sb-alien:make-alien type size

The sb-alien:make-alien macro returns a dynamically allocated foreign value 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:

— Function: sb-alien:free-alien foreign-value

The sb-alien:free-alien function frees the storage for foreign-value, which must have been allocated with Lisp make-alien or C malloc.

See also the sb-alien:with-alien macro, which allocates foreign values on the stack.