Allows Lisp arrays to be passed to foreign functions by copying their contents to malloc'ed foreign space, using only CFFI.
(LET ((U (MAKE-ARRAY 100 :ELEMENT-TYPE 'SINGLE-FLOAT :INITIAL-ELEMENT 1.0)))
(U PU :FLOAT ;; ARRAY POINTER-VAR CFFI-TYPE
:LISP-TYPE SINGLE-FLOAT ;; promise U is a single-float array
:START 2 ;; begin at index 2 of of Lisp array
:END 7 ;; last index used is 6
:COPY-TO-FOREIGN T ;; put contents of Lisp into foreign memory
:COPY-FROM-FOREIGN T) ;; copy back from FFI space
;; at this point, PU is a foreign pointer containing data copied
;; from the array, of type :FLOAT.
;; at end, all foreign memory is deallocated, and U has been copied
;; back from foreign space, but the 0th element of U is untouched
;; because START was 1, not 0
;; The options :START :END :COPY-TO-FOREIGN :COPY-FROM-FOREIGN are optional.
;; By default, :LISP-TYPE is T and any array can be given.
There exist a number of packages for passing arrays to foreign functions. Some pass pointers to lisp arrays, others allocate foreign memory and copy lisp arrays to it. This package takes the approach of allocating foreign memory. It is a reasonable approach because
However, if you are allocating the maximum possible amount of memory, then perhaps copying is not the approach to take. If you are copying tiny arrays, then the copying overhead might become comparable to the foreign workload, and and the consing to allocate the pointers might become an issue.