-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathMemoryOp.java
More file actions
254 lines (221 loc) · 8.76 KB
/
MemoryOp.java
File metadata and controls
254 lines (221 loc) · 8.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
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
package org.python.modules.jffi;
import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PyType;
/**
* Defines memory operations for a primitive type
*/
abstract class MemoryOp {
public static final MemoryOp INVALID = new InvalidOp();
public static final MemoryOp VOID = new VoidOp();
public static final MemoryOp BOOL = new BooleanOp();
public static final MemoryOp INT8 = new Signed8();
public static final MemoryOp UINT8 = new Unsigned8();
public static final MemoryOp INT16 = new Signed16();
public static final MemoryOp UINT16 = new Unsigned16();
public static final MemoryOp INT32 = new Signed32();
public static final MemoryOp UINT32 = new Unsigned32();
public static final MemoryOp INT64 = new Signed64();
public static final MemoryOp UINT64 = new Unsigned64();
public static final MemoryOp FLOAT = new Float32();
public static final MemoryOp DOUBLE = new Float64();
public static final MemoryOp POINTER = new PointerOp(PointerCData.TYPE, CType.POINTER);
public static final MemoryOp STRING = new StringOp();
public static final MemoryOp getMemoryOp(NativeType type) {
switch (type) {
case VOID:
return VOID;
case BYTE:
return INT8;
case UBYTE:
return UINT8;
case SHORT:
return INT16;
case USHORT:
return UINT16;
case INT:
return INT32;
case UINT:
return UINT32;
case LONGLONG:
return INT64;
case ULONGLONG:
return UINT64;
case LONG:
return com.kenai.jffi.Platform.getPlatform().longSize() == 32
? INT32 : INT64;
case ULONG:
return com.kenai.jffi.Platform.getPlatform().longSize() == 32
? UINT32 : UINT64;
case FLOAT:
return FLOAT;
case DOUBLE:
return DOUBLE;
case POINTER:
return POINTER;
case STRING:
return STRING;
case BOOL:
return BOOL;
default:
throw new UnsupportedOperationException("No MemoryOp for " + type);
}
}
abstract PyObject get(Memory mem, long offset);
abstract void put(Memory mem, long offset, PyObject value);
private static final class InvalidOp extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
throw Py.TypeError("invalid memory access");
}
public final PyObject get(Memory mem, long offset) {
throw Py.TypeError("invalid memory access");
}
}
private static final class VoidOp extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
throw Py.TypeError("Attempting to write void to memory");
}
public final PyObject get(Memory mem, long offset) {
throw Py.TypeError("Attempting to read void from memory");
}
}
private static final class BooleanOp extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putInt(offset, value.__nonzero__() ? 1 : 0);
}
public final PyObject get(Memory mem, long offset) {
return Py.newBoolean(mem.getInt(offset) != 0);
}
}
static final class Signed8 extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putByte(offset, Util.int8Value(value));
}
public final PyObject get(Memory mem, long offset) {
return Util.newSigned8(mem.getByte(offset));
}
}
static final class Unsigned8 extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putByte(offset, (byte) Util.uint8Value(value));
}
public final PyObject get(Memory mem, long offset) {
return Util.newUnsigned8(mem.getByte(offset));
}
}
static final class Signed16 extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putShort(offset, Util.int16Value(value));
}
public final PyObject get(Memory mem, long offset) {
return Util.newSigned16(mem.getShort(offset));
}
}
static final class Unsigned16 extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putShort(offset, (short) Util.uint16Value(value));
}
public final PyObject get(Memory mem, long offset) {
return Util.newUnsigned16(mem.getShort(offset));
}
}
static final class Signed32 extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putInt(offset, Util.int32Value(value));
}
public final PyObject get(Memory mem, long offset) {
return Util.newSigned32(mem.getInt(offset));
}
}
static final class Unsigned32 extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putInt(offset, (int) Util.uint32Value(value));
}
public final PyObject get(Memory mem, long offset) {
return Util.newUnsigned32(mem.getInt(offset));
}
}
static final class Signed64 extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putLong(offset, Util.int64Value(value));
}
public final PyObject get(Memory mem, long offset) {
return Util.newSigned64(mem.getLong(offset));
}
}
static final class Unsigned64 extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putLong(offset, Util.uint64Value(value));
}
public final PyObject get(Memory mem, long offset) {
return Util.newUnsigned64(mem.getLong(offset));
}
}
static final class Float32 extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putFloat(offset, Util.floatValue(value));
}
public final PyObject get(Memory mem, long offset) {
return Py.newFloat(mem.getFloat(offset));
}
}
static final class Float64 extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
mem.putDouble(offset, Util.doubleValue(value));
}
public final PyObject get(Memory mem, long offset) {
return Py.newFloat(mem.getDouble(offset));
}
}
static final class PointerOp extends MemoryOp {
private final PyType pytype;
private final CType ctype;
public PointerOp(PyType pytype, CType ctype) {
this.pytype = pytype;
this.ctype = ctype;
}
public final void put(Memory mem, long offset, PyObject value) {
if (value instanceof Pointer) {
mem.putAddress(offset, ((Pointer) value).getMemory().getAddress());
} else if (value == Py.None) {
mem.putAddress(offset, 0);
} else {
throw Py.RuntimeError("invalid pointer");
}
}
public final PyObject get(Memory mem, long offset) {
DirectMemory dm = new NativeMemory(mem.getAddress(offset));
return new PointerCData(pytype, ctype, dm, INVALID);
}
}
private static final class StringOp extends MemoryOp {
public final void put(Memory mem, long offset, PyObject value) {
throw Py.NotImplementedError("Cannot set String");
}
public final PyObject get(Memory mem, long offset) {
throw Py.NotImplementedError("Cannot get String");
}
}
static final class StructOp extends MemoryOp {
private final PyType type;
private final StructLayout layout;
public StructOp(PyType type) {
this.type = type;
PyObject l = type.__getattr__("_jffi_type");
if (!(l instanceof StructLayout)) {
throw Py.TypeError("invalid _jffi_type for " + type.fastGetName() + "; should be instance of jffi.StructLayout");
}
this.layout = (StructLayout) l;
}
public StructOp(PyType type, StructLayout layout) {
this.type = type;
this.layout = layout;
}
public final void put(Memory mem, long offset, PyObject value) {
throw Py.NotImplementedError("not implemented");
}
public final PyObject get(Memory mem, long offset) {
return new Structure(type, layout, mem.slice(offset));
}
}
}