Next: , Up: Operations On Foreign Values


7.3.1 Accessing Foreign Values

— Function: sb-alien:deref pointer-or-array &rest indices

The sb-alien:deref function returns the value pointed to by a foreign pointer, or the value of a foreign array element. When dereferencing 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. When dereferencing 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.

— Function: sb-alien:slot struct-or-union slot-name

The sb-alien:slot function extracts the value of the slot named slot-name from a foreign struct or union. If struct-or-union is a pointer to a structure or union, then it is automatically dereferenced. sb-alien:slot 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).

7.3.1.1 Untyped memory

As noted at the beginning of the chapter, the System Area Pointer facilities allow untyped access to foreign memory. SAPs can be converted to and from the usual typed foreign values using sap-alien and alien-sap (described elsewhere), and also to and from integers - raw machine addresses. They should thus be used with caution; corrupting the Lisp heap or other memory with SAPs is trivial.

— Function: sb-sys:int-sap machine-address

Creates a SAP pointing at the virtual address machine-address.

— Function: sb-sys:sap-ref-32 sap offset

Access the value of the memory location at offset bytes from sap. This form may also be used with setf to alter the memory at that location.

— Function: sb-sys:sap= sap1 sap2

Compare sap1 and sap2 for equality.

Similarly named functions exist for accessing other sizes of word, other comparisons, and other conversions. The reader is invited to use apropos and describe for more details

     (apropos "sap" :sb-sys)