On 4. Jul 2006, at 20:34, Frank Buss wrote:

I was at the conference yesterday and there was an interesting presentation
about how to create a Lispy interface to foreign libraries:

http://lisp-ecoop06.bknr.net/pdf/92988

Maybe we should use the class-concept for mapping SDL surfaces. Now that CFFI provides a callback when the GC is called for releasing the native Surface object, you don't have to release the surfaces yourself anymore (but the with-macros are still useful, because surfaces could be large and would be a good idea to release it, if you need it only within one code- block).

Hi Frank and everyone, first of all thanks for the kind words, and the discussions in Nantes. I might have been a bit unclear wrt administration of foreign resources; here is what I currently think is a good approach:

- Provide wrapper objects for each foreign resource that is visible in Lisp, so that it can be inspected, have methods defined etc.

(defclass surface () (sdl-surface))

- Automate object creation as far as possible, provide a safety net for when the user just drops the object

(defmethod initialize-instance :after ((instance surface) &key some- sdl-option) (let ((sdl-surface (sdl_make_surface some-sdl-option))) ;; pseudo- code
    (setf (slot-value instance 'sdl-surface) sdl-surface)
(cffi:finalize instance (lambda () (sdl_deallocate_surface sdl- surface)))))

- For non-memory foreign resources, or when the Lisp object is tiny and the foreign memory consumption huge, provide explicit close and with-foo functionality

(defun close-surface (surface)
  (cffi:cancel-finalization surface)
  (sdl_deallocate_surface (slot-value surface 'sdl-surface)
  (setf (slot-value surface 'sdl-surface) nil))

(defmacro with-surface (surface)
  ... ;; the usual unwind-protect magic
  )

This last step is critical: you really don't want the Lisp gc to be responsible for non-memory or non-Lisp memory. Note that this interface is alike to what CL itself provides for files.

I've discussed with the author, Rudi Schlatte, and currently they are using SWIG for creating the initial bindings, but for things like defining which objects needs to be destroyed, they do it by hand after generating it. Maybe we can work together to enhance our SWIG definition files with annotations
to generate this automaticly.

I have not looked at SWIG annotations in detail. Maybe cffi will have to be adapted as well; e.g. Luis gave me code to implemented a caller-deallocated foreign string that might not be in cffi yet. I'm interested in sharing experiences in that area.

Cheers,

Rudi

Attachment: PGP.sig
Description: This is a digitally signed message part

_______________________________________________
application-builder mailing list
[email protected]
http://www.lispniks.com/mailman/listinfo/application-builder

Reply via email to