-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathFFMArray.java
More file actions
142 lines (124 loc) · 2.83 KB
/
Copy pathFFMArray.java
File metadata and controls
142 lines (124 loc) · 2.83 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
package javaforce.ffm;
import java.lang.foreign.*;
import javaforce.*;
/** FFMArray.
*
* Array Factory used by native C code to create Java arrays.
*
* Also serves as a cache for arrays.
*
* @author pquiring
*/
public class FFMArray {
private Object obj;
private long ptr;
private long ref;
private boolean string2;
public long pin() {
ptr = JFHeap.pin(obj);
return ptr;
}
public void unpin() {
JFHeap.unpin(obj, ptr, true);
ptr = 0;
}
public long ref() {
ref = JFHeap.ref(obj);
return ref;
}
public void unref() {
JFHeap.unref(ref);
}
private byte[] ba;
public long NewByteArray(int size) {
if (ptr != 0) unpin();
if (ba == null || ba.length != size) {
ba = new byte[size];
}
obj = ba;
return pin();
}
private short[] sa;
public long NewShortArray(int size) {
if (ptr != 0) unpin();
if (sa == null || sa.length != size) {
sa = new short[size];
}
obj = sa;
return pin();
}
private int[] ia;
public long NewIntArray(int size) {
if (ptr != 0) unpin();
if (ia == null || ia.length != size) {
ia = new int[size];
}
obj = ia;
return pin();
}
private long[] la;
public long NewLongArray(int size) {
if (ptr != 0) unpin();
if (la == null || la.length != size) {
la = new long[size];
}
obj = la;
return pin();
}
private float[] fa;
public long NewFloatArray(int size) {
if (ptr != 0) unpin();
if (fa == null || fa.length != size) {
fa = new float[size];
}
obj = fa;
return pin();
}
private double[] da;
public long NewDoubleArray(int size) {
if (ptr != 0) unpin();
if (da == null || da.length != size) {
da = new double[size];
}
obj = da;
return pin();
}
private String[] Sa;
public long NewStringArray(int size) {
if (ptr != 0) unpin();
//always create new String array
Sa = new String[size];
if (!string2) {
obj = Sa;
} else {
String[][] org = Sa2;
Sa2 = new String[org.length + 1][];
System.arraycopy(org, 0, Sa2, 0, org.length);
Sa2[org.length] = Sa;
}
return ref();
}
public void SetStringElement(int idx, MemorySegment str) {
if (obj == null) return;
if (idx < 0 || idx >= Sa.length) return;
Sa[idx] = FFM.getString(str);
}
private String[][] Sa2;
/** To create a String[][] call this method first and each time NewStringArray() is called it will be appended to this String[][] array. */
public long NewString2Array() {
if (ptr != 0) unpin();
//always create new String array
Sa2 = new String[0][];
obj = Sa2;
string2 = true;
return ref();
}
public Object getArray() {
if (obj == null) return null;
if (ptr != 0) unpin();
Object array = obj;
obj = null;
string2 = false;
return array;
}
}