public final class PlatformDependent
extends java.lang.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 | Class and Description |
---|---|
private static class |
PlatformDependent.AtomicLongCounter |
private static class |
PlatformDependent.Mpsc |
private static interface |
PlatformDependent.ThreadLocalRandomProvider |
Modifier and Type | Field and Description |
---|---|
private static int |
ADDRESS_SIZE |
static boolean |
BIG_ENDIAN_NATIVE_ORDER |
private static int |
BIT_MODE |
private static long |
BYTE_ARRAY_BASE_OFFSET |
private static boolean |
CAN_ENABLE_TCP_NODELAY_BY_DEFAULT |
private static Cleaner |
CLEANER |
private static int |
DEFAULT_MAX_MPSC_CAPACITY |
private static boolean |
DIRECT_BUFFER_PREFERRED |
private static java.util.concurrent.atomic.AtomicLong |
DIRECT_MEMORY_COUNTER |
private static long |
DIRECT_MEMORY_LIMIT |
private static boolean |
HAS_UNSAFE |
private static boolean |
IS_WINDOWS |
private static InternalLogger |
logger |
private static int |
MAX_ALLOWED_MPSC_CAPACITY |
private static long |
MAX_DIRECT_MEMORY |
private static java.util.regex.Pattern |
MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN |
private static boolean |
MAYBE_SUPER_USER |
private static int |
MIN_MAX_MPSC_CAPACITY |
private static int |
MPSC_CHUNK_SIZE |
private static Cleaner |
NOOP |
private static PlatformDependent.ThreadLocalRandomProvider |
RANDOM_PROVIDER |
private static java.io.File |
TMPDIR |
private static int |
UNINITIALIZED_ARRAY_ALLOCATION_THRESHOLD |
private static boolean |
USE_DIRECT_BUFFER_NO_CLEANER |
Modifier | Constructor and Description |
---|---|
private |
PlatformDependent() |
Modifier and Type | Method and Description |
---|---|
static int |
addressSize()
Return the address size of the OS.
|
private static int |
addressSize0() |
static java.nio.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.)
|
private static int |
bitMode0() |
private static long |
byteArrayBaseOffset0() |
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,
long dstAddr,
long length) |
static void |
copyMemory(long srcAddr,
byte[] dst,
int dstIndex,
long length) |
static void |
copyMemory(long srcAddr,
long dstAddr,
long length) |
private static void |
decrementMemoryCounter(int capacity) |
static java.nio.ByteBuffer |
directBuffer(long memoryAddress,
int size) |
static long |
directBufferAddress(java.nio.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. |
private static boolean |
equalsSafe(byte[] bytes1,
int startPos1,
byte[] bytes2,
int startPos2,
int length) |
static void |
freeDirectBuffer(java.nio.ByteBuffer buffer)
Try to deallocate the specified direct
ByteBuffer . |
static void |
freeDirectNoCleaner(java.nio.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(long address) |
static java.lang.ClassLoader |
getClassLoader(java.lang.Class<?> clazz)
Return the
ClassLoader for the given Class . |
static java.lang.ClassLoader |
getContextClassLoader()
Return the context
ClassLoader for the current Thread . |
static int |
getInt(byte[] data,
int index) |
static int |
getInt(long address) |
static int |
getInt(java.lang.Object object,
long fieldOffset) |
private static int |
getIntSafe(byte[] bytes,
int offset) |
static long |
getLong(byte[] data,
int index) |
static long |
getLong(long address) |
private static long |
getLongSafe(byte[] bytes,
int offset) |
static short |
getShort(byte[] data,
int index) |
static short |
getShort(long address) |
private static short |
getShortSafe(byte[] bytes,
int offset) |
static java.lang.ClassLoader |
getSystemClassLoader()
Return the system
ClassLoader . |
static int |
hashCodeAscii(byte[] bytes,
int startPos,
int length)
Calculate a hash code of a byte array assuming ASCII character encoding.
|
static int |
hashCodeAscii(java.lang.CharSequence bytes)
Calculate a hash code of a byte array assuming ASCII character encoding.
|
private static int |
hashCodeAsciiCompute(java.lang.CharSequence value,
int offset,
int hash)
Identical to
PlatformDependent0.hashCodeAsciiCompute(long, int) but for CharSequence . |
(package private) static int |
hashCodeAsciiSafe(byte[] bytes,
int startPos,
int length)
Package private for testing purposes only!
|
private static int |
hashCodeAsciiSanitizeByte(char value)
Identical to
PlatformDependent0.hashCodeAsciiSanitize(byte) but for CharSequence . |
private static int |
hashCodeAsciiSanitizeInt(java.lang.CharSequence value,
int offset)
Identical to
PlatformDependent0.hashCodeAsciiSanitize(int) but for CharSequence . |
private static int |
hashCodeAsciiSanitizeShort(java.lang.CharSequence value,
int offset)
Identical to
PlatformDependent0.hashCodeAsciiSanitize(short) but for CharSequence . |
static boolean |
hasUnsafe()
Return
true if sun.misc.Unsafe was found on the classpath and can be used for accelerated
direct memory access. |
private static boolean |
hasUnsafe0() |
private static void |
incrementMemoryCounter(int capacity) |
static boolean |
isAndroid()
Returns
true if and only if the current platform is Android |
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 |
private static boolean |
isWindows0() |
static boolean |
isZero(byte[] bytes,
int startPos,
int length)
Determine if a subsection of an array is zero.
|
private static boolean |
isZeroSafe(byte[] bytes,
int startPos,
int length) |
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.
|
private static long |
maxDirectMemory0() |
static boolean |
maybeSuperUser()
Return
true if the current user may be a super-user. |
private static boolean |
maybeSuperUser0() |
static <C> java.util.Deque<C> |
newConcurrentDeque()
Returns a new concurrent
Deque . |
static <K,V> java.util.concurrent.ConcurrentMap<K,V> |
newConcurrentHashMap()
Creates a new fastest
ConcurrentMap implementation for the current platform. |
static <K,V> java.util.concurrent.ConcurrentMap<K,V> |
newConcurrentHashMap(int initialCapacity)
Creates a new fastest
ConcurrentMap implementation for the current platform. |
static <K,V> java.util.concurrent.ConcurrentMap<K,V> |
newConcurrentHashMap(int initialCapacity,
float loadFactor)
Creates a new fastest
ConcurrentMap implementation for the current platform. |
static <K,V> java.util.concurrent.ConcurrentMap<K,V> |
newConcurrentHashMap(int initialCapacity,
float loadFactor,
int concurrencyLevel)
Creates a new fastest
ConcurrentMap implementation for the current platform. |
static <K,V> java.util.concurrent.ConcurrentMap<K,V> |
newConcurrentHashMap(java.util.Map<? extends K,? extends V> map)
Creates a new fastest
ConcurrentMap implementation for the current platform. |
static <T> java.util.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!) with the given fixes capacity . |
static LongCounter |
newLongCounter()
Creates a new fastest
LongCounter implementation for the current platform. |
static <T> java.util.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> java.util.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> java.util.Queue<T> |
newSpscQueue()
Create a new
Queue which is safe to use for single producer (one thread!) and a single
consumer (one thread!). |
static void |
putByte(byte[] data,
int index,
byte value) |
static void |
putByte(long address,
byte value) |
static void |
putInt(byte[] data,
int index,
int value) |
static void |
putInt(long address,
int value) |
static void |
putLong(byte[] data,
int index,
long value) |
static void |
putLong(long address,
long value) |
static void |
putShort(byte[] data,
int index,
short value) |
static void |
putShort(long address,
short value) |
static java.nio.ByteBuffer |
reallocateDirectNoCleaner(java.nio.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 java.util.Random |
threadLocalRandom()
Return a
Random which is not-threadsafe and so can only be used from the same thread. |
static void |
throwException(java.lang.Throwable t)
Raises an exception bypassing compiler checks for checked exceptions.
|
private static <E extends java.lang.Throwable> |
throwException0(java.lang.Throwable t) |
static java.io.File |
tmpdir()
Returns the temporary directory.
|
private static java.io.File |
tmpdir0() |
private static java.io.File |
toDirectory(java.lang.String path) |
static boolean |
useDirectBufferNoCleaner() |
private static final InternalLogger logger
private static final java.util.regex.Pattern MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN
private static final boolean IS_WINDOWS
private static final boolean MAYBE_SUPER_USER
private static final boolean CAN_ENABLE_TCP_NODELAY_BY_DEFAULT
private static final boolean HAS_UNSAFE
private static final boolean DIRECT_BUFFER_PREFERRED
private static final long MAX_DIRECT_MEMORY
private static final int MPSC_CHUNK_SIZE
private static final int MIN_MAX_MPSC_CAPACITY
private static final int DEFAULT_MAX_MPSC_CAPACITY
private static final int MAX_ALLOWED_MPSC_CAPACITY
private static final long BYTE_ARRAY_BASE_OFFSET
private static final java.io.File TMPDIR
private static final int BIT_MODE
private static final int ADDRESS_SIZE
private static final boolean USE_DIRECT_BUFFER_NO_CLEANER
private static final java.util.concurrent.atomic.AtomicLong DIRECT_MEMORY_COUNTER
private static final long DIRECT_MEMORY_LIMIT
private static final PlatformDependent.ThreadLocalRandomProvider RANDOM_PROVIDER
private static final Cleaner CLEANER
private static final int UNINITIALIZED_ARRAY_ALLOCATION_THRESHOLD
public static final boolean BIG_ENDIAN_NATIVE_ORDER
private static final Cleaner NOOP
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 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 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 java.io.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(java.lang.Throwable t)
private static <E extends java.lang.Throwable> void throwException0(java.lang.Throwable t) throws E extends java.lang.Throwable
E extends java.lang.Throwable
public static <K,V> java.util.concurrent.ConcurrentMap<K,V> newConcurrentHashMap()
ConcurrentMap
implementation for the current platform.public static LongCounter newLongCounter()
LongCounter
implementation for the current platform.public static <K,V> java.util.concurrent.ConcurrentMap<K,V> newConcurrentHashMap(int initialCapacity)
ConcurrentMap
implementation for the current platform.public static <K,V> java.util.concurrent.ConcurrentMap<K,V> newConcurrentHashMap(int initialCapacity, float loadFactor)
ConcurrentMap
implementation for the current platform.public static <K,V> java.util.concurrent.ConcurrentMap<K,V> newConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
ConcurrentMap
implementation for the current platform.public static <K,V> java.util.concurrent.ConcurrentMap<K,V> newConcurrentHashMap(java.util.Map<? extends K,? extends V> map)
ConcurrentMap
implementation for the current platform.public static void freeDirectBuffer(java.nio.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(java.nio.ByteBuffer buffer)
public static java.nio.ByteBuffer directBuffer(long memoryAddress, int size)
public static int getInt(java.lang.Object object, long fieldOffset)
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 short getShort(byte[] data, int index)
public static int getInt(byte[] data, int index)
public static long getLong(byte[] data, int index)
private static long getLongSafe(byte[] bytes, int offset)
private static int getIntSafe(byte[] bytes, int offset)
private static short getShortSafe(byte[] bytes, int offset)
private static int hashCodeAsciiCompute(java.lang.CharSequence value, int offset, int hash)
PlatformDependent0.hashCodeAsciiCompute(long, int)
but for CharSequence
.private static int hashCodeAsciiSanitizeInt(java.lang.CharSequence value, int offset)
PlatformDependent0.hashCodeAsciiSanitize(int)
but for CharSequence
.private static int hashCodeAsciiSanitizeShort(java.lang.CharSequence value, int offset)
PlatformDependent0.hashCodeAsciiSanitize(short)
but for CharSequence
.private static int hashCodeAsciiSanitizeByte(char value)
PlatformDependent0.hashCodeAsciiSanitize(byte)
but for CharSequence
.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 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 void copyMemory(long srcAddr, long dstAddr, long length)
public static void copyMemory(byte[] src, int srcIndex, long dstAddr, 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 java.nio.ByteBuffer allocateDirectNoCleaner(int capacity)
ByteBuffer
with the given capacity
. ByteBuffer
s allocated with
this method MUST be deallocated via freeDirectNoCleaner(ByteBuffer)
.public static java.nio.ByteBuffer reallocateDirectNoCleaner(java.nio.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(java.nio.ByteBuffer buffer)
ByteBuffer
s that were allocated via
allocateDirectNoCleaner(int)
.private static void incrementMemoryCounter(int capacity)
private static void decrementMemoryCounter(int capacity)
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(java.lang.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> java.util.Queue<T> newMpscQueue()
Queue
which is safe to use for multiple producers (different threads) and a single
consumer (one thread!).public static <T> java.util.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> java.util.Queue<T> newSpscQueue()
Queue
which is safe to use for single producer (one thread!) and a single
consumer (one thread!).public static <T> java.util.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 java.lang.ClassLoader getClassLoader(java.lang.Class<?> clazz)
ClassLoader
for the given Class
.public static java.lang.ClassLoader getContextClassLoader()
ClassLoader
for the current Thread
.public static java.lang.ClassLoader getSystemClassLoader()
ClassLoader
.public static <C> java.util.Deque<C> newConcurrentDeque()
Deque
.public static java.util.Random threadLocalRandom()
Random
which is not-threadsafe and so can only be used from the same thread.private static boolean isWindows0()
private static boolean maybeSuperUser0()
private static boolean hasUnsafe0()
private static long maxDirectMemory0()
private static java.io.File tmpdir0()
private static java.io.File toDirectory(java.lang.String path)
private static int bitMode0()
private static int addressSize0()
private static long byteArrayBaseOffset0()
private static boolean equalsSafe(byte[] bytes1, int startPos1, byte[] bytes2, int startPos2, int length)
private static boolean isZeroSafe(byte[] bytes, int startPos, int length)
static int hashCodeAsciiSafe(byte[] bytes, int startPos, int length)