r1613: Updated to use stack allocation
[uffi.git] / src / objects.cl
index b0b34f07ca98fb8191238cdf7f9fd8e5285c24eb..6a33f9b1cc04a9b145e6c27924ad13ddbef7d4e0 100644 (file)
@@ -1,4 +1,4 @@
-;;;; -*- Mode: ANSI-Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
+;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
 ;;;; *************************************************************************
 ;;;; FILE IDENTIFICATION
 ;;;;
@@ -7,7 +7,7 @@
 ;;;; Programmer:    Kevin M. Rosenberg
 ;;;; Date Started:  Feb 2002
 ;;;;
-;;;; $Id: objects.cl,v 1.11 2002/03/21 14:49:14 kevin Exp $
+;;;; $Id: objects.cl,v 1.13 2002/03/21 16:47:46 kevin Exp $
 ;;;;
 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
 ;;;;
@@ -64,6 +64,13 @@ an array of TYPE with size SIZE."
   #+lispworks `(fli:make-pointer :address 0 :type ,type)
   )
 
+(defmacro char-array-to-pointer (obj)
+  #+cmu `(alien:cast ,obj (* (alien:unsigned 8)))
+  #+lispworks `(fli:make-pointer :type '(:unsigned :char)
+                               :address (fli:pointer-address ,obj))
+  #+allegro obj
+  )
+
 (defmacro deref-pointer (ptr type)
   "Returns a object pointed"
   #+(or cmu lispworks) (declare (ignore type))
@@ -102,12 +109,21 @@ an array of TYPE with size SIZE."
 
 ;; Simple first pass. Will later create optimized routines for
 ;; various platforms.
-(defmacro with-foreign-object ((var type &rest etc) &rest body)
-  (let ((result (gensym)))
-    `(let* ((,var (allocate-foreign-object ,type ,@etc))
-           (,result (progn ,@body)))
-      (free-foreign-object ,var)
-      ,result)))
+(defmacro with-foreign-object ((var type) &rest body)
+  #+allegro
+  `(let ((,var (allocate-foreign-object ,type)))
+    (unwind-protect
+        (progn ,@body)
+      (free-foreign-object ,var)))
+  #+cmu
+  `(alien:with-alien ((,var ,(convert-from-uffi-type type :allocate)))
+    (setq ,var (alien:addr ,var))
+    ,@body)
+  #+lispworks
+  `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type type :allocate)))
+    ,@body)
+  )
+
 
 (defmacro with-foreign-objects (bindings &rest body)
   (if bindings