-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathc-object.lisp
73 lines (61 loc) · 2.45 KB
/
c-object.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(in-package #:org.shirakumo.fraf.mixed)
(defvar *c-object-table* (make-hash-table :test 'eql))
(defun init ()
(flet ((load-library (lib)
(unless (cffi:foreign-library-loaded-p lib)
(cffi:load-foreign-library lib))))
#+windows (load-library 'mixed::winpthread)
#+windows (load-library 'mixed::gcc-s)
(load-library 'mixed::libmixed)))
(defmethod handle (thing)
(etypecase thing
(cffi:foreign-pointer thing)))
(defclass c-object ()
((handle :initarg :handle :initform NIL :accessor handle)))
(defmethod initialize-instance ((object c-object) &key)
(call-next-method)
(unless (handle object)
(let ((handle (allocate-handle object)))
(setf (handle object) handle)
(setf (pointer->object handle) object))))
(defmethod initialize-instance :around ((object c-object) &key handle)
(if handle
(call-next-method)
(with-cleanup-on-failure (free object)
(call-next-method))))
(defmethod free ((object c-object))
(error "Don't know how to free ~s" object))
(defmethod free :after ((object c-object))
(when (handle object)
(setf (pointer->object (handle object)) NIL)
(cffi:foreign-free (handle object))
(setf (handle object) NIL)))
(declaim (inline pointer->object (setf pointer->object)))
(defun pointer->object (pointer &optional errorp)
(let ((address (etypecase pointer
(cffi:foreign-pointer (cffi:pointer-address pointer))
((unsigned-byte 64) pointer))))
(or (gethash address *c-object-table*)
(when errorp
(error "No object associated with address ~x" address)))))
(defun (setf pointer->object) (object pointer)
(let ((address (etypecase pointer
(cffi:foreign-pointer (cffi:pointer-address pointer))
(integer pointer))))
(if object
(setf (gethash address *c-object-table*) object)
(remhash address *c-object-table*))))
(defmacro with-fetched-object ((obj &optional (pointer obj)) &body body)
`(let ((,obj (pointer->object ,pointer)))
(when ,obj ,@body)))
(defmacro with-objects (bindings &body body)
`(let ,(mapcar #'first bindings)
(unwind-protect
(progn
,@(loop for (var init) in bindings
collect `(setf ,var ,init))
(let ,(loop for (var) in bindings
collect `(,var ,var))
,@body))
,@(loop for (var) in bindings
collect `(when ,var (free ,var))))))