-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathFFM.java
More file actions
803 lines (693 loc) · 27.4 KB
/
Copy pathFFM.java
File metadata and controls
803 lines (693 loc) · 27.4 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
package javaforce.ffm;
import java.io.*;
import java.lang.foreign.*;
import java.lang.invoke.*;
import static java.lang.foreign.ValueLayout.*;
import javaforce.*;
import javaforce.io.*;
import javaforce.ui.*;
import javaforce.linux.*;
import javaforce.media.*;
/** FFM support class.
*
* The FFM system also uses JNI to pin arrays for best performance (hybrid system).
*
* @author pquiring
*/
public class FFM {
private static FFM instance;
/** Returns singleton instance of this class. */
public static FFM getInstance() {
if (instance == null) {
instance = new FFM();
}
return instance;
}
/** Returns FFM enabled state. */
public static boolean enabled() {
return true;
}
/** Enable FFM.
*
* SymbolLookup will be auto detected to use executable or shared library.
*/
public static void enable() {
//nop
}
/** Enable FFM.
*
* SymbolLookup will be from the supplied library.
*/
public static void enable(String lib) {
if (!new File(lib).exists()) {
JFLog.log("FFM:Error:Library not found:" + lib);
return;
}
FFM.lib = lib;
}
/** Disable FFM and use JNI instead. */
public static void disable() {
//nop
}
/** Enable FFM/JNI hybrid system (pinning arrays for best performance).
* Default = true
*/
public static void enableJNI() {
}
/** Disable FFM/JNI hybrid system. Performance will be degraded.
*/
public static void disableJNI() {
}
/** Returns JF native library from standard locations. */
public static String getLibrary() {
if (JF.isWindows()) {
return System.getenv("ProgramData") + "/JavaForce/jfnative64.dll";
} else {
return "/usr/lib/jfnative64.so";
}
}
public static boolean debug = false;
private static String lib; //shared JF library
private static Linker linker;
private static Arena arena;
private static SymbolLookup lookup;
private static ExecSymbolLookup execlookup;
private FFM() {
setupLinker();
setupJFHeap();
setupUpcalls();
}
private static void setupLinker() {
if (debug) JFLog.log("FFM.setupLinker");
try {
linker = Linker.nativeLinker();
if (lib == null) {
//auto detect executable or shared library
if (!JF.isJavaForceLoader()) {
lib = getLibrary();
if (!new File(lib).exists()) {
throw new Exception("FFM:Error:Library not found:" + lib);
}
}
}
if (lib != null) {
if (debug) JFLog.log("Loading FFM from:" + lib);
//symbol lookup from supplied shared library
arena = Arena.ofAuto(); //freed by gc (which will also close the library at that time)
lookup = SymbolLookup.libraryLookup(lib, arena);
} else {
if (debug) JFLog.log("Loading FFM from executable");
//symbol lookup from executable of this JVM (JavaForce native loader)
lookup = linker.defaultLookup();
execlookup = new ExecSymbolLookup();
execlookup.init();
}
} catch (Throwable t) {
JFLog.log(t);
}
}
/** Assign a new symbol lookup provider. */
public static void setSymbolLookup(SymbolLookup lookup) {
if (debug) JFLog.log("lookup=" + lookup);
FFM.lookup = lookup;
}
/** Get native FunctionDescriptor for function with no args and specified return value. */
public static FunctionDescriptor getFunctionDesciptor(MemoryLayout ret) {
return FunctionDescriptor.of(ret);
}
/** Get native FunctionDescriptor for function with specified return value and arguments. */
public static FunctionDescriptor getFunctionDesciptor(MemoryLayout ret, MemoryLayout... args) {
return FunctionDescriptor.of(ret, args);
}
/** Get native FunctionDescriptor for function with void return value and arguments. */
public static FunctionDescriptor getFunctionDesciptorVoid(MemoryLayout... args) {
return FunctionDescriptor.ofVoid(args);
}
/** Get native MethodHandle for specified function name with FunctionDescriptor. */
public static MethodHandle getFunction(String name, FunctionDescriptor fd) {
if (debug) JFLog.log("FFM:getFunction:" + name);
try {
MemorySegment addr = lookup.findOrThrow(name);
if (addr == null || addr.address() == 0) {
JFLog.log("FFM:Function not found(1):" + name + "=" + addr);
return null;
}
return linker.downcallHandle(addr, fd);
} catch (Exception e) {
JFLog.logTrace("FFM:Function not found(e):" + name);
return null;
}
}
/** Get native MethodHandle for specified function name with FunctionDescriptor where the symbol is a function pointer. */
public static MethodHandle getFunctionPtr(String name, FunctionDescriptor fd) {
if (debug) JFLog.log("FFM:getFunctionPtr:" + name);
try {
MemorySegment addr = lookup.findOrThrow(name);
if (addr == null || addr.address() == 0) {
JFLog.log("FFM:FunctionPtr not found(1):" + name + "=" + addr);
return null;
}
//symbols have zero length, need to reinterpret as a pointer
addr = addr.reinterpret(ADDRESS_SIZE);
if (addr == null || addr.address() == 0) {
JFLog.log("FFM:FunctionPtr not found(2):" + name + "=" + addr);
return null;
}
//now get the contents of the pointer
addr = addr.get(ADDRESS, 0);
if (addr == null || addr.address() == 0) {
JFLog.log("FFM:FunctionPtr not found(3):" + name + "=" + addr);
return null;
}
return linker.downcallHandle(addr, fd);
} catch (Exception e) {
JFLog.logTrace("FFM:FunctionPtr not found(e):" + name);
return null;
}
}
//upcall helpers
private static ValueLayout classToValueLayout(Class<?> cls) {
if (cls == boolean.class) return JAVA_BOOLEAN;
if (cls == byte.class) return JAVA_BYTE;
if (cls == short.class) return JAVA_SHORT;
if (cls == char.class) return JAVA_CHAR;
if (cls == int.class) return JAVA_INT;
if (cls == long.class) return JAVA_LONG;
if (cls == float.class) return JAVA_FLOAT;
if (cls == double.class) return JAVA_DOUBLE;
return ADDRESS;
}
private static Class[] ClassArrayType = new Class[0];
private static FunctionDescriptor convert(MethodType mt) {
// Map MethodType return type to MemoryLayout
Class returnClass = mt.returnType();
// Map MethodType parameters to MemoryLayouts
Class[] paramClasses = mt.parameterList().toArray(ClassArrayType);
int count = paramClasses.length;
ValueLayout[] paramLayouts = new ValueLayout[paramClasses.length];
for(int i=0;i<count;i++) {
Class cls = paramClasses[i];
paramLayouts[i] = classToValueLayout(cls);
}
if (returnClass == void.class) {
return FunctionDescriptor.ofVoid(paramLayouts);
} else {
return FunctionDescriptor.of(classToValueLayout(returnClass), paramLayouts);
}
}
/** Create up call stub to virtual function.
*
* UpCalls are EXPENSIVE and should be limited.
* The JVM tries to cache them and holds on to them for a while causing performance issues.
*/
public static MemorySegment getFunctionUpCall(Object obj, String method, Class ret, Class[] args, Arena arena) {
MethodType mt;
MethodHandle mh, bmh;
MethodHandles.Lookup lookup = MethodHandles.lookup();
mt = MethodType.methodType(ret, args);
try {
mh = lookup.findVirtual(obj.getClass(), method, mt);
bmh = mh.bindTo(obj);
return linker.upcallStub(bmh, convert(mt), arena);
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
/** Create up call stub to static function.
*
* UpCalls are EXPENSIVE and should be limited.
* The JVM tries to cache them and holds on to them for a while causing performance issues.
*/
public static MemorySegment getFunctionUpCallStatic(Class cls, String method, Class ret, Class[] args, Arena arena) {
MethodType mt;
MethodHandle mh;
MethodHandles.Lookup lookup = MethodHandles.lookup();
mt = MethodType.methodType(ret, args);
try {
mh = lookup.findStatic(cls, method, mt);
return linker.upcallStub(mh, convert(mt), arena);
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
/** Create java String from native String (char*) and then free() the native string. */
public static String getString(MemorySegment ms) {
if (ms == null || ms.address() == 0) return null;
String str = ms.reinterpret(Long.MAX_VALUE).getString(0L);
return str;
}
//array helpers
/** Create native array from float[] */
public static MemorySegment toMemory(Arena arena, float[] m) {
if (m == null) return MemorySegment.NULL;
return MemorySegment.ofAddress(JFHeap.pin(m));
}
/** Create native array from double[] */
public static MemorySegment toMemory(Arena arena, double[] m) {
if (m == null) return MemorySegment.NULL;
return MemorySegment.ofAddress(JFHeap.pin(m));
}
/** Create native array from long[] */
public static MemorySegment toMemory(Arena arena, long[] m) {
if (m == null) return MemorySegment.NULL;
return MemorySegment.ofAddress(JFHeap.pin(m));
}
/** Create native array from int[] */
public static MemorySegment toMemory(Arena arena, int[] m) {
if (m == null) return MemorySegment.NULL;
return MemorySegment.ofAddress(JFHeap.pin(m));
}
/** Create native array from short[] */
public static MemorySegment toMemory(Arena arena, short[] m) {
if (m == null) return MemorySegment.NULL;
return MemorySegment.ofAddress(JFHeap.pin(m));
}
/** Create native array from byte[] */
public static MemorySegment toMemory(Arena arena, byte[] m) {
if (m == null) return MemorySegment.NULL;
return MemorySegment.ofAddress(JFHeap.pin(m));
}
/** Create native array from String[] */
public static MemorySegment toMemory(Arena arena, String[] strs) {
if (strs == null) return MemorySegment.NULL;
int length = strs.length;
if (length == 0 || strs[length-1] != null) {
length++; //add NULL string to end of array
}
MemorySegment ptrs = arena.allocate(ADDRESS, length);
int idx = 0;
for(String str : strs) {
if (str == null) {
idx++;
continue;
}
MemorySegment ba = arena.allocateFrom(str);
ptrs.setAtIndex(ADDRESS, idx++, ba);
}
return ptrs;
}
/** Create native array from native void* array */
public static MemorySegment toMemory(Arena arena, MemorySegment[] ptrs) {
if (ptrs == null) return MemorySegment.NULL;
int length = ptrs.length;
if (length == 0 || ptrs[length-1] != null) {
length++; //add NULL pointer to end of array
}
MemorySegment array = arena.allocate(ADDRESS, length);
int idx = 0;
for(MemorySegment ptr : ptrs) {
if (ptr == null) {
idx++;
continue;
}
array.setAtIndex(ADDRESS, idx++, ptr);
}
return array;
}
public static MemorySegment toMemory(Arena arena, FFMStruct[] structs) {
if (structs == null) return MemorySegment.NULL;
int length = structs.length;
if (length == 0) return MemorySegment.NULL;
int size = structs[0].getSize();
MemorySegment array = arena.allocate(JAVA_BYTE, length * size);
int idx = 0;
for(FFMStruct struct : structs) {
MemorySegment src = struct.marshall(arena);
MemorySegment dst = array.asSlice(idx * size, size);
dst.copyFrom(src);
idx++;
}
return array;
}
public static MemorySegment toMemory(Arena arena, FFMType.Uint32[] types) {
if (types == null) return MemorySegment.NULL;
int length = types.length;
MemorySegment array = arena.allocate(JAVA_INT, length);
int idx = 0;
for(FFMType.Uint32 type : types) {
if (type == null) {
idx++;
continue;
}
array.setAtIndex(JAVA_INT, idx++, type.getValue());
}
return array;
}
public static MemorySegment toMemory(Arena arena, FFMType.Uint64[] types) {
if (types == null) return MemorySegment.NULL;
int length = types.length;
MemorySegment array = arena.allocate(JAVA_LONG, length);
int idx = 0;
for(FFMType.Uint64 type : types) {
if (type == null) {
idx++;
continue;
}
array.setAtIndex(JAVA_LONG, idx++, type.getValue());
}
return array;
}
private static final long JAVA_LONG_SIZE = JAVA_LONG.byteSize();
private static final long JAVA_INT_SIZE = JAVA_INT.byteSize();
private static final long JAVA_SHORT_SIZE = JAVA_SHORT.byteSize();
private static final long JAVA_BYTE_SIZE = JAVA_BYTE.byteSize();
private static final long ADDRESS_SIZE = ADDRESS.byteSize();
/** Copy back native array after function returns. */
public static void copyBack(MemorySegment seg, float[] m) {
if (seg == null || m == null) return;
JFHeap.unpin(m, seg.address(), true);
}
/** Copy back native array after function returns. */
public static void copyBack(MemorySegment seg, double[] m) {
if (seg == null || m == null) return;
JFHeap.unpin(m, seg.address(), true);
}
/** Copy back native array after function returns. */
public static void copyBack(MemorySegment seg, long[] m) {
if (seg == null || m == null) return;
JFHeap.unpin(m, seg.address(), true);
}
/** Copy back native array after function returns. */
public static void copyBack(MemorySegment seg, int[] m) {
if (seg == null || m == null) return;
JFHeap.unpin(m, seg.address(), true);
}
/** Copy back native array after function returns. */
public static void copyBack(MemorySegment seg, short[] m) {
if (seg == null || m == null) return;
JFHeap.unpin(m, seg.address(), true);
}
/** Copy back native array after function returns. */
public static void copyBack(MemorySegment seg, byte[] m) {
if (seg == null || m == null) return;
JFHeap.unpin(m, seg.address(), true);
}
/** Copy back native array after function returns. */
public static void copyBack(MemorySegment seg, String[] m) {
//nop
}
/** Copy back native array after function returns. */
public static void copyBack(MemorySegment seg, FFMStruct[] structs) {
if (structs == null) return;
int length = structs.length;
if (length == 0) return;
int size = structs[0].getSize();
int idx = 0;
for(FFMStruct struct : structs) {
MemorySegment src = seg.asSlice(idx * size, size);
MemorySegment dst = struct.getMemorySegment();
dst.copyFrom(src);
struct.unmarshall();
idx++;
}
}
/** Copy back native array after function returns. */
public static void copyBack(MemorySegment seg, FFMType.Uint32[] ints) {
if (seg == null || ints == null) return;
for(int idx=0;idx<ints.length;idx++) {
if (ints[idx] == null) continue;
ints[idx].setValue(seg.getAtIndex(JAVA_INT, idx));
}
}
/** Copy back native array after function returns. */
public static void copyBack(MemorySegment seg, FFMType.Uint64[] longs) {
if (seg == null || longs == null) return;
for(int idx=0;idx<longs.length;idx++) {
if (longs[idx] == null) continue;
longs[idx].setValue(seg.getAtIndex(JAVA_LONG, idx));
}
}
private static ThreadLocal<FFMArray> FFMArrayBin = new ThreadLocal<>();
public static void createFFMArray() {
FFMArray array = FFMArrayBin.get();
if (array == null) {
FFMArrayBin.set(new FFMArray());
}
}
private static long FFMArray_Pin() {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return 0;
return instance.pin();
}
private static void FFMArray_Unpin() {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return;
instance.unpin();
}
private static long FFMArray_NewByteArray(int size) {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return -1;
return instance.NewByteArray(size);
}
private static long FFMArray_NewShortArray(int size) {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return -1;
return instance.NewShortArray(size);
}
private static long FFMArray_NewIntArray(int size) {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return -1;
return instance.NewIntArray(size);
}
private static long FFMArray_NewLongArray(int size) {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return -1;
return instance.NewLongArray(size);
}
private static long FFMArray_NewFloatArray(int size) {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return -1;
return instance.NewFloatArray(size);
}
private static long FFMArray_NewDoubleArray(int size) {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return -1;
return instance.NewDoubleArray(size);
}
private static long FFMArray_NewStringArray(int size) {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return -1;
return instance.NewStringArray(size);
}
private static void FFMArray_SetStringElement(int idx, MemorySegment str) {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return;
instance.SetStringElement(idx, str);
}
private static long FFMArray_NewString2Array() {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return -1;
return instance.NewString2Array();
}
public static Object getArray() {
FFMArray instance = FFMArrayBin.get();
if (instance == null) return null;
return instance.getArray();
}
private static ThreadLocal<MediaIO> MediaIOBin = new ThreadLocal<>();
public static void setMediaIO(MediaIO io) {
MediaIOBin.set(io);
}
private static int MediaIO_read(MemorySegment data, int size) {
MediaIO instance = MediaIOBin.get();
if (instance == null) return -1;
byte[] byteArray = new byte[size];
int read = instance.read(byteArray);
if (read > 0) {
MemorySegment.copy(byteArray, 0, data.reinterpret(read), JAVA_BYTE, 0, read);
}
return read;
}
private static int MediaIO_write(MemorySegment data, int size) {
MediaIO instance = MediaIOBin.get();
if (instance == null) return -1;
byte[] byteArray = data.reinterpret(size).asSlice(0, size).toArray(JAVA_BYTE);
return instance.write(byteArray);
}
private static long MediaIO_seek(long pos, int how) {
MediaIO instance = MediaIOBin.get();
if (instance == null) return -1;
return instance.seek(pos, how);
}
private static ThreadLocal<FolderListener> FolderListenerBin = new ThreadLocal<>();
public static void setFolderListener(FolderListener listener) {
FolderListenerBin.set(listener);
}
private static void FolderListener_folderChangeEvent(MemorySegment event, MemorySegment path) {
FolderListener instance = FolderListenerBin.get();
if (instance == null) return;
String event_str = event.reinterpret(1024).getString(0);
if (debug) JFLog.log("event=" + event_str);
String path_str = path.reinterpret(1024).getString(0);
if (debug) JFLog.log("path=" + path_str);
instance.folderChangeEvent(event_str, path_str);
}
private static ThreadLocal<UIEvents> UIEventsBin = new ThreadLocal<>();
public static void setUIEvents(UIEvents events) {
UIEventsBin.set(events);
}
private static void UIEvents_dispatchEvent(int type, int v1, int v2) {
UIEvents instance = UIEventsBin.get();
if (instance == null) return;
instance.dispatchEvent(type, v1, v2);
}
private static ThreadLocal<X11Listener> X11ListenerBin = new ThreadLocal<>();
public static void setX11Listener(X11Listener listener) {
X11ListenerBin.set(listener);
}
public static void X11Listener_trayIconAdded(int count) {
X11Listener listener = X11ListenerBin.get();
if (listener == null) return;
listener.trayIconAdded(count);
}
public static void X11Listener_trayIconRemoved(int count) {
X11Listener listener = X11ListenerBin.get();
if (listener == null) return;
listener.trayIconRemoved(count);
}
public static void X11Listener_windowsChanged() {
X11Listener listener = X11ListenerBin.get();
if (listener == null) return;
listener.windowsChanged();
}
public static void X11Listener_windowAdded(long xid, int pid, MemorySegment title, MemorySegment name, MemorySegment res_name, MemorySegment res_class) {
X11Listener listener = X11ListenerBin.get();
if (listener == null) return;
listener.windowAdded(xid, pid, title.reinterpret(1024).getString(0), name.reinterpret(1024).getString(0), res_name.reinterpret(1024).getString(0), res_class.reinterpret(1024).getString(0));
}
public static void X11Listener_windowDeleted(long xid) {
X11Listener listener = X11ListenerBin.get();
if (listener == null) return;
listener.windowDeleted(xid);
}
private static Arena global;
public static MemorySegment upcall_FFMArray;
public static MemorySegment upcall_FFMArray_Pin;
public static MemorySegment upcall_FFMArray_Unpin;
public static MemorySegment upcall_FFMArray_NewByteArray;
public static MemorySegment upcall_FFMArray_NewShortArray;
public static MemorySegment upcall_FFMArray_NewIntArray;
public static MemorySegment upcall_FFMArray_NewLongArray;
public static MemorySegment upcall_FFMArray_NewFloatArray;
public static MemorySegment upcall_FFMArray_NewDoubleArray;
public static MemorySegment upcall_FFMArray_NewStringArray;
public static MemorySegment upcall_FFMArray_SetStringElement;
public static MemorySegment upcall_FFMArray_NewString2Array;
public static MemorySegment upcall_MediaIO;
public static MemorySegment upcall_MediaIO_read;
public static MemorySegment upcall_MediaIO_write;
public static MemorySegment upcall_MediaIO_seek;
public static MemorySegment upcall_FolderListener_folderChangeEvent;
public static MemorySegment upcall_UIEvents_dispatchEvent;
public static MemorySegment upcall_X11Listener;
public static MemorySegment upcall_X11Listener_trayIconAdded;
public static MemorySegment upcall_X11Listener_trayIconRemoved;
public static MemorySegment upcall_X11Listener_windowsChanged;
public static MemorySegment upcall_X11Listener_windowAdded;
public static MemorySegment upcall_X11Listener_windowDeleted;
/** Setup static C up calls.
*
* Upcalls are used from C to invoke Java methods.
*/
public static void setupUpcalls() {
if (debug) JFLog.log("FFM.setupUpcalls");
if (global != null) return;
global = Arena.global();
if (linker == null) {
setupLinker();
}
Class cls = FFM.class;
upcall_FFMArray_Pin = getFunctionUpCallStatic(cls, "FFMArray_Pin", long.class, new Class[] {}, global);
upcall_FFMArray_Unpin = getFunctionUpCallStatic(cls, "FFMArray_Unpin", void.class, new Class[] {}, global);
upcall_FFMArray_NewByteArray = getFunctionUpCallStatic(cls, "FFMArray_NewByteArray", long.class, new Class[] {int.class}, global);
upcall_FFMArray_NewShortArray = getFunctionUpCallStatic(cls, "FFMArray_NewShortArray", long.class, new Class[] {int.class}, global);
upcall_FFMArray_NewIntArray = getFunctionUpCallStatic(cls, "FFMArray_NewIntArray", long.class, new Class[] {int.class}, global);
upcall_FFMArray_NewLongArray = getFunctionUpCallStatic(cls, "FFMArray_NewLongArray", long.class, new Class[] {int.class}, global);
upcall_FFMArray_NewFloatArray = getFunctionUpCallStatic(cls, "FFMArray_NewFloatArray", long.class, new Class[] {int.class}, global);
upcall_FFMArray_NewDoubleArray = getFunctionUpCallStatic(cls, "FFMArray_NewDoubleArray", long.class, new Class[] {int.class}, global);
upcall_FFMArray_NewStringArray = getFunctionUpCallStatic(cls, "FFMArray_NewStringArray", long.class, new Class[] {int.class}, global);
upcall_FFMArray_SetStringElement = getFunctionUpCallStatic(cls, "FFMArray_SetStringElement", void.class, new Class[] {int.class, MemorySegment.class}, global);
upcall_FFMArray_NewString2Array = getFunctionUpCallStatic(cls, "FFMArray_NewString2Array", long.class, new Class[] {}, global);
upcall_FFMArray = toMemory(global, new MemorySegment[] {
upcall_FFMArray_Pin,
upcall_FFMArray_Unpin,
upcall_FFMArray_NewByteArray,
upcall_FFMArray_NewShortArray,
upcall_FFMArray_NewIntArray,
upcall_FFMArray_NewLongArray,
upcall_FFMArray_NewFloatArray,
upcall_FFMArray_NewDoubleArray,
upcall_FFMArray_NewStringArray,
upcall_FFMArray_SetStringElement,
upcall_FFMArray_NewString2Array,
});
upcall_MediaIO_read = getFunctionUpCallStatic(cls, "MediaIO_read", int.class, new Class[] {MemorySegment.class, int.class}, global);
upcall_MediaIO_write = getFunctionUpCallStatic(cls, "MediaIO_write", int.class, new Class[] {MemorySegment.class, int.class}, global);
upcall_MediaIO_seek = getFunctionUpCallStatic(cls, "MediaIO_seek", long.class, new Class[] {long.class, int.class}, global);
upcall_MediaIO = toMemory(global, new MemorySegment[] {
upcall_MediaIO_read,
upcall_MediaIO_write,
upcall_MediaIO_seek,
});
upcall_FolderListener_folderChangeEvent = getFunctionUpCallStatic(cls, "FolderListener_folderChangeEvent", void.class, new Class[] {MemorySegment.class, MemorySegment.class}, global);
upcall_UIEvents_dispatchEvent = getFunctionUpCallStatic(cls, "UIEvents_dispatchEvent", void.class, new Class[] {int.class, int.class, int.class}, global);
upcall_X11Listener_trayIconAdded = getFunctionUpCallStatic(cls, "X11Listener_trayIconAdded", void.class, new Class[] {int.class}, global);
upcall_X11Listener_trayIconRemoved = getFunctionUpCallStatic(cls, "X11Listener_trayIconRemoved", void.class, new Class[] {int.class}, global);
upcall_X11Listener_windowsChanged = getFunctionUpCallStatic(cls, "X11Listener_windowsChanged", void.class, new Class[] {}, global);
upcall_X11Listener_windowAdded = getFunctionUpCallStatic(cls, "X11Listener_windowAdded", void.class, new Class[] {long.class,int.class,MemorySegment.class,MemorySegment.class,MemorySegment.class,MemorySegment.class}, global);
upcall_X11Listener_windowDeleted = getFunctionUpCallStatic(cls, "X11Listener_windowDeleted", void.class, new Class[] {long.class}, global);
upcall_X11Listener = toMemory(global, new MemorySegment[] {
upcall_X11Listener_trayIconAdded,
upcall_X11Listener_trayIconRemoved,
upcall_X11Listener_windowsChanged,
upcall_X11Listener_windowAdded,
upcall_X11Listener_windowDeleted,
});
//pass FFMArray upcalls to native system
MethodHandle setup = FFM.getFunctionPtr("_set_upcall_FFMArray", FFM.getFunctionDesciptorVoid(ADDRESS));
try {
setup.invokeExact(upcall_FFMArray);
} catch (Throwable t) {
JFLog.log(t);
}
}
/** Get Native JF Version */
public String getVersion() {
if (debug) JFLog.log("FFM.getVersion");
MethodHandle setup = FFM.getFunctionPtr("_get_javaforce_version", FFM.getFunctionDesciptor(ADDRESS));
String version = null;
try {
MemorySegment seg = (MemorySegment)setup.invokeExact();
version = seg.reinterpret(1024).getString(0);
} catch (Throwable t) {
JFLog.log(t);
}
return version;
}
/** Setup JNI methods in JFHeap */
private static void setupJFHeap() {
if (debug) JFLog.log("FFM.setupJNI");
MethodHandle setup = FFM.getFunctionPtr("_setup_JFHeap", FFM.getFunctionDesciptor(JAVA_BOOLEAN));
try {
boolean result = (boolean)setup.invokeExact();
if (!result) {
throw new Exception("FFM.setupJFHeap() failed!");
}
} catch (Throwable t) {
JFLog.log(t);
}
}
private static ThreadLocal<Long> obj_ref = new ThreadLocal<>();
/** Reference object (limit 1 per method invocation) */
public static long ref_object(Object obj) {
long ref = JFHeap.ref(obj);
obj_ref.set(ref);
return ref;
}
/** Free reference to object. */
public static void unref_object(Object obj) {
JFHeap.unref(obj_ref.get());
}
}