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 ,(intern (concatenate 'string (symbol-name type) "." (symbol-name slot))
126 (defmacro set-slot-value (obj type slot value) ;use setf to set values
127 `(setf (ccl:pref ,obj ,(read-from-string (format nil ":~a.~a" (keyword type) (keyword slot)))) ,value))
130 (defsetf get-slot-value set-slot-value)
133 (defmacro get-slot-pointer (obj type slot)
134 #+(or lispworks cmu sbcl scl) (declare (ignore type))
136 `(ff:fslot-value-typed ,type :c ,obj ,slot)
138 `(fli:foreign-slot-pointer ,obj ,slot)
140 `(alien:slot ,obj ,slot)
142 `(sb-alien:slot ,obj ,slot)
143 #+(and mcl (not openmcl))
144 `(ccl:%int-to-ptr (+ (ccl:%ptr-to-int ,obj) (the fixnum (ccl:field-info ,type ,slot))))
146 `(let ((field (ccl::%find-foreign-record-type-field ,type ,slot)))
147 (ccl:%int-to-ptr (+ (ccl:%ptr-to-int ,obj) (the fixnum (ccl::foreign-record-field-offset field)))))
150 ; so we could allow '(:array :long) or deref with other type like :long only
152 (defun array-type (type)
155 (let ((type-list (if (eq (car type) 'quote) (nth 1 type) type)))
156 (when (and (listp type-list) (eq (car type-list) :array))
157 (setf result (cadr type-list)))))
161 (defmacro deref-array (obj type i)
162 "Returns a field from a row"
163 #+(or lispworks cmu sbcl scl) (declare (ignore type))
164 #+(or cmu scl) `(alien:deref ,obj ,i)
165 #+sbcl `(sb-alien:deref ,obj ,i)
166 #+lispworks `(fli:dereference ,obj :index ,i :copy-foreign-object nil)
167 #+allegro `(ff:fslot-value-typed (quote ,(convert-from-uffi-type type :type)) :c ,obj ,i)
169 (let* ((array-type (array-type type))
170 (local-type (convert-from-uffi-type array-type :allocation))
171 (element-size-in-bits (ccl::%foreign-type-or-record-size local-type :bits)))
172 (ccl::%foreign-access-form
174 (ccl::%foreign-type-or-record local-type)
175 `(* ,i ,element-size-in-bits)
177 #+(and mcl (not openmcl))
178 (let* ((array-type (array-type type))
179 (local-type (convert-from-uffi-type array-type :allocation))
180 (accessor (first (macroexpand `(ccl:pref obj ,local-type)))))
183 (* (the fixnum ,i) ,(size-of-foreign-type local-type))))
186 ; this expands to the %set-xx functions which has different params than %put-xx
187 #+(and mcl (not openmcl))
188 (defmacro deref-array-set (obj type i value)
189 (let* ((array-type (array-type type))
190 (local-type (convert-from-uffi-type array-type :allocation))
191 (accessor (first (macroexpand `(ccl:pref obj ,local-type))))
192 (settor (first (macroexpand `(setf (,accessor obj ,local-type) value)))))
195 (* (the fixnum ,i) ,(size-of-foreign-type local-type))
198 #+(and mcl (not openmcl))
199 (defsetf deref-array deref-array-set)
201 (defmacro def-union (name &rest fields)
203 `(ff:def-foreign-type ,name (:union ,@(process-struct-fields name fields)))
205 `(fli:define-c-union ,name ,@(process-struct-fields name fields))
207 `(alien:def-alien-type ,name (alien:union ,name ,@(process-struct-fields name fields)))
209 `(sb-alien:define-alien-type ,name (sb-alien:union ,name ,@(process-struct-fields name fields)))
210 #+(and mcl (not openmcl))
211 `(ccl:defrecord ,name (:variant ,@(process-struct-fields name fields t)))
213 `(ccl::def-foreign-type nil
214 (:union ,name ,@(process-struct-fields name fields)))
219 (defun convert-from-foreign-usb8 (s len)
220 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
222 (let ((a (make-array len :element-type '(unsigned-byte 8))))
225 (setf (aref a i) (uffi:deref-array s '(:array :unsigned-byte) i)))))
228 (defun convert-from-foreign-usb8 (s len)
229 (declare (type sb-sys:system-area-pointer sap))
231 (declare (optimize (speed 3) (safety 0)))
232 (let ((result (make-array len :element-type '(unsiged-byte 8))))
233 (sb-kernel:copy-from-system-area s 0
234 result (* sb-vm:vector-data-offset
236 (* len sb-vm:n-byte-bits))
240 (defun convert-from-foreign-usb8 (s le)
241 (declare (type system:system-area-pointer sap))
243 (declare (optimize (speed 3) (safety 0)))
244 (let ((result (make-array len :element-type '(unsiged-byte 8))))
245 (kernel:copy-from-system-area s 0
246 result (* vm:vector-data-offset
248 (* len vm:byte-bits))