r2905: *** empty log message ***
[uffi.git] / src / objects.cl
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.cl,v 1.24 2002/09/30 07:51:01 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 (defmacro allocate-foreign-object (type &optional (size :unspecified))
23   "Allocates an instance of TYPE. If size is specified, then allocate
24 an array of TYPE with size SIZE. The TYPE parameter is evaluated."
25   (if (eq size :unspecified)
26       (progn
27         #+cmu
28         `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
29         #+lispworks
30         `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
31         #+allegro
32         `(ff:allocate-fobject ,(convert-from-uffi-type type :allocate) :c)
33         #+mcl
34         `(new-ptr ,(size-of-foreign-type (convert-from-uffi-type type :allocation)))
35         )
36       (progn
37         #+cmu
38         `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
39         #+lispworks
40         `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
41         #+allegro
42         `(ff:allocate-fobject '(:array ,(convert-from-uffi-type (eval type) :allocate) ,(eval size)) :c)
43         #+mcl
44         `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type type :allocation))))
45         )))
46
47 (defmacro free-foreign-object (obj)
48   #+cmu
49   `(alien:free-alien ,obj)
50   #+lispworks
51   `(fli:free-foreign-object ,obj)
52   #+allegro
53   `(ff:free-fobject ,obj)
54   #+mcl
55   `(dispose-ptr ,obj)
56   )
57
58 (defmacro null-pointer-p (obj)
59   #+lispworks `(fli:null-pointer-p ,obj)
60   #+allegro `(zerop ,obj)
61   #+cmu   `(alien:null-alien ,obj)
62   #+mcl   `(ccl:%null-ptr-p ,obj)
63   )
64
65 (defmacro size-of-foreign-type (type)
66   #+lispworks `(fli:size-of ,type)
67   #+allegro `(ff:sizeof-fobject ,type)
68   #+cmu   `(alien:alien-size ,type)
69   #+clisp   `(values (ffi:size-of ,type))
70   #+(and mcl (not openmcl))
71   `(let ((mcl-type (ccl:find-mactype ,type nil t)))
72      (if mcl-type 
73          (ccl::mactype-record-size mcl-type)
74        (ccl::record-descriptor-length (ccl:find-record-descriptor ,type t t)))) ;error if not a record
75   #+opencml   `(ccl::%foreign-type-or-record-size ,type :bytes)
76   )
77
78
79 (defmacro make-null-pointer (type)
80   #+(or allegro cmu mcl) (declare (ignore type))
81   
82   #+cmu `(system:int-sap 0)
83   #+allegro 0
84   #+lispworks `(fli:make-pointer :address 0 :type ,type)
85   #+mcl `(ccl:%null-ptr)
86   )
87
88 (defmacro char-array-to-pointer (obj)
89   #+cmu `(alien:cast ,obj (* (alien:unsigned 8)))
90   #+lispworks `(fli:make-pointer :type '(:unsigned :char)
91                                 :address (fli:pointer-address ,obj))
92   #+allegro obj
93   #+mcl obj
94   )
95
96 (defmacro deref-pointer (ptr type)
97   "Returns a object pointed"
98   #+(or cmu lispworks) (declare (ignore type))
99   #+cmu  `(alien:deref ,ptr)
100   #+lispworks `(fli:dereference ,ptr)
101   #+allegro `(ff:fslot-value-typed ,(convert-from-uffi-type type :deref) :c ,ptr)
102   #+mcl `(ccl:pref ,ptr ,(convert-from-uffi-type type :deref))
103   )
104
105 #+mcl
106 (defmacro deref-pointer-set (ptr type value)
107   `(setf (ccl:pref ,ptr ,(convert-from-uffi-type type :deref)) ,value))
108
109 #+mcl
110 (defsetf deref-pointer deref-pointer-set)
111
112 #+(or lispworks (and mcl (not openmcl))) ;; with LW, deref is a character
113 (defmacro ensure-char-character (obj)
114   obj)
115
116 #+(or allegro cmu openmcl)
117 (defmacro ensure-char-character (obj)
118   `(code-char ,obj))
119   
120 #+(or lispworks (and mcl (not openmcl)))
121 (defmacro ensure-char-integer (obj)
122  `(char-code ,obj))
123
124 #+(or allegro cmu openmcl)
125 (defmacro ensure-char-integer (obj)
126   obj)
127
128 (defmacro pointer-address (obj)
129   #+cmu
130   `(system:sap-int (alien:alien-sap ,obj))
131   #+lispworks
132   `(fli:pointer-address ,obj)
133   #+allegro
134   obj
135   #+mcl
136   `(ccl:%ptr-to-int ,obj)  
137   )
138
139 ;; TYPE is evaluated.
140 #-mcl
141 (defmacro with-foreign-object ((var type) &rest body)
142   #-(or cmu lispworks) ; default version
143   `(let ((,var (allocate-foreign-object ,type)))
144     (unwind-protect
145          (progn ,@body)
146       (free-foreign-object ,var)))
147   #+cmu
148   (let ((obj (gensym)))
149     `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
150        (let ((,var (alien:addr ,obj)))
151          ,@body)))
152   #+lispworks
153   `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
154                                               (eval type) :allocate)))
155     ,@body)
156   )
157
158 #+mcl
159 (defmacro with-foreign-object ((var type) &rest body)
160   `(with-foreign-objects ((,var ,type)) 
161      ,@body))
162
163 #-mcl
164 (defmacro with-foreign-objects (bindings &rest body)
165   (if bindings
166       `(with-foreign-object ,(car bindings)
167         (with-foreign-objects ,(cdr bindings)
168           ,@body))
169       `(progn ,@body)))
170
171 #+mcl
172 (defmacro with-foreign-objects (bindings &rest body)
173   (let ((params nil) type count)
174     (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
175       (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
176       (setf count 1)
177       (when (and (listp type) (eq (first type) :array))
178         (setf count (nth 2 type))
179         (unless (integerp count) (error "Invalid size for array: ~a" type))
180         (setf type (nth 1 type)))
181       (push (list (first spec) (* count (size-of-foreign-type type))) params))
182     `(ccl:%stack-block ,params ,@body)))
183