[cffi-devel] Could use a little help writing a defmacro for a defcfun and defun combo

Martin Simmons martin at lispworks.com
Fri Oct 25 11:17:34 UTC 2013


>>>>> On Thu, 24 Oct 2013 17:30:49 -0700 (PDT), Joeish W said:
> 
> here is the defcfun and deffun combo
> 
> ;; void cvSplit(const CvArr* src, CvArr* dst0, CvArr* dst1, CvArr* dst2, CvArr* dst3) 
> ;; Note: I had to use "CV-MERGE" instead of "MERGE" for the Common Lisp name of CV-MERGE so I named this  
> ;; function CV-SPLIT so they would match. 
> (cffi:defcfun ("cvSplit" %split) :void 
>   (src cv-arr) 
>   (dest-0 cv-arr) 
>   (dest-1 cv-arr) 
>   (dest-2 cv-arr) 
>   (dest-3 cv-arr)) 
>  
> (defun cv-split (src dest-0 &optional (dest-1 (cffi:null-pointer)) (dest-2 (cffi:null-pointer)) (dest-3 (cffi:null-pointer))) 
>   "Divides a multi-channel array into several single-channel arrays." 
>   (%split src dest-0 dest-1 dest-2 dest-3)) 
> 
> 
> it works as expected ...no errors at all
> 
> my attempt at a macro(new to macros) below works in one piece of code when the defun it is in is evaluated at the repl then ran at the repl
> 
> 
> ;; #define cvCvtPixToPlane cvSplit 
> (defmacro cvt-pix-to-plane (src dest-0 &optional (dest-1 (cffi:null-pointer))  
>                 (dest-2 (cffi:null-pointer)) (dest-3 (cffi:null-pointer))) 
>   "Macro for CV-SPLIT" 
>          `(cv-split ,src ,dest-0 ,dest-1 ,dest-2 ,dest-3))
> 
> but in the same code when used the exact same way    inside emacs (using slime/sbcl on ubuntu saucy)  when it is placed inside a newly opened file (.lisp) or even my well used test.lisp file  and ran with slime-compile-and-load-file i get
> 
> 
> 
>   error: 
>     Objects of type SB-SYS:SYSTEM-AREA-POINTER can't be dumped into fasl files.
>     --> CL-OPENCV::CV-SPLIT 
>     ==>
>       #.(SB-SYS:INT-SAP #X00000000)
> 
> can you help me figure out the issue and write macros with null-pointers correctly

This is a common gotcha with &optional/&key in macros: the default value is
evaluated during macroexpansion, so in general it should be a form.

I.e. you need `(cffi:null-pointer) instead of (cffi:null-pointer).

__Martin



More information about the cffi-devel mailing list