forked from skypher/cl-store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularities.lisp
263 lines (222 loc) · 9.83 KB
/
circularities.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;; See the file LICENCE for licence information.
;; Defines a special backend type which specializes various methods
;; in plumbing.lisp to make it nice and easy to
;; resolve possible circularities in objects.
;; Most of the work is done using the resolving-object
;; macro which knows how to handle an object which
;; is a referrer to a previously restored value.
;; Backends wanting to make use of this should take
;; a look at default-backend.lisp and xml-backend.lisp
;; paying special attention to the defbackend form and the
;; defrestore definitions for cons, array, simple-vector
;; array and hash-table.
;;
;; As a note this will ignore integers, symbols or characters
;; as referrer values. It will handle all other EQ number although
;; software depending on eq numbers are not conforming
;; programs according to the Hyperspec(notes in EQ).
(in-package :cl-store)
(defvar *check-for-circs* t)
(defstruct delay
value (completed nil))
(defmacro delay (&rest body)
`(make-delay :value #'(lambda () ,@body)))
(defun force (delay)
(unless (delay-completed delay)
(setf (delay-value delay) (funcall (the function (delay-value delay)))
(delay-completed delay) t))
(delay-value delay))
;; The definitions for setting and setting-hash sits in resolving-object.
(defmacro setting (place get)
"Resolve the possible referring object retrieved by GET and
set it into PLACE. Only usable within a resolving-object form."
(declare (ignore place get))
#+ecl nil
#-ecl (error "setting can only be used inside a resolving-object form."))
(defmacro setting-hash (getting-key getting-value)
"Insert the value retrieved by GETTING-VALUE with the key
retrieved by GETTING-KEY, resolving possible circularities.
Only usable within a resolving-object form."
(declare (ignore getting-key getting-value))
#+ecl nil
#-ecl (error "setting-hash can only be used inside a resolving-object form."))
(defmacro resolving-object ((var create) &body body)
"Execute body attempting to resolve circularities found in
form CREATE."
(with-gensyms (value key)
`(macrolet ((setting (place getting)
`(let ((,',value ,getting))
(if (referrer-p ,',value)
(if *check-for-circs*
(push (delay (setf ,place
(referred-value ,',value
*restored-values*)))
*need-to-fix*)
(restore-error "Found a circular values with *check-for-circs* = nil"))
(setf ,place ,',value))))
(setting-hash (getting-key getting-place)
`(let ((,',key ,getting-key))
(if (referrer-p ,',key)
(let ((,',value ,getting-place))
(unless *check-for-circs*
(restore-error "Found a circular values with *check-for-circs* = nil"))
(push (delay (setf (gethash (referred-value ,',key *restored-values*)
,',var)
(if (referrer-p ,',value)
(referred-value ,',value *restored-values*)
,',value)))
*need-to-fix*))
(setting (gethash ,',key ,',var) ,getting-place)))))
(let ((,var ,create))
,@body
,var))))
(defstruct referrer val)
(defun referred-value (referrer hash)
"Return the value REFERRER is meant to be by looking in HASH."
(gethash (referrer-val referrer)
hash))
(defclass resolving-backend (backend)
()
(:documentation "A backend which does the setup for resolving circularities."))
(declaim (type (or fixnum null) *stored-counter*))
(defvar *stored-counter*)
(defvar *stored-values*)
(defvar *store-hash-size* 50)
(defvar *restore-hash-size* 50)
(defvar *grouped-store-hash*)
(defvar *grouped-restore-hash*)
(defun create-serialize-hash (&key (size *store-hash-size*))
(make-hash-table :test #'eql :size size))
(defmacro with-serialization-unit ((&key store-hash restore-hash)
&body body)
"Executes body in a single serialization unit allowing various internal data
structures to be reused.
The keys store-hash and restore-hash are expected to be either nil or
hash-tables as produced by the function create-serialize-hash."
`(let ((*grouped-store-hash* (or ,store-hash
(create-serialize-hash :size *store-hash-size*)))
(*grouped-restore-hash* (or ,restore-hash
(create-serialize-hash :size *restore-hash-size*))))
,@body))
(defun get-store-hash ()
(when *check-for-circs*
(if (boundp '*grouped-store-hash*)
(clrhash *grouped-store-hash*)
(create-serialize-hash :size *store-hash-size*))))
(defun get-restore-hash ()
(when *check-for-circs*
(if (boundp '*grouped-restore-hash*)
(clrhash *grouped-restore-hash*)
(create-serialize-hash :size *restore-hash-size*))))
(defmethod backend-store :around ((backend resolving-backend) (place t) (obj t))
(call-next-method))
(defmethod backend-store ((backend resolving-backend) (place stream) (obj t))
"Store OBJ into PLACE. Does the setup for counters and seen values."
(declare (optimize speed (safety 1) (debug 0)))
(let ((*stored-counter* 0)
(*stored-values* (get-store-hash)))
(store-backend-code backend place)
(backend-store-object backend obj place)
obj))
(defun seen (obj)
"Has this object already been stored?"
(declare (optimize speed (safety 0) (debug 0)))
(incf *stored-counter*)
(gethash obj *stored-values*))
(defun update-seen (obj)
"Register OBJ as having been stored."
(declare (optimize speed (safety 0) (debug 0)))
(setf (gethash obj *stored-values*) *stored-counter*)
nil)
(deftype not-circ ()
"Type grouping integers and characters, which we
don't bother to check if they have been stored before"
'(or integer character (member t nil)))
(defun needs-checkp (obj)
"Do we need to check if this object has been stored before?"
(not (typep obj 'not-circ)))
(defgeneric store-referrer (backend obj place)
(:documentation "Store the number OBJ into PLACE as a referrer for BACKEND.")
(:method ((backend resolving-backend) (obj t) (place t))
(store-error "store-referrer must be specialized for backend ~(~A~)."
(name backend))))
(defun get-ref (obj)
(declare (optimize speed (safety 0) (debug 0)))
(if (needs-checkp obj)
(multiple-value-bind (val win) (seen obj)
(if (or val win)
val
(update-seen obj)))
nil))
(defmethod backend-store-object ((backend resolving-backend) (obj t) (place t))
"Store object if we have not seen this object before, otherwise retrieve
the referrer object for it and store that using store-referrer."
(aif (and *check-for-circs* (get-ref obj))
(store-referrer backend it place)
(internal-store-object backend obj place)))
;; Restoration.
(declaim (type (or fixnum null) *restore-counter*))
(defvar *restore-counter*)
(defvar *need-to-fix*)
(defvar *restored-values*)
(defmethod backend-restore ((backend resolving-backend) (place stream))
"Restore an object from PLACE using BACKEND. Does the setup for
various variables used by resolving-object."
(let ((*restore-counter* 0)
(*need-to-fix* nil)
(*restored-values* (get-restore-hash)))
(check-magic-number backend place)
(prog1
(backend-restore-object backend place)
(dolist (fn *need-to-fix*)
(force fn)))))
(defun update-restored (spot val)
(declare (optimize speed (safety 0) (debug 0)))
(setf (gethash spot *restored-values*) val))
(defun handle-normal (backend reader place)
(declare (optimize speed (safety 1) (debug 0)))
(let ((spot (incf *restore-counter*))
(vals (new-val (internal-restore-object backend reader place))))
(update-restored spot vals)
vals))
(defgeneric referrerp (backend reader)
(:method ((backend t) (reader t))
(error "referrerp must be specialized for backend ~A." (name backend))))
(defun handle-restore (place backend)
(declare (optimize speed (safety 1) (debug 0)))
(let ((reader (get-next-reader backend place)))
(declare (type symbol reader))
(cond ((referrerp backend reader)
(incf *restore-counter*)
(new-val (internal-restore-object backend reader place)))
((not (int-or-char-p backend reader))
(handle-normal backend reader place))
(t (new-val (internal-restore-object backend reader place))))))
(defmethod backend-restore-object ((backend resolving-backend) (place t))
"Retrieve a object from PLACE, does housekeeping for circularity fixing."
(declare (optimize speed (safety 1) (debug 0)))
(if *check-for-circs*
(handle-restore place backend)
(call-next-method)))
; This used to be called int-sym-or-char-p
; but was renamed to handle eq symbols (gensym's mainly).
; The basic concept is that we don't bother
; checking for circularities with integers or
; characters since these aren't gauranteed to be eq
; even if they are the same object.
; (notes for eq in CLHS).
(defgeneric int-or-char-p (backend fn)
(:method ((backend backend) (fn symbol))
"Is function FN registered to restore an integer or character in BACKEND."
(member fn '(integer character))))
(defun new-val (val)
"Tries to get a referred value to reduce unnecessary cirularity fixing."
(declare (optimize speed (safety 1) (debug 0)))
(if (referrer-p val)
(multiple-value-bind (new-val win) (referred-value val *restored-values*)
(if (or new-val win)
new-val
val))
val))
;; EOF