6
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
186
|
3 |
All Rights Reserved
|
6
|
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 |
Array variableSubclass:#WeakArray
|
|
14 |
instanceVariableNames:'watcher dependents'
|
216
|
15 |
classVariableNames:'RegistrationFailedSignal AlreadyInitialized'
|
6
|
16 |
poolDictionaries:''
|
159
|
17 |
category:'Collections-Arrayed'
|
6
|
18 |
!
|
|
19 |
|
|
20 |
WeakArray comment:'
|
|
21 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
186
|
22 |
All Rights Reserved
|
95
|
23 |
|
290
|
24 |
$Header: /cvs/stx/stx/libbasic/WeakArray.st,v 1.9 1995-02-28 20:54:39 claus Exp $
|
6
|
25 |
'!
|
|
26 |
|
|
27 |
!WeakArray class methodsFor:'documentation'!
|
|
28 |
|
88
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
186
|
32 |
All Rights Reserved
|
88
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
290
|
45 |
$Header: /cvs/stx/stx/libbasic/WeakArray.st,v 1.9 1995-02-28 20:54:39 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
6
|
49 |
documentation
|
|
50 |
"
|
88
|
51 |
WeakArrays can be used to trace disposal of objects; in contrast to other
|
290
|
52 |
objects, references by WeakArrays will NOT keep an object from dieing.
|
88
|
53 |
Instead, whenever an object kept in a WeakArray dies, its entry is nilled,
|
|
54 |
and the WeakArray informed by the storage manager.
|
290
|
55 |
|
95
|
56 |
You can use WeakArrays to track disposal of objects which keep external
|
|
57 |
world resources. For example, FileStreams must close their underlying
|
290
|
58 |
file when disposed (otherwise you could run out of OS filedescriptors).
|
95
|
59 |
This can be done by keeping the FileStream objects in a weakArray, and
|
|
60 |
keep a parallel array of filedescriptors. Whenever a fileStream gets
|
|
61 |
free, search both arrays for an index where the stream is nil, but the
|
|
62 |
filedescriptor is non-nil. Then close that file, and nil the filedescriptor
|
290
|
63 |
entry. Notice, that there is a class (Registry) which does exactly this in
|
|
64 |
a more programmer friendly way.
|
95
|
65 |
|
290
|
66 |
Another application is caching of data: keep it in a weakArray, so the
|
95
|
67 |
data in that cache will not be unreclaimable due to being cached.
|
|
68 |
(for example, the ResourcePack class uses a WeakArray to cache recently
|
|
69 |
used resource data for a while).
|
88
|
70 |
|
186
|
71 |
The way in which weakArrays get informed by the runtime system is via
|
|
72 |
an interrupt (DisposeInterrupt). The reason for not sending messages
|
216
|
73 |
directly from the VM is to make it possible to run the finalization
|
186
|
74 |
code at lower priority or from another class.
|
|
75 |
Also, as a side effect, it is possible to delay finalization by blocking
|
216
|
76 |
interrupts.
|
186
|
77 |
|
290
|
78 |
This interrupt is cought in ObjectMemory and informs the weakArray,
|
|
79 |
which in turn informs its watcher and pissibly the dependents.
|
|
80 |
Having two mechanisms here (i.e. watcher & dependent) is a historic leftover;
|
|
81 |
I dont know, which of the two mechanisms will survive in the long run -
|
|
82 |
I started with the watcher, but now switch to dependencies since they seem
|
|
83 |
to offer more flexibility (although watcher notification ).
|
|
84 |
Be prepared, that the watcher mechanism may vanish in the future
|
|
85 |
(i.e. use dependents for your applications).
|
186
|
86 |
|
290
|
87 |
NOTICE: WeakArray handling adds some overhead to the VM (each weakarray
|
216
|
88 |
is scanned after each GC). It is uncertain, if the current mechanism works well
|
|
89 |
with (say) ten-thousands of weakArrays.
|
290
|
90 |
|
|
91 |
instanceVariables:
|
|
92 |
|
|
93 |
watcher if non-nil, gets informed via #informDispose
|
|
94 |
that the weakArray has lost pointers.
|
|
95 |
|
|
96 |
dependents get informed via #change
|
|
97 |
that the weakArray has lost pointers.
|
|
98 |
|
|
99 |
|
|
100 |
classVariables:
|
|
101 |
|
|
102 |
RegistrationFailedSignal raised if a weakArray cannot be
|
|
103 |
registered by the VM. This only happens,
|
|
104 |
if the VM has to resize its shadow tables
|
|
105 |
and is running out of malloc-memory.
|
6
|
106 |
"
|
|
107 |
! !
|
|
108 |
|
159
|
109 |
!WeakArray class methodsFor:'initialization'!
|
|
110 |
|
|
111 |
initialize
|
|
112 |
"setup the private signal"
|
|
113 |
|
|
114 |
RegistrationFailedSignal isNil ifTrue:[
|
186
|
115 |
Object initialize.
|
159
|
116 |
|
186
|
117 |
RegistrationFailedSignal := Object errorSignal newSignalMayProceed:true.
|
|
118 |
RegistrationFailedSignal nameClass:self message:#registrationFailedSignal.
|
|
119 |
RegistrationFailedSignal notifierString:'weakArray registration failed'.
|
159
|
120 |
]
|
|
121 |
! !
|
|
122 |
|
6
|
123 |
!WeakArray class methodsFor:'instance creation'!
|
|
124 |
|
|
125 |
new:size
|
95
|
126 |
"return a new weakArray with size slots"
|
6
|
127 |
|
95
|
128 |
"This is a kludge: I would like to set WEAK-flag in the classes
|
216
|
129 |
initialize method, but (currently) the order in which the class-initialize
|
290
|
130 |
methods are called is not defined ...
|
216
|
131 |
... therefore it could happen, that a WeakArray is used by other
|
|
132 |
classes initialize method BEFORE this method is evaluated.
|
|
133 |
To avoid this, the WEAK bit in the class is set here, when the very first
|
|
134 |
WeakArray is created."
|
95
|
135 |
|
216
|
136 |
AlreadyInitialized isNil ifTrue:[
|
|
137 |
self flags:(Behavior flagWeakPointers).
|
|
138 |
AlreadyInitialized := true
|
|
139 |
].
|
95
|
140 |
|
159
|
141 |
^ (self basicNew:size) registerAsWeakArray
|
|
142 |
! !
|
|
143 |
|
|
144 |
!WeakArray methodsFor:'GC registration'!
|
|
145 |
|
|
146 |
registerAsWeakArray
|
|
147 |
"register the receiver in the VM -
|
|
148 |
i.e. tell the VM to nil disposed entries in the receiver
|
|
149 |
and notify the disposeInterruptHandler whenever that happened."
|
|
150 |
|
|
151 |
|ok|
|
6
|
152 |
%{
|
|
153 |
OBJ __addShadowObject();
|
|
154 |
|
159
|
155 |
ok = __addShadowObject(self, 0);
|
6
|
156 |
if (ok == false) {
|
186
|
157 |
/*
|
|
158 |
* this happens when too many shadow objects are
|
|
159 |
* already there, collect garbage to get rid of
|
|
160 |
* obsolete ones, and try again.
|
|
161 |
* since a full collect is expensive, we try
|
|
162 |
* a scavenge first, doing a full collect only if
|
|
163 |
* that does not help.
|
290
|
164 |
*
|
|
165 |
* THIS MAY OR MAY NOT BE A GOOD IDEA: although it reduces
|
|
166 |
* the number of shadow objects that have to be
|
|
167 |
* processed at GC time, it may create a long delay here,
|
|
168 |
* at shadow object creation time.
|
|
169 |
* Dont know which is better ...
|
186
|
170 |
*/
|
|
171 |
nonTenuringScavenge(__context);
|
|
172 |
ok = __addShadowObject(self, 0);
|
|
173 |
if (ok == false) {
|
290
|
174 |
/*
|
|
175 |
* try more ...
|
|
176 |
*/
|
186
|
177 |
scavenge(__context);
|
|
178 |
ok = __addShadowObject(self, 0);
|
|
179 |
if (ok == false) {
|
290
|
180 |
/*
|
|
181 |
* hard stuff - need full collect
|
|
182 |
*/
|
|
183 |
#ifdef OLD
|
186
|
184 |
__garbageCollect(__context);
|
290
|
185 |
|
|
186 |
#else
|
|
187 |
markAndSweep(__context);
|
|
188 |
#endif
|
186
|
189 |
ok = __addShadowObject(self, 0);
|
|
190 |
if (ok == false) {
|
290
|
191 |
/*
|
|
192 |
* mhmh - it seems that there are really many shadow
|
|
193 |
* objects around - force creation
|
|
194 |
*/
|
186
|
195 |
ok = __addShadowObject(self, 1);
|
|
196 |
if (ok == false) {
|
|
197 |
/* no chance - something must be wrong */
|
|
198 |
}
|
|
199 |
}
|
|
200 |
}
|
|
201 |
}
|
6
|
202 |
}
|
159
|
203 |
%}.
|
|
204 |
ok ifFalse:[
|
290
|
205 |
"
|
|
206 |
the VM was not able to register the new weakArray
|
|
207 |
This can only happen, if the VM has to resize its tables,
|
|
208 |
and a malloc request failed. Usually, this smells like big
|
|
209 |
trouble being on the way (soon running out of memory in
|
|
210 |
other places as well).
|
|
211 |
Configure your OS for more swap space.
|
|
212 |
"
|
186
|
213 |
^ RegistrationFailedSignal raiseRequestWith:self
|
159
|
214 |
]
|
6
|
215 |
! !
|
|
216 |
|
290
|
217 |
!WeakArray methodsFor:'notification'!
|
6
|
218 |
|
290
|
219 |
lostPointer
|
|
220 |
"I lost a pointer; tell watcher and dependents.
|
|
221 |
This is sent from the finalization in ObjectMemory."
|
6
|
222 |
|
290
|
223 |
dependents notNil ifTrue:[
|
|
224 |
self changed.
|
|
225 |
].
|
|
226 |
watcher notNil ifTrue:[
|
|
227 |
watcher informDispose
|
6
|
228 |
]
|
|
229 |
! !
|
|
230 |
|
159
|
231 |
!WeakArray methodsFor:'copying'!
|
|
232 |
|
|
233 |
postCopy
|
|
234 |
"copying alone does not really help - we have to tell
|
|
235 |
the VM, that there is a new WeakArray around ...
|
|
236 |
Q: who copies weakArrays ?"
|
|
237 |
|
290
|
238 |
dependents := nil.
|
159
|
239 |
self registerAsWeakArray.
|
|
240 |
! !
|
|
241 |
|
6
|
242 |
!WeakArray methodsFor:'accessing'!
|
|
243 |
|
|
244 |
dependents
|
10
|
245 |
"return the dependents of the receiver"
|
|
246 |
|
6
|
247 |
^ dependents
|
|
248 |
!
|
|
249 |
|
|
250 |
dependents:aCollection
|
10
|
251 |
"set the dependents of the receiver"
|
|
252 |
|
6
|
253 |
dependents := aCollection
|
|
254 |
!
|
|
255 |
|
|
256 |
watcher
|
10
|
257 |
"return the watcher of the receiver.
|
|
258 |
The watcher-stuff is a leftover from an old implementation
|
|
259 |
and will vanish soon"
|
|
260 |
|
6
|
261 |
^ watcher
|
|
262 |
!
|
|
263 |
|
|
264 |
watcher:anObject
|
10
|
265 |
"set the watcher of the receiver.
|
|
266 |
The watcher-stuff is a leftover from an old implementation
|
|
267 |
and will vanish soon"
|
|
268 |
|
6
|
269 |
watcher := anObject
|
|
270 |
! !
|