[cffi-devel] functions that take pointers

J. T.K. jetmonk at gmail.com
Mon Jan 15 07:05:52 UTC 2007


> Stephen Compall <s11 at member.fsf.org> wrote:
> ...
> The attached should give you a good general idea of how to do it.  It is
> untested. ....
>





Thank you for the pointers.

I've managed to write a macro that does the wrapping that is necessary to
implement sbcl-style
:out and :in-out pointers using the tools in cffi.  This is useful for
porting sbcl/cmucl FFIs over to
CFFI.  Having always used SBCL in the past, I've always found the :OUT and
:IN-OUT syntax
of their FFI to be very nice because it allows one to hide a lot of pointer
ugliness with just a single
keyword.  I wonder it something like this would be useful for CFFI defcfun?
But as the attached
macro code shows, it seems to be easy to implement atop CFFI.

Thanks again for the tips.
Jan


;; example use of %define-alien-routine macro to implement :OUT and :IN-OUT
args like SBCL

(defctype int :int) ;; define a new type to be more like SBCL

(macroexpand
     '(%define-alien-routine ("ffopen" fits-file-open) int
       (fptr int :out)
       (filename :string)
       (mode int)
       (status int :in-out)))

==> ;; expands to

(progn
 (declaim (inline %defcfun-ffopen))
 (defcfun ("ffopen" %defcfun-ffopen) int (fptr :pointer) (filename :string)
          (mode int) (status :pointer))
 (defun fits-file-open (filename mode status)
   (with-foreign-object (#:status2488 'int)
                        (setf (mem-ref #:status2488 'int 0) status)
                        (let ((#:mode2487 mode))
                          (let ((#:filename2486 filename))
                            (with-foreign-object (#:fptr2485 'int) nil
                                                 (values
                                                  (%defcfun-ffopen
#:fptr2485

#:filename2486

#:mode2487

#:status2488)
                                                  (mem-ref #:fptr2485 'int
0)
                                                  (mem-ref #:status2488 'int
                                                           0))))))))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20070114/66c1eb89/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sbcl2cffi.lisp
Type: application/octet-stream
Size: 3572 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20070114/66c1eb89/attachment.obj>


More information about the cffi-devel mailing list