forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeakHashtable.cs
281 lines (234 loc) · 5.99 KB
/
WeakHashtable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using NHibernate.DebugHelpers;
namespace NHibernate.Util
{
// This class does not inherit from WeakReference but uses composition
// instead to avoid requiring UnmanagedCode permission.
[DebuggerTypeProxy(typeof(DictionaryProxy))]
[Serializable]
public class WeakRefWrapper : IDeserializationCallback
{
private WeakReference reference;
// hashcode may vary among processes, they cannot be stored and have to be re-computed after deserialization
[NonSerialized]
private int? _hashCode;
public WeakRefWrapper(object target)
{
reference = new WeakReference(target);
// No need to delay computation here, but we need the lazy for the deserialization case.
_hashCode = GenerateHashCode();
}
public override bool Equals(object obj)
{
if (this == obj)
{
return true;
}
WeakRefWrapper that = obj as WeakRefWrapper;
if (that == null)
{
return false;
}
object target = Target;
if (target == null)
{
// If the reference is dead, we are only equal to ourselves,
// and this was checked above.
return false;
}
return GetHashCode() == that.GetHashCode() &&
Equals(target, that.Target);
}
public override int GetHashCode()
{
// If the object is put in a set or dictionary during deserialization, the hashcode will not yet be
// computed. Compute the hashcode on the fly. So long as this happens only during deserialization, there
// will be no thread safety issues. For the hashcode to be always defined after deserialization, the
// deserialization callback is used.
// As the reference may (unlikely) die between the computation here and the deserialization callback,
// better also store the computed on the fly value.
return _hashCode ?? (_hashCode = GenerateHashCode()).Value;
}
/// <inheritdoc />
public void OnDeserialization(object sender)
{
// If the value has already been computed for some reasons, keep-it. Otherwise the hashcode may change
// due to the reference having died.
if (!_hashCode.HasValue)
_hashCode = GenerateHashCode();
}
private int GenerateHashCode()
{
// Defaulting to the base value is not a trouble, because a dead reference is considered only equal to
// itself.
return reference.Target?.GetHashCode() ?? base.GetHashCode();
}
public object Target
{
get { return reference.Target; }
}
public bool IsAlive
{
get { return reference.IsAlive; }
}
public static WeakRefWrapper Wrap(object value)
{
return new WeakRefWrapper(value);
}
public static object Unwrap(object value)
{
if (value == null)
{
return null;
}
return ((WeakRefWrapper) value).Target;
}
}
public class WeakEnumerator : IDictionaryEnumerator
{
private IDictionaryEnumerator innerEnumerator;
public WeakEnumerator(IDictionaryEnumerator innerEnumerator)
{
this.innerEnumerator = innerEnumerator;
}
// Unwrapped key and value stored here so that they
// do not get collected between calls to MoveNext and Current.
private object currentKey, currentValue;
public object Key
{
get { return currentKey; }
}
public object Value
{
get { return currentValue; }
}
public DictionaryEntry Entry
{
get { return new DictionaryEntry(currentKey, currentValue); }
}
public bool MoveNext()
{
// Skip any garbage-collected key/value pairs
while (true)
{
currentKey = null;
currentValue = null;
if (!innerEnumerator.MoveNext())
{
return false;
}
currentKey = WeakRefWrapper.Unwrap(innerEnumerator.Key);
currentValue = WeakRefWrapper.Unwrap(innerEnumerator.Value);
if (currentKey != null && currentValue != null)
{
break;
}
}
return true;
}
public void Reset()
{
innerEnumerator.Reset();
currentKey = currentValue = null;
}
public object Current
{
get { return Entry; }
}
}
[Serializable]
public class WeakHashtable : IDictionary
{
private Hashtable innerHashtable = new Hashtable();
public void Scavenge()
{
var deadKeys = new List<object>();
foreach (DictionaryEntry de in innerHashtable)
{
WeakRefWrapper key = (WeakRefWrapper) de.Key;
WeakRefWrapper value = (WeakRefWrapper) de.Value;
if (!key.IsAlive || !value.IsAlive)
{
deadKeys.Add(key);
}
}
foreach (object key in deadKeys)
{
innerHashtable.Remove(key);
}
}
public bool Contains(object key)
{
return innerHashtable.Contains(WeakRefWrapper.Wrap(key));
}
public void Add(object key, object value)
{
Scavenge();
innerHashtable.Add(WeakRefWrapper.Wrap(key), WeakRefWrapper.Wrap(value));
}
public void Clear()
{
innerHashtable.Clear();
}
public IDictionaryEnumerator GetEnumerator()
{
return new WeakEnumerator(innerHashtable.GetEnumerator());
}
public void Remove(object key)
{
innerHashtable.Remove(WeakRefWrapper.Wrap(key));
}
public object this[object key]
{
get { return WeakRefWrapper.Unwrap(innerHashtable[WeakRefWrapper.Wrap(key)]); }
set
{
Scavenge();
innerHashtable[WeakRefWrapper.Wrap(key)] = WeakRefWrapper.Wrap(value);
}
}
public ICollection Keys
{
get { throw new NotImplementedException(); }
}
public ICollection Values
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { return innerHashtable.IsReadOnly; }
}
public bool IsFixedSize
{
get { return innerHashtable.IsFixedSize; }
}
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
/// <summary>
/// Count of elements in the collection. Unreliable!
/// </summary>
public int Count
{
get { return innerHashtable.Count; }
}
public object SyncRoot
{
get { return innerHashtable.SyncRoot; }
}
public bool IsSynchronized
{
get { return innerHashtable.IsSynchronized; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}