forked from loongly/PureScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectStore.wrapper.cs
More file actions
204 lines (168 loc) · 4.76 KB
/
ObjectStore.wrapper.cs
File metadata and controls
204 lines (168 loc) · 4.76 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
public class WObject
{
private int _handle;
internal int Handle { get { return _handle; } }
internal void SetHandle(int handle)
{
_handle = handle;
}
}
public static class WObjectExtend
{
internal static int __GetHandle(this WObject obj)
{
if (object.ReferenceEquals(obj , null) )
return 0;
return obj.Handle;
}
}
internal static class ObjectStore
{
// Lookup handles by object.
static ConditionalWeakTable<object, HandleRef> objectHandleCache = new ConditionalWeakTable<object, HandleRef>();
// Stored objects. The first is never used so 0 can be "null".
static GCHandle[] Handles;
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern object NewObject(Type type);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern object OnException(Exception e);
private static Handler handler;
private static List<int> dropList = new List<int>();
static ObjectStore()
{
Init(1 << 16);
}
public static void Init(int maxObjects)
{
handler = Custom.GetHandler((int)HandlerType.Object);
// Initialize the objects as all null plus room for the
// first to always be null.
Handles = new GCHandle[maxObjects + 1];
}
public static int Store(object obj, int target = 0)
{
// Null is always zero
if (object.ReferenceEquals(obj, null))
{
return 0;
}
lock (Handles)
{
if (target == 0)
{
// Get handle from object cache
var h = GetHandle(obj);
if (h > 0)
return h;
target = handler.GrabHandle();
}
// Store the object
StoreInternal(target, obj);
return target;
}
}
private static void StoreInternal(int handle, object obj)
{
Handles[handle] = GCHandle.Alloc(obj, GCHandleType.Weak);
objectHandleCache.Add(obj, handle);
CheckDropList();
}
public static int GetHandle(object obj)
{
if (objectHandleCache.TryGetValue(obj, out var targetRef))
return targetRef.Handle;
return 0;
}
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Get(int handle)
{
var gcHandle = Handles[handle];
if (gcHandle.IsAllocated)
return gcHandle.Target;
return null;
}
public static T Get<T>(int handle)
where T : WObject
{
if (handle == 0)
return null;
var obj = Get(handle);
if(obj != null)
return (T) obj;
// create wrapper
var nObj = (T)NewObject(typeof(T));
nObj.SetHandle(handle);
StoreInternal(handle, nObj);
return nObj;
}
/// <summary>
/// when ~HandleRef()
/// </summary>
internal static void OnRemove(int handle)
{
// Null is never stored, so there's nothing to remove
if (handle == 0)
return;
lock (Handles)
{
dropList.Add(handle);
}
}
private static void CheckDropList()
{
lock (Handles)
{
if (dropList.Count > 0)
{
foreach (var handle in dropList)
{
// Forget the object
ref var gcHandle = ref Handles[handle];
if (gcHandle.IsAllocated)
{
var obj = gcHandle.Target;
gcHandle.Free();
//disable handle
int cur = handler.DropHandle(handle);
if (cur == handle)
Custom.RemoveHandle(handle);
// Remove the object from the cache
if (obj != null)
objectHandleCache.Remove(obj);
}
}
dropList.Clear();
}
}
}
/// <summary>
/// wrap int Handle to class
/// </summary>
internal class HandleRef
{
public int Handle;
public HandleRef(int handle)
{
Handle = handle;
}
public static implicit operator HandleRef(int handle)
{
return new HandleRef(handle);
}
public static implicit operator Int32(HandleRef r)
{
return r.Handle;
}
~HandleRef()
{
ObjectStore.OnRemove(Handle);
}
}
}