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.cl,v 1.24 2002/09/30 07:51:01 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 (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)
28 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
30 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
32 `(ff:allocate-fobject ,(convert-from-uffi-type type :allocate) :c)
34 `(new-ptr ,(size-of-foreign-type (convert-from-uffi-type type :allocation)))
38 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
40 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
42 `(ff:allocate-fobject '(:array ,(convert-from-uffi-type (eval type) :allocate) ,(eval size)) :c)
44 `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type type :allocation))))
47 (defmacro free-foreign-object (obj)
49 `(alien:free-alien ,obj)
51 `(fli:free-foreign-object ,obj)
53 `(ff:free-fobject ,obj)
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)
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)))
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)
79 (defmacro make-null-pointer (type)
80 #+(or allegro cmu mcl) (declare (ignore type))
82 #+cmu `(system:int-sap 0)
84 #+lispworks `(fli:make-pointer :address 0 :type ,type)
85 #+mcl `(ccl:%null-ptr)
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))
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))
106 (defmacro deref-pointer-set (ptr type value)
107 `(setf (ccl:pref ,ptr ,(convert-from-uffi-type type :deref)) ,value))
110 (defsetf deref-pointer deref-pointer-set)
112 #+(or lispworks (and mcl (not openmcl))) ;; with LW, deref is a character
113 (defmacro ensure-char-character (obj)
116 #+(or allegro cmu openmcl)
117 (defmacro ensure-char-character (obj)
120 #+(or lispworks (and mcl (not openmcl)))
121 (defmacro ensure-char-integer (obj)
124 #+(or allegro cmu openmcl)
125 (defmacro ensure-char-integer (obj)
128 (defmacro pointer-address (obj)
130 `(system:sap-int (alien:alien-sap ,obj))
132 `(fli:pointer-address ,obj)
136 `(ccl:%ptr-to-int ,obj)
139 ;; TYPE is evaluated.
141 (defmacro with-foreign-object ((var type) &rest body)
142 #-(or cmu lispworks) ; default version
143 `(let ((,var (allocate-foreign-object ,type)))
146 (free-foreign-object ,var)))
148 (let ((obj (gensym)))
149 `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
150 (let ((,var (alien:addr ,obj)))
153 `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
154 (eval type) :allocate)))
159 (defmacro with-foreign-object ((var type) &rest body)
160 `(with-foreign-objects ((,var ,type))
164 (defmacro with-foreign-objects (bindings &rest body)
166 `(with-foreign-object ,(car bindings)
167 (with-foreign-objects ,(cdr bindings)
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))
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)))