/
CArrayInstance.java
268 lines (230 loc) · 9.51 KB
/
CArrayInstance.java
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
package org.perl6.nqp.sixmodel.reprs;
import java.util.Arrays;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import org.perl6.nqp.runtime.ExceptionHandling;
import org.perl6.nqp.runtime.NativeCallOps;
import org.perl6.nqp.runtime.Ops;
import org.perl6.nqp.runtime.ThreadContext;
import org.perl6.nqp.sixmodel.SixModelObject;
import org.perl6.nqp.sixmodel.reprs.CArrayREPRData.ElemKind;
import org.perl6.nqp.sixmodel.reprs.NativeCall.ArgType;
public class CArrayInstance extends SixModelObject implements Refreshable {
public Pointer storage;
public SixModelObject[] child_objs;
public boolean managed;
public long allocated;
public long elems;
public void at_pos_native(ThreadContext tc, long index) {
CArrayREPRData repr_data = (CArrayREPRData) st.REPRData;
if (managed && index >= elems) {
if (repr_data.elem_kind == ElemKind.INTEGER) {
tc.native_type = ThreadContext.NATIVE_INT;
tc.native_i = 0;
}
else if (repr_data.elem_kind == ElemKind.NUMERIC) {
tc.native_type = ThreadContext.NATIVE_NUM;
tc.native_n = 0;
}
else {
ExceptionHandling.dieInternal(tc, "CArray can only at_pos_native with ints and nums.");
}
return;
}
if (repr_data.elem_kind == ElemKind.INTEGER) {
tc.native_type = ThreadContext.NATIVE_INT;
if (repr_data.elem_size == 8) {
tc.native_i = storage.getByte(index*repr_data.jna_size);
}
else if (repr_data.elem_size == 16) {
tc.native_i = storage.getShort(index*repr_data.jna_size);
}
else if (repr_data.elem_size == 32) {
tc.native_i = storage.getInt(index*repr_data.jna_size);
}
else if (repr_data.elem_size == 64) {
tc.native_i = storage.getLong(index*repr_data.jna_size);
}
}
else if (repr_data.elem_kind == ElemKind.NUMERIC) {
tc.native_type = ThreadContext.NATIVE_NUM;
if (repr_data.elem_size == 32) {
tc.native_n = storage.getFloat(index*repr_data.jna_size);
}
else if (repr_data.elem_size == 64) {
tc.native_n = storage.getDouble(index*repr_data.jna_size);
}
}
else {
ExceptionHandling.dieInternal(tc, "CArray can only at_pos_native with ints and nums.");
}
}
public SixModelObject at_pos_boxed(ThreadContext tc, long index) {
CArrayREPRData repr_data = (CArrayREPRData) st.REPRData;
int intidx = (int) index;
/* TODO: Die if this is a NUMERIC/INTEGER CArray. */
if (managed && index >= elems)
return repr_data.elem_type;
else if (index >= allocated)
expand(tc, index+1);
if (child_objs[intidx] != null) {
return child_objs[intidx];
}
else {
SixModelObject obj = makeObject(tc, storage.getPointer(index*repr_data.jna_size));
child_objs[intidx] = obj;
return obj;
}
}
public void bind_pos_native(ThreadContext tc, long index) {
CArrayREPRData repr_data = (CArrayREPRData) st.REPRData;
if (index >= allocated) {
expand(tc, index+1);
}
if (repr_data.elem_kind == ElemKind.INTEGER) {
tc.native_type = ThreadContext.NATIVE_INT;
if (repr_data.elem_size == 8) {
storage.setByte(index*repr_data.jna_size, (byte) tc.native_i);
}
else if (repr_data.elem_size == 16) {
storage.setShort(index*repr_data.jna_size, (short) tc.native_i);
}
else if (repr_data.elem_size == 32) {
storage.setInt(index*repr_data.jna_size, (int) tc.native_i);
}
else if (repr_data.elem_size == 64) {
storage.setLong(index*repr_data.jna_size, tc.native_i);
}
}
else if (repr_data.elem_kind == ElemKind.NUMERIC) {
tc.native_type = ThreadContext.NATIVE_NUM;
if (repr_data.elem_size == 32) {
storage.setFloat(index*repr_data.jna_size, (float) tc.native_n);
}
else if (repr_data.elem_size == 64) {
storage.setDouble(index*repr_data.jna_size, tc.native_n);
}
}
else {
ExceptionHandling.dieInternal(tc, "CArray can only bind_pos_native with ints and nums.");
}
}
public void bind_pos_boxed(ThreadContext tc, long index, SixModelObject value) {
CArrayREPRData repr_data = (CArrayREPRData) st.REPRData;
int intidx = (int) index;
value = Ops.decont(value, tc);
/* TODO: Die if this is a NUMERIC/INTEGER CArray. */
if (index >= allocated)
expand(tc, index+1);
Pointer ptr = null;
if (Ops.isconcrete(value, tc) != 0) {
switch (repr_data.elem_kind) {
case STRING:
byte[] bytes = Native.toByteArray(value.get_str(tc));
ptr = new Memory(bytes.length);
ptr.write(0, bytes, 0, bytes.length);
break;
case CARRAY:
ptr = ((CArrayInstance) value).storage;
break;
case CSTRUCT:
ptr = ((CStructInstance) value).storage.getPointer();
break;
case CPOINTER:
ptr = ((CPointerInstance) value).pointer;
break;
default:
ExceptionHandling.dieInternal(tc, "CArray.bind_pos_boxed reached its default case. This should never happen.");
}
}
child_objs[intidx] = value;
storage.setPointer(index*repr_data.jna_size, ptr);
}
private void expand(ThreadContext tc, long new_size) {
CArrayREPRData repr_data = (CArrayREPRData) st.REPRData;
if (managed) {
Memory new_storage = new Memory(new_size*repr_data.jna_size);
new_storage.clear();
if (storage != null) {
Memory old_storage = (Memory) storage;
new_storage.write(0, old_storage.getByteArray(0, (int) old_storage.size()), 0, (int) old_storage.size());
}
storage = new_storage;
}
boolean complex = repr_data.elem_kind == ElemKind.CARRAY
|| repr_data.elem_kind == ElemKind.CPOINTER
|| repr_data.elem_kind == ElemKind.CSTRUCT
|| repr_data.elem_kind == ElemKind.STRING;
if (complex) {
child_objs = child_objs == null?
new SixModelObject[(int) new_size]:
Arrays.copyOf(child_objs, (int) new_size);
}
elems = new_size;
allocated = new_size;
}
private SixModelObject makeObject(ThreadContext tc, Pointer ptr) {
CArrayREPRData repr_data = (CArrayREPRData) st.REPRData;
if (ptr == null)
return repr_data.elem_type;
switch (repr_data.elem_kind) {
case STRING:
return NativeCallOps.toNQPType(tc, ArgType.UTF8STR, repr_data.elem_type, ptr.getString(0));
case CARRAY:
return NativeCallOps.toNQPType(tc, ArgType.CARRAY, repr_data.elem_type, ptr);
case CPOINTER:
return NativeCallOps.toNQPType(tc, ArgType.CPOINTER, repr_data.elem_type, ptr);
case CSTRUCT:
Class<?> structClass = ((CStructREPRData) repr_data.elem_type.st.REPRData).structureClass;
return NativeCallOps.toNQPType(tc, ArgType.CSTRUCT, repr_data.elem_type, Structure.newInstance(structClass, ptr));
default:
ExceptionHandling.dieInternal(tc, "CArray can only makeObject strings, arrays, structs and pointers");
}
/* And a dummy return statement to placate Java's flow analysis. */
return null;
}
public void refresh(ThreadContext tc) {
CArrayREPRData repr_data = (CArrayREPRData) st.REPRData;
// No need to refresh if we don't have any cached children.
if (child_objs == null) return;
if (repr_data.elem_kind == ElemKind.CARRAY || repr_data.elem_kind == ElemKind.CSTRUCT) {
refreshComplex(tc);
}
else {
refreshSimple(tc);
}
}
/**
* Refresh logic for CArray of complex (CArray or CStruct) types.
*/
private void refreshComplex(ThreadContext tc) {
for (int i = 0; i < child_objs.length; i++) {
SixModelObject child = child_objs[i];
// No cache for this element? Go to next.
if (child == null) continue;
/* Invalidate cache and recursively refresh child too. Future
* versions here should only invalidate the cache if C memory has
* a different pointer than the cached object.
*/
child_objs[i] = null;
NativeCallOps.refresh(child, tc);
}
}
/**
* Refresh logic for CArray of simple (CPointer) types.
*/
private void refreshSimple(ThreadContext tc) {
for (int i = 0; i < child_objs.length; i++) {
SixModelObject child = child_objs[i];
// No cache for this element? Go to next.
if (child == null) continue;
/* Invalidate cache. Future versions here should only invalidate
* the cache if C memory has a different pointer than the cached
* object.
*/
child_objs[i] = null;
}
}
}