with-foreign-object wrapper

Kevin Rosenberg <kevin-HJRc7zDS/[email protected]> Thu, 21 Mar 2002 08:56:38 -0700 (MST)
Newsgroups gmane.lisp.uffi.general
Message-ID <[email protected]>
I've added a with-foreign-object wrapper to UFFI. Right now,
it still does only static allocation. However, it hides the details
from the programmer.

I've added a new function to the gethostname example file which uses
this wrapper:
(defun gethostname2 ()
  "Returns the hostname"
  (uffi:with-foreign-object (name (:array :unsigned-char 256))
    (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
        (uffi:convert-from-foreign-string name)
        (error "gethostname() failed."))))

At some point, I can modify the wrapper to use stack vs. static allocation
as appropriate for the implementation. The main problem that I see with
doing this for CMUCL is that objects are quite a bit different when
allocated with make-alien vs. with-alien. There is basically an extra 
level of indirection with make-alien. This indirection consistent is
the other implementations. At this point, I'm not sure how cleanly I can
use CMUCL stack allocation and still keep pointer references consistent
with make-alien objects and with objects in other implementations.

-- 
Kevin Rosenberg
kevin-HJRc7zDS/[email protected]