[cffi-devel] handling lisp to c arrays (and back) in CFFI (beginner questions)

Kevin Smith k2msmith at gmail.com
Thu Jul 22 17:45:43 UTC 2010


Hello, Sorry for the long winded questions...  I was wondering if someone
could point me to any example example on how to set/get arrays in C from
lisp ? I am using CFFI and Swig.  currently I have only primitive interface
functions that pass pointers and floats.  I am thinking of three
array cases. I'm a bit of a beginner here, so please bear with me - the
existing documentation is not completely clear (to me, as a lisp beginner
:-)..


1)  the C function allocates an array (as a side effect) and passes back a
pointer to array to lisp . I would later have to free memory for this array
explicitly.

2) the lisp function allocates an array and passes a pointer to the "C"
function,  The C function modifies the array.  In this case lisp would be
allocating the array.   Side question:  Do I need to use CFFI in lisp to
allocate the array ? - or can this be done transparently by the lisp dynamic
typing when the array variable is bound ?

3) I have several simple C functions that return smaller arrays (like [ x,
y, z], where x,y,z = float or integer.).  Would be nice to be able to pass
by value as a return value on the stack, but I am not sure how to handle
this from C to lisp.  Example:

C function:

Color getColorValue ( Image *image, x, y),  Where color is a array  triplet
 [r, g, b],, could be floats or ints depending...

Desired lisp function:

(let ((color (get-color-value image x y))
   ; then do something with list "color

4) unrelated question...maybe this should be posted to swig list (but i am
not sure if there are any active CFFI users in that group).
Is there an easy way to automatically create lisp-style function names ?

For example:

swig generates:

(cffi:defcfun ("frameHeight" frameHeight) :int
  (frame :pointer))

I would like to generate translate the C function name to "frame-height" (to
make it more of a lisp style).

I tried using

%rename frameHeight frame-height

in the swig. ".i" file template, but that doesn't seem to work.  The notes
in the documentation on "lispify" I am not at the stage where I understand
it.

I thought that some google searches would turn up some canonical examples or
tutorials on this, but there are not many (except the curl example - and it
makes me wonder how many people are actually using it :-)

thanks again,

K
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20100722/6c90103b/attachment.html>


More information about the cffi-devel mailing list