Novikov Leonid ln@bk.ru writes:
Yes really this works well. However this will not help for instance in such event: (defun any-func () (let ((ptr (uffi:allocate-foreign-object '(:array :int 10)))) ; some actions are produced with ptr (uffi:free-foreign-object ptr)) )
Ok, one more patch:
diff -rN -u cffi-old/uffi-compat/uffi-compat.lisp cffi-new/uffi-compat/uffi-compat.lisp --- cffi-old/uffi-compat/uffi-compat.lisp 2005-12-21 12:00:02.000000000 +0000 +++ cffi-new/uffi-compat/uffi-compat.lisp 2005-12-26 12:59:25.000000000 +0000 @@ -255,7 +255,10 @@ (defmacro allocate-foreign-object (type &optional (size 1)) "Allocate one or more instance of a foreign type." - `(cffi:foreign-alloc (convert-uffi-type ,type) :count ,size)) + `(cffi:foreign-alloc ,(if (constantp type) + `',(convert-uffi-type (eval type)) + `(convert-uffi-type ,type)) + :count ,size)) (defmacro free-foreign-object (ptr) "Free a foreign object allocated by ALLOCATE-FOREIGN-OBJECT."
So, with this patch you get:
UFFI> (macroexpand-1 '(allocate-foreign-object '(:array :int 10))) (CFFI:FOREIGN-ALLOC '(UFFI-ARRAY :INT 10) :COUNT 1)
This foreign-alloc call will then be optimized by the (yet to be written) compiler macro on foreign-alloc which will calculate the size beforehand when TYPE and COUNT are constant.
Object uffi-array-type will create On each call of this functions. If for time of the functioning the program such call several thousand that computer memory ends.
Well, since those instances of foreign-type won't be needed anymore, they should be garbage collected. They only reason they aren't is because I was saving them in the *foreign-types* hashtable for lame reasons that no longer exist, so this patch will fix that:
diff -rN -u cffi-old/src/early-types.lisp cffi-new/src/early-types.lisp --- cffi-old/src/early-types.lisp 2005-12-11 04:54:41.000000000 +0000 +++ cffi-new/src/early-types.lisp 2005-12-26 13:35:50.000000000 +0000 @@ -92,9 +92,7 @@ (let* ((type-spec (mklist type-spec-or-name)) (parser (find-type-parser (car type-spec)))) (if parser - (let ((new-type (apply parser (cdr type-spec)))) - (notice-foreign-type new-type) - new-type) + (apply parser (cdr type-spec)) (error "Unknown CFFI type: ~S." type-spec-or-name))))) ;;;# Generic Functions on Types
Many thanks for pointing these issues out. Also, I'll keep in mind your patch if the need for a general way to cache types arises.