-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathHeapMemory.java
More file actions
442 lines (393 loc) · 15.9 KB
/
HeapMemory.java
File metadata and controls
442 lines (393 loc) · 15.9 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
package org.python.modules.jffi;
import com.kenai.jffi.Platform;
import java.nio.ByteOrder;
import java.util.Arrays;
public final class HeapMemory implements Memory {
protected static final ArrayIO IO = getArrayIO();
protected static final int LONG_SIZE = Platform.getPlatform().longSize() / 8;
protected static final int ADDRESS_SIZE = Platform.getPlatform().addressSize() / 8;
protected final byte[] buffer;
protected final int offset, length;
public HeapMemory(byte[] buffer, int offset, int length) {
this.buffer = buffer;
this.offset = offset;
this.length = length;
}
public HeapMemory(int size) {
this(new byte[size], 0, size);
}
private final void checkBounds(long off, long len) {
Util.checkBounds(arrayLength(), off, len);
}
public final byte[] array() {
return buffer;
}
public final int arrayOffset() {
return offset;
}
public final int arrayLength() {
return length;
}
private static final ArrayIO getArrayIO() {
if (ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) {
return Platform.getPlatform().addressSize() == 64
? newBE64ArrayIO() : newBE32ArrayIO();
} else {
return Platform.getPlatform().addressSize() == 64
? newLE64ArrayIO() : newLE32ArrayIO();
}
}
private static final ArrayIO newBE64ArrayIO() {
return new BE64ArrayIO();
}
private static final ArrayIO newBE32ArrayIO() {
return new BE32ArrayIO();
}
private static final ArrayIO newLE64ArrayIO() {
return new LE64ArrayIO();
}
private static final ArrayIO newLE32ArrayIO() {
return new LE32ArrayIO();
}
protected final int index(long off) {
return this.offset + (int) off;
}
public final boolean isNull() {
return false;
}
public final boolean isDirect() {
return false;
}
public HeapMemory slice(long offset) {
checkBounds(offset, 1);
return offset == 0 ? this : new HeapMemory(array(), arrayOffset() + (int) offset, arrayLength() - (int) offset);
}
public final DirectMemory getMemory(long offset) {
checkBounds(offset, ADDRESS_SIZE);
final long ptr = getAddress(offset);
return ptr != 0 ? new NativeMemory(ptr) : null;
}
public final void putAddress(long offset, Memory value) {
checkBounds(offset, ADDRESS_SIZE);
putAddress(offset, ((DirectMemory) value).getAddress());
}
public final byte getByte(long offset) {
checkBounds(offset, 1);
return (byte) (buffer[index(offset)] & 0xff);
}
public final short getShort(long offset) {
checkBounds(offset, 2);
return IO.getInt16(buffer, index(offset));
}
public final int getInt(long offset) {
checkBounds(offset, 4);
return IO.getInt32(buffer, index(offset));
}
public final long getLong(long offset) {
checkBounds(offset, 8);
return IO.getInt64(buffer, index(offset));
}
public final long getNativeLong(long offset) {
return LONG_SIZE == 4 ? getInt(offset) : getLong(offset);
}
public final float getFloat(long offset) {
checkBounds(offset, 4);
return IO.getFloat32(buffer, index(offset));
}
public final double getDouble(long offset) {
checkBounds(offset, 8);
return IO.getFloat64(buffer, index(offset));
}
public final long getAddress(long offset) {
checkBounds(offset, ADDRESS_SIZE);
return IO.getAddress(buffer, index(offset));
}
public final byte[] getZeroTerminatedByteArray(long offset) {
checkBounds(offset, 1);
int len = indexOf(offset, (byte) 0);
byte[] bytes = new byte[len != -1 ? len : length - (int) offset];
System.arraycopy(buffer, index(offset), bytes, 0, bytes.length);
return bytes;
}
public final void putByte(long offset, byte value) {
checkBounds(offset, 1);
buffer[index(offset)] = value;
}
public final void putShort(long offset, short value) {
checkBounds(offset, 2);
IO.putInt16(buffer, index(offset), value);
}
public final void putInt(long offset, int value) {
checkBounds(offset, 4);
IO.putInt32(buffer, index(offset), value);
}
public final void putLong(long offset, long value) {
checkBounds(offset, 8);
IO.putInt64(buffer, index(offset), value);
}
public final void putNativeLong(long offset, long value) {
if (LONG_SIZE == 4) {
putInt(offset, (int) value);
} else {
putLong(offset, value);
}
}
public final void putFloat(long offset, float value) {
checkBounds(offset, 4);
IO.putFloat32(buffer, index(offset), value);
}
public final void putDouble(long offset, double value) {
checkBounds(offset, 8);
IO.putFloat64(buffer, index(offset), value);
}
public final void putAddress(long offset, long value) {
checkBounds(offset, ADDRESS_SIZE);
IO.putAddress(buffer, index(offset), value);
}
public void putZeroTerminatedByteArray(long offset, byte[] bytes, int off, int len) {
// Ensure room for terminating zero byte
checkBounds(offset, len + 1);
System.arraycopy(bytes, off, buffer, index(offset), len);
buffer[len] = (byte) 0;
}
public final void get(long offset, byte[] dst, int off, int len) {
checkBounds(offset, len);
System.arraycopy(buffer, index(offset), dst, off, len);
}
public final void put(long offset, byte[] src, int off, int len) {
checkBounds(offset, len);
System.arraycopy(src, off, buffer, index(offset), len);
}
public final void get(long offset, short[] dst, int off, int len) {
checkBounds(offset, len << 1);
int begin = index(offset);
for (int i = 0; i < len; ++i) {
dst[off + i] = IO.getInt16(buffer, begin + (i << 1));
}
}
public final void put(long offset, short[] src, int off, int len) {
checkBounds(offset, len << 1);
int begin = index(offset);
for (int i = 0; i < len; ++i) {
IO.putInt16(buffer, begin + (i << 1), src[off + i]);
}
}
public final void get(long offset, int[] dst, int off, int len) {
checkBounds(offset, len << 2);
int begin = index(offset);
for (int i = 0; i < len; ++i) {
dst[off + i] = IO.getInt32(buffer, begin + (i << 2));
}
}
public final void put(long offset, int[] src, int off, int len) {
checkBounds(offset, len << 2);
int begin = index(offset);
for (int i = 0; i < len; ++i) {
IO.putInt32(buffer, begin + (i << 2), src[off + i]);
}
}
public final void get(long offset, long[] dst, int off, int len) {
checkBounds(offset, len << 3);
int begin = index(offset);
for (int i = 0; i < len; ++i) {
dst[off + i] = IO.getInt64(buffer, begin + (i << 3));
}
}
public final void put(long offset, long[] src, int off, int len) {
checkBounds(offset, len << 3);
int begin = index(offset);
for (int i = 0; i < len; ++i) {
IO.putInt64(buffer, begin + (i << 3), src[off + i]);
}
}
public final void get(long offset, float[] dst, int off, int len) {
checkBounds(offset, len << 2);
int begin = index(offset);
for (int i = 0; i < len; ++i) {
dst[off + i] = IO.getFloat32(buffer, begin + (i << 2));
}
}
public final void put(long offset, float[] src, int off, int len) {
checkBounds(offset, len << 2);
int begin = index(offset);
for (int i = 0; i < len; ++i) {
IO.putFloat32(buffer, begin + (i << 2), src[off + i]);
}
}
public final void get(long offset, double[] dst, int off, int len) {
checkBounds(offset, len << 3);
int begin = index(offset);
for (int i = 0; i < len; ++i) {
dst[off + i] = IO.getFloat64(buffer, begin + (i << 3));
}
}
public final void put(long offset, double[] src, int off, int len) {
checkBounds(offset, len << 3);
int begin = index(offset);
for (int i = 0; i < len; ++i) {
IO.putFloat64(buffer, begin + (i << 3), src[off + i]);
}
}
public final int indexOf(long offset, byte value) {
int off = index(offset);
for (int i = 0; i < length; ++i) {
if (buffer[off + i] == value) {
return i;
}
}
return -1;
}
public final int indexOf(long offset, byte value, int maxlen) {
int off = index(offset);
for (int i = 0; i < Math.min(length, maxlen); ++i) {
if (buffer[off + i] == value) {
return i;
}
}
return -1;
}
public final void setMemory(long offset, long size, byte value) {
checkBounds(offset, size);
Arrays.fill(buffer, index(offset), (int) size, value);
}
public final void clear() {
Arrays.fill(buffer, offset, length, (byte) 0);
}
protected static abstract class ArrayIO {
public abstract short getInt16(byte[] buffer, int offset);
public abstract int getInt32(byte[] buffer, int offset);
public abstract long getInt64(byte[] buffer, int offset);
public abstract long getAddress(byte[] buffer, int offset);
public abstract void putInt16(byte[] buffer, int offset, int value);
public abstract void putInt32(byte[] buffer, int offset, int value);
public abstract void putInt64(byte[] buffer, int offset, long value);
public abstract void putAddress(byte[] buffer, int offset, long value);
public final float getFloat32(byte[] buffer, int offset) {
return Float.intBitsToFloat(getInt32(buffer, offset));
}
public final void putFloat32(byte[] buffer, int offset, float value) {
putInt32(buffer, offset, Float.floatToRawIntBits(value));
}
public final double getFloat64(byte[] buffer, int offset) {
return Double.longBitsToDouble(getInt64(buffer, offset));
}
public final void putFloat64(byte[] buffer, int offset, double value) {
putInt64(buffer, offset, Double.doubleToRawLongBits(value));
}
}
private static abstract class LittleEndianArrayIO extends ArrayIO {
public final short getInt16(byte[] array, int offset) {
return (short) ((array[offset] & 0xff) | ((array[offset + 1] & 0xff) << 8));
}
public final int getInt32(byte[] array, int offset) {
return ((array[offset + 0] & 0xff) << 0)
| ((array[offset + 1] & 0xff) << 8)
| ((array[offset + 2] & 0xff) << 16)
| ((array[offset + 3] & 0xff) << 24);
}
public final long getInt64(byte[] array, int offset) {
return (((long)array[offset + 0] & 0xff) << 0)
| (((long)array[offset + 1] & 0xff) << 8)
| (((long)array[offset + 2] & 0xff) << 16)
| (((long)array[offset + 3] & 0xff) << 24)
| (((long)array[offset + 4] & 0xff) << 32)
| (((long)array[offset + 5] & 0xff) << 40)
| (((long)array[offset + 6] & 0xff) << 48)
| (((long)array[offset + 7] & 0xff) << 56);
}
public final void putInt16(byte[] buffer, int offset, int value) {
buffer[offset + 0] = (byte) (value >> 0);
buffer[offset + 1] = (byte) (value >> 8);
}
public final void putInt32(byte[] buffer, int offset, int value) {
buffer[offset + 0] = (byte) (value >> 0);
buffer[offset + 1] = (byte) (value >> 8);
buffer[offset + 2] = (byte) (value >> 16);
buffer[offset + 3] = (byte) (value >> 24);
}
public final void putInt64(byte[] buffer, int offset, long value) {
buffer[offset + 0] = (byte) (value >> 0);
buffer[offset + 1] = (byte) (value >> 8);
buffer[offset + 2] = (byte) (value >> 16);
buffer[offset + 3] = (byte) (value >> 24);
buffer[offset + 4] = (byte) (value >> 32);
buffer[offset + 5] = (byte) (value >> 40);
buffer[offset + 6] = (byte) (value >> 48);
buffer[offset + 7] = (byte) (value >> 56);
}
}
private static abstract class BigEndianArrayIO extends ArrayIO {
public short getInt16(byte[] array, int offset) {
return (short) (((array[offset + 0] & 0xff) << 8)
| (array[offset + 1] & 0xff));
}
public int getInt32(byte[] array, int offset) {
return ((array[offset + 0] & 0xff) << 24)
| ((array[offset + 1] & 0xff) << 16)
| ((array[offset + 2] & 0xff) << 8)
| ((array[offset + 3] & 0xff) << 0);
}
public long getInt64(byte[] array, int offset) {
return (((long)array[offset + 0] & 0xff) << 56)
| (((long)array[offset + 1] & 0xff) << 48)
| (((long)array[offset + 2] & 0xff) << 40)
| (((long)array[offset + 3] & 0xff) << 32)
| (((long)array[offset + 4] & 0xff) << 24)
| (((long)array[offset + 5] & 0xff) << 16)
| (((long)array[offset + 6] & 0xff) << 8)
| (((long)array[offset + 7] & 0xff) << 0);
}
public final void putInt16(byte[] buffer, int offset, int value) {
buffer[offset + 0] = (byte) (value >> 8);
buffer[offset + 1] = (byte) (value >> 0);
}
public final void putInt32(byte[] buffer, int offset, int value) {
buffer[offset + 0] = (byte) (value >> 24);
buffer[offset + 1] = (byte) (value >> 16);
buffer[offset + 2] = (byte) (value >> 8);
buffer[offset + 3] = (byte) (value >> 0);
}
public final void putInt64(byte[] buffer, int offset, long value) {
buffer[offset + 0] = (byte) (value >> 56);
buffer[offset + 1] = (byte) (value >> 48);
buffer[offset + 2] = (byte) (value >> 40);
buffer[offset + 3] = (byte) (value >> 32);
buffer[offset + 4] = (byte) (value >> 24);
buffer[offset + 5] = (byte) (value >> 16);
buffer[offset + 6] = (byte) (value >> 8);
buffer[offset + 7] = (byte) (value >> 0);
}
}
private static final class LE32ArrayIO extends LittleEndianArrayIO {
public final long getAddress(byte[] buffer, int offset) {
return ((long) getInt32(buffer, offset)) & 0xffffffffL;
}
public final void putAddress(byte[] buffer, int offset, long value) {
putInt32(buffer, offset, (int) value);
}
}
private static final class LE64ArrayIO extends LittleEndianArrayIO {
public final long getAddress(byte[] buffer, int offset) {
return getInt64(buffer, offset);
}
public final void putAddress(byte[] buffer, int offset, long value) {
putInt64(buffer, offset, value);
}
}
private static final class BE32ArrayIO extends BigEndianArrayIO {
public final long getAddress(byte[] buffer, int offset) {
return ((long) getInt32(buffer, offset)) & 0xffffffffL;
}
public final void putAddress(byte[] buffer, int offset, long value) {
putInt32(buffer, offset, (int) value);
}
}
private static final class BE64ArrayIO extends BigEndianArrayIO {
public final long getAddress(byte[] buffer, int offset) {
return getInt64(buffer, offset);
}
public final void putAddress(byte[] buffer, int offset, long value) {
putInt64(buffer, offset, value);
}
}
}