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'
|
159
|
15 |
classVariableNames:'RegistrationFailedSignal'
|
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 |
|
186
|
24 |
$Header: /cvs/stx/stx/libbasic/Attic/WeakArr.st,v 1.7 1994-10-28 01:30:48 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 |
"
|
186
|
45 |
$Header: /cvs/stx/stx/libbasic/Attic/WeakArr.st,v 1.7 1994-10-28 01:30:48 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
|
|
52 |
objects, references by WeakArrays will NOT keep an object from dying.
|
|
53 |
Instead, whenever an object kept in a WeakArray dies, its entry is nilled,
|
|
54 |
and the WeakArray informed by the storage manager.
|
95
|
55 |
You can use WeakArrays to track disposal of objects which keep external
|
|
56 |
world resources. For example, FileStreams must close their underlying
|
|
57 |
file when disposed (otherwise you would run out of filedescriptors).
|
|
58 |
This can be done by keeping the FileStream objects in a weakArray, and
|
|
59 |
keep a parallel array of filedescriptors. Whenever a fileStream gets
|
|
60 |
free, search both arrays for an index where the stream is nil, but the
|
|
61 |
filedescriptor is non-nil. Then close that file, and nil the filedescriptor
|
|
62 |
entry.
|
|
63 |
|
|
64 |
Another application is caching of data - keep it in a weakArray, so the
|
|
65 |
data in that cache will not be unreclaimable due to being cached.
|
|
66 |
(for example, the ResourcePack class uses a WeakArray to cache recently
|
|
67 |
used resource data for a while).
|
88
|
68 |
|
186
|
69 |
The way in which weakArrays get informed by the runtime system is via
|
|
70 |
an interrupt (DisposeInterrupt). The reason for not sending messages
|
|
71 |
directly from the VM is to make it possible, to run the finalization
|
|
72 |
code at lower priority or from another class.
|
|
73 |
Also, as a side effect, it is possible to delay finalization by blocking
|
|
74 |
interrupts
|
|
75 |
|
88
|
76 |
This interrupt is cought here and informs the watcher and, if there is
|
|
77 |
a dependent, this one gets informed as well (I dont know, which of the
|
|
78 |
two mechanisms will survive in the long run - I started with the watcher,
|
|
79 |
but now switch to dependencies since they seem to provide more flexibility).
|
|
80 |
Therefore, be prepared, that the watcher mechanism will vanish in the
|
|
81 |
future.
|
186
|
82 |
|
|
83 |
NOTICE: weakArray handling add some (small) overhead to the VM (each weakarray
|
|
84 |
is scanner after each GC). It is uncertain, if the current mechanism works well
|
|
85 |
with (say) thousands of weakArrays.
|
6
|
86 |
"
|
|
87 |
! !
|
|
88 |
|
159
|
89 |
!WeakArray class methodsFor:'initialization'!
|
|
90 |
|
|
91 |
initialize
|
|
92 |
"setup the private signal"
|
|
93 |
|
|
94 |
RegistrationFailedSignal isNil ifTrue:[
|
186
|
95 |
Object initialize.
|
159
|
96 |
|
186
|
97 |
RegistrationFailedSignal := Object errorSignal newSignalMayProceed:true.
|
|
98 |
RegistrationFailedSignal nameClass:self message:#registrationFailedSignal.
|
|
99 |
RegistrationFailedSignal notifierString:'weakArray registration failed'.
|
159
|
100 |
]
|
|
101 |
! !
|
|
102 |
|
6
|
103 |
!WeakArray class methodsFor:'instance creation'!
|
|
104 |
|
|
105 |
new:size
|
95
|
106 |
"return a new weakArray with size slots"
|
6
|
107 |
|
95
|
108 |
"This is a kludge: I would like to set WEAK-flag in the classes
|
159
|
109 |
initialize method, but no order of class-init is defined (yet) ...
|
95
|
110 |
Therefore it could happen, that a WeakArray is used by other
|
|
111 |
class inits BEFORE this initialize is evaluated. To avoid this,
|
159
|
112 |
the WEAK bit in the class is set here, when the first WeakArray is
|
95
|
113 |
created."
|
|
114 |
|
|
115 |
self flags:(Behavior flagWeakPointers).
|
6
|
116 |
ObjectMemory disposeInterruptHandler:self.
|
95
|
117 |
|
159
|
118 |
^ (self basicNew:size) registerAsWeakArray
|
|
119 |
! !
|
|
120 |
|
|
121 |
!WeakArray methodsFor:'GC registration'!
|
|
122 |
|
|
123 |
registerAsWeakArray
|
|
124 |
"register the receiver in the VM -
|
|
125 |
i.e. tell the VM to nil disposed entries in the receiver
|
|
126 |
and notify the disposeInterruptHandler whenever that happened."
|
|
127 |
|
|
128 |
|ok|
|
6
|
129 |
%{
|
|
130 |
OBJ __addShadowObject();
|
|
131 |
|
159
|
132 |
ok = __addShadowObject(self, 0);
|
6
|
133 |
if (ok == false) {
|
186
|
134 |
/*
|
|
135 |
* this happens when too many shadow objects are
|
|
136 |
* already there, collect garbage to get rid of
|
|
137 |
* obsolete ones, and try again.
|
|
138 |
* since a full collect is expensive, we try
|
|
139 |
* a scavenge first, doing a full collect only if
|
|
140 |
* that does not help.
|
|
141 |
*/
|
|
142 |
nonTenuringScavenge(__context);
|
|
143 |
ok = __addShadowObject(self, 0);
|
|
144 |
if (ok == false) {
|
|
145 |
/* try more ... */
|
|
146 |
scavenge(__context);
|
|
147 |
ok = __addShadowObject(self, 0);
|
|
148 |
if (ok == false) {
|
|
149 |
/* hard stuff - need full collect */
|
|
150 |
__garbageCollect(__context);
|
|
151 |
ok = __addShadowObject(self, 0);
|
|
152 |
if (ok == false) {
|
|
153 |
/* mhmh - it seems that there are really many shadow
|
|
154 |
objects around - force creation */
|
|
155 |
ok = __addShadowObject(self, 1);
|
|
156 |
if (ok == false) {
|
|
157 |
/* no chance - something must be wrong */
|
|
158 |
}
|
|
159 |
}
|
|
160 |
}
|
|
161 |
}
|
6
|
162 |
}
|
159
|
163 |
%}.
|
|
164 |
ok ifFalse:[
|
186
|
165 |
^ RegistrationFailedSignal raiseRequestWith:self
|
159
|
166 |
]
|
6
|
167 |
! !
|
|
168 |
|
|
169 |
!WeakArray class methodsFor:'dispose handling'!
|
|
170 |
|
|
171 |
allShadowObjectsDo:aBlock
|
|
172 |
"evaluate the argument, aBlock for all known shadow objects"
|
|
173 |
%{
|
|
174 |
__allShadowObjectsDo(&aBlock, __context);
|
|
175 |
%}
|
|
176 |
!
|
|
177 |
|
|
178 |
allChangedShadowObjectsDo:aBlock
|
|
179 |
"evaluate the argument, aBlock for all known shadow objects which have
|
|
180 |
lost a pointer recently."
|
|
181 |
%{
|
|
182 |
__allChangedShadowObjectsDo(&aBlock, __context);
|
|
183 |
%}
|
|
184 |
!
|
|
185 |
|
|
186 |
disposeInterrupt
|
|
187 |
"this is triggered by the garbage collector,
|
|
188 |
whenever any shadowArray looses a pointer."
|
|
189 |
|
|
190 |
self allChangedShadowObjectsDo:[:aShadowArray |
|
186
|
191 |
aShadowArray changed.
|
|
192 |
aShadowArray watcher notNil ifTrue:[
|
|
193 |
aShadowArray watcher informDispose
|
|
194 |
]
|
6
|
195 |
]
|
|
196 |
! !
|
|
197 |
|
159
|
198 |
!WeakArray methodsFor:'copying'!
|
|
199 |
|
|
200 |
postCopy
|
|
201 |
"copying alone does not really help - we have to tell
|
|
202 |
the VM, that there is a new WeakArray around ...
|
|
203 |
Q: who copies weakArrays ?"
|
|
204 |
|
|
205 |
self dependents:nil.
|
|
206 |
self registerAsWeakArray.
|
|
207 |
! !
|
|
208 |
|
6
|
209 |
!WeakArray methodsFor:'accessing'!
|
|
210 |
|
|
211 |
dependents
|
10
|
212 |
"return the dependents of the receiver"
|
|
213 |
|
6
|
214 |
^ dependents
|
|
215 |
!
|
|
216 |
|
|
217 |
dependents:aCollection
|
10
|
218 |
"set the dependents of the receiver"
|
|
219 |
|
6
|
220 |
dependents := aCollection
|
|
221 |
!
|
|
222 |
|
|
223 |
watcher
|
10
|
224 |
"return the watcher of the receiver.
|
|
225 |
The watcher-stuff is a leftover from an old implementation
|
|
226 |
and will vanish soon"
|
|
227 |
|
6
|
228 |
^ watcher
|
|
229 |
!
|
|
230 |
|
|
231 |
watcher:anObject
|
10
|
232 |
"set the watcher of the receiver.
|
|
233 |
The watcher-stuff is a leftover from an old implementation
|
|
234 |
and will vanish soon"
|
|
235 |
|
6
|
236 |
watcher := anObject
|
|
237 |
! !
|