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

Contents of /struct.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (hide annotations)
Sun Oct 7 11:59:54 2012 UTC (18 months, 1 week ago) by rklochkov
File size: 8861 byte(s)
Fixed double free of object due to use of with-slots instead of setf (pointer object)
1 rklochkov 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 rklochkov 5 ; (break)
25 rklochkov 7 ;(format t "Free ~a ~a~%" class value)
26 rklochkov 1 (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 rklochkov 5 (setf (slot-value struct 'free-after) nil)
34 rklochkov 1 (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 rklochkov 8 '(:struct ,struct-name) ',x)))
55 rklochkov 1 (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 rklochkov 8 '(:struct ,struct-name) ',x)
62 rklochkov 1 val)))
63     (save-setter ,class-name ,x)))
64 rklochkov 8 (foreign-slot-names `(:struct ,struct-name))))))
65 rklochkov 1
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 rklochkov 12 (setf (foreign-slot-value res (list :struct class) slot)
103     val))))
104 rklochkov 1 (foreign-slot-names class))
105     res)
106 rklochkov 4 (pointer object)))
107 rklochkov 1
108     (defun struct->clos (class struct &optional object)
109 rklochkov 5 "Translates pointer STRUCT to object OBJECT (if not supplied, then to new
110     object).
111     I suppose, that by default it should convert data from pointer to struct.
112     Only exception is the presence of OBJECT with not boundp value"
113     (let ((%object (or object
114     (unless (null-pointer-p struct)
115     (make-instance class :pointer struct)))))
116     (when %object
117     (if (slot-boundp %object 'value)
118     (progn
119     (setf (slot-value %object 'value) nil)
120     (unless (null-pointer-p struct)
121     (dolist (slot (foreign-slot-names class))
122     (setf (getf (slot-value %object 'value) slot)
123 rklochkov 12 (foreign-slot-value struct (list :struct class) slot)))))
124 rklochkov 5 (setf (pointer %object) struct))
125     %object)))
126 rklochkov 1
127     (define-foreign-type cffi-struct (cffi-object freeable-out)
128     ()
129     (:actual-type :pointer))
130    
131 rklochkov 5 (defmethod free-sent-ptr ((type cffi-struct) ptr place)
132 rklochkov 12 (when (and (not (null-pointer-p ptr)) (slot-boundp place 'value))
133 rklochkov 5 (free-struct (object-class type) ptr)))
134    
135 rklochkov 9 (defmethod free-returned-ptr ((type cffi-struct) ptr)
136     (unless (null-pointer-p ptr)
137     (free-struct (object-class type) ptr)))
138 rklochkov 5
139 rklochkov 9
140 rklochkov 1 (defmethod foreign-type-size ((type cffi-struct))
141     "Return the size in bytes of a foreign typedef."
142     (foreign-type-size (object-class type)))
143    
144 rklochkov 8 (define-parse-method struct (class &rest rest)
145     (apply #'make-instance 'cffi-struct :class class rest))
146 rklochkov 1
147     (defun %class (type value)
148     (or (object-class type) (class-name (class-of value))))
149    
150     (defmethod copy-from-foreign ((type cffi-object) ptr place)
151 rklochkov 5 (when (slot-boundp place 'value)
152 rklochkov 1 (struct->clos (%class type place) ptr place)))
153    
154 rklochkov 5 ;; cffi-object is not tyoo. It is for use struct with object designator
155 rklochkov 1 (defmethod translate-to-foreign ((value struct) (type cffi-object))
156     (values (clos->new-struct (%class type value) value) value))
157    
158     (defmethod translate-from-foreign (value (type cffi-struct))
159     (struct->clos (object-class type) value))
160    
161     ;;; Allowed use with object designator
162 rklochkov 3 ;; object == (struct nil)
163 rklochkov 1
164    
165     ;; to allow using array of structs
166     (eval-when (:compile-toplevel :load-toplevel :execute)
167     (unless (get 'mem-ref 'struct)
168     (let ((old (fdefinition 'mem-ref)))
169     (fmakunbound 'mem-ref)
170     (defun mem-ref (ptr type &optional (offset 0))
171     (let ((ptype (cffi::parse-type type)))
172     (if (subtypep (type-of ptype) 'cffi-struct)
173     (translate-from-foreign (inc-pointer ptr offset) ptype)
174     (funcall old ptr type offset)))))
175     (setf (get 'mem-ref 'struct) t)))
176    
177    
178     (defun from-foreign (var type count)
179     "VAR - symbol; type - symbol or list -- CFFI type; count -- integer"
180     (if count
181     (let ((res (make-array count)))
182     (dotimes (i count)
183     (setf (aref res i)
184     (mem-aref var type i)))
185     res)
186     (mem-ref var type)))
187    
188    
189     (defmacro with-foreign-out ((var type &optional count) return-result &body body)
190     "The same as WITH-FOREIGN-OBJECT, but returns value of object"
191     (let ((value `(from-foreign ,var ,type ,count)))
192     `(with-foreign-object (,var ,type ,@(when count (list count)))
193     ,(if (eq return-result :ignore)
194     `(progn ,@body ,value)
195     `(let ((res ,@body))
196     ,(ecase return-result
197     (:if-success `(when res ,value))
198     (:return `(values res ,value))))))))
199    
200     (flet
201     ((make-with-foreign-outs (res-fun bindings return-result body)
202     (let ((values-form (mapcar (lambda (x)
203     (destructuring-bind
204     (var type &optional count) x
205     `(from-foreign ,var ,type ,count)))
206     bindings)))
207     `(with-foreign-objects ,bindings
208     ,(if (eq return-result :ignore)
209     `(progn ,@body (,res-fun ,@values-form))
210     `(let ((res ,@body))
211     ,(ecase return-result
212     (:if-success
213     `(when res (,res-fun ,@values-form)))
214     (:return
215     `(,res-fun res ,@values-form)))))))))
216    
217     (defmacro with-foreign-outs (bindings return-result &body body)
218     "The same as WITH-FOREIGN-OBJECTS, but returns (values ...)
219     of result and binded vars, RETURN-RESULT may be
220     :RETURN - return result and values
221     :IF-SUCCESS - return values if result t
222     :IGNORE - discard result"
223     (make-with-foreign-outs 'values bindings return-result body))
224    
225     (defmacro with-foreign-outs-list (bindings return-result &body body)
226     "The same as WITH-FOREIGN-OBJECTS, but returns list"
227     (make-with-foreign-outs 'list bindings return-result body)))

  ViewVC Help
Powered by ViewVC 1.1.5