-
Notifications
You must be signed in to change notification settings - Fork 48
/
DirectBufferDeallocator.java
98 lines (81 loc) · 3.22 KB
/
DirectBufferDeallocator.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
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
package tlschannel.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Access to NIO sun.misc.Cleaner, allowing caller to deterministically deallocate a given
* sun.nio.ch.DirectBuffer.
*/
public class DirectBufferDeallocator {
private static final Logger logger = Logger.getLogger(DirectBufferDeallocator.class.getName());
private interface Deallocator {
void free(ByteBuffer bb);
}
private static class Java8Deallocator implements Deallocator {
/*
* Getting instance of cleaner from buffer (sun.misc.Cleaner)
*/
final Method cleanerAccessor;
final Method clean;
Java8Deallocator() {
try {
cleanerAccessor = Class.forName("sun.nio.ch.DirectBuffer").getMethod("cleaner");
clean = Class.forName("sun.misc.Cleaner").getMethod("clean");
} catch (NoSuchMethodException | ClassNotFoundException t) {
throw new RuntimeException(t);
}
}
@Override
public void free(ByteBuffer bb) {
try {
clean.invoke(cleanerAccessor.invoke(bb));
} catch (IllegalAccessException | InvocationTargetException t) {
throw new RuntimeException(t);
}
}
}
private static class Java9Deallocator implements Deallocator {
/*
* Clean is of type jdk.internal.ref.Cleaner, but this type is not accessible, as it is not exported by default.
* Using workaround through sun.misc.Unsafe.
*/
final Object unsafe;
final Method invokeCleaner;
Java9Deallocator() {
try {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
// avoiding getUnsafe methods, as it is explicitly filtered out from reflection API
Field theUnsafe = unsafeClass.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
unsafe = theUnsafe.get(null);
invokeCleaner = unsafeClass.getMethod("invokeCleaner", ByteBuffer.class);
} catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | NoSuchFieldException t) {
throw new RuntimeException(t);
}
}
@Override
public void free(ByteBuffer bb) {
try {
invokeCleaner.invoke(unsafe, bb);
} catch (IllegalAccessException | InvocationTargetException t) {
throw new RuntimeException(t);
}
}
}
private final Deallocator deallocator;
public DirectBufferDeallocator() {
if (Util.getJavaMajorVersion() >= 9) {
deallocator = new Java9Deallocator();
logger.log(Level.FINEST, "initialized direct buffer deallocator for java >= 9");
} else {
deallocator = new Java8Deallocator();
logger.log(Level.FINEST, "initialized direct buffer deallocator for java < 9");
}
}
public void deallocate(ByteBuffer buffer) {
deallocator.free(buffer);
}
}