This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Could use help with foreign-free in Lisp's CFFI ?

1 post · 2820 views

Some one suggested i call foreign-alloc inside my defun associated with my defcfun....the defun & defcfun are here:

[/code]
 ;; CvMatND* cvInitMatNDHeader(CvMatND* mat, 
   int dims, const int* sizes, int type, void* data=NULL)
(cffi:defcfun ("cvInitMatNDHeader" %init-mat-nd-header) cv-mat-nd
  (mat cv-mat-nd)
  (dims :int)
  (sizes :pointer)
  (type :int)
  (data :pointer))

(defun init-mat-nd-header (mat dims sizes type &optional (data (cffi:null-pointer)))
  "Initializes a pre-allocated multi-dimensional array header."
  (%init-mat-nd-header mat dims (cffi:foreign-alloc :int 
   :initial-contents sizes) type 
     (cffi:foreign-alloc :int :initial-contents data)))
its a nice idea because i can write my code w/o usind the long foreign-alloc's in it but i was wondering how i use foreign-free then

I tried
  (defun init-mat-nd-header (mat dims sizes type &optional 
     (data (cffi:null-pointer)))
     "Initializes a pre-allocated multi-dimensional array header."
     (%init-mat-nd-header mat dims (cffi:foreign-alloc :int 
                                      :initial-contents sizes) type 
    (cffi:foreign-alloc :int :initial-contents data))               
              (return-from init-mat-nd-header data))
and just
 (defun init-mat-nd-header (mat dims sizes type &optional 
     (data (cffi:null-pointer)))
     "Initializes a pre-allocated multi-dimensional array header."
     (%init-mat-nd-header mat dims (cffi:foreign-alloc :int 
                                     :initial-contents sizes) type 
    (cffi:foreign-alloc :int :initial-contents data))               
              data)
I thought of a block return-from but that seems like the same thing as above if you are a cffi user....how would you accomplish this: