author | Jan Vrany <jan.vrany@labware.com> |
Tue, 01 Jun 2021 20:19:13 +0100 | |
branch | jv |
changeset 25424 | 51bd8a6b196f |
parent 23107 | 40173e082cbc |
permissions | -rw-r--r-- |
1 | 1 |
" |
20041 | 2 |
COPYRIGHT (c) 1993,2015 by Claus Gittinger |
23107 | 3 |
COPYRIGHT (c) 2017 Jan Vrany |
20041 | 4 |
All Rights Reserved |
1 | 5 |
|
6 |
This software is furnished under a license and may be used |
|
7 |
only in accordance with the terms of that license and with the |
|
8 |
inclusion of the above copyright notice. This software may not |
|
9 |
be provided or otherwise made available to, or used by, any |
|
10 |
other person. No title to or ownership of the software is |
|
11 |
hereby transferred. |
|
12 |
" |
|
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
4608
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
4608
diff
changeset
|
14 |
|
18788 | 15 |
"{ NameSpace: Smalltalk }" |
16 |
||
20041 | 17 |
WeakIdentityDictionary subclass:#Registry |
18 |
instanceVariableNames:'' |
|
1286 | 19 |
classVariableNames:'' |
20 |
poolDictionaries:'' |
|
20041 | 21 |
category:'Collections-Weak' |
1 | 22 |
! |
23 |
||
1961 | 24 |
!Registry class methodsFor:'documentation'! |
1 | 25 |
|
88 | 26 |
copyright |
27 |
" |
|
20041 | 28 |
COPYRIGHT (c) 1993,2015 by Claus Gittinger |
23107 | 29 |
COPYRIGHT (c) 2017 Jan Vrany |
20041 | 30 |
All Rights Reserved |
88 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
1 | 41 |
documentation |
42 |
" |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
15350
diff
changeset
|
43 |
Registries provide an easy interface to using WeakArrays. |
69 | 44 |
A class, which wants to be informed of instance-death, can put a created object |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
15350
diff
changeset
|
45 |
into a registry. The registry will create an executor, which is a (shallow-)copy |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
15350
diff
changeset
|
46 |
of the object, and watch out for death of the original object. When it dies, |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6421
diff
changeset
|
47 |
the executor will be sent a #finalize message. |
69 | 48 |
The trick with the shallow copy is especially nice, you can think of it as |
49 |
being the original object which died. |
|
1 | 50 |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
15350
diff
changeset
|
51 |
All objects, which keep external resources (such as fileDescriptors, fonts, |
69 | 52 |
colormap-entries etc.) should be registered, so that the underlying resource |
53 |
can be freed when the object goes away. |
|
1 | 54 |
|
69 | 55 |
Of course, you too can use it to do whatever you need to do in case of the |
56 |
death of an object. |
|
159 | 57 |
|
20118 | 58 |
Registries use #executor to acquire the copy of the original, |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
15350
diff
changeset
|
59 |
this can be redefined in individual classes for faster copying |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
15350
diff
changeset
|
60 |
(typically, not all internal state, but only some device handles are needed for |
14352 | 61 |
finalization). If the to-be-registered object is large, this method may also |
317 | 62 |
return a stub (placeHolder) object. (i.e. there is no need for the copy to be |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6421
diff
changeset
|
63 |
of the same class as the original, as long as it implements #finalize and frees |
14352 | 64 |
the relevant OS resources. Some classes return a specialized private-class instance, |
65 |
which only holds on the handle and implements #finalize.) |
|
1286 | 66 |
Example uses are found in Form, Color, ExternalStream and Font |
159 | 67 |
|
1286 | 68 |
[author:] |
20041 | 69 |
Claus Gittinger |
1286 | 70 |
|
71 |
[see also:] |
|
20041 | 72 |
WeakArray WeakIdentityDictionary |
73 |
Font Form Color Cursor ExternalStream |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
15350
diff
changeset
|
74 |
|
1 | 75 |
" |
76 |
! ! |
|
77 |
||
78 |
!Registry methodsFor:'accessing'! |
|
79 |
||
80 |
contents |
|
20993 | 81 |
"return the collection of registered objects" |
1 | 82 |
|
20041 | 83 |
^ self keys. |
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
84 |
! ! |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
85 |
|
20041 | 86 |
!Registry methodsFor:'element disposal'! |
87 |
||
88 |
clearDeadSlots |
|
89 |
|wasBlocked executors| |
|
90 |
||
91 |
" |
|
92 |
have to block here - dispose may be done at a low priority |
|
93 |
from the background finalizer. If new items are added by a |
|
94 |
higher prio process, the dictionary might get corrupted otherwise |
|
95 |
" |
|
96 |
wasBlocked := OperatingSystem blockInterrupts. |
|
97 |
[ |
|
98 |
keyArray |
|
99 |
forAllDeadIndicesDo:[:idx | |
|
100 |
executors isNil ifTrue:[ |
|
101 |
executors := OrderedCollection new. |
|
102 |
]. |
|
103 |
executors add:(valueArray basicAt:idx). |
|
104 |
valueArray basicAt:idx put:nil. |
|
105 |
tally := tally - 1. |
|
106 |
] |
|
107 |
replacingCorpsesWith:DeletedEntry. |
|
108 |
] ensure:[ |
|
109 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
110 |
executors notNil ifTrue:[ |
|
111 |
executors do:[:eachExecutor| |
|
112 |
[ |
|
113 |
self informDispose:eachExecutor. |
|
114 |
] on:Error do:[:ex| |
|
115 |
Logger error:'Error %1 during finalization of: %2' with:ex description with:eachExecutor. |
|
116 |
ex suspendedContext fullPrintAllLevels:10. |
|
117 |
]. |
|
118 |
]. |
|
119 |
]. |
|
120 |
]. |
|
121 |
! |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
122 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
123 |
informDispose:someHandle |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6421
diff
changeset
|
124 |
"send a dispose message - this is sent to the executor, |
2181 | 125 |
since the original is already gone" |
126 |
||
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6421
diff
changeset
|
127 |
someHandle finalize |
1 | 128 |
! |
129 |
||
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
130 |
update:something with:aParameter from:changedObject |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
131 |
"an instance has been destroyed - look which one it was" |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
132 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
133 |
something == #ElementExpired ifTrue:[ |
20041 | 134 |
self clearDeadSlots. |
135 |
^ self. |
|
2181 | 136 |
]. |
20041 | 137 |
something == #earlyRestart ifTrue:[ |
138 |
self removeAll. |
|
139 |
]. |
|
140 |
super update:something with:aParameter from:changedObject. |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
141 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
142 |
"Created: 15.6.1996 / 15:24:41 / cg" |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
143 |
"Modified: 8.1.1997 / 14:05:02 / stefan" |
2669
7cf9ba3c7817
care for size changes while handling dispose-updates
Claus Gittinger <cg@exept.de>
parents:
2557
diff
changeset
|
144 |
"Modified: 2.6.1997 / 18:15:23 / cg" |
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
145 |
! ! |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
146 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
147 |
!Registry methodsFor:'enumerating'! |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
148 |
|
15007 | 149 |
detect:aBlock ifNone:exceptionValue |
20993 | 150 |
"detect a key, for which aBlock answers true" |
151 |
||
152 |
keyArray validElementsDo:[:eachElement | |
|
153 |
eachElement ~~ DeletedEntry ifTrue:[ |
|
154 |
|realObject| |
|
155 |
||
156 |
realObject := eachElement. |
|
157 |
eachElement == NilEntry ifTrue:[realObject := nil]. |
|
158 |
(aBlock value:realObject) ifTrue:[^ realObject]. |
|
159 |
]. |
|
15007 | 160 |
]. |
161 |
^ exceptionValue value |
|
162 |
! |
|
163 |
||
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
164 |
do:aBlock |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
165 |
"evaluate aBlock for each registered object" |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
166 |
|
20993 | 167 |
^ self keysDo:aBlock. |
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
168 |
! ! |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
169 |
|
2182 | 170 |
!Registry methodsFor:'private'! |
171 |
||
20041 | 172 |
findKeyOrNil:key |
173 |
"Look for the key in the receiver. |
|
174 |
If it is found, return the index, |
|
175 |
otherwise the index of the first unused slot. |
|
176 |
Grow the receiver, if key was not found, and no unused slots were present. |
|
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
177 |
|
20041 | 178 |
Warning: an empty slot MUST be filled by the sender - it is only to be sent |
179 |
by at:put: / add: - like methods. |
|
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
180 |
|
20041 | 181 |
Redefined to not nil values of expired keys here." |
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
182 |
|
20041 | 183 |
|index "{ Class:SmallInteger }" |
184 |
length "{ Class:SmallInteger }" |
|
185 |
startIndex probe |
|
186 |
delIndex "{ Class:SmallInteger }"| |
|
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
187 |
|
20041 | 188 |
(OperatingSystem blockInterrupts) ifFalse:[ |
189 |
"/ |
|
190 |
"/ may never be entered with interrupts enabled |
|
191 |
"/ |
|
192 |
OperatingSystem unblockInterrupts. |
|
193 |
self error:'unblocked call of findKeyOrNil'. |
|
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
194 |
]. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
195 |
|
20041 | 196 |
delIndex := 0. |
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
197 |
|
20041 | 198 |
length := keyArray basicSize. |
199 |
startIndex := index := self initialIndexForKey:key. |
|
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
200 |
|
20041 | 201 |
[ |
202 |
probe := keyArray basicAt:index. |
|
203 |
key == probe ifTrue:[^ index]. |
|
204 |
probe isNil ifTrue:[ |
|
205 |
delIndex == 0 ifTrue:[^ index]. |
|
206 |
keyArray basicAt:delIndex put:nil. |
|
207 |
^ delIndex |
|
208 |
]. |
|
2182 | 209 |
|
20041 | 210 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
211 |
delIndex := index |
|
212 |
]. |
|
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
213 |
|
20041 | 214 |
index == length ifTrue:[ |
215 |
index := 1 |
|
216 |
] ifFalse:[ |
|
217 |
index := index + 1 |
|
218 |
]. |
|
219 |
index == startIndex ifTrue:[ |
|
220 |
delIndex ~~ 0 ifTrue:[ |
|
221 |
keyArray basicAt:delIndex put:nil. |
|
222 |
^ delIndex |
|
223 |
]. |
|
224 |
self grow. |
|
225 |
length := keyArray basicSize. |
|
226 |
startIndex := index := self initialIndexForKey:key. |
|
227 |
]. |
|
228 |
] loop. |
|
4435
6461ba759d1b
changes to allow effective subclassing by CachingRegistry
Claus Gittinger <cg@exept.de>
parents:
4257
diff
changeset
|
229 |
|
20041 | 230 |
"Modified: 30.1.1997 / 15:04:34 / cg" |
231 |
"Modified: 1.10.1997 / 11:25:32 / stefan" |
|
20235 | 232 |
! |
233 |
||
234 |
findKeyOrNilOrDeletedEntry:key |
|
235 |
"Look for the key in the receiver. |
|
236 |
If it is found, return the index, |
|
237 |
otherwise the index of the first unused slot. |
|
238 |
Grow the receiver, if key was not found, and no unused slots were present. |
|
239 |
||
240 |
Redefined to not nil values of expired keys here." |
|
241 |
||
242 |
|index "{ Class:SmallInteger }" |
|
243 |
length "{ Class:SmallInteger }" |
|
244 |
startIndex probe |
|
245 |
delIndex "{ Class:SmallInteger }"| |
|
246 |
||
247 |
(OperatingSystem blockInterrupts) ifFalse:[ |
|
248 |
"/ |
|
249 |
"/ may never be entered with interrupts enabled |
|
250 |
"/ |
|
251 |
OperatingSystem unblockInterrupts. |
|
252 |
self error:'unblocked call of findKeyOrNil'. |
|
253 |
]. |
|
254 |
||
255 |
delIndex := 0. |
|
256 |
||
257 |
length := keyArray basicSize. |
|
258 |
startIndex := index := self initialIndexForKey:key. |
|
259 |
||
260 |
[ |
|
261 |
probe := keyArray basicAt:index. |
|
262 |
key == probe ifTrue:[^ index]. |
|
263 |
probe isNil ifTrue:[ |
|
264 |
delIndex == 0 ifTrue:[^ index]. |
|
265 |
^ delIndex |
|
266 |
]. |
|
267 |
||
268 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
|
269 |
delIndex := index |
|
270 |
]. |
|
271 |
||
272 |
index == length ifTrue:[ |
|
273 |
index := 1 |
|
274 |
] ifFalse:[ |
|
275 |
index := index + 1 |
|
276 |
]. |
|
277 |
index == startIndex ifTrue:[ |
|
278 |
delIndex ~~ 0 ifTrue:[ |
|
279 |
^ delIndex |
|
280 |
]. |
|
281 |
self grow. |
|
282 |
length := keyArray basicSize. |
|
283 |
startIndex := index := self initialIndexForKey:key. |
|
284 |
]. |
|
285 |
] loop. |
|
286 |
||
287 |
"Modified: 30.1.1997 / 15:04:34 / cg" |
|
288 |
"Modified: 1.10.1997 / 11:25:32 / stefan" |
|
22129
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
289 |
! |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
290 |
|
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
291 |
grow:newSize |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
292 |
"Grow the receiver to make space for at least newSize elements. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
293 |
To do this, we have to rehash into the new arrays. (which is done |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
294 |
by re-adding all elements to a new, empty key/value array pair). |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
295 |
|
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
296 |
Redefined here again to avoid higher-prio process to modify the |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
297 |
receiver while grwoing and to handle corpses. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
298 |
" |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
299 |
|
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
300 |
|
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
301 |
| key deletedEntry oldKeyArray oldValueArray n |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
302 |
oldSize "{ Class:SmallInteger }" |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
303 |
newIndex "{ Class:SmallInteger }" |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
304 |
wasBlocked |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
305 |
executors | |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
306 |
|
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
307 |
oldKeyArray := keyArray. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
308 |
oldValueArray := valueArray. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
309 |
|
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
310 |
n := self class goodSizeFrom:newSize. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
311 |
oldSize := oldKeyArray size. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
312 |
n == oldSize ifTrue:[^ self]. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
313 |
|
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
314 |
keyArray := self keyContainerOfSize:n. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
315 |
valueArray := self valueContainerOfSize:n. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
316 |
|
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
317 |
|
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
318 |
deletedEntry := DeletedEntry. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
319 |
wasBlocked := OperatingSystem blockInterrupts. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
320 |
1 to:oldSize do:[:index | |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
321 |
key := oldKeyArray basicAt:index. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
322 |
(key notNil and:[key ~~ deletedEntry]) ifTrue:[ |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
323 |
key class == SmallInteger ifTrue:[ |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
324 |
"/ Oops, we found a corpse, register it |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
325 |
"/ and continue. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
326 |
tally := tally - 1. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
327 |
executors isNil ifTrue:[ |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
328 |
executors := OrderedCollection new. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
329 |
]. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
330 |
executors add:(oldValueArray basicAt:index). |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
331 |
] ifFalse:[ |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
332 |
newIndex := self findNil:key. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
333 |
keyArray basicAt:newIndex put:key. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
334 |
valueArray basicAt:newIndex put:(oldValueArray basicAt:index). |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
335 |
]. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
336 |
] |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
337 |
]. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
338 |
wasBlocked ifFalse:[ OperatingSystem unblockInterrupts ]. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
339 |
executors notNil ifTrue:[ |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
340 |
executors do:[:eachExecutor| |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
341 |
[ |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
342 |
self informDispose:eachExecutor. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
343 |
] on:Error do:[:ex| |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
344 |
Logger error:'Error %1 during finalization of: %2' with:ex description with:eachExecutor. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
345 |
ex suspendedContext fullPrintAllLevels:10. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
346 |
]. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
347 |
]. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
348 |
]. |
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
349 |
|
697c819c7674
Fixed a baaad bug in `WeakIdentityDictionary` w.r.t growing
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21026
diff
changeset
|
350 |
"Created: / 31-07-2017 / 09:19:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
7810 | 351 |
! ! |
352 |
||
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
353 |
!Registry methodsFor:'registering objects'! |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
354 |
|
1 | 355 |
register:anObject |
356 |
"register anObject, so that a copy of it gets the disposed message |
|
2 | 357 |
when anObject dies (some time in the future)" |
1 | 358 |
|
6788 | 359 |
|executor| |
360 |
||
361 |
executor := anObject executor. |
|
362 |
executor notNil ifTrue:[ |
|
20041 | 363 |
self register:anObject as:executor. |
6788 | 364 |
]. |
375 | 365 |
! |
366 |
||
367 |
register:anObject as:aHandle |
|
368 |
"register anObject, so that I later receive informDispose: with aHandle |
|
369 |
(some time in the future)" |
|
370 |
||
20041 | 371 |
aHandle isNil ifTrue:[ |
372 |
self unregister:anObject. |
|
373 |
^ self. |
|
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
374 |
]. |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
375 |
|
20041 | 376 |
self at:anObject put:aHandle. |
1 | 377 |
! |
378 |
||
617 | 379 |
registerChange:anObject |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6421
diff
changeset
|
380 |
"a registered object has changed, create a new executor" |
617 | 381 |
|
20041 | 382 |
self register:anObject as:anObject executor. |
617 | 383 |
! |
384 |
||
1 | 385 |
unregister:anObject |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6421
diff
changeset
|
386 |
"remove registration of anObject, without telling the executor; |
759
908363ce8a32
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
630
diff
changeset
|
387 |
should be sent, if we are no more interested in destruction of |
1 | 388 |
anObject (i.e. it no longer holds external resources)." |
389 |
||
20041 | 390 |
self removeKey:anObject ifAbsent:[]. |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
1961
diff
changeset
|
391 |
! |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
1961
diff
changeset
|
392 |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
393 |
unregisterAllForWhich:aBlock |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
394 |
"remove registration of all entries, for which the argument block |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
395 |
evaluates to true. |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
396 |
should be sent, if we are no more interested in destruction of |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
397 |
a group of objects (i.e. it no longer holds external resources)." |
617 | 398 |
|
20041 | 399 |
|wasBlocked any| |
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
400 |
|
20041 | 401 |
wasBlocked := OperatingSystem blockInterrupts. |
402 |
keyArray validElementsDo:[:eachObject| |
|
403 |
(eachObject ~~ DeletedEntry and:[aBlock value:eachObject]) ifTrue:[ |
|
404 |
self safeRemoveKey:eachObject. |
|
405 |
any := true. |
|
18788 | 406 |
]. |
20041 | 407 |
]. |
408 |
any ifTrue:[ self possiblyShrink ]. |
|
409 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
9009
9da0a95a3994
addons and fixes for X11-deviceHandle fix
Claus Gittinger <cg@exept.de>
parents:
7810
diff
changeset
|
410 |
! |
9da0a95a3994
addons and fixes for X11-deviceHandle fix
Claus Gittinger <cg@exept.de>
parents:
7810
diff
changeset
|
411 |
|
9da0a95a3994
addons and fixes for X11-deviceHandle fix
Claus Gittinger <cg@exept.de>
parents:
7810
diff
changeset
|
412 |
unregisterAllForWhichHandle:aBlock |
20041 | 413 |
"remove registration of all handles, for which the argument block |
414 |
evaluates to true. |
|
415 |
should be sent, if we are no more interested in destruction of |
|
416 |
a group of objects (i.e. it no longer holds external resources)." |
|
9009
9da0a95a3994
addons and fixes for X11-deviceHandle fix
Claus Gittinger <cg@exept.de>
parents:
7810
diff
changeset
|
417 |
|
20041 | 418 |
|wasBlocked any| |
9009
9da0a95a3994
addons and fixes for X11-deviceHandle fix
Claus Gittinger <cg@exept.de>
parents:
7810
diff
changeset
|
419 |
|
20041 | 420 |
wasBlocked := OperatingSystem blockInterrupts. |
421 |
self keysAndValuesDo:[:eachObject :eachHandle| |
|
422 |
(eachObject class ~~ SmallInteger and:[aBlock value:eachHandle]) ifTrue:[ |
|
423 |
self safeRemoveKey:eachObject. |
|
424 |
any := true. |
|
18788 | 425 |
]. |
20041 | 426 |
]. |
427 |
any ifTrue:[ self possiblyShrink ]. |
|
428 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
617 | 429 |
! ! |
430 |
||
1961 | 431 |
!Registry class methodsFor:'documentation'! |
630 | 432 |
|
433 |
version |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
15350
diff
changeset
|
434 |
^ '$Header$' |
13012 | 435 |
! |
436 |
||
437 |
version_CVS |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
15350
diff
changeset
|
438 |
^ '$Header$' |
630 | 439 |
! ! |
15007 | 440 |