r2912: rename .cl to .lisp
[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.1 2002/09/30 10:02:36 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   #+cmu  (ash (eval `(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         #+cmu
42         `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
43         #+lispworks
44         `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
45         #+allegro
46         `(ff:allocate-fobject ,(convert-from-uffi-type type :allocate) :c)
47         #+mcl
48         `(new-ptr ,(size-of-foreign-type (convert-from-uffi-type type :allocation)))
49         )
50       (progn
51         #+cmu
52         `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
53         #+lispworks
54         `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
55         #+allegro
56         `(ff:allocate-fobject '(:array ,(convert-from-uffi-type (eval type) :allocate) ,(eval size)) :c)
57         #+mcl
58         `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type type :allocation))))
59         )))
60
61 (defmacro free-foreign-object (obj)
62   #+cmu
63   `(alien:free-alien ,obj)
64   #+lispworks
65   `(fli:free-foreign-object ,obj)
66   #+allegro
67   `(ff:free-fobject ,obj)
68   #+mcl
69   `(dispose-ptr ,obj)
70   )
71
72 (defmacro null-pointer-p (obj)
73   #+lispworks `(fli:null-pointer-p ,obj)
74   #+allegro `(zerop ,obj)
75   #+cmu   `(alien:null-alien ,obj)
76   #+mcl   `(ccl:%null-ptr-p ,obj)
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-objects (bindings &rest body)
160   (if bindings
161       `(with-foreign-object ,(car bindings)
162         (with-foreign-objects ,(cdr bindings)
163           ,@body))
164       `(progn ,@body)))
165
166 #+mcl
167 (defmacro with-foreign-objects (bindings &rest body)
168   (let ((params nil) type count)
169     (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
170       (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
171       (setf count 1)
172       (when (and (listp type) (eq (first type) :array))
173         (setf count (nth 2 type))
174         (unless (integerp count) (error "Invalid size for array: ~a" type))
175         (setf type (nth 1 type)))
176       (push (list (first spec) (* count (size-of-foreign-type type))) params))
177     `(ccl:%stack-block ,params ,@body)))
178                                  
179 #+mcl
180 (defmacro with-foreign-object ((var type) &rest body)
181   `(with-foreign-objects ((,var ,type)) 
182      ,@body))
183