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