forked from ilitirit/manardb
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mop.lisp
196 lines (142 loc) · 6.04 KB
/
mop.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
;;;;; -*- mode: common-lisp; common-lisp-style: modern; coding: utf-8; -*-
;;;;;
(in-package :manardb)
(defclass mm-metaclass (standard-class)
((mtagmap
:accessor mm-metaclass-mtagmap
:initform nil)
(tag
:reader mm-metaclass-tag
:initform nil)
(len
:initform 0
:accessor mm-metaclass-len)
(default-instantiator
:initform nil)
(default-walker
:initform nil)
(walker
:initarg walker
:initform nil)
(instantiator
:accessor mm-instantiator
:initarg instantiator
:initform nil)
(allocator
:initform nil))
(:documentation "Metaclass for memory mapped objects."))
(defclass mm-object ()
((%ptr :type mptr :accessor %ptr :initarg %ptr))
(:documentation "Base class for all memory mapped objects."))
(declaim (ftype (function (mm-object) (mptr)) ptr))
(defun ptr (object)
(declare (type mm-object object))
(the mptr (%ptr object)))
#+(or)
(define-compiler-macro ptr (object)
`(the mptr (%ptr (the mm-object ,object))))
(defun mm-object-pointer (mm-object)
(mptr-pointer (ptr mm-object)))
(defmethod initialize-instance :around ((class mm-metaclass) &rest all-keys)
(ensure-inherits-from-mm-object class #'call-next-method all-keys))
(defmethod reinitialize-instance :around ((class mm-metaclass) &rest all-keys)
(ensure-inherits-from-mm-object class #'call-next-method all-keys))
(defun ensure-inherits-from-mm-object (class next-method all-keys)
(let ((parent (find-class 'mm-object)))
(labels ((inherits-from (classes)
(loop
for class in classes
thereis (or (subtypep class parent)
(inherits-from (class-direct-subclasses class))))))
(let ((all-keys (copy-list all-keys)))
(symbol-macrolet ((direct-superclasses (getf all-keys :direct-superclasses)))
(setf direct-superclasses (if (inherits-from direct-superclasses)
direct-superclasses
(cons parent direct-superclasses))))
(apply next-method class all-keys)))))
(deftype mm-slot-definition-reader ()
`(function (mm-object) t))
(deftype mm-slot-definition-writer ()
`(function (t mm-object) t))
(eval-when (:compile-toplevel :load-toplevel)
(defgeneric slot-definition-memory-mapped (slotd)
(:method (slotd) (declare (ignorable slotd)))))
(defgeneric slot-definition-mmap-pointer-p (slotd)
(:method (slotd) (declare (ignorable slotd))))
(defclass mm-slot-definition (slot-definition)
((persistent
:initarg :persistent
:reader slot-definition-memory-mapped-p
:reader slot-definition-memory-mapped
:initform t)))
(defclass mm-effective-slot-definition (mm-slot-definition standard-effective-slot-definition)
((offset
:initarg :offset
:reader mm-slot-definition-offset)
(mmap-pointer-p
:initform nil
:initarg :mmap-pointer-p
:accessor slot-definition-mmap-pointer-p)
(writer-function
:accessor slot-definition-writer-function)
(reader-function
:accessor slot-definition-reader-function)))
(defclass mm-direct-slot-definition (standard-direct-slot-definition mm-slot-definition)
())
(defmethod validate-superclass ((class mm-metaclass) (super standard-class))
"Memory mapped classes may inherit from ordinary classes."
t)
(defmethod validate-superclass ((class standard-class) (super mm-metaclass))
"Ordinary classes may NOT inherit from memory mapped classes."
nil)
(defmethod slot-definition-allocation ((slotd mm-slot-definition))
(if (slot-definition-memory-mapped slotd)
:memory
(call-next-method)))
(defmethod direct-slot-definition-class ((class mm-metaclass) &rest initargs)
(declare (ignore initargs))
(find-class 'mm-direct-slot-definition))
(defvar *mop-hack-effective-slot-definition-class* nil)
(defmethod effective-slot-definition-class ((class mm-metaclass) &rest initargs)
(declare (ignore initargs))
(or *mop-hack-effective-slot-definition-class* (call-next-method)))
(defmethod compute-slots :before ((class mm-metaclass))
(with-slots (len) class
(setf len 0)))
(defmethod compute-effective-slot-definition :around ((class mm-metaclass) name dslotds)
(declare (ignorable name))
(let ((dslotds (remove nil dslotds)))
(let ((last-dslot (first (last dslotds))))
(let ((*mop-hack-effective-slot-definition-class*
(when (slot-definition-memory-mapped last-dslot)
(find-class 'mm-effective-slot-definition))))
(let ((eslot (call-next-method)))
(when (slot-definition-memory-mapped eslot)
(setf (slot-definition-mmap-pointer-p eslot)
(loop
for dslot in dslotds
always (or (eq 'mptr (slot-definition-type dslot))
(eq 'mm-box (slot-definition-mm-type dslot)))))
(let ((type (slot-definition-type eslot)))
(with-slots (len) class
(setf (slot-value eslot 'offset) len)
(incf len (stored-type-size type)))))
eslot)))))
(defmethod slot-value-using-class ((class mm-metaclass) object
(slotd mm-effective-slot-definition))
(declare (ignorable class))
(funcall (the mm-slot-definition-reader (slot-definition-reader-function slotd))
object))
(defmethod (setf slot-value-using-class) (new-value (class mm-metaclass) object
(slotd mm-effective-slot-definition))
(declare (ignorable class))
(funcall (the mm-slot-definition-writer (slot-definition-writer-function slotd))
new-value object))
(defmethod slot-boundp-using-class ((class mm-metaclass) object
(slotd mm-effective-slot-definition))
(declare (ignorable class object slotd))
t)
(defmethod slot-makunbound-using-class ((class mm-metaclass) object
(slotd mm-effective-slot-definition))
(declare (ignorable class object slotd))
(error "Memory mapped slots cannot be unbound."))