author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 18:20:46 +0200 | |
changeset 1290 | 15ba3221b89b |
parent 1286 | 4270a0b4917d |
child 1338 | 72dc10fbe8fd |
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 |
|
1286 | 14 |
instanceVariableNames:'registeredObjects handleArray cleanState' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'System-Support' |
|
1 | 18 |
! |
19 |
||
20 |
!Registry class methodsFor:'documentation'! |
|
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 |
|
42 |
be sent the #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 |
|
75 |
"return the collection of registered objects" |
|
76 |
||
77 |
^ registeredObjects |
|
78 |
! |
|
79 |
||
80 |
register:anObject |
|
81 |
"register anObject, so that a copy of it gets the disposed message |
|
2 | 82 |
when anObject dies (some time in the future)" |
1 | 83 |
|
375 | 84 |
^ self register:anObject as:(anObject shallowCopyForFinalization) |
85 |
! |
|
86 |
||
87 |
register:anObject as:aHandle |
|
88 |
"register anObject, so that I later receive informDispose: with aHandle |
|
89 |
(some time in the future)" |
|
90 |
||
91 |
|newColl newPhantoms |
|
159 | 92 |
size "{ Class: SmallInteger }" |
93 |
index "{ Class: SmallInteger }" |
|
94 |
p| |
|
1 | 95 |
|
96 |
registeredObjects isNil ifTrue:[ |
|
217 | 97 |
registeredObjects := WeakArray new:10. |
98 |
registeredObjects watcher:self. |
|
375 | 99 |
handleArray := Array basicNew:10. |
217 | 100 |
registeredObjects at:1 put:anObject. |
375 | 101 |
handleArray at:1 put:aHandle. |
217 | 102 |
cleanState := true. |
103 |
ObjectMemory addDependent:self. |
|
104 |
^ self |
|
1 | 105 |
]. |
106 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
107 |
index := registeredObjects identityIndexOf:anObject ifAbsent:0. |
1 | 108 |
index ~~ 0 ifTrue:[ |
217 | 109 |
"already registered" |
375 | 110 |
handleArray at:index put:aHandle. |
217 | 111 |
self error:'object is already registered'. |
112 |
^ self |
|
1 | 113 |
]. |
114 |
||
115 |
"search for a free slot, on the fly look for leftovers" |
|
159 | 116 |
index := registeredObjects identityIndexOf:nil startingAt:1. |
117 |
index ~~ 0 ifTrue:[ |
|
118 |
"is there a leftover ?" |
|
375 | 119 |
p := handleArray at:index. |
217 | 120 |
p notNil ifTrue:[ |
159 | 121 |
"tell the phantom" |
375 | 122 |
handleArray at:index put:nil. |
123 |
self informDispose:p. |
|
217 | 124 |
p := nil. |
125 |
]. |
|
126 |
registeredObjects at:index put:anObject. |
|
375 | 127 |
handleArray at:index put:aHandle. |
217 | 128 |
^ self |
1 | 129 |
]. |
130 |
||
131 |
"no free slot, add at the end" |
|
132 |
||
159 | 133 |
size := registeredObjects size. |
134 |
index := size + 1. |
|
135 |
newColl := WeakArray new:(size * 2). |
|
136 |
newColl replaceFrom:1 to:size with:registeredObjects. |
|
1 | 137 |
registeredObjects := newColl. |
138 |
registeredObjects watcher:self. |
|
159 | 139 |
registeredObjects at:index put:anObject. |
1 | 140 |
|
359 | 141 |
newPhantoms := Array basicNew:(size * 2). |
375 | 142 |
newPhantoms replaceFrom:1 to:size with:handleArray. |
143 |
handleArray := newPhantoms. |
|
144 |
handleArray at:index put:aHandle. |
|
1 | 145 |
! |
146 |
||
617 | 147 |
registerChange:anObject |
148 |
"a registered object has changed, create a new phantom" |
|
149 |
||
150 |
|index| |
|
151 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
152 |
index := registeredObjects identityIndexOf:anObject ifAbsent:0. |
617 | 153 |
index ~~ 0 ifTrue:[ |
154 |
handleArray at:index put:anObject shallowCopyForFinalization. |
|
155 |
] |
|
156 |
! |
|
157 |
||
1 | 158 |
unregister:anObject |
159 |
"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
|
160 |
should be sent, if we are no more interested in destruction of |
1 | 161 |
anObject (i.e. it no longer holds external resources)." |
162 |
||
163 |
|index| |
|
164 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
165 |
index := registeredObjects identityIndexOf:anObject ifAbsent:0. |
1 | 166 |
index ~~ 0 ifTrue:[ |
375 | 167 |
handleArray at:index put:nil. |
217 | 168 |
registeredObjects at:index put:nil |
1 | 169 |
] |
170 |
! ! |
|
2 | 171 |
|
617 | 172 |
!Registry methodsFor:'dispose handling'! |
173 |
||
174 |
informDispose |
|
175 |
"an instance has been destroyed - look which one it was" |
|
176 |
||
177 |
|phantom |
|
178 |
sz "{ Class: SmallInteger }"| |
|
179 |
||
180 |
cleanState ifTrue:[ |
|
181 |
sz := handleArray size. |
|
182 |
1 to:sz do:[:index | |
|
183 |
(registeredObjects at:index) isNil ifTrue:[ |
|
184 |
phantom := handleArray at:index. |
|
185 |
phantom notNil ifTrue:[ |
|
186 |
handleArray at:index put:nil. |
|
187 |
self informDispose:phantom |
|
188 |
] |
|
189 |
] |
|
190 |
] |
|
191 |
] |
|
192 |
! |
|
193 |
||
194 |
informDispose:someHandle |
|
195 |
someHandle disposed |
|
196 |
! ! |
|
197 |
||
198 |
!Registry methodsFor:'enumerating'! |
|
199 |
||
200 |
do:aBlock |
|
201 |
"evaluate aBlock for each registered object" |
|
202 |
||
203 |
registeredObjects notNil ifTrue:[ |
|
204 |
registeredObjects nonNilElementsDo:aBlock |
|
205 |
] |
|
206 |
! ! |
|
207 |
||
2 | 208 |
!Registry methodsFor:'restart handling'! |
209 |
||
210 |
update:aParameter |
|
10 | 211 |
aParameter == #earlyRestart ifTrue:[ |
375 | 212 |
handleArray notNil ifTrue:[ |
213 |
handleArray atAllPut:nil |
|
217 | 214 |
] |
10 | 215 |
]. |
216 |
aParameter == #returnFromSnapshot ifTrue:[ |
|
217 | 217 |
cleanState := true |
2 | 218 |
] |
219 |
! ! |
|
617 | 220 |
|
630 | 221 |
!Registry class methodsFor:'documentation'! |
222 |
||
223 |
version |
|
1286 | 224 |
^ '$Header: /cvs/stx/stx/libbasic/Registry.st,v 1.24 1996-04-25 15:59:38 cg Exp $' |
630 | 225 |
! ! |