author | Claus Gittinger <cg@exept.de> |
Tue, 07 Jan 1997 12:02:05 +0100 | |
changeset 2072 | e84dbf5e5424 |
parent 2027 | 4a2cdf9946ae |
child 2091 | c11bb3e29a1b |
permissions | -rw-r--r-- |
6 | 1 |
" |
2 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
358 | 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 |
IdentityDictionary subclass:#WeakIdentityDictionary |
|
1054 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Unordered' |
|
6 | 18 |
! |
19 |
||
10 | 20 |
!WeakIdentityDictionary class methodsFor:'documentation'! |
21 |
||
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
358 | 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 |
||
10 | 36 |
documentation |
37 |
" |
|
1263 | 38 |
WeakIdentityDictionaries behave like IdentityDictionaries, |
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
39 |
as long as the keys are still referenced by some |
1263 | 40 |
other (non-weak) object. |
41 |
However, once the last non-weak reference ceases to exist, |
|
42 |
the object will be automatically removed from the Weakcollection |
|
43 |
(with some delay: it will be removed after the next garbage collect). |
|
44 |
||
45 |
This class was added to support keeping track of dependents without |
|
88 | 46 |
keeping the values alive - values simply become nil when no one else |
47 |
references it. The original dependency mechanism used a regular Dictionary, |
|
48 |
which usually leads to a lot of garbage being kept due to a forgotten |
|
49 |
release. Using a WeakDictionary may be incompatible to ST-80 but is much |
|
50 |
more comfortable, since no manual release of dependents is needed. |
|
1263 | 51 |
|
1290 | 52 |
[author:] |
53 |
Claus Gittinger |
|
54 |
||
1263 | 55 |
[See also:] |
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
56 |
WeakArray WeakValueDictionary WeakIdentitySet |
10 | 57 |
" |
58 |
! ! |
|
59 |
||
359 | 60 |
!WeakIdentityDictionary methodsFor:'adding & removing'! |
61 |
||
1256 | 62 |
at:key put:anObject |
63 |
"add the argument anObject under key, aKey to the receiver. |
|
64 |
Return anObject (sigh). |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
65 |
Redefined to block interrupts, to avoid trouble when dependencies |
1256 | 66 |
are added within interrupting high prio processes." |
67 |
||
359 | 68 |
|wasBlocked| |
69 |
||
70 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1790 | 71 |
[ |
72 |
super at:key put:anObject. |
|
73 |
] valueNowOrOnUnwindDo:[ |
|
74 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
75 |
]. |
|
1256 | 76 |
^ anObject |
1223 | 77 |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
78 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
1790 | 79 |
"Modified: 20.10.1996 / 14:04:46 / cg" |
359 | 80 |
! |
81 |
||
82 |
removeKey:aKey ifAbsent:aBlock |
|
1256 | 83 |
"remove the association under aKey from the collection, |
84 |
return the value previously stored there.. |
|
85 |
If it was not in the collection return the result |
|
86 |
from evaluating aBlock. |
|
87 |
||
88 |
Redefined to avoid synchronization problems, in case |
|
89 |
of interrupts (otherwise, there could be some other operation |
|
90 |
on the receiver done by another process, which garbles my contents)." |
|
1054 | 91 |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
92 |
|wasBlocked ret| |
359 | 93 |
|
94 |
wasBlocked := OperatingSystem blockInterrupts. |
|
95 |
[ |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
96 |
ret := super removeKey:aKey ifAbsent:aBlock |
359 | 97 |
] valueNowOrOnUnwindDo:[ |
1054 | 98 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts] |
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
99 |
]. |
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
100 |
^ ret |
1054 | 101 |
|
1256 | 102 |
"Modified: 22.4.1996 / 17:39:57 / cg" |
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
103 |
"Modified: 6.5.1996 / 12:44:51 / stefan" |
359 | 104 |
! ! |
105 |
||
10 | 106 |
!WeakIdentityDictionary methodsFor:'element disposal'! |
107 |
||
108 |
informDispose |
|
109 |
"an element (either key or value) died - rehash" |
|
6 | 110 |
|
1780
1b3a4ddc5b94
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1343
diff
changeset
|
111 |
| wasBlocked | |
358 | 112 |
|
113 |
" |
|
114 |
have to block here - dispose may be done at a low priority |
|
115 |
from the background finalizer. If new views are opened in |
|
116 |
the foreground, the dependency dictionary may get corrupted |
|
117 |
otherwise |
|
118 |
" |
|
119 |
wasBlocked := OperatingSystem blockInterrupts. |
|
10 | 120 |
|
1780
1b3a4ddc5b94
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1343
diff
changeset
|
121 |
keyArray |
2027
4a2cdf9946ae
replace items with DeletedEntry, when objects are collected.
ca
parents:
1790
diff
changeset
|
122 |
forAllDeadIndicesDo:[:idx | |
4a2cdf9946ae
replace items with DeletedEntry, when objects are collected.
ca
parents:
1790
diff
changeset
|
123 |
(valueArray at:idx) notNil ifTrue:[ |
4a2cdf9946ae
replace items with DeletedEntry, when objects are collected.
ca
parents:
1790
diff
changeset
|
124 |
keyArray basicAt:idx put:DeletedEntry. |
4a2cdf9946ae
replace items with DeletedEntry, when objects are collected.
ca
parents:
1790
diff
changeset
|
125 |
valueArray basicAt:idx put:nil. |
4a2cdf9946ae
replace items with DeletedEntry, when objects are collected.
ca
parents:
1790
diff
changeset
|
126 |
tally := tally - 1. |
4a2cdf9946ae
replace items with DeletedEntry, when objects are collected.
ca
parents:
1790
diff
changeset
|
127 |
] |
4a2cdf9946ae
replace items with DeletedEntry, when objects are collected.
ca
parents:
1790
diff
changeset
|
128 |
] |
4a2cdf9946ae
replace items with DeletedEntry, when objects are collected.
ca
parents:
1790
diff
changeset
|
129 |
replacingCorpsesWith:DeletedEntry. |
358 | 130 |
|
359 | 131 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6 | 132 |
! ! |
609 | 133 |
|
134 |
!WeakIdentityDictionary methodsFor:'private'! |
|
135 |
||
136 |
keyContainerOfSize:n |
|
137 |
"return a container for keys of size n. |
|
138 |
use WeakArrays here." |
|
139 |
||
140 |
|w| |
|
141 |
||
142 |
w := WeakArray new:n. |
|
143 |
w watcher:self. |
|
144 |
^ w |
|
145 |
! ! |
|
146 |
||
635 | 147 |
!WeakIdentityDictionary class methodsFor:'documentation'! |
148 |
||
149 |
version |
|
2027
4a2cdf9946ae
replace items with DeletedEntry, when objects are collected.
ca
parents:
1790
diff
changeset
|
150 |
^ '$Header: /cvs/stx/stx/libbasic/WeakIdentityDictionary.st,v 1.22 1997-01-02 18:10:12 ca Exp $' |
635 | 151 |
! ! |