2
Copyright (C) 2006, 2007 Jeroen Frijters
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
24
package ikvm.internal;
27
import cli.System.WeakReference;
30
public final class WeakIdentityMap
32
private WeakReference[] keys = new WeakReference[16];
33
private Object[] values = new Object[keys.length];
35
public WeakIdentityMap()
37
for (int i = 0; i < keys.length; i++)
39
keys[i] = new WeakReference(null, true);
40
// NOTE we suppress finalization, to make sure the WeakReference continues to work
41
// while the AppDomain is finalizing for unload (note that for this to work,
42
// the code that instantiates us also has to call SuppressFinalize on us.)
43
GC.SuppressFinalize(keys[i]);
47
protected void finalize()
49
for (int i = 0; i < keys.length; i++)
53
GC.ReRegisterForFinalize(keys[i]);
58
public synchronized Object remove(Object key)
60
for (int i = 0; i < keys.length; i++)
62
if (keys[i].get_Target() == key)
64
Object value = values[i];
65
keys[i].set_Target(null);
73
// Note that null values are supported, null keys are not
74
public synchronized void put(Object key, Object value)
77
throw new NullPointerException();
78
putImpl(key, value, true);
81
private void putImpl(Object key, Object value, boolean tryGC)
85
for (int i = 0; i < keys.length; i++)
87
Object k = keys[i].get_Target();
100
values[keySlot] = value;
102
else if (emptySlot != -1)
104
keys[emptySlot].set_Target(key);
105
values[emptySlot] = value;
112
putImpl(key, value, false);
115
int len = keys.length;
116
WeakReference[] newkeys = new WeakReference[len * 2];
117
Object[] newvalues = new Object[newkeys.length];
118
cli.System.Array.Copy((cli.System.Array)(Object)keys, (cli.System.Array)(Object)newkeys, len);
119
cli.System.Array.Copy((cli.System.Array)(Object)values, (cli.System.Array)(Object)newvalues, len);
122
for (int i = len; i < keys.length; i++)
124
keys[i] = new WeakReference(null, true);
125
GC.SuppressFinalize(keys[i]);
127
keys[len].set_Target(key);
132
public synchronized Object get(Object key)
134
for (int i = 0; i < keys.length; i++)
136
if (keys[i].get_Target() == key)
144
public synchronized boolean containsKey(Object key)
146
for (int i = 0; i < keys.length; i++)
148
if (keys[i].get_Target() == key)