1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: objects.lisp
6 ;;;; Purpose: UFFI source to handle objects and pointers
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: objects.lisp,v 1.13 2003/06/06 21:59:18 kevin Exp $
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
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)))
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)
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)
42 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
44 `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
46 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
48 `(ff:allocate-fobject ',(convert-from-uffi-type type :allocate) :c)
50 `(new-ptr ,(size-of-foreign-type (convert-from-uffi-type type :allocation)))
54 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
56 `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
58 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
60 `(ff:allocate-fobject (list :array (quote ,(convert-from-uffi-type type :allocate)) ,size) :c)
62 `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type type :allocation))))
65 (defmacro free-foreign-object (obj)
67 `(alien:free-alien ,obj)
69 `(sb-alien:free-alien ,obj)
71 `(fli:free-foreign-object ,obj)
73 `(ff:free-fobject ,obj)
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)
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)))
92 #+mcl `(ccl:%null-ptr)
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))
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))
115 (defmacro deref-pointer-set (ptr type value)
116 `(setf (ccl:pref ,ptr ,(convert-from-uffi-type type :deref)) ,value))
119 (defsetf deref-pointer deref-pointer-set)
121 #+(or (and mcl (not openmcl))) ;; with LW, deref is a character
122 (defmacro ensure-char-character (obj)
125 #+(or allegro lispworks cmu sbcl scl openmcl)
126 (defmacro ensure-char-character (obj)
129 #+(or lispworks (and mcl (not openmcl)))
130 (defmacro ensure-char-integer (obj)
133 #+(or allegro cmu sbcl scl openmcl)
134 (defmacro ensure-char-integer (obj)
137 ;; Returns the numeric value of a byte at a pointer
139 (defmacro byte-value-at-pointer (ptr)
142 (defmacro pointer-address (obj)
144 `(system:sap-int (alien:alien-sap ,obj))
146 `(sb-sys:sap-int (sb-alien:alien-sap ,obj))
148 `(fli:pointer-address ,obj)
152 `(ccl:%ptr-to-int ,obj)
155 ;; TYPE is evaluated.
157 (defmacro with-foreign-object ((var type) &rest body)
158 #-(or cmu sbcl lispworks scl) ; default version
159 `(let ((,var (allocate-foreign-object ,type)))
162 (free-foreign-object ,var)))
164 (let ((obj (gensym)))
165 `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
166 (let ((,var (alien:addr ,obj)))
169 (let ((obj (gensym)))
170 `(sb-alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
171 (let ((,var (sb-alien:addr ,obj)))
174 `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
175 (eval type) :allocate)))
180 (defmacro with-foreign-objects (bindings &rest body)
182 `(with-foreign-object ,(car bindings)
183 (with-foreign-objects ,(cdr bindings)
188 (defmacro with-foreign-objects (bindings &rest body)
189 (let ((params nil) type count)
190 (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
191 (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
193 (when (and (listp type) (eq (first type) :array))
194 (setf count (nth 2 type))
195 (unless (integerp count) (error "Invalid size for array: ~a" type))
196 (setf type (nth 1 type)))
197 (push (list (first spec) (* count (size-of-foreign-type type))) params))
198 `(ccl:%stack-block ,params ,@body)))
201 (defmacro with-foreign-object ((var type) &rest body)
202 `(with-foreign-objects ((,var ,type))