-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathNativeMemory.java
More file actions
192 lines (151 loc) · 5.77 KB
/
NativeMemory.java
File metadata and controls
192 lines (151 loc) · 5.77 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
package org.python.modules.jffi;
import com.kenai.jffi.Platform;
class NativeMemory implements Memory, DirectMemory {
protected static final com.kenai.jffi.MemoryIO IO = com.kenai.jffi.MemoryIO.getInstance();
final NativeMemory parent; // keep a reference to avoid the memory being freed
final long address;
NativeMemory(long address) {
this.address = address;
this.parent = null;
}
private NativeMemory(NativeMemory parent, long offset) {
this.parent = parent;
this.address = parent.address + offset;
}
public final long getAddress() {
return address;
}
public NativeMemory slice(long offset) {
return offset == 0 ? this :new NativeMemory(this, offset);
}
@Override
public final boolean equals(Object obj) {
return (obj instanceof DirectMemory) && ((DirectMemory) obj).getAddress() == address;
}
@Override
public final int hashCode() {
int hash = 5;
hash = 53 * hash + (int) (this.address ^ (this.address >>> 32));
return hash;
}
public final boolean isNull() {
return address == 0;
}
public final boolean isDirect() {
return true;
}
public final byte getByte(long offset) {
return IO.getByte(address + offset);
}
public final short getShort(long offset) {
return IO.getShort(address + offset);
}
public final int getInt(long offset) {
return IO.getInt(address + offset);
}
public final long getLong(long offset) {
return IO.getLong(address + offset);
}
public final long getNativeLong(long offset) {
return Platform.getPlatform().longSize() == 32
? IO.getInt(address + offset)
: IO.getLong(address + offset);
}
public final float getFloat(long offset) {
return IO.getFloat(address + offset);
}
public final double getDouble(long offset) {
return IO.getDouble(address + offset);
}
public final long getAddress(long offset) {
return IO.getAddress(address + offset);
}
public final DirectMemory getMemory(long offset) {
final long ptr = IO.getAddress(address + offset);
return ptr != 0 ? new NativeMemory(ptr) : null;
}
public final byte[] getZeroTerminatedByteArray(long offset) {
return IO.getZeroTerminatedByteArray(address + offset);
}
public void putZeroTerminatedByteArray(long offset, byte[] bytes, int off, int len) {
IO.putZeroTerminatedByteArray(address + offset, bytes, off, len);
}
public final void putByte(long offset, byte value) {
IO.putByte(address + offset, value);
}
public final void putShort(long offset, short value) {
IO.putShort(address + offset, value);
}
public final void putInt(long offset, int value) {
IO.putInt(address + offset, value);
}
public final void putLong(long offset, long value) {
IO.putLong(address + offset, value);
}
public final void putNativeLong(long offset, long value) {
if (Platform.getPlatform().longSize() == 32) {
IO.putInt(address + offset, (int) value);
} else {
IO.putLong(address + offset, value);
}
}
public final void putAddress(long offset, long value) {
IO.putAddress(address + offset, value);
}
public final void putFloat(long offset, float value) {
IO.putFloat(address + offset, value);
}
public final void putDouble(long offset, double value) {
IO.putDouble(address + offset, value);
}
public final void putAddress(long offset, Memory value) {
IO.putAddress(address + offset, ((DirectMemory) value).getAddress());
}
public final void get(long offset, byte[] dst, int off, int len) {
IO.getByteArray(address + offset, dst, off, len);
}
public final void put(long offset, byte[] src, int off, int len) {
IO.putByteArray(address + offset, src, off, len);
}
public final void get(long offset, short[] dst, int off, int len) {
IO.getShortArray(address + offset, dst, off, len);
}
public final void put(long offset, short[] src, int off, int len) {
IO.putShortArray(address + offset, src, off, len);
}
public final void get(long offset, int[] dst, int off, int len) {
IO.getIntArray(address + offset, dst, off, len);
}
public final void put(long offset, int[] src, int off, int len) {
IO.putIntArray(address + offset, src, off, len);
}
public final void get(long offset, long[] dst, int off, int len) {
IO.getLongArray(address + offset, dst, off, len);
}
public final void put(long offset, long[] src, int off, int len) {
IO.putLongArray(address + offset, src, off, len);
}
public final void get(long offset, float[] dst, int off, int len) {
IO.getFloatArray(address + offset, dst, off, len);
}
public final void put(long offset, float[] src, int off, int len) {
IO.putFloatArray(address + offset, src, off, len);
}
public final void get(long offset, double[] dst, int off, int len) {
IO.getDoubleArray(address + offset, dst, off, len);
}
public final void put(long offset, double[] src, int off, int len) {
IO.putDoubleArray(address + offset, src, off, len);
}
public final int indexOf(long offset, byte value) {
return value == 0
? (int) IO.getStringLength(address + offset)
: (int) IO.indexOf(address + offset, value);
}
public final int indexOf(long offset, byte value, int maxlen) {
return (int) IO.indexOf(address, value, maxlen);
}
public final void setMemory(long offset, long size, byte value) {
IO.setMemory(address + offset, size, value);
}
}