-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
UnsafeUtil.java
53 lines (45 loc) · 1.4 KB
/
UnsafeUtil.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
package me.nallar.unsafe;
import java.lang.reflect.Array;
import sun.misc.Unsafe;
public class UnsafeUtil {
private static final Unsafe $ = UnsafeAccess.$;
private static final Object[] addressGet = new Object[1];
private static final long baseOffset = $.arrayBaseOffset(Object[].class);
private static final int addressSize = $.addressSize();
public static boolean arrayEquals(Object a, Object b) {
if (a == b) {
return true;
}
if (Array.getLength(a) != Array.getLength(b) || !a.getClass().equals(b.getClass())) {
return false;
}
int baseOffset = $.arrayBaseOffset(a.getClass());
int scaleOffset = $.arrayIndexScale(a.getClass());
int numLongs = Array.getLength(a);
for (long currentOffset = baseOffset, i = 0; i < numLongs; ++i, currentOffset += scaleOffset) {
long l1 = $.getLong(a, currentOffset);
long l2 = $.getLong(b, currentOffset);
if (l1 != l2) {
return false;
}
}
return true;
}
public synchronized static long getAddress(Object o) {
addressGet[0] = o;
switch (addressSize) {
case 4:
return $.getInt(addressGet, baseOffset);
case 8:
return $.getLong(addressGet, baseOffset);
}
throw new Error("unsupported address size: " + addressSize);
}
public static <T> T initialise(Class<T> c) {
try {
return (T) $.allocateInstance(c);
} catch (Exception e) {
throw new Error(e);
}
}
}