-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Pointer.java
39 lines (33 loc) · 950 Bytes
/
Pointer.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
package nallar.unsafe;
import sun.misc.Unsafe;
public class Pointer {
public Object o;
private static final Unsafe $ = UnsafeAccess.$;
private static final long offset = getOffset();
private static final int addressSize = $.addressSize();
private static long getOffset() {
try {
return $.objectFieldOffset(Pointer.class.getField("o"));
} catch (NoSuchFieldException e) {
throw new UnsupportedOperationException("Unsupported JVM - can't get field offset", e);
}
}
public void setAddress(long a) {
if (addressSize == 4) {
// TODO: denormalize
$.putInt(this, offset, (int) a);
} else {
$.putLong(this, offset, a);
}
}
public long getAddress() {
if (addressSize == 4) {
return normalize($.getInt(this, offset));
} else {
return $.getLong(this, offset);
}
}
private static long normalize(int value) {
return (value >= 0) ? value : (~0L >>> 32) & value;
}
}