-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathLWJGLSaferAllocMemoryAllocator.java
More file actions
272 lines (236 loc) · 8.81 KB
/
LWJGLSaferAllocMemoryAllocator.java
File metadata and controls
272 lines (236 loc) · 8.81 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
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
package com.jme3.util;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.logging.Logger;
import org.lwjgl.system.MemoryUtil;
public final class LWJGLSaferAllocMemoryAllocator implements MemoryUtil.MemoryAllocator {
private static final Logger logger = Logger.getLogger(LWJGLSaferAllocMemoryAllocator.class.getName());
public static final String SAFER_BUFFER_ALLOCATOR_CLASS = "com.jme3.util.SaferBufferAllocator";
private static final Bindings BINDINGS = Bindings.create();
private final long fnMalloc;
private final long fnCalloc;
private final long fnRealloc;
private final long fnFree;
private final long fnAlignedAlloc;
private final long fnAlignedFree;
public static boolean isAvailable() {
return BINDINGS != null;
}
public LWJGLSaferAllocMemoryAllocator() {
if (BINDINGS == null) {
throw new IllegalStateException(SAFER_BUFFER_ALLOCATOR_CLASS + " is not available on classpath.");
}
logger.info(getClass().getSimpleName() + " enabled!");
this.fnMalloc = BINDINGS.getMallocFunctionPointer();
this.fnCalloc = BINDINGS.getCallocFunctionPointer();
this.fnRealloc = BINDINGS.getReallocFunctionPointer();
this.fnFree = BINDINGS.getFreeFunctionPointer();
this.fnAlignedAlloc = BINDINGS.getAlignedAllocFunctionPointer();
this.fnAlignedFree = BINDINGS.getAlignedFreeFunctionPointer();
}
@Override
public long getMalloc() {
return fnMalloc;
}
@Override
public long getCalloc() {
return fnCalloc;
}
@Override
public long getRealloc() {
return fnRealloc;
}
@Override
public long getFree() {
return fnFree;
}
@Override
public long getAlignedAlloc() {
return fnAlignedAlloc;
}
@Override
public long getAlignedFree() {
return fnAlignedFree;
}
@Override
public long malloc(long size) {
return BINDINGS.malloc(size);
}
@Override
public long calloc(long num, long size) {
return BINDINGS.calloc(num, size);
}
@Override
public long realloc(long ptr, long size) {
return BINDINGS.realloc(ptr, size);
}
@Override
public void free(long ptr) {
BINDINGS.free(ptr);
}
@Override
public long aligned_alloc(long alignment, long size) {
return BINDINGS.alignedAlloc(alignment, size);
}
@Override
public void aligned_free(long ptr) {
BINDINGS.alignedFree(ptr);
}
private static final class Bindings {
private final MethodHandle mallocFnPtr;
private final MethodHandle callocFnPtr;
private final MethodHandle reallocFnPtr;
private final MethodHandle freeFnPtr;
private final MethodHandle alignedAllocFnPtr;
private final MethodHandle alignedFreeFnPtr;
private final MethodHandle malloc;
private final MethodHandle calloc;
private final MethodHandle realloc;
private final MethodHandle free;
private final MethodHandle alignedAlloc;
private final MethodHandle alignedFree;
private Bindings(
MethodHandle mallocFnPtr,
MethodHandle callocFnPtr,
MethodHandle reallocFnPtr,
MethodHandle freeFnPtr,
MethodHandle alignedAllocFnPtr,
MethodHandle alignedFreeFnPtr,
MethodHandle malloc,
MethodHandle calloc,
MethodHandle realloc,
MethodHandle free,
MethodHandle alignedAlloc,
MethodHandle alignedFree) {
this.mallocFnPtr = mallocFnPtr;
this.callocFnPtr = callocFnPtr;
this.reallocFnPtr = reallocFnPtr;
this.freeFnPtr = freeFnPtr;
this.alignedAllocFnPtr = alignedAllocFnPtr;
this.alignedFreeFnPtr = alignedFreeFnPtr;
this.malloc = malloc;
this.calloc = calloc;
this.realloc = realloc;
this.free = free;
this.alignedAlloc = alignedAlloc;
this.alignedFree = alignedFree;
}
static Bindings create() {
try {
Class<?> clazz = Class.forName(SAFER_BUFFER_ALLOCATOR_CLASS, true,
LWJGLSaferAllocMemoryAllocator.class.getClassLoader());
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
return new Bindings(
lookup.findStatic(clazz, "getMallocFunctionPointer", MethodType.methodType(long.class)),
lookup.findStatic(clazz, "getCallocFunctionPointer", MethodType.methodType(long.class)),
lookup.findStatic(clazz, "getReallocFunctionPointer", MethodType.methodType(long.class)),
lookup.findStatic(clazz, "getFreeFunctionPointer", MethodType.methodType(long.class)),
lookup.findStatic(clazz, "getAlignedAllocFunctionPointer", MethodType.methodType(long.class)),
lookup.findStatic(clazz, "getAlignedFreeFunctionPointer", MethodType.methodType(long.class)),
lookup.findStatic(clazz, "malloc", MethodType.methodType(long.class, long.class)),
lookup.findStatic(clazz, "calloc", MethodType.methodType(long.class, long.class, long.class)),
lookup.findStatic(clazz, "realloc", MethodType.methodType(long.class, long.class, long.class)),
lookup.findStatic(clazz, "free", MethodType.methodType(void.class, long.class)),
lookup.findStatic(clazz, "alignedAlloc",
MethodType.methodType(long.class, long.class, long.class)),
lookup.findStatic(clazz, "alignedFree", MethodType.methodType(void.class, long.class)));
} catch (ReflectiveOperationException | LinkageError e) {
return null;
}
}
long getMallocFunctionPointer() {
try {
return (long) mallocFnPtr.invokeExact();
} catch (Throwable t) {
throw unchecked(t);
}
}
long getCallocFunctionPointer() {
try {
return (long) callocFnPtr.invokeExact();
} catch (Throwable t) {
throw unchecked(t);
}
}
long getReallocFunctionPointer() {
try {
return (long) reallocFnPtr.invokeExact();
} catch (Throwable t) {
throw unchecked(t);
}
}
long getFreeFunctionPointer() {
try {
return (long) freeFnPtr.invokeExact();
} catch (Throwable t) {
throw unchecked(t);
}
}
long getAlignedAllocFunctionPointer() {
try {
return (long) alignedAllocFnPtr.invokeExact();
} catch (Throwable t) {
throw unchecked(t);
}
}
long getAlignedFreeFunctionPointer() {
try {
return (long) alignedFreeFnPtr.invokeExact();
} catch (Throwable t) {
throw unchecked(t);
}
}
long malloc(long size) {
try {
return (long) malloc.invokeExact(size);
} catch (Throwable t) {
throw unchecked(t);
}
}
long calloc(long num, long size) {
try {
return (long) calloc.invokeExact(num, size);
} catch (Throwable t) {
throw unchecked(t);
}
}
long realloc(long ptr, long size) {
try {
return (long) realloc.invokeExact(ptr, size);
} catch (Throwable t) {
throw unchecked(t);
}
}
void free(long ptr) {
try {
free.invokeExact(ptr);
} catch (Throwable t) {
throw unchecked(t);
}
}
long alignedAlloc(long alignment, long size) {
try {
return (long) alignedAlloc.invokeExact(alignment, size);
} catch (Throwable t) {
throw unchecked(t);
}
}
void alignedFree(long ptr) {
try {
alignedFree.invokeExact(ptr);
} catch (Throwable t) {
throw unchecked(t);
}
}
private RuntimeException unchecked(Throwable t) {
if (t instanceof RuntimeException) {
return (RuntimeException) t;
}
if (t instanceof Error) {
throw (Error) t;
}
return new RuntimeException(t);
}
}
}