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