Use sun.misc.Unsafe to access a direct ByteBuffer
- Add PooledUnsafeDirectByteBuf, a variant of PooledDirectByteBuf, which accesses its underlying direct ByteBuffer using sun.misc.Unsafe. - To decouple Netty from sun.misc.*, sun.misc.Unsafe is accessed via PlatformDependent. - This change solely introduces about 8+% improvement in direct memory access according to the tests conducted as described in #918
This commit is contained in:
parent
e564157381
commit
04bae9bceb
@ -19,7 +19,6 @@ package io.netty.buffer;
|
|||||||
import io.netty.util.internal.PlatformDependent;
|
import io.netty.util.internal.PlatformDependent;
|
||||||
import io.netty.util.internal.StringUtil;
|
import io.netty.util.internal.StringUtil;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
@ -355,22 +354,6 @@ abstract class PoolArena<T> {
|
|||||||
|
|
||||||
static final class DirectArena extends PoolArena<ByteBuffer> {
|
static final class DirectArena extends PoolArena<ByteBuffer> {
|
||||||
|
|
||||||
private static final boolean UNALIGNED;
|
|
||||||
|
|
||||||
static {
|
|
||||||
boolean unaligned = false;
|
|
||||||
try {
|
|
||||||
Class<?> bitsClass = Class.forName("java.nio.Bits", false, ClassLoader.getSystemClassLoader());
|
|
||||||
Method unalignedMethod = bitsClass.getDeclaredMethod("unaligned");
|
|
||||||
unalignedMethod.setAccessible(true);
|
|
||||||
unaligned = Boolean.TRUE.equals(unalignedMethod.invoke(null));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
UNALIGNED = unaligned;
|
|
||||||
}
|
|
||||||
|
|
||||||
DirectArena(PooledByteBufAllocator parent, int pageSize, int maxOrder, int pageShifts, int chunkSize) {
|
DirectArena(PooledByteBufAllocator parent, int pageSize, int maxOrder, int pageShifts, int chunkSize) {
|
||||||
super(parent, pageSize, maxOrder, pageShifts, chunkSize);
|
super(parent, pageSize, maxOrder, pageShifts, chunkSize);
|
||||||
}
|
}
|
||||||
@ -393,7 +376,7 @@ abstract class PoolArena<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PooledByteBuf<ByteBuffer> newByteBuf(int maxCapacity) {
|
protected PooledByteBuf<ByteBuffer> newByteBuf(int maxCapacity) {
|
||||||
if (UNALIGNED) {
|
if (PlatformDependent.isUnaligned()) {
|
||||||
return new PooledUnsafeDirectByteBuf(maxCapacity);
|
return new PooledUnsafeDirectByteBuf(maxCapacity);
|
||||||
} else {
|
} else {
|
||||||
return new PooledDirectByteBuf(maxCapacity);
|
return new PooledDirectByteBuf(maxCapacity);
|
||||||
@ -406,12 +389,18 @@ abstract class PoolArena<T> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We must duplicate the NIO buffers because they may be accessed by other Netty buffers.
|
if (PlatformDependent.isUnaligned()) {
|
||||||
src = src.duplicate();
|
PlatformDependent.copyMemory(
|
||||||
dst = dst.duplicate();
|
PlatformDependent.directBufferAddress(src) + srcOffset,
|
||||||
src.position(srcOffset).limit(srcOffset + length);
|
PlatformDependent.directBufferAddress(dst) + dstOffset, length);
|
||||||
dst.position(dstOffset);
|
} else {
|
||||||
dst.put(src);
|
// We must duplicate the NIO buffers because they may be accessed by other Netty buffers.
|
||||||
|
src = src.duplicate();
|
||||||
|
dst = dst.duplicate();
|
||||||
|
src.position(srcOffset).limit(srcOffset + length);
|
||||||
|
dst.position(dstOffset);
|
||||||
|
dst.put(src);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012 The Netty Project
|
* Copyright 2013 The Netty Project
|
||||||
*
|
*
|
||||||
* The Netty Project licenses this file to you under the Apache License,
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
@ -17,13 +17,10 @@
|
|||||||
package io.netty.buffer;
|
package io.netty.buffer;
|
||||||
|
|
||||||
import io.netty.util.internal.PlatformDependent;
|
import io.netty.util.internal.PlatformDependent;
|
||||||
import sun.misc.Unsafe;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.nio.Buffer;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.channels.ClosedChannelException;
|
import java.nio.channels.ClosedChannelException;
|
||||||
@ -32,39 +29,8 @@ import java.nio.channels.ScatteringByteChannel;
|
|||||||
|
|
||||||
final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||||
|
|
||||||
private static final Field ADDRESS_FIELD;
|
|
||||||
private static final Unsafe UNSAFE;
|
|
||||||
private static final boolean NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
private static final boolean NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
||||||
|
|
||||||
static {
|
|
||||||
Unsafe unsafe;
|
|
||||||
try {
|
|
||||||
Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe");
|
|
||||||
singleoneInstanceField.setAccessible(true);
|
|
||||||
unsafe = (Unsafe) singleoneInstanceField.get(null);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
throw new Error(t);
|
|
||||||
}
|
|
||||||
UNSAFE = unsafe;
|
|
||||||
|
|
||||||
Field addressField;
|
|
||||||
try {
|
|
||||||
addressField = Buffer.class.getDeclaredField("address");
|
|
||||||
addressField.setAccessible(true);
|
|
||||||
if (addressField.getLong(ByteBuffer.allocate(1)) != 0) {
|
|
||||||
throw new Error("heap buffer address must be 0");
|
|
||||||
}
|
|
||||||
ByteBuffer directBuf = ByteBuffer.allocateDirect(1);
|
|
||||||
if (addressField.getLong(directBuf) == 0) {
|
|
||||||
throw new Error("direct buffer address must be non-zero");
|
|
||||||
}
|
|
||||||
PlatformDependent.freeDirectBuffer(directBuf);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
throw new Error(t);
|
|
||||||
}
|
|
||||||
ADDRESS_FIELD = addressField;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long memoryAddress;
|
private long memoryAddress;
|
||||||
|
|
||||||
PooledUnsafeDirectByteBuf(int maxCapacity) {
|
PooledUnsafeDirectByteBuf(int maxCapacity) {
|
||||||
@ -74,22 +40,17 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
@Override
|
@Override
|
||||||
void init(PoolChunk<ByteBuffer> chunk, long handle, int offset, int length, int maxLength) {
|
void init(PoolChunk<ByteBuffer> chunk, long handle, int offset, int length, int maxLength) {
|
||||||
super.init(chunk, handle, offset, length, maxLength);
|
super.init(chunk, handle, offset, length, maxLength);
|
||||||
initiMemoryAddress();
|
initMemoryAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void initUnpooled(PoolChunk<ByteBuffer> chunk, int length) {
|
void initUnpooled(PoolChunk<ByteBuffer> chunk, int length) {
|
||||||
super.initUnpooled(chunk, length);
|
super.initUnpooled(chunk, length);
|
||||||
initiMemoryAddress();
|
initMemoryAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initiMemoryAddress() {
|
private void initMemoryAddress() {
|
||||||
ByteBuffer memory = this.memory;
|
memoryAddress = PlatformDependent.directBufferAddress(memory) + offset;
|
||||||
try {
|
|
||||||
memoryAddress = ADDRESS_FIELD.getLong(memory) + offset;
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new Error("failed to get the address of a direct buffer", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -105,42 +66,37 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
@Override
|
@Override
|
||||||
public byte getByte(int index) {
|
public byte getByte(int index) {
|
||||||
checkIndex(index);
|
checkIndex(index);
|
||||||
return UNSAFE.getByte(addr(index));
|
return PlatformDependent.getByte(addr(index));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public short getShort(int index) {
|
public short getShort(int index) {
|
||||||
checkIndex(index, 2);
|
checkIndex(index, 2);
|
||||||
if (NATIVE_ORDER) {
|
short v = PlatformDependent.getShort(addr(index));
|
||||||
return UNSAFE.getShort(addr(index));
|
return NATIVE_ORDER? v : Short.reverseBytes(v);
|
||||||
}
|
|
||||||
return Short.reverseBytes(UNSAFE.getShort(addr(index)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getUnsignedMedium(int index) {
|
public int getUnsignedMedium(int index) {
|
||||||
checkIndex(index, 3);
|
checkIndex(index, 3);
|
||||||
long addr = addr(index);
|
long addr = addr(index);
|
||||||
return (UNSAFE.getByte(addr) & 0xff) << 16 | (UNSAFE.getByte(addr + 1) & 0xff) << 8 |
|
return (PlatformDependent.getByte(addr) & 0xff) << 16 |
|
||||||
UNSAFE.getByte(addr + 2) & 0xff;
|
(PlatformDependent.getByte(addr + 1) & 0xff) << 8 |
|
||||||
|
PlatformDependent.getByte(addr + 2) & 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getInt(int index) {
|
public int getInt(int index) {
|
||||||
checkIndex(index, 4);
|
checkIndex(index, 4);
|
||||||
if (NATIVE_ORDER) {
|
int v = PlatformDependent.getInt(addr(index));
|
||||||
return UNSAFE.getInt(addr(index));
|
return NATIVE_ORDER? v : Integer.reverseBytes(v);
|
||||||
}
|
|
||||||
return Integer.reverseBytes(UNSAFE.getInt(addr(index)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getLong(int index) {
|
public long getLong(int index) {
|
||||||
checkIndex(index, 8);
|
checkIndex(index, 8);
|
||||||
if (NATIVE_ORDER) {
|
long v = PlatformDependent.getLong(addr(index));
|
||||||
return UNSAFE.getLong(addr(index));
|
return NATIVE_ORDER? v : Long.reverseBytes(v);
|
||||||
}
|
|
||||||
return Long.reverseBytes(UNSAFE.getLong(addr(index)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -148,7 +104,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
if (dst instanceof PooledUnsafeDirectByteBuf) {
|
if (dst instanceof PooledUnsafeDirectByteBuf) {
|
||||||
PooledUnsafeDirectByteBuf bbdst = (PooledUnsafeDirectByteBuf) dst;
|
PooledUnsafeDirectByteBuf bbdst = (PooledUnsafeDirectByteBuf) dst;
|
||||||
UNSAFE.copyMemory(addr(index), bbdst.addr(dstIndex), length);
|
PlatformDependent.copyMemory(addr(index), bbdst.addr(dstIndex), length);
|
||||||
} else if (dst.hasArray()) {
|
} else if (dst.hasArray()) {
|
||||||
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
|
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
|
||||||
} else {
|
} else {
|
||||||
@ -209,14 +165,14 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf setByte(int index, int value) {
|
public ByteBuf setByte(int index, int value) {
|
||||||
checkIndex(index);
|
checkIndex(index);
|
||||||
UNSAFE.putByte(addr(index), (byte) value);
|
PlatformDependent.putByte(addr(index), (byte) value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf setShort(int index, int value) {
|
public ByteBuf setShort(int index, int value) {
|
||||||
checkIndex(index, 2);
|
checkIndex(index, 2);
|
||||||
UNSAFE.putShort(addr(index), NATIVE_ORDER? (short) value : Short.reverseBytes((short) value));
|
PlatformDependent.putShort(addr(index), NATIVE_ORDER? (short) value : Short.reverseBytes((short) value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,23 +180,23 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
public ByteBuf setMedium(int index, int value) {
|
public ByteBuf setMedium(int index, int value) {
|
||||||
checkIndex(index, 3);
|
checkIndex(index, 3);
|
||||||
long addr = addr(index);
|
long addr = addr(index);
|
||||||
UNSAFE.putByte(addr, (byte) (value >>> 16));
|
PlatformDependent.putByte(addr, (byte) (value >>> 16));
|
||||||
UNSAFE.putByte(addr + 1, (byte) (value >>> 8));
|
PlatformDependent.putByte(addr + 1, (byte) (value >>> 8));
|
||||||
UNSAFE.putByte(addr + 2, (byte) value);
|
PlatformDependent.putByte(addr + 2, (byte) value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf setInt(int index, int value) {
|
public ByteBuf setInt(int index, int value) {
|
||||||
checkIndex(index, 4);
|
checkIndex(index, 4);
|
||||||
UNSAFE.putInt(addr(index), NATIVE_ORDER? value : Integer.reverseBytes(value));
|
PlatformDependent.putInt(addr(index), NATIVE_ORDER? value : Integer.reverseBytes(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf setLong(int index, long value) {
|
public ByteBuf setLong(int index, long value) {
|
||||||
checkIndex(index, 8);
|
checkIndex(index, 8);
|
||||||
UNSAFE.putLong(addr(index), NATIVE_ORDER? value : Long.reverseBytes(value));
|
PlatformDependent.putLong(addr(index), NATIVE_ORDER? value : Long.reverseBytes(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,7 +205,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
if (src instanceof PooledUnsafeDirectByteBuf) {
|
if (src instanceof PooledUnsafeDirectByteBuf) {
|
||||||
PooledUnsafeDirectByteBuf bbsrc = (PooledUnsafeDirectByteBuf) src;
|
PooledUnsafeDirectByteBuf bbsrc = (PooledUnsafeDirectByteBuf) src;
|
||||||
UNSAFE.copyMemory(bbsrc.addr(srcIndex), addr(index), length);
|
PlatformDependent.copyMemory(bbsrc.addr(srcIndex), addr(index), length);
|
||||||
} else if (src.hasArray()) {
|
} else if (src.hasArray()) {
|
||||||
setBytes(index, src.array(), src.arrayOffset() + srcIndex, length);
|
setBytes(index, src.array(), src.arrayOffset() + srcIndex, length);
|
||||||
} else {
|
} else {
|
||||||
@ -313,7 +269,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
public ByteBuf copy(int index, int length) {
|
public ByteBuf copy(int index, int length) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
PooledUnsafeDirectByteBuf copy = (PooledUnsafeDirectByteBuf) alloc().directBuffer(capacity(), maxCapacity());
|
PooledUnsafeDirectByteBuf copy = (PooledUnsafeDirectByteBuf) alloc().directBuffer(capacity(), maxCapacity());
|
||||||
UNSAFE.copyMemory(addr(index), copy.addr(index), length);
|
PlatformDependent.copyMemory(addr(index), copy.addr(index), length);
|
||||||
copy.setIndex(index, index + length);
|
copy.setIndex(index, index + length);
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
@ -97,6 +97,10 @@ public final class PlatformDependent {
|
|||||||
return IS_UNALIGNED;
|
return IS_UNALIGNED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static long directBufferAddress(ByteBuffer buffer) {
|
||||||
|
return PlatformDependent0.directBufferAddress(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try to deallocate the specified direct {@link ByteBuffer}. Please note this method does nothing if
|
* Try to deallocate the specified direct {@link ByteBuffer}. Please note this method does nothing if
|
||||||
* the current platform does not support this operation or the specified buffer is not a direct buffer.
|
* the current platform does not support this operation or the specified buffer is not a direct buffer.
|
||||||
@ -115,6 +119,42 @@ public final class PlatformDependent {
|
|||||||
return PlatformDependent0.objectFieldOffset(field);
|
return PlatformDependent0.objectFieldOffset(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte getByte(long address) {
|
||||||
|
return PlatformDependent0.getByte(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static short getShort(long address) {
|
||||||
|
return PlatformDependent0.getShort(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getInt(long address) {
|
||||||
|
return PlatformDependent0.getInt(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long getLong(long address) {
|
||||||
|
return PlatformDependent0.getLong(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void putByte(long address, byte value) {
|
||||||
|
PlatformDependent0.putByte(address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void putShort(long address, short value) {
|
||||||
|
PlatformDependent0.putShort(address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void putInt(long address, int value) {
|
||||||
|
PlatformDependent0.putInt(address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void putLong(long address, long value) {
|
||||||
|
PlatformDependent0.putLong(address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void copyMemory(long srcAddr, long dstAddr, long length) {
|
||||||
|
PlatformDependent0.copyMemory(srcAddr, dstAddr, length);
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean isAndroid0() {
|
private static boolean isAndroid0() {
|
||||||
boolean android;
|
boolean android;
|
||||||
try {
|
try {
|
||||||
|
@ -20,6 +20,7 @@ import sun.misc.Unsafe;
|
|||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.nio.Buffer;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -28,7 +29,8 @@ import java.nio.ByteBuffer;
|
|||||||
final class PlatformDependent0 {
|
final class PlatformDependent0 {
|
||||||
|
|
||||||
private static final Unsafe UNSAFE;
|
private static final Unsafe UNSAFE;
|
||||||
private static final Field CLEANER_FIELD;
|
private static final long CLEANER_FIELD_OFFSET;
|
||||||
|
private static final long ADDRESS_FIELD_OFFSET;
|
||||||
private static final boolean UNALIGNED;
|
private static final boolean UNALIGNED;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
@ -42,28 +44,57 @@ final class PlatformDependent0 {
|
|||||||
}
|
}
|
||||||
UNSAFE = unsafe;
|
UNSAFE = unsafe;
|
||||||
|
|
||||||
ByteBuffer direct = ByteBuffer.allocateDirect(1);
|
if (unsafe == null) {
|
||||||
Field cleanerField;
|
CLEANER_FIELD_OFFSET = -1;
|
||||||
try {
|
ADDRESS_FIELD_OFFSET = -1;
|
||||||
cleanerField = direct.getClass().getDeclaredField("cleaner");
|
UNALIGNED = false;
|
||||||
cleanerField.setAccessible(true);
|
} else {
|
||||||
Cleaner cleaner = (Cleaner) cleanerField.get(direct);
|
ByteBuffer direct = ByteBuffer.allocateDirect(1);
|
||||||
cleaner.clean();
|
Field cleanerField;
|
||||||
} catch (Throwable t) {
|
try {
|
||||||
cleanerField = null;
|
cleanerField = direct.getClass().getDeclaredField("cleaner");
|
||||||
}
|
cleanerField.setAccessible(true);
|
||||||
CLEANER_FIELD = cleanerField;
|
Cleaner cleaner = (Cleaner) cleanerField.get(direct);
|
||||||
|
cleaner.clean();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
cleanerField = null;
|
||||||
|
}
|
||||||
|
CLEANER_FIELD_OFFSET = cleanerField != null? objectFieldOffset(cleanerField) : -1;
|
||||||
|
|
||||||
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());
|
||||||
Method unalignedMethod = bitsClass.getDeclaredMethod("unaligned");
|
Method unalignedMethod = bitsClass.getDeclaredMethod("unaligned");
|
||||||
unalignedMethod.setAccessible(true);
|
unalignedMethod.setAccessible(true);
|
||||||
unaligned = Boolean.TRUE.equals(unalignedMethod.invoke(null));
|
unaligned = Boolean.TRUE.equals(unalignedMethod.invoke(null));
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
unaligned = false;
|
unaligned = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unaligned) {
|
||||||
|
Field addressField = null;
|
||||||
|
try {
|
||||||
|
addressField = Buffer.class.getDeclaredField("address");
|
||||||
|
addressField.setAccessible(true);
|
||||||
|
if (addressField.getLong(ByteBuffer.allocate(1)) != 0) {
|
||||||
|
unaligned = false;
|
||||||
|
} else {
|
||||||
|
ByteBuffer directBuf = ByteBuffer.allocateDirect(1);
|
||||||
|
if (addressField.getLong(directBuf) == 0) {
|
||||||
|
unaligned = false;
|
||||||
|
}
|
||||||
|
freeDirectBuffer(directBuf);
|
||||||
|
}
|
||||||
|
} catch (Throwable t) {
|
||||||
|
unaligned = false;
|
||||||
|
}
|
||||||
|
ADDRESS_FIELD_OFFSET = addressField != null? objectFieldOffset(addressField) : -1;
|
||||||
|
} else {
|
||||||
|
ADDRESS_FIELD_OFFSET = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
UNALIGNED = unaligned;
|
||||||
}
|
}
|
||||||
UNALIGNED = unaligned;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean hasUnsafe() {
|
static boolean hasUnsafe() {
|
||||||
@ -71,13 +102,21 @@ final class PlatformDependent0 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static boolean canFreeDirectBuffer() {
|
static boolean canFreeDirectBuffer() {
|
||||||
return CLEANER_FIELD != null;
|
return ADDRESS_FIELD_OFFSET >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static long directBufferAddress(ByteBuffer buffer) {
|
||||||
|
if (!isUnaligned()) {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
|
||||||
|
return getLong(buffer, ADDRESS_FIELD_OFFSET);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void freeDirectBuffer(ByteBuffer buffer) {
|
static void freeDirectBuffer(ByteBuffer buffer) {
|
||||||
Cleaner cleaner;
|
Cleaner cleaner;
|
||||||
try {
|
try {
|
||||||
cleaner = (Cleaner) CLEANER_FIELD.get(buffer);
|
cleaner = (Cleaner) getObject(buffer, CLEANER_FIELD_OFFSET);
|
||||||
cleaner.clean();
|
cleaner.clean();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
// Nothing we can do here.
|
// Nothing we can do here.
|
||||||
@ -92,10 +131,50 @@ final class PlatformDependent0 {
|
|||||||
return UNSAFE.getObject(object, fieldOffset);
|
return UNSAFE.getObject(object, fieldOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static long getLong(Object object, long fieldOffset) {
|
||||||
|
return UNSAFE.getLong(object, fieldOffset);
|
||||||
|
}
|
||||||
|
|
||||||
static long objectFieldOffset(Field field) {
|
static long objectFieldOffset(Field field) {
|
||||||
return UNSAFE.objectFieldOffset(field);
|
return UNSAFE.objectFieldOffset(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static byte getByte(long address) {
|
||||||
|
return UNSAFE.getByte(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
static short getShort(long address) {
|
||||||
|
return UNSAFE.getShort(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getInt(long address) {
|
||||||
|
return UNSAFE.getInt(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long getLong(long address) {
|
||||||
|
return UNSAFE.getLong(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void putByte(long address, byte value) {
|
||||||
|
UNSAFE.putByte(address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void putShort(long address, short value) {
|
||||||
|
UNSAFE.putShort(address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void putInt(long address, int value) {
|
||||||
|
UNSAFE.putInt(address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void putLong(long address, long value) {
|
||||||
|
UNSAFE.putLong(address, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void copyMemory(long srcAddr, long dstAddr, long length) {
|
||||||
|
UNSAFE.copyMemory(srcAddr, dstAddr, length);
|
||||||
|
}
|
||||||
|
|
||||||
private PlatformDependent0() {
|
private PlatformDependent0() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user