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