r5496: def-foreign-var support
[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: objects.lisp,v 1.17 2003/08/14 21:40:13 kevin Exp $
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         `(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 char-array-to-pointer (obj)
96   #+(or cmu scl) `(alien:cast ,obj (* (alien:unsigned 8)))
97   #+sbcl `(sb-alien:cast ,obj (* (sb-alien:unsigned 8)))
98   #+lispworks `(fli:make-pointer :type '(:unsigned :char)
99                                 :address (fli:pointer-address ,obj))
100   #+allegro obj
101   #+mcl obj
102   )
103
104 (defmacro deref-pointer (ptr type)
105   "Returns a object pointed"
106   #+(or cmu sbcl lispworks scl) (declare (ignore type))
107   #+(or cmu scl)  `(alien:deref ,ptr)
108   #+sbcl  `(sb-alien:deref ,ptr)
109   #+lispworks `(fli:dereference ,ptr)
110   #+allegro `(ff:fslot-value-typed (quote ,(convert-from-uffi-type type :deref)) :c ,ptr)
111   #+mcl `(ccl:pref ,ptr ,(convert-from-uffi-type type :deref))
112   )
113
114 #+mcl
115 (defmacro deref-pointer-set (ptr type value)
116   `(setf (ccl:pref ,ptr ,(convert-from-uffi-type type :deref)) ,value))
117
118 #+mcl
119 (defsetf deref-pointer deref-pointer-set)
120
121 #+lispworks
122 (defmacro ensure-char-character (obj)
123   `(if (characterp ,obj) ,obj (code-char ,obj)))
124
125 #+(and mcl (not openmcl)) 
126 (defmacro ensure-char-character (obj)
127   obj)
128
129 #+(or allegro cmu sbcl scl openmcl)
130 (defmacro ensure-char-character (obj)
131   `(code-char ,obj))
132   
133 #+(or lispworks (and mcl (not openmcl)))
134 (defmacro ensure-char-integer (obj)
135  `(char-code ,obj))
136
137 #+(or allegro cmu sbcl scl openmcl)
138 (defmacro ensure-char-integer (obj)
139   obj)
140
141 (defmacro pointer-address (obj)
142   #+(or cmu scl)
143   `(system:sap-int (alien:alien-sap ,obj))
144   #+sbcl
145   `(sb-sys:sap-int (sb-alien:alien-sap ,obj))
146   #+lispworks
147   `(fli:pointer-address ,obj)
148   #+allegro
149   obj
150   #+mcl
151   `(ccl:%ptr-to-int ,obj)  
152   )
153
154 ;; TYPE is evaluated.
155 #-mcl
156 (defmacro with-foreign-object ((var type) &rest body)
157   #-(or cmu sbcl lispworks scl) ; default version
158   `(let ((,var (allocate-foreign-object ,type)))
159     (unwind-protect
160          (progn ,@body)
161       (free-foreign-object ,var)))
162   #+(or cmu scl)
163   (let ((obj (gensym)))
164     `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
165        (let ((,var (alien:addr ,obj)))
166          ,@body)))
167   #+sbcl
168   (let ((obj (gensym)))
169     `(sb-alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
170        (let ((,var (sb-alien:addr ,obj)))
171          ,@body)))
172   #+lispworks
173   `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
174                                               (eval type) :allocate)))
175     ,@body)
176   )
177
178 #-mcl
179 (defmacro with-foreign-objects (bindings &rest body)
180   (if bindings
181       `(with-foreign-object ,(car bindings)
182         (with-foreign-objects ,(cdr bindings)
183           ,@body))
184       `(progn ,@body)))
185
186 #+mcl
187 (defmacro with-foreign-objects (bindings &rest body)
188   (let ((params nil) type count)
189     (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
190       (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
191       (setf count 1)
192       (when (and (listp type) (eq (first type) :array))
193         (setf count (nth 2 type))
194         (unless (integerp count) (error "Invalid size for array: ~a" type))
195         (setf type (nth 1 type)))
196       (push (list (first spec) (* count (size-of-foreign-type type))) params))
197     `(ccl:%stack-block ,params ,@body)))
198                                  
199 #+mcl
200 (defmacro with-foreign-object ((var type) &rest body)
201   `(with-foreign-objects ((,var ,type)) 
202      ,@body))
203
204 #+lispworks
205 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
206   `(fli:with-coerced-pointer (,binding-name
207                           :type ',(convert-from-uffi-type (eval type) :type))
208       ,pointer
209     ,@body))
210
211 #+(or cmu scl sbcl)
212 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
213   `(let ((,binding-name
214           (#+(or cmu scl) alien:cast
215            #+sbcl sb-alien:cast
216            ,pointer (* ,(convert-from-uffi-type (eval type) :type)))))
217     ,@body))
218
219 #+allegro
220 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
221   (declare (ignore type))
222   `(let ((,binding-name ,pointer))
223     ,@body))
224
225 #-(or lispworks cmu scl sbcl allegro)
226 (defmacro with-cast-pointer ((binding-name pointer type) &body body)
227   (declare (ignore binding-name pointer type))
228   '(error "WITH-CAST-POINTER not (yet) implemented for ~A"
229           (lisp-implementation-type)))
230
231 (defmacro def-foreign-var (names type module)
232   #-lispworks (declare (ignore module))
233   (let ((foreign-name (if (atom names) names (first names)))
234         (lisp-name (if (atom names) (uffi::make-lisp-name names) (second names)))
235         (var-type (uffi::convert-from-uffi-type type :foreign-var)))
236     #+(or cmu scl)
237     `(alien:def-alien-variable (,foreign-name ,lisp-name) ,var-type)
238     #+sbcl
239     `(sb-alien:define-alien-variable (,foreign-name ,lisp-name) ,var-type)
240     #+allegro
241     `(ff:def-foreign-variable (,lisp-name ,foreign-name) :convention :c
242       :type ,var-type)
243     #+lispworks
244     (let ((temp-name (gensym)))
245       `(progn
246         (fli:define-foreign-variable (,temp-name ,foreign-name) :type ,var-type :module ,module)
247         (define-symbol-macro ,lisp-name (,temp-name))))
248     #-(or allegro cmu scl sbcl lispworks)
249     `(define-symbol-macro ,lisp-name
250       '(error "DEF-FOREIGN-VAR not (yet) defined for ~A"
251         (lisp-implementation-type)))))