forked from coderplay/javaopt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsafeDirectByteBuffer.java
More file actions
94 lines (82 loc) · 2.53 KB
/
UnsafeDirectByteBuffer.java
File metadata and controls
94 lines (82 loc) · 2.53 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
package javaopt.queue;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class UnsafeDirectByteBuffer {
private static final long addressOffset;
public static final int CACHE_LINE_SIZE = 64;
public static final int PAGE_SIZE = UnsafeAccess.unsafe.pageSize();
static {
try {
addressOffset = UnsafeAccess.unsafe.objectFieldOffset(Buffer.class
.getDeclaredField("address"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static long getAddress(ByteBuffer buffy) {
return UnsafeAccess.unsafe.getLong(buffy, addressOffset);
}
/**
* put byte and skip position update and boundary checks
*
* @param buffy
* @param b
*/
public static void putByte(long address, int position, byte b) {
UnsafeAccess.unsafe.putByte(address + (position << 0), b);
}
public static void putByte(long address, byte b) {
UnsafeAccess.unsafe.putByte(address, b);
}
public static ByteBuffer allocateAlignedByteBuffer(int capacity, long align) {
if (Long.bitCount(align) != 1) {
throw new IllegalArgumentException("Alignment must be a power of 2");
}
ByteBuffer buffy = ByteBuffer.allocateDirect((int) (capacity + align));
return alignedSlice(capacity, align, buffy);
}
public static ByteBuffer alignedSlice(int capacity, long align,
ByteBuffer buffy) {
long address = getAddress(buffy);
if ((address & (align - 1)) == 0) {
return buffy;
} else {
int newPosition = (int) (align - (address & (align - 1)));
if (newPosition + capacity > buffy.capacity()) {
throw new IllegalArgumentException("it's impossible!");
}
int oldPosition = buffy.position();
buffy.position(newPosition);
int newLimit = newPosition + capacity;
buffy.limit(newLimit);
ByteBuffer slice = buffy.slice();
buffy.position(oldPosition);
return slice;
}
}
public static boolean isPageAligned(ByteBuffer buffy) {
return isPageAligned(getAddress(buffy));
}
/**
* This assumes cache line is 64b
*/
public static boolean isCacheAligned(ByteBuffer buffy) {
return isCacheAligned(getAddress(buffy));
}
public static boolean isPageAligned(long address) {
return (address & (PAGE_SIZE - 1)) == 0;
}
/**
* This assumes cache line is 64b
*/
public static boolean isCacheAligned(long address) {
return (address & (CACHE_LINE_SIZE - 1)) == 0;
}
public static boolean isAligned(long address, long align) {
if (Long.bitCount(align) != 1) {
throw new IllegalArgumentException("Alignment must be a power of 2");
}
return (address & (align - 1)) == 0;
}
}