r9418: rework cmucl/sbcl arrays in deref-array, allocate-foreign-object, and with...
[uffi.git] / src / objects.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          objects.lisp
6 ;;;; Purpose:       UFFI source to handle objects and pointers
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package #:uffi)
20
21 (defun size-of-foreign-type (type)
22   #+lispworks (fli:size-of type)
23   #+allegro (ff:sizeof-fobject type)
24   #+(or cmu scl)  (ash (eval `(alien:alien-size ,type)) -3) ;; convert from bits to bytes
25   #+sbcl  (ash (eval `(sb-alien:alien-size ,type)) -3) ;; convert from bits to bytes
26   #+clisp (values (ffi:size-of type))
27   #+(and mcl (not openmcl))
28   (let ((mcl-type (ccl:find-mactype type nil t)))
29     (if mcl-type 
30         (ccl::mactype-record-size mcl-type)
31       (ccl::record-descriptor-length (ccl:find-record-descriptor type t t)))) ;error if not a record
32   #+openmcl (ccl::%foreign-type-or-record-size type :bytes)
33   )
34
35
36 (defmacro allocate-foreign-object (type &optional (size :unspecified))
37   "Allocates an instance of TYPE. If size is specified, then allocate
38 an array of TYPE with size SIZE. The TYPE parameter is evaluated."
39   (if (eq size :unspecified)
40       (progn
41         #+(or cmu scl)
42         `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
43         #+sbcl
44         `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
45         #+lispworks
46         `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
47         #+allegro
48         `(ff:allocate-fobject ',(convert-from-uffi-type type :allocate) :c)
49         #+mcl
50         `(new-ptr ,(size-of-foreign-type (convert-from-uffi-type type :allocation)))
51         )
52       (progn
53         #+(or cmu scl)
54         (if (integerp size)
55             `(alien:cast
56               (alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
57               (array ,(convert-from-uffi-type (eval type) :allocation) ,size))
58             `(alien:cast
59               (alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
60               (array ,(convert-from-uffi-type (eval type) :allocation) nil)))
61         #+sbcl
62         (if (integerp size)
63             `(sb-alien:cast
64               (sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
65               (array ,(convert-from-uffi-type (eval type) :allocation) ,size))
66             `(sb-alien:cast
67               (sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
68               (array ,(convert-from-uffi-type (eval type) :allocation) nil)))
69         #+lispworks
70         `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
71         #+allegro
72         `(ff:allocate-fobject (list :array (quote ,(convert-from-uffi-type type :allocate)) ,size) :c)
73         #+mcl
74         `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type type :allocation))))
75         )))
76
77 (defmacro free-foreign-object (obj)
78   #+(or cmu scl)
79   `(alien:free-alien ,obj)
80   #+sbcl
81   `(sb-alien:free-alien ,obj)
82   #+lispworks
83   `(fli:free-foreign-object ,obj)
84   #+allegro
85   `(ff:free-fobject ,obj)
86   #+mcl
87   `(dispose-ptr ,obj)
88   )
89
90 (defmacro null-pointer-p (obj)
91   #+lispworks `(fli:null-pointer-p ,obj)
92   #+allegro `(zerop ,obj)
93   #+(or cmu scl)   `(alien:null-alien ,obj)
94   #+sbcl   `(sb-alien:null-alien ,obj)
95   #+mcl   `(ccl:%null-ptr-p ,obj)
96   )
97
98 (defmacro make-null-pointer (type)
99   #+(or allegro mcl) (declare (ignore type))
100   #+(or cmu scl) `(alien:sap-alien (system:int-sap 0) (* ,(convert-from-uffi-type (eval type) :type)))
101   #+sbcl `(sb-alien:sap-alien (sb-sys:int-sap 0) (* ,(convert-from-uffi-type (eval type) :type)))
102   #+lispworks `(fli:make-pointer :address 0 :type (quote ,(convert-from-uffi-type (eval type) :type)))
103   #+allegro 0
104   #+mcl `(ccl:%null-ptr)
105   )
106
107 (defmacro make-pointer (addr type)
108   #+(or allegro mcl) (declare (ignore type))
109   #+(or cmu scl) `(alien:sap-alien (system:int-sap ,addr) (* ,(convert-from-uffi-type (eval type) :type)))
110   #+sbcl `(sb-alien:sap-alien (sb-sys:int-sap ,addr) (* ,(convert-from-uffi-type (eval type) :type)))
111   #+lispworks `(fli:make-pointer :address ,addr :type (quote ,(convert-from-uffi-type (eval type) :type)))
112   #+allegro addr
113   #+mcl `(ccl:%int-to-ptr ,addr)
114   )
115
116
117 (defmacro char-array-to-pointer (obj)
118   #+(or cmu scl) `(alien:cast ,obj (* (alien:unsigned 8)))
119   #+sbcl `(sb-alien:cast ,obj (* (sb-alien:unsigned 8)))
120   #+lispworks `(fli:make-pointer :type '(:unsigned :char)
121                                 :address (fli:pointer-address ,obj))
122   #+allegro obj
123   #+mcl obj
124   )
125
126 (defmacro deref-pointer (ptr type)
127   "Returns a object pointed"
128   #+(or cmu sbcl lispworks scl) (declare (ignore type))
129   #+(or cmu scl)  `(alien:deref ,ptr)
130   #+sbcl  `(sb-alien:deref ,ptr)
131   #+lispworks `(fli:dereference ,ptr)
132   #+allegro `(ff:fslot-value-typed (quote ,(convert-from-uffi-type type :deref)) :c ,ptr)
133   #+mcl `(ccl:pref ,ptr ,(convert-from-uffi-type type :deref))
134   )
135
136 #+mcl
137 (defmacro deref-pointer-set (ptr type value)
138   `(setf (ccl:pref ,ptr ,(convert-from-uffi-type type :deref)) ,value))
139
140 #+mcl
141 (defsetf deref-pointer deref-pointer-set)
142
143 (defmacro ensure-char-character (obj)
144   #+(or (and mcl (not openmcl))) obj
145   #+(or allegro cmu sbcl scl openmcl) `(code-char ,obj)
146   ;; lispworks varies whether deref'ing array vs. slot access of a char
147   #+lispworks
148   `(if (characterp ,obj) ,obj (code-char ,obj)))
149   
150 (defmacro ensure-char-integer (obj)
151   #+(or (and mcl (not openmcl))) `(char-code ,obj)
152   #+(or allegro cmu sbcl scl openmcl) obj
153   `(if (characterp ,obj) (char-code ,obj) ,obj))
154
155 (defmacro ensure-char-storable (obj)
156   #+(or lispworks (and mcl (not openmcl))) obj
157   #+(or allegro cmu sbcl scl openmcl) `(char-code ,obj))
158
159 (defmacro pointer-address (obj)
160   #+(or cmu scl)
161   `(system:sap-int (alien:alien-sap ,obj))
162   #+sbcl
163   `(sb-sys:sap-int (sb-alien:alien-sap ,obj))
164   #+lispworks
165   `(fli:pointer-address ,obj)
166   #+allegro
167   obj
168   #+mcl
169   `(ccl:%ptr-to-int ,obj)  
170   )
171
172 ;; TYPE is evaluated.
173 #-mcl
174 (defmacro with-foreign-object ((var type) &rest body)
175   #-(or cmu sbcl lispworks scl) ; default version
176   `(let ((,var (allocate-foreign-object ,type)))
177     (unwind-protect
178          (progn ,@body)
179       (free-foreign-object ,var)))
180   #+(or cmu scl)
181   (let ((obj (gensym)))
182     `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
183        (let ((,var (alien:addr ,obj)))
184          ,@body)))
185   #+sbcl
186   (let ((obj (gensym))
187         (ctype (convert-from-uffi-type (eval type) :allocate)))
188     (if (and (consp ctype) (eq 'array (car ctype)))
189         `(sb-alien:with-alien ((,obj ,ctype))
190           (let* ((,var ,obj))
191             ,@body))
192         `(sb-alien:with-alien ((,obj ,ctype))
193           (let* ((,var (sb-alien:addr ,obj)))
194             ,@body))))
195   #+lispworks
196   `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
197                                               (eval type) :allocate)))
198     ,@body)
199   )
200
201 #-mcl
202 (defmacro with-foreign-objects (bindings &rest body)
203   (if bindings
204       `(with-foreign-object ,(car bindings)
205         (with-foreign-objects ,(cdr bindings)
206           ,@body))
207       `(progn ,@body)))
208
209 #+mcl
210 (defmacro with-foreign-objects (bindings &rest body)
211   (let ((params nil) type count)
212     (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
213       (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
214       (setf count 1)
215       (when (and (listp type) (eq (first type) :array))
216         (setf count (nth 2 type))
217         (unless (integerp count) (error "Invalid size for array: ~a" type))
218         (setf type (nth 1 type)))
219       (push (list (first spec) (* count (size-of-foreign-type type))) params))
220     `(ccl:%stack-block ,params ,@body)))
221                                  
222 #+mcl
223 (defmacro with-foreign-object ((var type) &rest body)
224   `(with-foreign-objects ((,var ,type)) 
225      ,@body))
226
227 #+lispworks
228 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
229   `(fli:with-coerced-pointer (,binding-name
230                           :type ',(convert-from-uffi-type (eval type) :type))
231       ,pointer
232     ,@body))
233
234 #+(or cmu scl sbcl)
235 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
236   `(let ((,binding-name
237           (#+(or cmu scl) alien:cast
238            #+sbcl sb-alien:cast
239            ,pointer (* ,(convert-from-uffi-type (eval type) :type)))))
240     ,@body))
241
242 #+(or allegro openmcl)
243 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
244   (declare (ignore type))
245   `(let ((,binding-name ,pointer))
246     ,@body))
247
248 #-(or lispworks cmu scl sbcl allegro)
249 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
250   (declare (ignore binding-name pointer type))
251   '(error "WITH-CAST-POINTER not (yet) implemented for ~A"
252           (lisp-implementation-type)))
253
254 (defmacro def-foreign-var (names type module)
255   #-lispworks (declare (ignore module))
256   (let ((foreign-name (if (atom names) names (first names)))
257         (lisp-name (if (atom names) (make-lisp-name names) (second names)))
258         #-allegro
259         (var-type (convert-from-uffi-type type :type)))
260     #+(or cmu scl)
261     `(alien:def-alien-variable (,foreign-name ,lisp-name) ,var-type)
262     #+sbcl
263     `(sb-alien:define-alien-variable (,foreign-name ,lisp-name) ,var-type)
264     #+allegro
265     `(define-symbol-macro ,lisp-name
266       (ff:fslot-value-typed (quote ,(convert-from-uffi-type type :deref))
267                             :c (ff:get-entry-point ,foreign-name)))
268     #+lispworks
269     `(progn
270       (fli:define-foreign-variable (,lisp-name ,foreign-name)
271                                     :accessor :address-of
272                                     :type ,var-type
273                                     :module ,module)
274       (define-symbol-macro ,lisp-name (fli:dereference (,lisp-name)
275                                                         :copy-foreign-object nil)))
276     #-(or allegro cmu scl sbcl lispworks)
277     `(define-symbol-macro ,lisp-name
278       '(error "DEF-FOREIGN-VAR not (yet) defined for ~A"
279         (lisp-implementation-type)))))