Help with garbage collection in CFFI/TG

Discussion of Common Lisp

Help with garbage collection in CFFI/TG

Postby joeish80829 » Sun Apr 20, 2014 4:53 am

Thanks for your replies to my question, I was planning to go the trivial-garbage route and someone from CFFI-DEVEL kindly gave me this code to do GC automatically using my mat and del-mat functions

Code: Select all
 (defstruct (cvmatrix (:constructor %make-cvmatrix))
  (sap (mat) :type sb-sys:system-area-pointer :read-only t))

 (defun make-cvmatrix ()
  (let* ((matrix (%make-cvmatrix))
          (sap (cvmatrix-sap matrix)))
    (tg:finalize matrix (lambda (x) (del-mat sap)))

When I run the make-cvmatrix function to create a matrix it outputs a struct instead of a pointer like i needed. I changed the last line of the make-cvmatrix function to be "sap" as below:

Code: Select all
    (tg:finalize matrix (lambda (x) (del-mat sap)))

...and it appears to run fine with only minor variations in my ram levels which i suppose is just a result of lisps GC in process.
Since I'm new to GC w/ trivial-garbage, I was hoping someone can verify first that the change to sap was an ok move.

I was hoping someone could edit my code so I have a good example of Lisp GC with TG to work from and if I write a defcfun for this C wrapper :

Code: Select all
Mat* cv_create_Mat_typed(int rows, int cols, int type) {
    return new Mat(rows, cols, type);


(defcfun ("cv_create_Mat_typed" mat-typed)  (:pointer mat)
  "MAT constructor with a row, column and type parameter."
  (rows :int)
  (cols :int)
  (type :int))

show me where the rows cols type params get placed in an edited finalizer/constructor above..I would definately appreciate greatly:), concrete examples using the code I posted.
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 7 guests