IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
JNIGlobalRefTable.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Runtime.InteropServices;
4
5namespace IKVM.Runtime.JNI
6{
7
11 static class JNIGlobalRefTable
12 {
13
14 readonly static List<object> globalRefs = new List<object>();
15 internal static readonly object weakRefLock = new object();
16 internal static GCHandle[] weakRefs = new GCHandle[16];
17
23 internal static object Unwrap(nint z)
24 {
25 var i = -(int)z;
26 if ((i & (1 << 30)) != 0)
27 lock (weakRefLock)
28 return weakRefs[i - (1 << 30)].Target;
29 else
30 lock (globalRefs)
31 return globalRefs[i - 1];
32 }
33
39 public static nint AddGlobalRef(object o)
40 {
41 lock (globalRefs)
42 {
43 int index = globalRefs.IndexOf(null);
44 if (index >= 0)
45 {
46 globalRefs[index] = o;
47 }
48 else
49 {
50 index = globalRefs.Count;
51 globalRefs.Add(o);
52 }
53
54 return -(index + 1);
55 }
56 }
57
62 public static void DeleteGlobalRef(nint o)
63 {
64 lock (globalRefs)
65 globalRefs[(-(int)o) - 1] = null;
66 }
67
73 public static nint AddWeakGlobalRef(object o)
74 {
75 lock (weakRefLock)
76 {
77 for (int i = 0; i < weakRefs.Length; i++)
78 {
79 if (weakRefs[i].IsAllocated == false)
80 {
81 weakRefs[i] = GCHandle.Alloc(o, GCHandleType.WeakTrackResurrection);
82 return -(i | (1 << 30));
83 }
84 }
85
86 var len = weakRefs.Length;
87 var tmp = new GCHandle[len * 2];
88 Array.Copy(weakRefs, 0, tmp, 0, len);
89 tmp[len] = GCHandle.Alloc(o, GCHandleType.WeakTrackResurrection);
90 weakRefs = tmp;
91 return -(len | (1 << 30));
92 }
93 }
94
95
100 public static void DeleteWeakGlobalRef(nint o)
101 {
102 lock (weakRefLock)
103 weakRefs[-o - (1 << 30)].Free();
104 }
105
106 }
107
108}
Maintains a set of global refereces to instances.
static void DeleteGlobalRef(nint o)
Deletes a global ref from the table.
static nint AddWeakGlobalRef(object o)
Adds a new global ref to the table.
static void DeleteWeakGlobalRef(nint o)
Deletes a weak global ref from the table.
static nint AddGlobalRef(object o)
Adds a new global ref to the table.