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