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