9 other person. No title to or ownership of the software is |
9 other person. No title to or ownership of the software is |
10 hereby transferred. |
10 hereby transferred. |
11 " |
11 " |
12 |
12 |
13 Object subclass:#Registry |
13 Object subclass:#Registry |
14 instanceVariableNames:'registeredObjects phantomArray cleanState' |
14 instanceVariableNames:'registeredObjects handleArray cleanState' |
15 classVariableNames:'' |
15 classVariableNames:'' |
16 poolDictionaries:'' |
16 poolDictionaries:'' |
17 category:'System-Support' |
17 category:'System-Support' |
18 ! |
18 ! |
19 |
19 |
20 Registry comment:' |
20 Registry comment:' |
21 COPYRIGHT (c) 1993 by Claus Gittinger |
21 COPYRIGHT (c) 1993 by Claus Gittinger |
22 All Rights Reserved |
22 All Rights Reserved |
23 |
23 |
24 $Header: /cvs/stx/stx/libbasic/Registry.st,v 1.15 1995-06-27 02:14:12 claus Exp $ |
24 $Header: /cvs/stx/stx/libbasic/Registry.st,v 1.16 1995-08-08 00:48:37 claus Exp $ |
25 '! |
25 '! |
26 |
26 |
27 !Registry class methodsFor:'documentation'! |
27 !Registry class methodsFor:'documentation'! |
28 |
28 |
29 copyright |
29 copyright |
75 " |
75 " |
76 ! ! |
76 ! ! |
77 |
77 |
78 !Registry methodsFor:'dispose handling'! |
78 !Registry methodsFor:'dispose handling'! |
79 |
79 |
|
80 informDispose:someHandle |
|
81 someHandle disposed |
|
82 ! |
|
83 |
80 informDispose |
84 informDispose |
81 "an instance has been destroyed - look which one it was" |
85 "an instance has been destroyed - look which one it was" |
82 |
86 |
83 |phantom |
87 |phantom |
84 sz "{ Class: SmallInteger }"| |
88 sz "{ Class: SmallInteger }"| |
85 |
89 |
86 cleanState ifTrue:[ |
90 cleanState ifTrue:[ |
87 sz := phantomArray size. |
91 sz := handleArray size. |
88 1 to:sz do:[:index | |
92 1 to:sz do:[:index | |
89 (registeredObjects at:index) isNil ifTrue:[ |
93 (registeredObjects at:index) isNil ifTrue:[ |
90 phantom := phantomArray at:index. |
94 phantom := handleArray at:index. |
91 phantom notNil ifTrue:[ |
95 phantom notNil ifTrue:[ |
92 phantomArray at:index put:nil. |
96 handleArray at:index put:nil. |
93 phantom disposed |
97 self informDispose:phantom |
94 ] |
98 ] |
95 ] |
99 ] |
96 ] |
100 ] |
97 ] |
101 ] |
98 ! ! |
102 ! ! |
113 "return the collection of registered objects" |
117 "return the collection of registered objects" |
114 |
118 |
115 ^ registeredObjects |
119 ^ registeredObjects |
116 ! |
120 ! |
117 |
121 |
118 changed:anObject |
122 registerChange:anObject |
119 "a registered object has changed, create a new phantom" |
123 "a registered object has changed, create a new phantom" |
120 |
124 |
121 |index| |
125 |index| |
122 |
126 |
123 index := registeredObjects identityIndexOf:anObject ifAbsent:[0]. |
127 index := registeredObjects identityIndexOf:anObject ifAbsent:[0]. |
124 index ~~ 0 ifTrue:[ |
128 index ~~ 0 ifTrue:[ |
125 phantomArray at:index put:anObject shallowCopyForFinalization. |
129 handleArray at:index put:anObject shallowCopyForFinalization. |
126 ] |
130 ] |
127 ! |
131 ! |
128 |
132 |
129 register:anObject |
133 register:anObject |
130 "register anObject, so that a copy of it gets the disposed message |
134 "register anObject, so that a copy of it gets the disposed message |
131 when anObject dies (some time in the future)" |
135 when anObject dies (some time in the future)" |
132 |
136 |
133 |phantom newColl newPhantoms |
137 ^ self register:anObject as:(anObject shallowCopyForFinalization) |
|
138 ! |
|
139 |
|
140 register:anObject as:aHandle |
|
141 "register anObject, so that I later receive informDispose: with aHandle |
|
142 (some time in the future)" |
|
143 |
|
144 |newColl newPhantoms |
134 size "{ Class: SmallInteger }" |
145 size "{ Class: SmallInteger }" |
135 index "{ Class: SmallInteger }" |
146 index "{ Class: SmallInteger }" |
136 p| |
147 p| |
137 |
148 |
138 phantom := anObject shallowCopyForFinalization. |
|
139 |
|
140 registeredObjects isNil ifTrue:[ |
149 registeredObjects isNil ifTrue:[ |
141 registeredObjects := WeakArray new:10. |
150 registeredObjects := WeakArray new:10. |
142 registeredObjects watcher:self. |
151 registeredObjects watcher:self. |
143 phantomArray := Array basicNew:10. |
152 handleArray := Array basicNew:10. |
144 registeredObjects at:1 put:anObject. |
153 registeredObjects at:1 put:anObject. |
145 phantomArray at:1 put:phantom. |
154 handleArray at:1 put:aHandle. |
146 cleanState := true. |
155 cleanState := true. |
147 ObjectMemory addDependent:self. |
156 ObjectMemory addDependent:self. |
148 ^ self |
157 ^ self |
149 ]. |
158 ]. |
150 |
159 |
151 index := registeredObjects identityIndexOf:anObject ifAbsent:[0]. |
160 index := registeredObjects identityIndexOf:anObject ifAbsent:[0]. |
152 index ~~ 0 ifTrue:[ |
161 index ~~ 0 ifTrue:[ |
153 "already registered" |
162 "already registered" |
154 phantomArray at:index put:phantom. |
163 handleArray at:index put:aHandle. |
155 self error:'object is already registered'. |
164 self error:'object is already registered'. |
156 ^ self |
165 ^ self |
157 ]. |
166 ]. |
158 |
167 |
159 "search for a free slot, on the fly look for leftovers" |
168 "search for a free slot, on the fly look for leftovers" |
160 index := registeredObjects identityIndexOf:nil startingAt:1. |
169 index := registeredObjects identityIndexOf:nil startingAt:1. |
161 index ~~ 0 ifTrue:[ |
170 index ~~ 0 ifTrue:[ |
162 "is there a leftover ?" |
171 "is there a leftover ?" |
163 p := phantomArray at:index. |
172 p := handleArray at:index. |
164 p notNil ifTrue:[ |
173 p notNil ifTrue:[ |
165 "tell the phantom" |
174 "tell the phantom" |
166 phantomArray at:index put:nil. |
175 handleArray at:index put:nil. |
167 p disposed. |
176 self informDispose:p. |
168 p := nil. |
177 p := nil. |
169 ]. |
178 ]. |
170 registeredObjects at:index put:anObject. |
179 registeredObjects at:index put:anObject. |
171 phantomArray at:index put:phantom. |
180 handleArray at:index put:aHandle. |
172 ^ self |
181 ^ self |
173 ]. |
182 ]. |
174 |
183 |
175 "no free slot, add at the end" |
184 "no free slot, add at the end" |
176 |
185 |
181 registeredObjects := newColl. |
190 registeredObjects := newColl. |
182 registeredObjects watcher:self. |
191 registeredObjects watcher:self. |
183 registeredObjects at:index put:anObject. |
192 registeredObjects at:index put:anObject. |
184 |
193 |
185 newPhantoms := Array basicNew:(size * 2). |
194 newPhantoms := Array basicNew:(size * 2). |
186 newPhantoms replaceFrom:1 to:size with:phantomArray. |
195 newPhantoms replaceFrom:1 to:size with:handleArray. |
187 phantomArray := newPhantoms. |
196 handleArray := newPhantoms. |
188 phantomArray at:index put:phantom. |
197 handleArray at:index put:aHandle. |
189 ! |
198 ! |
190 |
199 |
191 unregister:anObject |
200 unregister:anObject |
192 "remove registration of anObject, without telling the phantom; |
201 "remove registration of anObject, without telling the phantom; |
193 should be sent, if we are no more interrested in destruction of |
202 should be sent, if we are no more interrested in destruction of |
195 |
204 |
196 |index| |
205 |index| |
197 |
206 |
198 index := registeredObjects identityIndexOf:anObject ifAbsent:[0]. |
207 index := registeredObjects identityIndexOf:anObject ifAbsent:[0]. |
199 index ~~ 0 ifTrue:[ |
208 index ~~ 0 ifTrue:[ |
200 phantomArray at:index put:nil. |
209 handleArray at:index put:nil. |
201 registeredObjects at:index put:nil |
210 registeredObjects at:index put:nil |
202 ] |
211 ] |
203 ! ! |
212 ! ! |
204 |
213 |
205 !Registry methodsFor:'restart handling'! |
214 !Registry methodsFor:'restart handling'! |
206 |
215 |
207 update:aParameter |
216 update:aParameter |
208 aParameter == #earlyRestart ifTrue:[ |
217 aParameter == #earlyRestart ifTrue:[ |
209 phantomArray notNil ifTrue:[ |
218 handleArray notNil ifTrue:[ |
210 phantomArray atAllPut:nil |
219 handleArray atAllPut:nil |
211 ] |
220 ] |
212 ]. |
221 ]. |
213 aParameter == #returnFromSnapshot ifTrue:[ |
222 aParameter == #returnFromSnapshot ifTrue:[ |
214 cleanState := true |
223 cleanState := true |
215 ] |
224 ] |