author | Claus Gittinger <cg@exept.de> |
Wed, 16 Apr 1997 15:22:10 +0200 | |
changeset 2552 | c4eae9ddfd0a |
parent 2522 | 4e9a6a3fb8c5 |
child 2557 | 903104e6e26b |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
217 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Object subclass:#Registry |
|
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
14 |
instanceVariableNames:'registeredObjects handleArray tally indexTable' |
1286 | 15 |
classVariableNames:'' |
16 |
poolDictionaries:'' |
|
17 |
category:'System-Support' |
|
1 | 18 |
! |
19 |
||
1961 | 20 |
!Registry class methodsFor:'documentation'! |
1 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
217 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
1 | 36 |
documentation |
37 |
" |
|
69 | 38 |
Registries provide an easy interface to using WeakArrays. |
39 |
A class, which wants to be informed of instance-death, can put a created object |
|
159 | 40 |
into a registry. The registry will create a (shallow-)copy of the object, and |
41 |
watch out for death of the original object. When it dies, the copy will |
|
1961 | 42 |
be sent a #disposed-message. |
69 | 43 |
The trick with the shallow copy is especially nice, you can think of it as |
44 |
being the original object which died. |
|
1 | 45 |
|
69 | 46 |
All objects, which keep external resources (such as fileDescriptors, fonts, |
47 |
colormap-entries etc.) should be registered, so that the underlying resource |
|
48 |
can be freed when the object goes away. |
|
1 | 49 |
|
69 | 50 |
Of course, you too can use it to do whatever you need to do in case of the |
51 |
death of an object. |
|
159 | 52 |
|
53 |
Registries use #shallowCopyForFinalization to aquire the copy of the original, |
|
54 |
this can be redefined in registered classes for faster copying |
|
55 |
(typically, not all internal state but only some device handles are needed for |
|
317 | 56 |
finalization). I if the to-be-registered object is large, this method may also |
57 |
return a stub (placeHolder) object. (i.e. there is no need for the copy to be |
|
58 |
of the same class as the original, as long as it implements disposed and frees |
|
59 |
the relevant OS resources ...) |
|
1286 | 60 |
Example uses are found in Form, Color, ExternalStream and Font |
159 | 61 |
|
1286 | 62 |
[author:] |
63 |
Claus Gittinger |
|
64 |
||
65 |
[see also:] |
|
66 |
WeakArray WeakIdentityDictionary WeakIdentitySet |
|
67 |
Font Form Color Cursor ExternalStream |
|
68 |
||
1 | 69 |
" |
70 |
! ! |
|
71 |
||
72 |
!Registry methodsFor:'accessing'! |
|
73 |
||
74 |
contents |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
75 |
"return the collection of registered objects. |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
76 |
Warning: this returns a weak collection." |
1 | 77 |
|
78 |
^ registeredObjects |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
79 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
80 |
"Modified: 16.1.1997 / 16:40:46 / cg" |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
81 |
! ! |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
82 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
83 |
!Registry methodsFor:'dispose handling'! |
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 |
informDispose:someHandle |
2181 | 86 |
"send a dispose message - this is sent to the phantom, |
87 |
since the original is already gone" |
|
88 |
||
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
89 |
someHandle disposed |
2181 | 90 |
|
91 |
"Modified: 16.1.1997 / 17:23:46 / cg" |
|
1 | 92 |
! |
93 |
||
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
94 |
update:something with:aParameter from:changedObject |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
95 |
"an instance has been destroyed - look which one it was" |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
96 |
|
2181 | 97 |
|phantom |
98 |
dstIdx "{ Class: SmallInteger }" |
|
99 |
sz "{ Class: SmallInteger }" |
|
100 |
newObjects newHandles o wasBlocked |
|
101 |
myHandleArray myRegisteredObjects| |
|
102 |
||
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
103 |
something == #ElementExpired ifTrue:[ |
2181 | 104 |
"/ |
105 |
"/ use local refs, in case I shrink in an interrupt |
|
106 |
"/ |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
107 |
|
2181 | 108 |
myHandleArray := handleArray. |
109 |
myRegisteredObjects := registeredObjects. |
|
110 |
||
111 |
sz := myHandleArray size. |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
112 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
113 |
1 to:sz do:[:index | |
2181 | 114 |
|
115 |
o := myRegisteredObjects at:index. |
|
116 |
o notNil ifTrue:[ |
|
117 |
o == 0 ifTrue:[ |
|
2522 | 118 |
phantom := myHandleArray at:index. |
2181 | 119 |
myRegisteredObjects at:index put:nil. |
120 |
phantom notNil ifTrue:[ |
|
121 |
myHandleArray at:index put:nil. |
|
2182 | 122 |
tally := tally - 1. |
2181 | 123 |
self informDispose:phantom. |
124 |
] |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
125 |
] |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
126 |
] |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
127 |
]. |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
128 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
129 |
(sz > 50 and:[tally < (sz // 2)]) ifTrue:[ |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
130 |
"/ shrink |
2182 | 131 |
self resize |
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
132 |
] |
2181 | 133 |
] ifFalse:[ |
134 |
something == #earlyRestart ifTrue:[ |
|
135 |
handleArray notNil ifTrue:[ |
|
136 |
handleArray atAllPut:nil. |
|
137 |
] |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
138 |
] |
2181 | 139 |
]. |
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
140 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
141 |
"Created: 15.6.1996 / 15:24:41 / cg" |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
142 |
"Modified: 8.1.1997 / 14:05:02 / stefan" |
2522 | 143 |
"Modified: 3.4.1997 / 17:59:11 / cg" |
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
144 |
! ! |
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 |
!Registry methodsFor:'enumerating'! |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
147 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
148 |
do:aBlock |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
149 |
"evaluate aBlock for each registered object" |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
150 |
|
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
151 |
registeredObjects notNil ifTrue:[ |
2189 | 152 |
registeredObjects validElementsDo:aBlock |
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
153 |
] |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
154 |
! ! |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
155 |
|
2182 | 156 |
!Registry methodsFor:'private'! |
157 |
||
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
158 |
repairTally |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
159 |
|sz "{ Class: SmallInteger }" |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
160 |
cnt "{ Class: SmallInteger }" |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
161 |
phantom wasBlocked| |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
162 |
|
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
163 |
wasBlocked := OperatingSystem blockInterrupts. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
164 |
|
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
165 |
indexTable := WeakIdentityDictionary new. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
166 |
|
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
167 |
sz := registeredObjects size. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
168 |
cnt := 0. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
169 |
|
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
170 |
1 to:sz do:[:index | |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
171 |
((phantom := registeredObjects at:index) notNil |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
172 |
and:[phantom ~~ 0]) ifTrue:[ |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
173 |
indexTable at:phantom put:index. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
174 |
cnt := cnt + 1. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
175 |
] ifFalse:[ |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
176 |
handleArray at:index put:nil. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
177 |
registeredObjects at:index put:nil. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
178 |
] |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
179 |
]. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
180 |
|
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
181 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
182 |
|
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
183 |
"Created: 6.3.1997 / 22:31:09 / cg" |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
184 |
! |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
185 |
|
2182 | 186 |
resize |
187 |
|sz "{ Class: SmallInteger }" |
|
188 |
dstIndex "{ Class: SmallInteger }" |
|
189 |
realNewSize "{ Class: SmallInteger }" |
|
190 |
newObjects newHandles wasBlocked |
|
191 |
phantom| |
|
192 |
||
193 |
sz := registeredObjects size. |
|
194 |
||
195 |
(sz > 50 and:[tally < (sz // 2)]) ifTrue:[ |
|
196 |
"/ shrink |
|
197 |
||
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
198 |
wasBlocked := OperatingSystem blockInterrupts. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
199 |
|
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
200 |
sz := registeredObjects size. |
2182 | 201 |
realNewSize := tally * 3 // 2. |
202 |
newObjects := WeakArray new:realNewSize. |
|
203 |
newHandles := Array new:realNewSize. |
|
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
204 |
indexTable := WeakIdentityDictionary new. |
2182 | 205 |
|
206 |
dstIndex := 1. |
|
207 |
1 to:sz do:[:index | |
|
208 |
(phantom := registeredObjects at:index) notNil ifTrue:[ |
|
209 |
dstIndex > realNewSize ifTrue:[ |
|
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
210 |
'Registry [error]: size given is too small in resize' errorPrintCR. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
211 |
self repairTally. |
2182 | 212 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
213 |
^ self |
|
214 |
]. |
|
215 |
newObjects at:dstIndex put:phantom. |
|
216 |
newHandles at:dstIndex put:(handleArray at:index). |
|
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
217 |
indexTable at:phantom put:dstIndex. |
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
218 |
|
2182 | 219 |
dstIndex := dstIndex + 1 |
220 |
] |
|
221 |
]. |
|
222 |
||
223 |
registeredObjects removeDependent:self. |
|
2278
f4b82ee501cc
remove old dependency when resizing the weak array
Claus Gittinger <cg@exept.de>
parents:
2189
diff
changeset
|
224 |
newObjects addDependent:self. |
2182 | 225 |
registeredObjects := newObjects. |
226 |
handleArray := newHandles. |
|
227 |
||
228 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
229 |
] |
|
230 |
||
231 |
"Created: 16.1.1997 / 18:08:00 / cg" |
|
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
232 |
"Modified: 6.3.1997 / 22:29:58 / cg" |
2182 | 233 |
! ! |
234 |
||
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
235 |
!Registry methodsFor:'registering objects'! |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
236 |
|
1 | 237 |
register:anObject |
238 |
"register anObject, so that a copy of it gets the disposed message |
|
2 | 239 |
when anObject dies (some time in the future)" |
1 | 240 |
|
375 | 241 |
^ self register:anObject as:(anObject shallowCopyForFinalization) |
242 |
! |
|
243 |
||
244 |
register:anObject as:aHandle |
|
245 |
"register anObject, so that I later receive informDispose: with aHandle |
|
246 |
(some time in the future)" |
|
247 |
||
248 |
|newColl newPhantoms |
|
159 | 249 |
size "{ Class: SmallInteger }" |
250 |
index "{ Class: SmallInteger }" |
|
2507
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
251 |
p wasBlocked idx0| |
1 | 252 |
|
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
253 |
wasBlocked := OperatingSystem blockInterrupts. |
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
254 |
|
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
255 |
registeredObjects size == 0 "isNil" ifTrue:[ |
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
256 |
registeredObjects := WeakArray new:10. |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
1961
diff
changeset
|
257 |
registeredObjects addDependent:self. |
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
258 |
handleArray := Array basicNew:10. |
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
259 |
indexTable := WeakIdentityDictionary new. |
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
260 |
|
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
261 |
registeredObjects at:1 put:anObject. |
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
262 |
handleArray at:1 put:aHandle. |
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
263 |
indexTable at:anObject put:1. |
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
264 |
|
2182 | 265 |
tally := 1. |
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
266 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
267 |
ObjectMemory addDependent:self. |
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
268 |
^ self |
1 | 269 |
]. |
270 |
||
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
271 |
wasBlocked ifFalse:[ |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
272 |
OperatingSystem unblockInterrupts. |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
273 |
OperatingSystem blockInterrupts. |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
274 |
]. |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
275 |
|
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
276 |
"/ index := registeredObjects identityIndexOf:anObject ifAbsent:0. |
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
277 |
index := indexTable at:anObject ifAbsent:0. |
1 | 278 |
index ~~ 0 ifTrue:[ |
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
279 |
"/ double check ... |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
280 |
(registeredObjects at:index) ~~ anObject ifTrue:[ |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
281 |
('Registry [warning]: index table clobbered') errorPrintCR. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
282 |
]. |
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
283 |
|
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
284 |
"already registered" |
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
285 |
|
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
286 |
handleArray at:index put:aHandle. |
2552 | 287 |
('Registry [info]: object (' , (registeredObjects at:index) printString , ') is already registered') infoPrintCR. |
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
288 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
289 |
^ self |
1 | 290 |
]. |
291 |
||
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
292 |
wasBlocked ifFalse:[ |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
293 |
OperatingSystem unblockInterrupts. |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
294 |
OperatingSystem blockInterrupts. |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
295 |
]. |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
296 |
|
2182 | 297 |
"search for a free slot, on the fly look for leftovers (no longer happens)" |
2507
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
298 |
idx0 := 1. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
299 |
index := registeredObjects identityIndexOf:nil startingAt:idx0. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
300 |
[index ~~ 0] whileTrue:[ |
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
301 |
"is there a leftover ?" |
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
302 |
p := handleArray at:index. |
2507
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
303 |
p isNil ifTrue:[ |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
304 |
registeredObjects at:index put:anObject. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
305 |
handleArray at:index put:aHandle. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
306 |
indexTable at:anObject put:index. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
307 |
|
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
308 |
tally := tally + 1. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
309 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
310 |
^ self |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
311 |
]. |
2182 | 312 |
|
2507
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
313 |
"/ mhmh - a registeredObject vanished, but its |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
314 |
"/ phantom is still there ... |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
315 |
|
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
316 |
'Registry [warning]: there should be no leftOvers' errorPrintCR. |
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
317 |
|
2507
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
318 |
"/ "tell the phantom" |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
319 |
"/ handleArray at:index put:nil. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
320 |
"/ tally := tally - 1. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
321 |
"/ self informDispose:p. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
322 |
"/ p := nil. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
323 |
|
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
324 |
idx0 := index + 1. |
8824b9953b54
dont perform disposes when registering an object
Claus Gittinger <cg@exept.de>
parents:
2449
diff
changeset
|
325 |
index := registeredObjects identityIndexOf:nil startingAt:idx0. |
1 | 326 |
]. |
327 |
||
328 |
"no free slot, add at the end" |
|
329 |
||
159 | 330 |
size := registeredObjects size. |
331 |
index := size + 1. |
|
332 |
newColl := WeakArray new:(size * 2). |
|
333 |
newColl replaceFrom:1 to:size with:registeredObjects. |
|
2278
f4b82ee501cc
remove old dependency when resizing the weak array
Claus Gittinger <cg@exept.de>
parents:
2189
diff
changeset
|
334 |
registeredObjects removeDependent:self. |
1 | 335 |
registeredObjects := newColl. |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
1961
diff
changeset
|
336 |
registeredObjects addDependent:self. |
159 | 337 |
registeredObjects at:index put:anObject. |
1 | 338 |
|
359 | 339 |
newPhantoms := Array basicNew:(size * 2). |
375 | 340 |
newPhantoms replaceFrom:1 to:size with:handleArray. |
341 |
handleArray := newPhantoms. |
|
342 |
handleArray at:index put:aHandle. |
|
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
343 |
indexTable at:anObject put:index. |
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
344 |
|
2182 | 345 |
tally := tally + 1. |
1479
396c633aee65
allow registering some object twice (handle as registerChange:)
Claus Gittinger <cg@exept.de>
parents:
1471
diff
changeset
|
346 |
|
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
347 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
348 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
1961
diff
changeset
|
349 |
"Modified: 7.1.1997 / 16:56:03 / stefan" |
2552 | 350 |
"Modified: 16.4.1997 / 15:21:54 / cg" |
1 | 351 |
! |
352 |
||
617 | 353 |
registerChange:anObject |
354 |
"a registered object has changed, create a new phantom" |
|
355 |
||
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
356 |
|index wasBlocked copy| |
617 | 357 |
|
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
358 |
wasBlocked := OperatingSystem blockInterrupts. |
1467 | 359 |
registeredObjects isNil ifTrue:[ |
360 |
index := 0 |
|
361 |
] ifFalse:[ |
|
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
362 |
"/ index := registeredObjects identityIndexOf:anObject ifAbsent:0. |
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
363 |
index := indexTable at:anObject ifAbsent:0. |
1467 | 364 |
]. |
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
365 |
copy := anObject shallowCopyForFinalization. |
617 | 366 |
index ~~ 0 ifTrue:[ |
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
367 |
handleArray at:index put:copy. |
1467 | 368 |
] ifFalse:[ |
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
369 |
self register:anObject as:copy |
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
370 |
]. |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
371 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1467 | 372 |
|
2449
788bba151e8a
validate - searching for a negative tally bug (olbi)
Claus Gittinger <cg@exept.de>
parents:
2414
diff
changeset
|
373 |
"Modified: 6.3.1997 / 22:24:15 / cg" |
617 | 374 |
! |
375 |
||
1 | 376 |
unregister:anObject |
377 |
"remove registration of anObject, without telling the phantom; |
|
759
908363ce8a32
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
630
diff
changeset
|
378 |
should be sent, if we are no more interested in destruction of |
1 | 379 |
anObject (i.e. it no longer holds external resources)." |
380 |
||
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
381 |
|index wasBlocked| |
1 | 382 |
|
1338 | 383 |
registeredObjects notNil ifTrue:[ |
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
384 |
wasBlocked := OperatingSystem blockInterrupts. |
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
385 |
"/ index := registeredObjects identityIndexOf:anObject ifAbsent:0. |
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
386 |
index := indexTable at:anObject ifAbsent:0. |
1338 | 387 |
index ~~ 0 ifTrue:[ |
388 |
handleArray at:index put:nil. |
|
2182 | 389 |
registeredObjects at:index put:nil. |
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
390 |
indexTable removeKey:anObject. |
2182 | 391 |
tally := tally - 1. |
1484
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
392 |
]. |
3f4a4e5c5eb7
block interrupts while updating a registry
Claus Gittinger <cg@exept.de>
parents:
1479
diff
changeset
|
393 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
2182 | 394 |
|
395 |
self resize |
|
1 | 396 |
] |
1338 | 397 |
|
2182 | 398 |
"Modified: 16.1.1997 / 18:08:42 / cg" |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
1961
diff
changeset
|
399 |
! |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
1961
diff
changeset
|
400 |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
401 |
unregisterAllForWhich:aBlock |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
402 |
"remove registration of all entries, for which the argument block |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
403 |
evaluates to true. |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
404 |
should be sent, if we are no more interested in destruction of |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
405 |
a group of objects (i.e. it no longer holds external resources)." |
617 | 406 |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
407 |
|n "{ Class: SmallInteger }" |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
408 |
obj wasBlocked| |
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
409 |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
410 |
registeredObjects notNil ifTrue:[ |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
411 |
wasBlocked := OperatingSystem blockInterrupts. |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
412 |
n := registeredObjects size. |
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
413 |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
414 |
1 to:n do:[:index | |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
415 |
obj := registeredObjects at:index. |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
416 |
(obj notNil and:[obj ~~ 0]) ifTrue:[ |
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
417 |
(aBlock value:obj) ifTrue:[ |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
1961
diff
changeset
|
418 |
handleArray at:index put:nil. |
2182 | 419 |
registeredObjects at:index put:nil. |
2414
215e58d26a05
added indexTable for fast finding of a registeredObjects slotIndex.
Claus Gittinger <cg@exept.de>
parents:
2278
diff
changeset
|
420 |
indexTable removeKey:obj. |
2182 | 421 |
tally := tally - 1. |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
1961
diff
changeset
|
422 |
] |
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
423 |
] |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
1961
diff
changeset
|
424 |
]. |
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
425 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
2182 | 426 |
self resize |
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
427 |
] |
1466
dc55982da0b8
shrink weakArray when too sparsely filled
Claus Gittinger <cg@exept.de>
parents:
1338
diff
changeset
|
428 |
|
2180
c3bfc8a9a114
added #unregisterAllForWhich:
Claus Gittinger <cg@exept.de>
parents:
2123
diff
changeset
|
429 |
"Created: 16.1.1997 / 16:39:18 / cg" |
2182 | 430 |
"Modified: 16.1.1997 / 18:08:47 / cg" |
617 | 431 |
! ! |
432 |
||
1961 | 433 |
!Registry class methodsFor:'documentation'! |
630 | 434 |
|
435 |
version |
|
2552 | 436 |
^ '$Header: /cvs/stx/stx/libbasic/Registry.st,v 1.44 1997-04-16 13:22:10 cg Exp $' |
630 | 437 |
! ! |