public final class PlatformDependent extends Object
sun.misc.Unsafe
object.
You can disable the use of sun.misc.Unsafe
if you specify
the system property io.netty.noUnsafe.
Modifier and Type | Field and Description |
---|---|
static boolean |
BIG_ENDIAN_NATIVE_ORDER |
Modifier and Type | Method and Description |
---|---|
static int |
addressSize()
Return the address size of the OS.
4 (for 32 bits systems ) and 8 (for 64 bits systems).
|
static long |
align(long value,
int alignment) |
static ByteBuffer |
alignDirectBuffer(ByteBuffer buffer,
int alignment) |
static ByteBuffer |
allocateDirectNoCleaner(int capacity)
Allocate a new
ByteBuffer with the given capacity . |
static long |
allocateMemory(long size) |
static byte[] |
allocateUninitializedArray(int size) |
static int |
bitMode()
Returns the bit mode of the current VM (usually 32 or 64.)
|
static long |
byteArrayBaseOffset() |
static boolean |
canEnableTcpNoDelayByDefault()
Returns
true if and only if it is fine to enable TCP_NODELAY socket option by default. |
static void |
copyMemory(byte[] src,
int srcIndex,
byte[] dst,
int dstIndex,
long length) |
static void |
copyMemory(byte[] src,
int srcIndex,
long dstAddr,
long length) |
static void |
copyMemory(long srcAddr,
byte[] dst,
int dstIndex,
long length) |
static void |
copyMemory(long srcAddr,
long dstAddr,
long length) |
static File |
createTempFile(String prefix,
String suffix,
File directory) |
static ByteBuffer |
directBuffer(long memoryAddress,
int size) |
static long |
directBufferAddress(ByteBuffer buffer) |
static boolean |
directBufferPreferred()
Returns
true if the platform has reliable low-level direct buffer access API and a user has not specified
-Dio.netty.noPreferDirect option. |
static boolean |
equals(byte[] bytes1,
int startPos1,
byte[] bytes2,
int startPos2,
int length)
Compare two
byte arrays for equality. |
static int |
equalsConstantTime(byte[] bytes1,
int startPos1,
byte[] bytes2,
int startPos2,
int length)
Compare two
byte arrays for equality without leaking timing information. |
static long |
estimateMaxDirectMemory()
Compute an estimate of the maximum amount of direct memory available to this JVM.
|
static void |
freeDirectBuffer(ByteBuffer buffer)
Try to deallocate the specified direct
ByteBuffer . |
static void |
freeDirectNoCleaner(ByteBuffer buffer)
This method MUST only be called for
ByteBuffer s that were allocated via
allocateDirectNoCleaner(int) . |
static void |
freeMemory(long address) |
static byte |
getByte(byte[] data,
int index) |
static byte |
getByte(byte[] data,
long index) |
static byte |
getByte(long address) |
static ClassLoader |
getClassLoader(Class<?> clazz)
Return the
ClassLoader for the given Class . |
static ClassLoader |
getContextClassLoader()
Return the context
ClassLoader for the current Thread . |
static int |
getInt(byte[] data,
int index) |
static int |
getInt(int[] data,
long index) |
static int |
getInt(long address) |
static int |
getInt(Object object,
long fieldOffset) |
static int |
getIntVolatile(long address) |
static long |
getLong(byte[] data,
int index) |
static long |
getLong(long address) |
static long |
getLong(long[] data,
long index) |
static Object |
getObject(Object object,
long fieldOffset) |
static short |
getShort(byte[] data,
int index) |
static short |
getShort(long address) |
static ClassLoader |
getSystemClassLoader()
Return the system
ClassLoader . |
static Throwable |
getUnsafeUnavailabilityCause()
Return the reason (if any) why
sun.misc.Unsafe was not available. |
static boolean |
hasAlignDirectByteBuffer() |
static boolean |
hasDirectBufferNoCleanerConstructor() |
static int |
hashCodeAscii(byte[] bytes,
int startPos,
int length)
Calculate a hash code of a byte array assuming ASCII character encoding.
|
static int |
hashCodeAscii(CharSequence bytes)
Calculate a hash code of a byte array assuming ASCII character encoding.
|
static boolean |
hasUnsafe()
Return
true if sun.misc.Unsafe was found on the classpath and can be used for accelerated
direct memory access. |
static boolean |
isAndroid()
Returns
true if and only if the current platform is Android |
static boolean |
isIkvmDotNet()
|
static boolean |
isJ9Jvm()
|
static boolean |
isOsx()
Return
true if the JVM is running on OSX / MacOS |
static boolean |
isUnaligned()
true if and only if the platform supports unaligned access. |
static boolean |
isWindows()
Return
true if the JVM is running on Windows |
static boolean |
isZero(byte[] bytes,
int startPos,
int length)
Determine if a subsection of an array is zero.
|
static int |
javaVersion()
Return the version of Java under which this library is used.
|
static long |
maxDirectMemory()
Returns the maximum memory reserved for direct buffer allocation.
|
static boolean |
maybeSuperUser()
Return
true if the current user may be a super-user. |
static <C> Deque<C> |
newConcurrentDeque()
Returns a new concurrent
Deque . |
static <K,V> ConcurrentMap<K,V> |
newConcurrentHashMap()
Creates a new fastest
ConcurrentMap implementation for the current platform. |
static <K,V> ConcurrentMap<K,V> |
newConcurrentHashMap(int initialCapacity)
Creates a new fastest
ConcurrentMap implementation for the current platform. |
static <K,V> ConcurrentMap<K,V> |
newConcurrentHashMap(int initialCapacity,
float loadFactor)
Creates a new fastest
ConcurrentMap implementation for the current platform. |
static <K,V> ConcurrentMap<K,V> |
newConcurrentHashMap(int initialCapacity,
float loadFactor,
int concurrencyLevel)
Creates a new fastest
ConcurrentMap implementation for the current platform. |
static <K,V> ConcurrentMap<K,V> |
newConcurrentHashMap(Map<? extends K,? extends V> map)
Creates a new fastest
ConcurrentMap implementation for the current platform. |
static <T> Queue<T> |
newFixedMpmcQueue(int capacity)
Create a new
Queue which is safe to use for multiple producers (different threads) and multiple
consumers with the given fixes capacity . |
static <T> Queue<T> |
newFixedMpscQueue(int capacity)
Create a new
Queue which is safe to use for multiple producers (different threads) and a single
consumer (one thread!) |
static <T> Queue<T> |
newFixedMpscUnpaddedQueue(int capacity)
Create a new un-padded
Queue which is safe to use for multiple producers (different threads) and a single
consumer (one thread!) |
static LongCounter |
newLongCounter()
Creates a new fastest
LongCounter implementation for the current platform. |
static <T> Queue<T> |
newMpscQueue()
Create a new
Queue which is safe to use for multiple producers (different threads) and a single
consumer (one thread!). |
static <T> Queue<T> |
newMpscQueue(int maxCapacity)
Create a new
Queue which is safe to use for multiple producers (different threads) and a single
consumer (one thread!). |
static <T> Queue<T> |
newMpscQueue(int chunkSize,
int maxCapacity)
Create a new
Queue which is safe to use for multiple producers (different threads) and a single
consumer (one thread!). |
static <T> Queue<T> |
newSpscQueue()
Create a new
Queue which is safe to use for single producer (one thread!) |
static String |
normalizedArch() |
static Set<String> |
normalizedLinuxClassifiers() |
static String |
normalizedOs() |
static long |
objectFieldOffset(Field field) |
static void |
putByte(byte[] data,
int index,
byte value) |
static void |
putByte(long address,
byte value) |
static void |
putByte(Object data,
long offset,
byte value) |
static void |
putInt(byte[] data,
int index,
int value) |
static void |
putInt(long address,
int value) |
static void |
putIntOrdered(long adddress,
int newValue) |
static void |
putLong(byte[] data,
int index,
long value) |
static void |
putLong(long address,
long value) |
static void |
putObject(Object o,
long offset,
Object x) |
static void |
putShort(byte[] data,
int index,
short value) |
static void |
putShort(long address,
short value) |
static ByteBuffer |
reallocateDirectNoCleaner(ByteBuffer buffer,
int capacity)
Reallocate a new
ByteBuffer with the given capacity . |
static long |
reallocateMemory(long address,
long newSize) |
static void |
setMemory(byte[] dst,
int dstIndex,
long bytes,
byte value) |
static void |
setMemory(long address,
long bytes,
byte value) |
static Random |
threadLocalRandom()
Return a
Random which is not-threadsafe and so can only be used from the same thread. |
static void |
throwException(Throwable t)
Raises an exception bypassing compiler checks for checked exceptions.
|
static File |
tmpdir()
Returns the temporary directory.
|
static long |
usedDirectMemory()
Returns the current memory reserved for direct buffer allocation.
|
static boolean |
useDirectBufferNoCleaner() |
public static long byteArrayBaseOffset()
public static boolean hasDirectBufferNoCleanerConstructor()
public static byte[] allocateUninitializedArray(int size)
public static boolean isAndroid()
true
if and only if the current platform is Androidpublic static boolean isWindows()
true
if the JVM is running on Windowspublic static boolean isOsx()
true
if the JVM is running on OSX / MacOSpublic static boolean maybeSuperUser()
true
if the current user may be a super-user. Be aware that this is just an hint and so it may
return false-positives.public static int javaVersion()
public static boolean canEnableTcpNoDelayByDefault()
true
if and only if it is fine to enable TCP_NODELAY socket option by default.public static boolean hasUnsafe()
true
if sun.misc.Unsafe
was found on the classpath and can be used for accelerated
direct memory access.public static Throwable getUnsafeUnavailabilityCause()
sun.misc.Unsafe
was not available.public static boolean isUnaligned()
true
if and only if the platform supports unaligned access.public static boolean directBufferPreferred()
true
if the platform has reliable low-level direct buffer access API and a user has not specified
-Dio.netty.noPreferDirect
option.public static long maxDirectMemory()
public static long usedDirectMemory()
maxDirectMemory()
public static File tmpdir()
public static int bitMode()
public static int addressSize()
public static long allocateMemory(long size)
public static void freeMemory(long address)
public static long reallocateMemory(long address, long newSize)
public static void throwException(Throwable t)
public static <K,V> ConcurrentMap<K,V> newConcurrentHashMap()
ConcurrentMap
implementation for the current platform.public static LongCounter newLongCounter()
LongCounter
implementation for the current platform.public static <K,V> ConcurrentMap<K,V> newConcurrentHashMap(int initialCapacity)
ConcurrentMap
implementation for the current platform.public static <K,V> ConcurrentMap<K,V> newConcurrentHashMap(int initialCapacity, float loadFactor)
ConcurrentMap
implementation for the current platform.public static <K,V> ConcurrentMap<K,V> newConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
ConcurrentMap
implementation for the current platform.public static <K,V> ConcurrentMap<K,V> newConcurrentHashMap(Map<? extends K,? extends V> map)
ConcurrentMap
implementation for the current platform.public static void freeDirectBuffer(ByteBuffer buffer)
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.public static long directBufferAddress(ByteBuffer buffer)
public static ByteBuffer directBuffer(long memoryAddress, int size)
public static int getInt(Object object, long fieldOffset)
public static int getIntVolatile(long address)
public static void putIntOrdered(long adddress, int newValue)
public static byte getByte(long address)
public static short getShort(long address)
public static int getInt(long address)
public static long getLong(long address)
public static byte getByte(byte[] data, int index)
public static byte getByte(byte[] data, long index)
public static short getShort(byte[] data, int index)
public static int getInt(byte[] data, int index)
public static int getInt(int[] data, long index)
public static long getLong(byte[] data, int index)
public static long getLong(long[] data, long index)
public static void putByte(long address, byte value)
public static void putShort(long address, short value)
public static void putInt(long address, int value)
public static void putLong(long address, long value)
public static void putByte(byte[] data, int index, byte value)
public static void putByte(Object data, long offset, byte value)
public static void putShort(byte[] data, int index, short value)
public static void putInt(byte[] data, int index, int value)
public static void putLong(byte[] data, int index, long value)
public static long objectFieldOffset(Field field)
public static void copyMemory(long srcAddr, long dstAddr, long length)
public static void copyMemory(byte[] src, int srcIndex, long dstAddr, long length)
public static void copyMemory(byte[] src, int srcIndex, byte[] dst, int dstIndex, long length)
public static void copyMemory(long srcAddr, byte[] dst, int dstIndex, long length)
public static void setMemory(byte[] dst, int dstIndex, long bytes, byte value)
public static void setMemory(long address, long bytes, byte value)
public static ByteBuffer allocateDirectNoCleaner(int capacity)
ByteBuffer
with the given capacity
. ByteBuffer
s allocated with
this method MUST be deallocated via freeDirectNoCleaner(ByteBuffer)
.public static ByteBuffer reallocateDirectNoCleaner(ByteBuffer buffer, int capacity)
ByteBuffer
with the given capacity
. ByteBuffer
s reallocated with
this method MUST be deallocated via freeDirectNoCleaner(ByteBuffer)
.public static void freeDirectNoCleaner(ByteBuffer buffer)
ByteBuffer
s that were allocated via
allocateDirectNoCleaner(int)
.public static boolean hasAlignDirectByteBuffer()
public static ByteBuffer alignDirectBuffer(ByteBuffer buffer, int alignment)
public static long align(long value, int alignment)
public static boolean useDirectBufferNoCleaner()
public static boolean equals(byte[] bytes1, int startPos1, byte[] bytes2, int startPos2, int length)
byte
arrays for equality. For performance reasons no bounds checking on the
parameters is performed.bytes1
- the first byte array.startPos1
- the position (inclusive) to start comparing in bytes1
.bytes2
- the second byte array.startPos2
- the position (inclusive) to start comparing in bytes2
.length
- the amount of bytes to compare. This is assumed to be validated as not going out of bounds
by the caller.public static boolean isZero(byte[] bytes, int startPos, int length)
bytes
- The byte array.startPos
- The starting index (inclusive) in bytes
.length
- The amount of bytes to check for zero.false
if bytes[startPos:startsPos+length)
contains a value other than zero.public static int equalsConstantTime(byte[] bytes1, int startPos1, byte[] bytes2, int startPos2, int length)
byte
arrays for equality without leaking timing information.
For performance reasons no bounds checking on the parameters is performed.
The int
return type is intentional and is designed to allow cascading of constant time operations:
byte[] s1 = new {1, 2, 3}; byte[] s2 = new {1, 2, 3}; byte[] s3 = new {1, 2, 3}; byte[] s4 = new {4, 5, 6}; boolean equals = (equalsConstantTime(s1, 0, s2, 0, s1.length) & equalsConstantTime(s3, 0, s4, 0, s3.length)) != 0;
bytes1
- the first byte array.startPos1
- the position (inclusive) to start comparing in bytes1
.bytes2
- the second byte array.startPos2
- the position (inclusive) to start comparing in bytes2
.length
- the amount of bytes to compare. This is assumed to be validated as not going out of bounds
by the caller.0
if not equal. 1
if equal.public static int hashCodeAscii(byte[] bytes, int startPos, int length)
bytes
- The array which contains the data to hash.startPos
- What index to start generating a hash code in bytes
length
- The amount of bytes that should be accounted for in the computation.bytes
assuming ASCII character encoding.
The resulting hash code will be case insensitive.public static int hashCodeAscii(CharSequence bytes)
This method assumes that bytes
is equivalent to a byte[]
but just using CharSequence
for storage. The upper most byte of each char
from bytes
is ignored.
bytes
- The array which contains the data to hash (assumed to be equivalent to a byte[]
).bytes
assuming ASCII character encoding.
The resulting hash code will be case insensitive.public static <T> Queue<T> newMpscQueue()
Queue
which is safe to use for multiple producers (different threads) and a single
consumer (one thread!).public static <T> Queue<T> newMpscQueue(int maxCapacity)
Queue
which is safe to use for multiple producers (different threads) and a single
consumer (one thread!).public static <T> Queue<T> newMpscQueue(int chunkSize, int maxCapacity)
Queue
which is safe to use for multiple producers (different threads) and a single
consumer (one thread!).
The queue will grow and shrink its capacity in units of the given chunk size.public static <T> Queue<T> newSpscQueue()
Queue
which is safe to use for single producer (one thread!) and a single
consumer (one thread!).public static <T> Queue<T> newFixedMpscQueue(int capacity)
Queue
which is safe to use for multiple producers (different threads) and a single
consumer (one thread!) with the given fixes capacity
.public static <T> Queue<T> newFixedMpscUnpaddedQueue(int capacity)
Queue
which is safe to use for multiple producers (different threads) and a single
consumer (one thread!) with the given fixes capacity
.newFixedMpscQueue(int)
when the queue is not to be heavily contended.public static <T> Queue<T> newFixedMpmcQueue(int capacity)
Queue
which is safe to use for multiple producers (different threads) and multiple
consumers with the given fixes capacity
.public static ClassLoader getClassLoader(Class<?> clazz)
ClassLoader
for the given Class
.public static ClassLoader getContextClassLoader()
ClassLoader
for the current Thread
.public static ClassLoader getSystemClassLoader()
ClassLoader
.public static Random threadLocalRandom()
Random
which is not-threadsafe and so can only be used from the same thread.public static boolean isJ9Jvm()
public static boolean isIkvmDotNet()
public static long estimateMaxDirectMemory()
The computation is not cached, so you probably want to use maxDirectMemory()
instead.
This will produce debug log output when called.
public static String normalizedArch()
public static String normalizedOs()
public static File createTempFile(String prefix, String suffix, File directory) throws IOException
IOException
Copyright © 2008–2024 The Netty Project. All rights reserved.