[#2599] Not use sun.nio.ch.DirectBuffer as it not exists on android
Motivation: During some refactoring we changed PlatformDependend0 to use sun.nio.ch.DirectBuffer for release direct buffers. This broke support for android as the class does not exist there and so an exception is thrown. Modification: Use again the fieldoffset to get access to Cleaner for release direct buffers. Result: Netty can be used on android again
This commit is contained in:
parent
087d329d07
commit
d4d27248b4
@ -19,7 +19,6 @@ import io.netty.util.internal.logging.InternalLogger;
|
|||||||
import io.netty.util.internal.logging.InternalLoggerFactory;
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||||
import sun.misc.Cleaner;
|
import sun.misc.Cleaner;
|
||||||
import sun.misc.Unsafe;
|
import sun.misc.Unsafe;
|
||||||
import sun.nio.ch.DirectBuffer;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@ -41,6 +40,7 @@ final class PlatformDependent0 {
|
|||||||
private static final Unsafe UNSAFE;
|
private static final Unsafe UNSAFE;
|
||||||
private static final boolean BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
private static final boolean BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
||||||
private static final long ADDRESS_FIELD_OFFSET;
|
private static final long ADDRESS_FIELD_OFFSET;
|
||||||
|
private static final long CLEANER_FIELD_OFFSET;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Limits the number of bytes to copy per {@link Unsafe#copyMemory(long, long, long)} to allow safepoint polling
|
* Limits the number of bytes to copy per {@link Unsafe#copyMemory(long, long, long)} to allow safepoint polling
|
||||||
@ -56,17 +56,19 @@ final class PlatformDependent0 {
|
|||||||
private static final boolean UNALIGNED;
|
private static final boolean UNALIGNED;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
boolean directBufferFreeable = false;
|
ByteBuffer direct = ByteBuffer.allocateDirect(1);
|
||||||
|
Field cleanerField;
|
||||||
try {
|
try {
|
||||||
Class<?> cls = Class.forName("sun.nio.ch.DirectBuffer", false, getClassLoader(PlatformDependent0.class));
|
cleanerField = direct.getClass().getDeclaredField("cleaner");
|
||||||
Method method = cls.getMethod("cleaner");
|
cleanerField.setAccessible(true);
|
||||||
if ("sun.misc.Cleaner".equals(method.getReturnType().getName())) {
|
Cleaner cleaner = (Cleaner) cleanerField.get(direct);
|
||||||
directBufferFreeable = true;
|
cleaner.clean();
|
||||||
}
|
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
// We don't have sun.nio.ch.DirectBuffer.cleaner().
|
// We don't have ByteBuffer.cleaner().
|
||||||
|
cleanerField = null;
|
||||||
}
|
}
|
||||||
logger.debug("sun.nio.ch.DirectBuffer.cleaner(): {}", directBufferFreeable? "available" : "unavailable");
|
|
||||||
|
logger.debug("java.nio.ByteBuffer.cleaner(): {}", cleanerField != null? "available" : "unavailable");
|
||||||
|
|
||||||
Field addressField;
|
Field addressField;
|
||||||
try {
|
try {
|
||||||
@ -76,7 +78,6 @@ final class PlatformDependent0 {
|
|||||||
// A heap buffer must have 0 address.
|
// A heap buffer must have 0 address.
|
||||||
addressField = null;
|
addressField = null;
|
||||||
} else {
|
} else {
|
||||||
ByteBuffer direct = ByteBuffer.allocateDirect(1);
|
|
||||||
if (addressField.getLong(direct) == 0) {
|
if (addressField.getLong(direct) == 0) {
|
||||||
// A direct buffer must have non-zero address.
|
// A direct buffer must have non-zero address.
|
||||||
addressField = null;
|
addressField = null;
|
||||||
@ -89,7 +90,7 @@ final class PlatformDependent0 {
|
|||||||
logger.debug("java.nio.Buffer.address: {}", addressField != null? "available" : "unavailable");
|
logger.debug("java.nio.Buffer.address: {}", addressField != null? "available" : "unavailable");
|
||||||
|
|
||||||
Unsafe unsafe;
|
Unsafe unsafe;
|
||||||
if (addressField != null && directBufferFreeable) {
|
if (addressField != null && cleanerField != null) {
|
||||||
try {
|
try {
|
||||||
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
|
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
|
||||||
unsafeField.setAccessible(true);
|
unsafeField.setAccessible(true);
|
||||||
@ -126,9 +127,11 @@ final class PlatformDependent0 {
|
|||||||
|
|
||||||
if (unsafe == null) {
|
if (unsafe == null) {
|
||||||
ADDRESS_FIELD_OFFSET = -1;
|
ADDRESS_FIELD_OFFSET = -1;
|
||||||
|
CLEANER_FIELD_OFFSET = -1;
|
||||||
UNALIGNED = false;
|
UNALIGNED = false;
|
||||||
} else {
|
} else {
|
||||||
ADDRESS_FIELD_OFFSET = objectFieldOffset(addressField);
|
ADDRESS_FIELD_OFFSET = objectFieldOffset(addressField);
|
||||||
|
CLEANER_FIELD_OFFSET = objectFieldOffset(cleanerField);
|
||||||
boolean unaligned;
|
boolean unaligned;
|
||||||
try {
|
try {
|
||||||
Class<?> bitsClass = Class.forName("java.nio.Bits", false, ClassLoader.getSystemClassLoader());
|
Class<?> bitsClass = Class.forName("java.nio.Bits", false, ClassLoader.getSystemClassLoader());
|
||||||
@ -145,6 +148,9 @@ final class PlatformDependent0 {
|
|||||||
UNALIGNED = unaligned;
|
UNALIGNED = unaligned;
|
||||||
logger.debug("java.nio.Bits.unaligned: {}", UNALIGNED);
|
logger.debug("java.nio.Bits.unaligned: {}", UNALIGNED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// free temporary buffer if possible
|
||||||
|
freeDirectBuffer(direct);
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean hasUnsafe() {
|
static boolean hasUnsafe() {
|
||||||
@ -156,11 +162,11 @@ final class PlatformDependent0 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void freeDirectBuffer(ByteBuffer buffer) {
|
static void freeDirectBuffer(ByteBuffer buffer) {
|
||||||
if (!(buffer instanceof DirectBuffer)) {
|
if (CLEANER_FIELD_OFFSET == -1 || !buffer.isDirect()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Cleaner cleaner = ((DirectBuffer) buffer).cleaner();
|
Cleaner cleaner = (Cleaner) getObject(buffer, CLEANER_FIELD_OFFSET);
|
||||||
if (cleaner != null) {
|
if (cleaner != null) {
|
||||||
cleaner.clean();
|
cleaner.clean();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user