Many FLI functions when evaluated return a pointer to the object created. For example, a command such as
(fli:allocate-foreign-object :type :int)
will return something similar to the following:
#<Pointer to type :INT = #x007608A0>
This is an FLI pointer object, pointing to an object at address #x007608A0
of type :int
. Note that the memory address given in the return string is in hexadecimal format, but when using the FLI pointer functions and macros discussed in this chapter you should pass values as decimals.
To use the pointer in the future it needs to be bound to a Lisp variable. This can be done by using setq
.
(setq point1 (fli:allocate-foreign-object :type :int)
However, to make a copy of a pointer it is not sufficient to do the following:
(setq point2 point1)
This simply sets point2
to contain the same pointer object as point1
. Thus if the pointer is changed using point1
, a similar change is observed when looking in point2
. To create a distinct copy of the pointer object you should use copy-pointer
, which returns a new pointer object with the same address and type as the old one, as the following example shows.
(setq point3 (fli:copy-pointer point1))
A pointer can be explicitly created, rather than being returned during the allocation of memory for an FLI object, by using fli:make-pointer
. In the next example a pointer is made pointing to an :int
type at the address 100
, and is bound to the Lisp variable point4
.
(setq point4 (fli:make-pointer :address 100 :type :int))
As a final note, pointers do take up memory. If a pointer is no longer needed it should be deallocated using fli:free-foreign-object
.