r3704: *** empty log message ***
[uffi.git] / src / objects.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          objects.cl
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.9 2002/12/30 20:39:22 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 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :uffi)
21
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
37 (defmacro allocate-foreign-object (type &optional (size :unspecified))
38   "Allocates an instance of TYPE. If size is specified, then allocate
39 an array of TYPE with size SIZE. The TYPE parameter is evaluated."
40   (if (eq size :unspecified)
41       (progn
42         #+(or cmu scl)
43         `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
44         #+sbcl
45         `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
46         #+lispworks
47         `(fli:allocate-foreign-object :type ',(convert-from-uffi-type (eval type) :allocate))
48         #+allegro
49         `(ff:allocate-fobject ',(convert-from-uffi-type (eval type) :allocate) :c)
50         #+mcl
51         `(new-ptr ,(size-of-foreign-type (convert-from-uffi-type (eval type) :allocation)))
52         )
53       (progn
54         #+(or cmu scl)
55         `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
56         #+sbcl
57         `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
58         #+lispworks
59         `(fli:allocate-foreign-object :type ',(convert-from-uffi-type (eval type) :allocate) :nelems ,size)
60         #+allegro
61         `(ff:allocate-fobject (list :array (quote ,(convert-from-uffi-type (eval type) :allocate)) ,size) :c)
62         #+mcl
63         `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type (eval type) :allocation))))
64         )))
65
66 (defmacro free-foreign-object (obj)
67   #+(or cmu scl)
68   `(alien:free-alien ,obj)
69   #+sbcl
70   `(sb-alien:free-alien ,obj)
71   #+lispworks
72   `(fli:free-foreign-object ,obj)
73   #+allegro
74   `(ff:free-fobject ,obj)
75   #+mcl
76   `(dispose-ptr ,obj)
77   )
78
79 (defmacro null-pointer-p (obj)
80   #+lispworks `(fli:null-pointer-p ,obj)
81   #+allegro `(zerop ,obj)
82   #+(or cmu scl)   `(alien:null-alien ,obj)
83   #+sbcl   `(sb-alien:null-alien ,obj)
84   #+mcl   `(ccl:%null-ptr-p ,obj)
85   )
86
87 (defmacro make-null-pointer (type)
88   #+(or allegro mcl) (declare (ignore type))
89   #+(or cmu scl) `(alien:sap-alien (system:int-sap 0) (* ,(convert-from-uffi-type (eval type) :type)))
90   #+sbcl `(sb-alien:sap-alien (sb-sys:int-sap 0) (* ,(convert-from-uffi-type (eval type) :type)))
91   #+lispworks `(fli:make-pointer :address 0 :type (quote ,(convert-from-uffi-type (eval type) :type)))
92   #+allegro 0
93   #+mcl `(ccl:%null-ptr)
94   )
95
96 (defmacro char-array-to-pointer (obj)
97   #+(or cmu scl) `(alien:cast ,obj (* (alien:unsigned 8)))
98   #+sbcl `(sb-alien:cast ,obj (* (sb-alien:unsigned 8)))
99   #+lispworks `(fli:make-pointer :type '(:unsigned :char)
100                                 :address (fli:pointer-address ,obj))
101   #+allegro obj
102   #+mcl obj
103   )
104
105 (defmacro deref-pointer (ptr type)
106   "Returns a object pointed"
107   #+(or cmu sbcl lispworks scl) (declare (ignore type))
108   #+(or cmu scl)  `(alien:deref ,ptr)
109   #+sbcl  `(sb-alien:deref ,ptr)
110   #+lispworks `(fli:dereference ,ptr)
111   #+allegro `(ff:fslot-value-typed (quote ,(convert-from-uffi-type type :deref)) :c ,ptr)
112   #+mcl `(ccl:pref ,ptr ,(convert-from-uffi-type type :deref))
113   )
114
115 #+mcl
116 (defmacro deref-pointer-set (ptr type value)
117   `(setf (ccl:pref ,ptr ,(convert-from-uffi-type type :deref)) ,value))
118
119 #+mcl
120 (defsetf deref-pointer deref-pointer-set)
121
122 #+(or lispworks (and mcl (not openmcl))) ;; with LW, deref is a character
123 (defmacro ensure-char-character (obj)
124   obj)
125
126 #+(or allegro cmu sbcl scl openmcl)
127 (defmacro ensure-char-character (obj)
128   `(code-char ,obj))
129   
130 #+(or lispworks (and mcl (not openmcl)))
131 (defmacro ensure-char-integer (obj)
132  `(char-code ,obj))
133
134 #+(or allegro cmu sbcl scl openmcl)
135 (defmacro ensure-char-integer (obj)
136   obj)
137
138 (defmacro pointer-address (obj)
139   #+(or cmu scl)
140   `(system:sap-int (alien:alien-sap ,obj))
141   #+sbcl
142   `(sb-sys:sap-int (sb-alien:alien-sap ,obj))
143   #+lispworks
144   `(fli:pointer-address ,obj)
145   #+allegro
146   obj
147   #+mcl
148   `(ccl:%ptr-to-int ,obj)  
149   )
150
151 ;; TYPE is evaluated.
152 #-mcl
153 (defmacro with-foreign-object ((var type) &rest body)
154   #-(or cmu sbcl lispworks scl) ; default version
155   `(let ((,var (allocate-foreign-object ,type)))
156     (unwind-protect
157          (progn ,@body)
158       (free-foreign-object ,var)))
159   #+(or cmu scl)
160   (let ((obj (gensym)))
161     `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
162        (let ((,var (alien:addr ,obj)))
163          ,@body)))
164   #+sbcl
165   (let ((obj (gensym)))
166     `(sb-alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
167        (let ((,var (sb-alien:addr ,obj)))
168          ,@body)))
169   #+lispworks
170   `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
171                                               (eval type) :allocate)))
172     ,@body)
173   )
174
175 #-mcl
176 (defmacro with-foreign-objects (bindings &rest body)
177   (if bindings
178       `(with-foreign-object ,(car bindings)
179         (with-foreign-objects ,(cdr bindings)
180           ,@body))
181       `(progn ,@body)))
182
183 #+mcl
184 (defmacro with-foreign-objects (bindings &rest body)
185   (let ((params nil) type count)
186     (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
187       (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
188       (setf count 1)
189       (when (and (listp type) (eq (first type) :array))
190         (setf count (nth 2 type))
191         (unless (integerp count) (error "Invalid size for array: ~a" type))
192         (setf type (nth 1 type)))
193       (push (list (first spec) (* count (size-of-foreign-type type))) params))
194     `(ccl:%stack-block ,params ,@body)))
195                                  
196 #+mcl
197 (defmacro with-foreign-object ((var type) &rest body)
198   `(with-foreign-objects ((,var ,type)) 
199      ,@body))
200