~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/classpath/ikvm/internal/WeakIdentityMap.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2006, 2007 Jeroen Frijters
 
3
 
 
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.
 
7
 
 
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:
 
11
 
 
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.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
package ikvm.internal;
 
25
 
 
26
import cli.System.GC;
 
27
import cli.System.WeakReference;
 
28
 
 
29
@ikvm.lang.Internal
 
30
public final class WeakIdentityMap
 
31
{
 
32
    private WeakReference[] keys = new WeakReference[16];
 
33
    private Object[] values = new Object[keys.length];
 
34
 
 
35
    public WeakIdentityMap()
 
36
    {
 
37
        for (int i = 0; i < keys.length; i++)
 
38
        {
 
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]);
 
44
        }
 
45
    }
 
46
 
 
47
    protected void finalize()
 
48
    {
 
49
        for (int i = 0; i < keys.length; i++)
 
50
        {
 
51
            if (keys[i] != null)
 
52
            {
 
53
                GC.ReRegisterForFinalize(keys[i]);
 
54
            }
 
55
        }
 
56
    }
 
57
 
 
58
    public synchronized Object remove(Object key)
 
59
    {
 
60
        for (int i = 0; i < keys.length; i++)
 
61
        {
 
62
            if (keys[i].get_Target() == key)
 
63
            {
 
64
                Object value = values[i];
 
65
                keys[i].set_Target(null);
 
66
                values[i] = null;
 
67
                return value;
 
68
            }
 
69
        }
 
70
        return null;
 
71
    }
 
72
 
 
73
    // Note that null values are supported, null keys are not
 
74
    public synchronized void put(Object key, Object value)
 
75
    {
 
76
        if (key == null)
 
77
            throw new NullPointerException();
 
78
        putImpl(key, value, true);
 
79
    }
 
80
 
 
81
    private void putImpl(Object key, Object value, boolean tryGC)
 
82
    {
 
83
        int emptySlot = -1;
 
84
        int keySlot = -1;
 
85
        for (int i = 0; i < keys.length; i++)
 
86
        {
 
87
            Object k = keys[i].get_Target();
 
88
            if (k == null)
 
89
            {
 
90
                emptySlot = i;
 
91
                values[i] = null;
 
92
            }
 
93
            else if (k == key)
 
94
            {
 
95
                keySlot = i;
 
96
            }
 
97
        }
 
98
        if (keySlot != -1)
 
99
        {
 
100
            values[keySlot] = value;
 
101
        }
 
102
        else if (emptySlot != -1)
 
103
        {
 
104
            keys[emptySlot].set_Target(key);
 
105
            values[emptySlot] = value;
 
106
        }
 
107
        else
 
108
        {
 
109
            if (tryGC)
 
110
            {
 
111
                GC.Collect(0);
 
112
                putImpl(key, value, false);
 
113
                return;
 
114
            }
 
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);
 
120
            keys = newkeys;
 
121
            values = newvalues;
 
122
            for (int i = len; i < keys.length; i++)
 
123
            {
 
124
                keys[i] = new WeakReference(null, true);
 
125
                GC.SuppressFinalize(keys[i]);
 
126
            }
 
127
            keys[len].set_Target(key);
 
128
            values[len] = value;
 
129
        }
 
130
    }
 
131
 
 
132
    public synchronized Object get(Object key)
 
133
    {
 
134
        for (int i = 0; i < keys.length; i++)
 
135
        {
 
136
            if (keys[i].get_Target() == key)
 
137
            {
 
138
                return values[i];
 
139
            }
 
140
        }
 
141
        return null;
 
142
    }
 
143
 
 
144
    public synchronized boolean containsKey(Object key)
 
145
    {
 
146
        for (int i = 0; i < keys.length; i++)
 
147
        {
 
148
            if (keys[i].get_Target() == key)
 
149
            {
 
150
                return true;
 
151
            }
 
152
        }
 
153
        return false;
 
154
    }
 
155
}