/[cffi-objects]/struct.lisp
ViewVC logotype

Contents of /struct.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (show annotations)
Tue Dec 25 01:10:43 2012 UTC (15 months, 3 weeks ago) by rklochkov
File size: 9879 byte(s)
Added initargs
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; struct.lisp --- CFFI wrapper for structs. We need to save on lisp
4 ;;; side only values of struct field, not pointer on
5 ;;; the struct to be able to garbage collect it
6 ;;;
7 ;;; Copyright (C) 2011, Roman Klochkov <kalimehtar@mail.ru>
8 ;;;
9
10 (in-package :cffi-objects)
11
12 (defclass struct (object)
13 ((value :documentation "plist ({field-name field-value}*)"))
14 (:documentation "If value bound, use it, else use pointer.
15 Struct may be used in OBJECT cffi-type or STRUCT cffi-type"))
16
17 (defgeneric new-struct (class)
18 (:method (class)
19 (foreign-alloc class)))
20
21 (defgeneric free-struct (class value)
22 (:method (class value)
23 (declare (ignore class))
24 ; (break)
25 ;(format t "Free ~a ~a~%" class value)
26 (foreign-free value)))
27
28 (defmethod gconstructor ((struct struct) &rest initargs
29 &key new-struct &allow-other-keys)
30 (let ((class-name (class-name (class-of struct)))
31 (pointer (null-pointer)))
32 (if new-struct
33 (setf pointer (new-struct class-name))
34 (progn
35 (setf (slot-value struct 'value) nil
36 (slot-value struct 'free-after) nil)))
37 (mapc
38 (lambda (field)
39 (let ((val (getf initargs (alexandria:make-keyword field))))
40 (if new-struct
41 (setf (foreign-slot-value pointer
42 (list :struct class-name) field) val)
43 (setf (getf (slot-value struct 'value) field) val))))
44 (foreign-slot-names (list :struct class-name)))
45 pointer))
46
47 (defun pair (maybe-pair)
48 (if (consp maybe-pair) maybe-pair (cons maybe-pair maybe-pair)))
49
50 (defmacro defcstruct-accessors (class)
51 "CLASS may be symbol = class-name = struct name,
52 or may be cons (class-name . struct-name)"
53 (destructuring-bind (class-name . struct-name) (pair class)
54 `(progn
55 (clear-setters ,class-name)
56 ,@(mapcar
57 (lambda (x)
58 `(progn
59 (unless (fboundp ',x)
60 (defgeneric ,x (,class-name)))
61 (defmethod ,x ((,class-name ,class-name))
62 (if (slot-boundp ,class-name 'value)
63 (getf (slot-value ,class-name 'value) ',x)
64 (foreign-slot-value (pointer ,class-name)
65 '(:struct ,struct-name) ',x)))
66 (unless (fboundp '(setf ,x))
67 (defgeneric (setf ,x) (val ,class-name)))
68 (defmethod (setf ,x) (val (,class-name ,class-name))
69 (if (slot-boundp ,class-name 'value)
70 (setf (getf (slot-value ,class-name 'value) ',x) val)
71 (setf (foreign-slot-value (pointer ,class-name)
72 '(:struct ,struct-name) ',x)
73 val)))
74 (save-setter ,class-name ,x)))
75 (foreign-slot-names `(:struct ,struct-name))))))
76
77 (defmacro defbitaccessors (class slot &rest fields)
78 (let ((pos 0))
79 (flet ((build-field (field)
80 (destructuring-bind (name type size) field
81 (prog1
82 `(progn
83 (unless (fboundp ',name)
84 (defgeneric ,name (,class)))
85 (defmethod ,name ((,class ,class))
86 (convert-from-foreign
87 (ldb (byte ,size ,pos) (slot-value ,class ',slot))
88 ,type))
89 (unless (fboundp '(setf ,name))
90 (defgeneric (setf ,name) (value ,class)))
91 (defmethod (setf ,name) (value (,class ,class))
92 (setf (ldb (byte ,size ,pos) (slot-value ,class ',slot))
93 (convert-to-foreign value ,type))))
94 (incf pos size)))))
95 (cons 'progn (mapcar #'build-field fields)))))
96
97
98 (defmacro defcstruct* (class &body body)
99 `(progn
100 (defclass ,class (struct) ())
101 (defcstruct ,class ,@body)
102 (defcstruct-accessors ,class)
103 (init-slots ,class)))
104
105 (defun clos->struct (class object struct)
106 (let ((default (gensym)))
107 (mapc (lambda (slot)
108 (let ((val (getf (slot-value object 'value) slot default)))
109 (unless (eq val default)
110 (setf (foreign-slot-value struct (list :struct class) slot)
111 val))))
112 (foreign-slot-names (list :struct class)))))
113
114 (defun clos->new-struct (class object)
115 (if (slot-boundp object 'value)
116 ;; use make-instance, not new-struct, because gconstructor
117 ;; may be redefined
118 (let ((res (make-instance class :new-struct t)))
119 (clos->struct class object (pointer res))
120 (pointer res))
121 (pointer object)))
122
123 (defun struct->clos (class struct &optional object)
124 "Translates pointer STRUCT to object OBJECT (if not supplied, then to new
125 object).
126 I suppose, that by default it should convert data from pointer to struct.
127 Only exception is the presence of OBJECT with not boundp value"
128 (let ((%object (or object
129 (unless (null-pointer-p struct)
130 (make-instance class)))))
131 (when %object
132 (if (slot-boundp %object 'value)
133 (progn
134 (setf (slot-value %object 'value) nil)
135 (unless (null-pointer-p struct)
136 (dolist (slot (foreign-slot-names (list :struct class)))
137 (setf (getf (slot-value %object 'value) slot)
138 (foreign-slot-value struct (list :struct class) slot)))))
139 (setf (pointer %object) struct))
140 %object)))
141
142 (define-foreign-type cffi-struct (cffi-object freeable-out)
143 ()
144 (:actual-type :pointer))
145
146 (defmethod free-sent-ptr ((type cffi-struct) ptr place)
147 (when (and (not (null-pointer-p ptr)) (slot-boundp place 'value))
148 (free-struct (object-class type) ptr)))
149
150 (defmethod free-returned-ptr ((type cffi-struct) ptr)
151 (unless (null-pointer-p ptr)
152 (free-struct (object-class type) ptr)))
153
154
155 (defmethod foreign-type-size ((type cffi-struct))
156 "Return the size in bytes of a foreign typedef."
157 (foreign-type-size (list :struct (object-class type))))
158
159 (define-parse-method struct (class &rest rest)
160 (apply #'make-instance 'cffi-struct :class class rest))
161
162 (defun %class (type value)
163 (or (object-class type) (class-name (class-of value))))
164
165 (defmethod copy-from-foreign ((type cffi-object) ptr place)
166 (when (slot-boundp place 'value)
167 (struct->clos (%class type place) ptr place)))
168
169 ;; cffi-object is not tyoo. It is for use struct with object designator
170 (defmethod translate-to-foreign ((value struct) (type cffi-object))
171 (values (clos->new-struct (%class type value) value) value))
172
173 (defmethod translate-from-foreign (value (type cffi-struct))
174 (struct->clos (object-class type) value))
175
176 ;;; Allowed use with object designator
177 ;; object == (struct nil)
178
179
180 ;; to allow using array of structs
181 ;; (eval-when (:compile-toplevel :load-toplevel :execute)
182 ;; (unless (get 'mem-ref 'struct)
183 ;; (let ((old (fdefinition 'mem-ref)))
184 ;; (fmakunbound 'mem-ref)
185 ;; (defun mem-ref (ptr type &optional (offset 0))
186 ;; (let ((ptype (cffi::parse-type type)))
187 ;; (if (subtypep (type-of ptype) 'cffi-struct)
188 ;; (translate-from-foreign (inc-pointer ptr offset) ptype)
189 ;; (funcall old ptr type offset)))))
190 ;; (setf (get 'mem-ref 'struct) t)))
191
192 (defun struct-p (type)
193 (and (consp type) (eq (car type) 'struct)))
194
195 (defun from-foreign (var type count)
196 "VAR - symbol; type - symbol or list -- CFFI type; count -- integer"
197 (if count
198 (let ((res (make-array count)))
199 (if (struct-p type)
200 (dotimes (i count)
201 (setf (aref res i)
202 (convert-from-foreign (mem-aptr var type i) type)))
203 (dotimes (i count)
204 (setf (aref res i)
205 (mem-aref var type i))))
206 res)
207 (mem-ref var type)))
208
209
210 (defmacro with-foreign-out ((var type &optional count) return-result &body body)
211 "The same as WITH-FOREIGN-OBJECT, but returns value of object"
212 (let ((value `(from-foreign ,var ,type ,count)))
213 `(with-foreign-object (,var ,type ,@(when count (list count)))
214 ,(if (eq return-result :ignore)
215 `(progn ,@body ,value)
216 `(let ((res ,@body))
217 ,(ecase return-result
218 (:if-success `(when res ,value))
219 (:return `(values res ,value))))))))
220
221 (flet
222 ((make-with-foreign-outs (res-fun bindings return-result body)
223 (let ((values-form (mapcar (lambda (x)
224 (destructuring-bind
225 (var type &optional count) x
226 `(from-foreign ,var ,type ,count)))
227 bindings)))
228 `(with-foreign-objects ,bindings
229 ,(if (eq return-result :ignore)
230 `(progn ,@body (,res-fun ,@values-form))
231 `(let ((res ,@body))
232 ,(ecase return-result
233 (:if-success
234 `(when res (,res-fun ,@values-form)))
235 (:return
236 `(,res-fun res ,@values-form)))))))))
237
238 (defmacro with-foreign-outs (bindings return-result &body body)
239 "The same as WITH-FOREIGN-OBJECTS, but returns (values ...)
240 of result and binded vars, RETURN-RESULT may be
241 :RETURN - return result and values
242 :IF-SUCCESS - return values if result t
243 :IGNORE - discard result"
244 (make-with-foreign-outs 'values bindings return-result body))
245
246 (defmacro with-foreign-outs-list (bindings return-result &body body)
247 "The same as WITH-FOREIGN-OBJECTS, but returns list"
248 (make-with-foreign-outs 'list bindings return-result body)))

  ViewVC Help
Powered by ViewVC 1.1.5