[Cffi-devel] Fw: Fw: What is the proper way to do GC in CFFI

Willem Rein Oudshoorn woudshoo at xs4all.nl
Sun Apr 13 17:50:26 UTC 2014


Joeish W <joeish80829 at yahoo.com> writes:

> If you get some free time I would love to see a concrete example, but
> I do appreciate you time so far and don't want to put you out in any
> way.. I'm building a big library and the better I can make each
> individual component the better

Here are some ideas on how I would start to approach this.
However, I am still curious what your motivation is of making
your bindings to OpenCV?

Note the following is from memory and not tested but 
I think you are better of with something along the following lines:

;;; UNTESTED CODE, LOOK AT REFERENCE MANUAL FOR WHAT I MEAN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Define the Bridge between the C-type and Lisp type

;;;; Define the C-type

(define-foreign-type cv-matrix-type ()  
   nil
  (:actual-type :pointer)
  (:simple-parser %cv-matrix))

;;;; Define the lisp type

(defstruct cvmatrix
  (sap))

;;;; Define the translation between the lisp and C world

(defmethod translate-to-foreign ((cvmatrix cvmatrix) (type cv-matrix-type))
  (declare (ignore type))
  (cvmatrix-sap cvmatrix))

(defmethod translate-from-foreign (value (type cv-matrix-type))
  (make-cvmatrix :sap value))

;;;; Define the functions

(defcfun ("cv_create_Mat" %mat) %cv-matrix)

(defcfun ("cv_create_Mat_with_data" mat-data) %cv-matrix
  (rows :int)
  (cols :int)
  (type :int)
  (data :pointer))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Now of course you want to change the :pointer in the definition
of %cv-matrix to a type lik %data or something similar.  

If you do that you can define translation methods on %data so you
can specify a lisp vector, list or array.

Also I would personally change the names of the functions `mat-data' and `%mat'.
They do not really say what they do.


Now for including garbage collection.  This depends on how you want your wrapper
to behave and on how the OpenCV library handles the memory management of the 
matrices.

But that is something for another post.

Kind regards,
Wim Oudshoorn





More information about the cffi-devel mailing list