1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: aggregates.lisp
6 ;;;; Purpose: UFFI source to handle aggregate types
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
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 (defmacro def-enum (enum-name args &key (separator-string "#"))
22 "Creates a constants for a C type enum list, symbols are created
23 in the created in the current package. The symbol is the concatenation
24 of the enum-name name, separator-string, and field-name"
28 (declare (fixnum counter))
30 (let ((name (if (listp arg) (car arg) arg))
31 (value (if (listp arg)
33 (setq counter (cadr arg))
38 (setq name (intern (concatenate 'string
39 (symbol-name enum-name)
42 (push `(uffi:def-constant ,name ,value) constants)))
43 (setf cmds (append '(progn)
44 #+allegro `((ff:def-foreign-type ,enum-name :int))
45 #+lispworks `((fli:define-c-typedef ,enum-name :int))
46 #+(or cmu scl) `((alien:def-alien-type ,enum-name alien:signed))
47 #+sbcl `((sb-alien:define-alien-type ,enum-name sb-alien:signed))
48 #+(and mcl (not openmcl)) `((def-mcl-type ,enum-name :integer))
49 #+openmcl `((ccl::def-foreign-type ,enum-name :int))
50 (nreverse constants)))
54 (defmacro def-array-pointer (name-array type)
56 `(ff:def-foreign-type ,name-array
57 (:array ,(convert-from-uffi-type type :array)))
59 `(fli:define-c-typedef ,name-array
60 (:c-array ,(convert-from-uffi-type type :array)))
62 `(alien:def-alien-type ,name-array
63 (* ,(convert-from-uffi-type type :array)))
65 `(sb-alien:define-alien-type ,name-array
66 (* ,(convert-from-uffi-type type :array)))
67 #+(and mcl (not openmcl))
68 `(def-mcl-type ,name-array '(:array ,type))
70 `(ccl::def-foreign-type ,name-array (:array ,(convert-from-uffi-type type :array)))
73 (defun process-struct-fields (name fields &optional (variant nil))
75 (dolist (field fields)
76 (let* ((field-name (car field))
78 (def (append (list field-name)
79 (if (eq type :pointer-self)
80 #+(or cmu scl) `((* (alien:struct ,name)))
81 #+sbcl `((* (sb-alien:struct ,name)))
82 #+mcl `((:* (:struct ,name)))
83 #+lispworks `((:pointer ,name))
84 #-(or cmu sbcl scl mcl lispworks) `((* ,name))
85 `(,(convert-from-uffi-type type :struct))))))
87 (push (list def) processed)
88 (push def processed))))
89 (nreverse processed)))
92 (defmacro def-struct (name &rest fields)
94 `(alien:def-alien-type ,name (alien:struct ,name ,@(process-struct-fields name fields)))
96 `(sb-alien:define-alien-type ,name (sb-alien:struct ,name ,@(process-struct-fields name fields)))
98 `(ff:def-foreign-type ,name (:struct ,@(process-struct-fields name fields)))
100 `(fli:define-c-struct ,name ,@(process-struct-fields name fields))
101 #+(and mcl (not openmcl))
102 `(ccl:defrecord ,name ,@(process-struct-fields name fields))
104 `(ccl::def-foreign-type
106 (:struct ,name ,@(process-struct-fields name fields)))
110 (defmacro get-slot-value (obj type slot)
111 #+(or lispworks cmu sbcl scl) (declare (ignore type))
113 `(ff:fslot-value-typed ,type :c ,obj ,slot)
115 `(fli:foreign-slot-value ,obj ,slot)
117 `(alien:slot ,obj ,slot)
119 `(sb-alien:slot ,obj ,slot)
121 `(ccl:pref ,obj ,(read-from-string (format nil ":~a.~a" (keyword type) (keyword slot))))
125 (defmacro set-slot-value (obj type slot value) ;use setf to set values
126 `(setf (ccl:pref ,obj ,(read-from-string (format nil ":~a.~a" (keyword type) (keyword slot)))) ,value))
129 (defsetf get-slot-value set-slot-value)
132 (defmacro get-slot-pointer (obj type slot)
133 #+(or lispworks cmu sbcl scl) (declare (ignore type))
135 `(ff:fslot-value-typed ,type :c ,obj ,slot)
137 `(fli:foreign-slot-pointer ,obj ,slot)
139 `(alien:slot ,obj ,slot)
141 `(sb-alien:slot ,obj ,slot)
142 #+(and mcl (not openmcl))
143 `(ccl:%int-to-ptr (+ (ccl:%ptr-to-int ,obj) (the fixnum (ccl:field-info ,type ,slot))))
145 `(let ((field (ccl::%find-foreign-record-type-field ,type ,slot)))
146 (ccl:%int-to-ptr (+ (ccl:%ptr-to-int ,obj) (the fixnum (ccl::foreign-record-field-offset field)))))
149 ;; necessary to eval at compile time for openmcl to compile convert-from-foreign-usb8
151 (eval-when (:compile-toplevel :load-toplevel :execute)
152 ;; so we could allow '(:array :long) or deref with other type like :long only
154 (defun array-type (type)
157 (let ((type-list (if (eq (car type) 'quote) (nth 1 type) type)))
158 (when (and (listp type-list) (eq (car type-list) :array))
159 (setf result (cadr type-list)))))
163 (defmacro deref-array (obj type i)
164 "Returns a field from a row"
165 #+(or lispworks cmu sbcl scl) (declare (ignore type))
166 #+(or cmu scl) `(alien:deref ,obj ,i)
167 #+sbcl `(sb-alien:deref ,obj ,i)
168 #+lispworks `(fli:dereference ,obj :index ,i :copy-foreign-object nil)
169 #+allegro `(ff:fslot-value-typed (quote ,(convert-from-uffi-type type :type)) :c ,obj ,i)
171 (let* ((array-type (array-type type))
172 (local-type (convert-from-uffi-type array-type :allocation))
173 (element-size-in-bits (ccl::%foreign-type-or-record-size local-type :bits)))
174 (ccl::%foreign-access-form
176 (ccl::%foreign-type-or-record local-type)
177 `(* ,i ,element-size-in-bits)
179 #+(and mcl (not openmcl))
180 (let* ((array-type (array-type type))
181 (local-type (convert-from-uffi-type array-type :allocation))
182 (accessor (first (macroexpand `(ccl:pref obj ,local-type)))))
185 (* (the fixnum ,i) ,(size-of-foreign-type local-type))))
188 ; this expands to the %set-xx functions which has different params than %put-xx
189 #+(and mcl (not openmcl))
190 (defmacro deref-array-set (obj type i value)
191 (let* ((array-type (array-type type))
192 (local-type (convert-from-uffi-type array-type :allocation))
193 (accessor (first (macroexpand `(ccl:pref obj ,local-type))))
194 (settor (first (macroexpand `(setf (,accessor obj ,local-type) value)))))
197 (* (the fixnum ,i) ,(size-of-foreign-type local-type))
200 #+(and mcl (not openmcl))
201 (defsetf deref-array deref-array-set)
203 (defmacro def-union (name &rest fields)
205 `(ff:def-foreign-type ,name (:union ,@(process-struct-fields name fields)))
207 `(fli:define-c-union ,name ,@(process-struct-fields name fields))
209 `(alien:def-alien-type ,name (alien:union ,name ,@(process-struct-fields name fields)))
211 `(sb-alien:define-alien-type ,name (sb-alien:union ,name ,@(process-struct-fields name fields)))
212 #+(and mcl (not openmcl))
213 `(ccl:defrecord ,name (:variant ,@(process-struct-fields name fields t)))
215 `(ccl::def-foreign-type nil
216 (:union ,name ,@(process-struct-fields name fields)))
221 (defun convert-from-foreign-usb8 (s len)
222 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
224 (let ((a (make-array len :element-type '(unsigned-byte 8))))
227 (setf (aref a i) (uffi:deref-array s '(:array :unsigned-byte) i)))))
230 (sb-ext:without-package-locks
231 (let ((copy-fn (if (fboundp (intern "COPY-FROM-SYSTEM-AREA" "SB-KERNEL"))
232 (intern "COPY-FROM-SYSTEM-AREA" "SB-KERNEL")
233 (intern "COPY-UB8-FROM-SYSTEM" "SB-KERNEL"))))
234 (defun convert-from-foreign-usb8 (s len)
235 (let ((sap (sb-alien:alien-sap s)))
236 (declare (type sb-sys:system-area-pointer sap))
238 (declare (optimize (speed 3) (safety 0)))
239 (let ((result (make-array len :element-type '(unsigned-byte 8))))
240 (funcall copy-fn sap 0
241 result (* sb-vm:vector-data-offset
243 (* len sb-vm:n-byte-bits))
247 (defun convert-from-foreign-usb8 (s len)
248 (let ((sap (alien:alien-sap s)))
249 (declare (type system:system-area-pointer sap))
251 (declare (optimize (speed 3) (safety 0)))
252 (let ((result (make-array len :element-type '(unsigned-byte 8))))
253 (kernel:copy-from-system-area sap 0
254 result (* vm:vector-data-offset
256 (* len vm:byte-bits))