[#1792] PlatformDependent.freeDirectBuffer(..) now respect hasUnsafe()

This commit is contained in:
Norman Maurer 2013-08-28 07:21:52 +02:00
parent 12fe7b52b6
commit 3aa77f54f7
2 changed files with 24 additions and 2 deletions

View File

@ -228,7 +228,11 @@ public final class PlatformDependent {
*/ */
public static void freeDirectBuffer(ByteBuffer buffer) { public static void freeDirectBuffer(ByteBuffer buffer) {
if (buffer.isDirect()) { if (buffer.isDirect()) {
PlatformDependent0.freeDirectBuffer(buffer); if (hasUnsafe()) {
PlatformDependent0.freeDirectBufferUnsafe(buffer);
} else {
PlatformDependent0.freeDirectBuffer(buffer);
}
} }
} }

View File

@ -37,6 +37,7 @@ final class PlatformDependent0 {
private static final long CLEANER_FIELD_OFFSET; private static final long CLEANER_FIELD_OFFSET;
private static final long ADDRESS_FIELD_OFFSET; private static final long ADDRESS_FIELD_OFFSET;
private static final Field CLEANER_FIELD;
/** /**
* {@code true} if and only if the platform supports unaligned access. * {@code true} if and only if the platform supports unaligned access.
@ -56,6 +57,7 @@ final class PlatformDependent0 {
} catch (Throwable t) { } catch (Throwable t) {
cleanerField = null; cleanerField = null;
} }
CLEANER_FIELD = cleanerField;
logger.debug("java.nio.ByteBuffer.cleaner: {}", cleanerField != null? "available" : "unavailable"); logger.debug("java.nio.ByteBuffer.cleaner: {}", cleanerField != null? "available" : "unavailable");
Field addressField; Field addressField;
@ -145,7 +147,7 @@ final class PlatformDependent0 {
UNSAFE.throwException(t); UNSAFE.throwException(t);
} }
static void freeDirectBuffer(ByteBuffer buffer) { static void freeDirectBufferUnsafe(ByteBuffer buffer) {
Cleaner cleaner; Cleaner cleaner;
try { try {
cleaner = (Cleaner) getObject(buffer, CLEANER_FIELD_OFFSET); cleaner = (Cleaner) getObject(buffer, CLEANER_FIELD_OFFSET);
@ -159,6 +161,22 @@ final class PlatformDependent0 {
} }
} }
static void freeDirectBuffer(ByteBuffer buffer) {
if (CLEANER_FIELD == null) {
return;
}
try {
Cleaner cleaner = (Cleaner) CLEANER_FIELD.get(buffer);
if (cleaner == null) {
throw new IllegalArgumentException(
"attempted to deallocate the buffer which was allocated via JNIEnv->NewDirectByteBuffer()");
}
cleaner.clean();
} catch (Throwable t) {
// Nothing we can do here.
}
}
static long directBufferAddress(ByteBuffer buffer) { static long directBufferAddress(ByteBuffer buffer) {
return getLong(buffer, ADDRESS_FIELD_OFFSET); return getLong(buffer, ADDRESS_FIELD_OFFSET);
} }