1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
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.10 2002/12/30 20:49:12 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 ;;;; *************************************************************************
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
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)))
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)
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)
43 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
45 `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
47 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
49 `(ff:allocate-fobject ',(convert-from-uffi-type type :allocate) :c)
51 `(new-ptr ,(size-of-foreign-type (convert-from-uffi-type type :allocation)))
55 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
57 `(sb-alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
59 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
61 `(ff:allocate-fobject (list :array (quote ,(convert-from-uffi-type type :allocate)) ,size) :c)
63 `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type type :allocation))))
66 (defmacro free-foreign-object (obj)
68 `(alien:free-alien ,obj)
70 `(sb-alien:free-alien ,obj)
72 `(fli:free-foreign-object ,obj)
74 `(ff:free-fobject ,obj)
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)
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)))
93 #+mcl `(ccl:%null-ptr)
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))
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))
116 (defmacro deref-pointer-set (ptr type value)
117 `(setf (ccl:pref ,ptr ,(convert-from-uffi-type type :deref)) ,value))
120 (defsetf deref-pointer deref-pointer-set)
122 #+(or lispworks (and mcl (not openmcl))) ;; with LW, deref is a character
123 (defmacro ensure-char-character (obj)
126 #+(or allegro cmu sbcl scl openmcl)
127 (defmacro ensure-char-character (obj)
130 #+(or lispworks (and mcl (not openmcl)))
131 (defmacro ensure-char-integer (obj)
134 #+(or allegro cmu sbcl scl openmcl)
135 (defmacro ensure-char-integer (obj)
138 (defmacro pointer-address (obj)
140 `(system:sap-int (alien:alien-sap ,obj))
142 `(sb-sys:sap-int (sb-alien:alien-sap ,obj))
144 `(fli:pointer-address ,obj)
148 `(ccl:%ptr-to-int ,obj)
151 ;; TYPE is evaluated.
153 (defmacro with-foreign-object ((var type) &rest body)
154 #-(or cmu sbcl lispworks scl) ; default version
155 `(let ((,var (allocate-foreign-object ,type)))
158 (free-foreign-object ,var)))
160 (let ((obj (gensym)))
161 `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
162 (let ((,var (alien:addr ,obj)))
165 (let ((obj (gensym)))
166 `(sb-alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
167 (let ((,var (sb-alien:addr ,obj)))
170 `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
171 (eval type) :allocate)))
176 (defmacro with-foreign-objects (bindings &rest body)
178 `(with-foreign-object ,(car bindings)
179 (with-foreign-objects ,(cdr bindings)
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))
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)))
197 (defmacro with-foreign-object ((var type) &rest body)
198 `(with-foreign-objects ((,var ,type))