6
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'System-Support'
|
|
18 |
!
|
|
19 |
|
|
20 |
WeakArray comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
44
|
25 |
$Header: /cvs/stx/stx/libbasic/WeakArray.st,v 1.3 1994-01-16 03:47:41 claus Exp $
|
6
|
26 |
written feb 91 by claus
|
|
27 |
'!
|
|
28 |
|
|
29 |
!WeakArray class methodsFor:'documentation'!
|
|
30 |
|
|
31 |
documentation
|
|
32 |
"
|
|
33 |
WeakArrays are used to trace disposal of objects; in contrast to other
|
|
34 |
objects, references by WeakArrays will NOT keep an object from dying.
|
|
35 |
Instead, whenever an object kept in a WeakArray dies, its entry is nilled,
|
10
|
36 |
and the WeakArray informed by the storage manager. This is done by sending a
|
|
37 |
disposeInterrupt.
|
6
|
38 |
This interrupt is cought here and informs the watcher and, if there is
|
|
39 |
a dependent, this one gets informed as well (I dont know, which of the
|
10
|
40 |
two mechanisms will survive in the long run - I started with the watcher,
|
44
|
41 |
but now switch to dependencies since they seem to provide more flexibility).
|
6
|
42 |
"
|
|
43 |
! !
|
|
44 |
|
|
45 |
!WeakArray class methodsFor:'instance creation'!
|
|
46 |
|
|
47 |
new:size
|
44
|
48 |
"return a new weakArray with size slots.
|
|
49 |
This is a kludge: would like to set WEAK-flag in class
|
6
|
50 |
initialize method, but no order of class-init is defined ...
|
|
51 |
(therefore it could happen, that a WeakArray is used by other
|
|
52 |
class inits before this init is evaluated. To avoid this,
|
44
|
53 |
the WEAK bit in the class is set when the first WeakArray is
|
|
54 |
created)"
|
6
|
55 |
|
44
|
56 |
|newArray|
|
|
57 |
|
|
58 |
self flags:5. "having a constant 5 here is very bad"
|
6
|
59 |
ObjectMemory disposeInterruptHandler:self.
|
|
60 |
newArray := self basicNew:size.
|
|
61 |
%{
|
|
62 |
OBJ ok;
|
|
63 |
OBJ __addShadowObject();
|
|
64 |
|
|
65 |
ok = __addShadowObject(newArray, 0);
|
|
66 |
if (ok == false) {
|
44
|
67 |
/*
|
|
68 |
* this happens when too many shadow objects are
|
|
69 |
* already there, collect garbage to get rid of
|
6
|
70 |
* obsolete ones, and try again.
|
44
|
71 |
* since a full collect is expensive, we try
|
|
72 |
* a scavenge first, doing a full collect only if
|
|
73 |
* that does not help.
|
|
74 |
*/
|
|
75 |
nonTenuringScavenge(__context);
|
|
76 |
ok = __addShadowObject(newArray, 0);
|
|
77 |
if (ok == false) {
|
|
78 |
/* try more ... */
|
|
79 |
scavenge(__context);
|
|
80 |
ok = __addShadowObject(newArray, 0);
|
|
81 |
if (ok == false) {
|
|
82 |
/* hard stuff - need full collect */
|
|
83 |
__garbageCollect(__context);
|
|
84 |
ok = __addShadowObject(newArray, 0);
|
|
85 |
if (ok == false) {
|
|
86 |
/* mhmh - it seems that there are really many shadow
|
|
87 |
objects around - force creation */
|
|
88 |
ok = __addShadowObject(newArray, 1);
|
|
89 |
if (ok == false) {
|
|
90 |
/* no chance - something must be wrong */
|
|
91 |
newArray = nil;
|
|
92 |
}
|
|
93 |
}
|
|
94 |
}
|
|
95 |
}
|
6
|
96 |
}
|
|
97 |
%}
|
|
98 |
.
|
|
99 |
^ newArray
|
|
100 |
! !
|
|
101 |
|
|
102 |
!WeakArray class methodsFor:'dispose handling'!
|
|
103 |
|
|
104 |
allShadowObjectsDo:aBlock
|
|
105 |
"evaluate the argument, aBlock for all known shadow objects"
|
|
106 |
%{
|
|
107 |
__allShadowObjectsDo(&aBlock, __context);
|
|
108 |
%}
|
|
109 |
!
|
|
110 |
|
|
111 |
allChangedShadowObjectsDo:aBlock
|
|
112 |
"evaluate the argument, aBlock for all known shadow objects which have
|
|
113 |
lost a pointer recently."
|
|
114 |
%{
|
|
115 |
__allChangedShadowObjectsDo(&aBlock, __context);
|
|
116 |
%}
|
|
117 |
!
|
|
118 |
|
|
119 |
disposeInterrupt
|
|
120 |
"this is triggered by the garbage collector,
|
|
121 |
whenever any shadowArray looses a pointer."
|
|
122 |
|
|
123 |
self allChangedShadowObjectsDo:[:aShadowArray |
|
|
124 |
aShadowArray changed.
|
|
125 |
aShadowArray watcher notNil ifTrue:[
|
|
126 |
aShadowArray watcher informDispose
|
|
127 |
]
|
|
128 |
]
|
|
129 |
! !
|
|
130 |
|
|
131 |
!WeakArray methodsFor:'accessing'!
|
|
132 |
|
|
133 |
dependents
|
10
|
134 |
"return the dependents of the receiver"
|
|
135 |
|
6
|
136 |
^ dependents
|
|
137 |
!
|
|
138 |
|
|
139 |
dependents:aCollection
|
10
|
140 |
"set the dependents of the receiver"
|
|
141 |
|
6
|
142 |
dependents := aCollection
|
|
143 |
!
|
|
144 |
|
|
145 |
watcher
|
10
|
146 |
"return the watcher of the receiver.
|
|
147 |
The watcher-stuff is a leftover from an old implementation
|
|
148 |
and will vanish soon"
|
|
149 |
|
6
|
150 |
^ watcher
|
|
151 |
!
|
|
152 |
|
|
153 |
watcher:anObject
|
10
|
154 |
"set the watcher of the receiver.
|
|
155 |
The watcher-stuff is a leftover from an old implementation
|
|
156 |
and will vanish soon"
|
|
157 |
|
6
|
158 |
watcher := anObject
|
|
159 |
! !
|