[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing arrays to C



> We are trying to port a bunch of code to MCL.  The
> code relies heavily on C routines to crunch numbers
> in multi-dimensional arrays.  We are trying to figure
> out how to pass such arrays to C.  Ideally, the
> C routine would just receive a pointer to the data
> and separately the dimensions of the array.
> 
>   a) Is there any documentation on the exact structures
>   used to store multidimensional arrays in Lisp?  (The
>   documentation is pretty vague on this point.)
> 
>   b) Is there any good (or bad) way to get a pointer
>   to the actual array data and hand that off to C?

There is a possible alternative to trying to pass a lisp array
to C, which is to allocate the array memory on the Lisp heap using #_NewPtr
or #_NewHandle, and pass this pointer to C.  This has the advantage
that the memory looks to C like a normal array; nothing needs
to be converted.
  Disadvantage #1: the array cannot be manipulated in lisp.  This
can be fixed by defining 'foreign array' access routines such as 
(farray-ref ptr type offset), where type somehow indicates the 
size of the array elements.  The access routines can be written
in C and ff-linked, or possibly they can be written using the
routines described in Low-level OS Interface.
  Disadvantage #2: lisp will print the array as a pointer.  If it
is a large array you probably do not want to print it all out anyway.
Custom functions to print part or all of the array could be written
in lisp using the access functions; the array just can't be printed
using format.