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

Liam Healy lnp at healy.washington.dc.us
Sat Jul 24 00:57:24 UTC 2010


You might want to take a look at GSD,
http://repo.or.cz/w/gsd.git,
in which I dealt with some of those issues.
If you're on an implementation that supports
static-vectors, you can allocate a foreign-array
from lisp and have access to the same data
in lisp through #'cl-array.

Your getting/setting will happen with
gref and (setf gref).  If your application is
non-numeric, you might need to add some
code (GSD was written for numerical
software, particularly GSLL, but should
be general).  However, I'm happy to
accept patches.

There is documentation, not entirely
complete, that should get you started,
but feel free to post on the gsll-devel
mailing list,
http://common-lisp.net/cgi-bin/mailman/listinfo/gsll-devel
with questions.

Liam

On Thu, Jul 22, 2010 at 1:45 PM, Kevin Smith <k2msmith at gmail.com> wrote:
> 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
>
>
>
>
>
>
> _______________________________________________
> cffi-devel mailing list
> cffi-devel at common-lisp.net
> http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel
>
>




More information about the cffi-devel mailing list