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.1 2002/09/30 10:02:36 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 #+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)))
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 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
46 `(ff:allocate-fobject ,(convert-from-uffi-type type :allocate) :c)
48 `(new-ptr ,(size-of-foreign-type (convert-from-uffi-type type :allocation)))
52 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
54 `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
56 `(ff:allocate-fobject '(:array ,(convert-from-uffi-type (eval type) :allocate) ,(eval size)) :c)
58 `(new-ptr (* ,size ,(size-of-foreign-type (convert-from-uffi-type type :allocation))))
61 (defmacro free-foreign-object (obj)
63 `(alien:free-alien ,obj)
65 `(fli:free-foreign-object ,obj)
67 `(ff:free-fobject ,obj)
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)
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-objects (bindings &rest body)
161 `(with-foreign-object ,(car bindings)
162 (with-foreign-objects ,(cdr bindings)
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))
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)))
180 (defmacro with-foreign-object ((var type) &rest body)
181 `(with-foreign-objects ((,var ,type))