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

Contents of /struct.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (show annotations)
Sun Jul 29 15:15:41 2012 UTC (20 months, 3 weeks ago) by rklochkov
File size: 8770 byte(s)
Fixed memory leaks
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) &key new-struct &allow-other-keys)
29 (if new-struct
30 (new-struct (class-name (class-of struct)))
31 (progn
32 (setf (slot-value struct 'value) nil)
33 (setf (slot-value struct 'free-after) nil)
34 (null-pointer))))
35
36 (defun pair (maybe-pair)
37 (if (consp maybe-pair) maybe-pair (cons maybe-pair maybe-pair)))
38
39 (defmacro defcstruct-accessors (class)
40 "CLASS may be symbol = class-name = struct name,
41 or may be cons (class-name . struct-name)"
42 (destructuring-bind (class-name . struct-name) (pair class)
43 `(progn
44 (clear-setters ,class-name)
45 ,@(mapcar
46 (lambda (x)
47 `(progn
48 (unless (fboundp ',x)
49 (defgeneric ,x (,class-name)))
50 (defmethod ,x ((,class-name ,class-name))
51 (if (slot-boundp ,class-name 'value)
52 (getf (slot-value ,class-name 'value) ',x)
53 (foreign-slot-value (pointer ,class-name)
54 ',struct-name ',x)))
55 (unless (fboundp '(setf ,x))
56 (defgeneric (setf ,x) (val ,class-name)))
57 (defmethod (setf ,x) (val (,class-name ,class-name))
58 (if (slot-boundp ,class-name 'value)
59 (setf (getf (slot-value ,class-name 'value) ',x) val)
60 (setf (foreign-slot-value (pointer ,class-name)
61 ',struct-name ',x)
62 val)))
63 (save-setter ,class-name ,x)))
64 (foreign-slot-names struct-name)))))
65
66 (defmacro defbitaccessors (class slot &rest fields)
67 (let ((pos 0))
68 (flet ((build-field (field)
69 (destructuring-bind (name type size) field
70 (prog1
71 `(progn
72 (unless (fboundp ',name)
73 (defgeneric ,name (,class)))
74 (defmethod ,name ((,class ,class))
75 (convert-from-foreign
76 (ldb (byte ,size ,pos) (slot-value ,class ',slot))
77 ,type))
78 (unless (fboundp '(setf ,name))
79 (defgeneric (setf ,name) (value ,class)))
80 (defmethod (setf ,name) (value (,class ,class))
81 (setf (ldb (byte ,size ,pos) (slot-value ,class ',slot))
82 (convert-to-foreign value ,type))))
83 (incf pos size)))))
84 (cons 'progn (mapcar #'build-field fields)))))
85
86
87 (defmacro defcstruct* (class &body body)
88 `(progn
89 (defclass ,class (struct) ())
90 (defcstruct ,class ,@body)
91 (defcstruct-accessors ,class)
92 (init-slots ,class)))
93
94
95 (defun clos->new-struct (class object)
96 (if (slot-boundp object 'value)
97 (let ((res (new-struct class))
98 (default (gensym)))
99 (mapc (lambda (slot)
100 (let ((val (getf (slot-value object 'value) slot default)))
101 (unless (eq val default)
102 (setf (foreign-slot-value res class slot) val))))
103 (foreign-slot-names class))
104 res)
105 (pointer object)))
106
107 (defun struct->clos (class struct &optional object)
108 "Translates pointer STRUCT to object OBJECT (if not supplied, then to new
109 object).
110 I suppose, that by default it should convert data from pointer to struct.
111 Only exception is the presence of OBJECT with not boundp value"
112 (let ((%object (or object
113 (unless (null-pointer-p struct)
114 (make-instance class :pointer struct)))))
115 (when %object
116 (if (slot-boundp %object 'value)
117 (progn
118 (setf (slot-value %object 'value) nil)
119 (unless (null-pointer-p struct)
120 (dolist (slot (foreign-slot-names class))
121 (setf (getf (slot-value %object 'value) slot)
122 (foreign-slot-value struct class slot)))))
123 (setf (pointer %object) struct))
124 %object)))
125
126 (define-foreign-type cffi-struct (cffi-object freeable-out)
127 ()
128 (:actual-type :pointer))
129
130 (defmethod free-ptr ((type cffi-struct) ptr)
131 (free-struct (object-class type) ptr))
132
133 (defmethod free-sent-ptr ((type cffi-struct) ptr place)
134 (when (and (slot-boundp place 'value) (not (null-pointer-p ptr)))
135 (free-struct (object-class type) ptr)))
136
137
138 (defmethod foreign-type-size ((type cffi-struct))
139 "Return the size in bytes of a foreign typedef."
140 (foreign-type-size (object-class type)))
141
142 (define-parse-method struct (class &key (free :no-transfer) out)
143 (make-instance 'cffi-struct
144 :class class :free free :out out))
145
146 (defun %class (type value)
147 (or (object-class type) (class-name (class-of value))))
148
149 (defmethod copy-from-foreign ((type cffi-object) ptr place)
150 (when (slot-boundp place 'value)
151 (struct->clos (%class type place) ptr place)))
152
153 ;; cffi-object is not tyoo. It is for use struct with object designator
154 (defmethod translate-to-foreign ((value struct) (type cffi-object))
155 (values (clos->new-struct (%class type value) value) value))
156
157 (defmethod translate-from-foreign (value (type cffi-struct))
158 (struct->clos (object-class type) value))
159
160 ;;; Allowed use with object designator
161 ;; object == (struct nil)
162
163
164 ;; to allow using array of structs
165 (eval-when (:compile-toplevel :load-toplevel :execute)
166 (unless (get 'mem-ref 'struct)
167 (let ((old (fdefinition 'mem-ref)))
168 (fmakunbound 'mem-ref)
169 (defun mem-ref (ptr type &optional (offset 0))
170 (let ((ptype (cffi::parse-type type)))
171 (if (subtypep (type-of ptype) 'cffi-struct)
172 (translate-from-foreign (inc-pointer ptr offset) ptype)
173 (funcall old ptr type offset)))))
174 (setf (get 'mem-ref 'struct) t)))
175
176
177 (defun from-foreign (var type count)
178 "VAR - symbol; type - symbol or list -- CFFI type; count -- integer"
179 (if count
180 (let ((res (make-array count)))
181 (dotimes (i count)
182 (setf (aref res i)
183 (mem-aref var type i)))
184 res)
185 (mem-ref var type)))
186
187
188 (defmacro with-foreign-out ((var type &optional count) return-result &body body)
189 "The same as WITH-FOREIGN-OBJECT, but returns value of object"
190 (let ((value `(from-foreign ,var ,type ,count)))
191 `(with-foreign-object (,var ,type ,@(when count (list count)))
192 ,(if (eq return-result :ignore)
193 `(progn ,@body ,value)
194 `(let ((res ,@body))
195 ,(ecase return-result
196 (:if-success `(when res ,value))
197 (:return `(values res ,value))))))))
198
199 (flet
200 ((make-with-foreign-outs (res-fun bindings return-result body)
201 (let ((values-form (mapcar (lambda (x)
202 (destructuring-bind
203 (var type &optional count) x
204 `(from-foreign ,var ,type ,count)))
205 bindings)))
206 `(with-foreign-objects ,bindings
207 ,(if (eq return-result :ignore)
208 `(progn ,@body (,res-fun ,@values-form))
209 `(let ((res ,@body))
210 ,(ecase return-result
211 (:if-success
212 `(when res (,res-fun ,@values-form)))
213 (:return
214 `(,res-fun res ,@values-form)))))))))
215
216 (defmacro with-foreign-outs (bindings return-result &body body)
217 "The same as WITH-FOREIGN-OBJECTS, but returns (values ...)
218 of result and binded vars, RETURN-RESULT may be
219 :RETURN - return result and values
220 :IF-SUCCESS - return values if result t
221 :IGNORE - discard result"
222 (make-with-foreign-outs 'values bindings return-result body))
223
224 (defmacro with-foreign-outs-list (bindings return-result &body body)
225 "The same as WITH-FOREIGN-OBJECTS, but returns list"
226 (make-with-foreign-outs 'list bindings return-result body)))

  ViewVC Help
Powered by ViewVC 1.1.5