public final class UnsafeUtil extends Object
assertHasUnsafe() method, before
calling any of the other methods.
Avoid `import static` for these individual methods. Always qualify method usages with `UnsafeUtil` so use sites show up in code greps.
| Modifier and Type | Field and Description |
|---|---|
static boolean |
allowUnalignedMemoryAccess |
static boolean |
storeByteOrderIsNative |
| Constructor and Description |
|---|
UnsafeUtil() |
| Modifier and Type | Method and Description |
|---|---|
static long |
alignedMemory(long pointer,
int alignBy)
Returns address pointer equal to or slightly after the given
pointer. |
static long |
allocateMemory(long sizeInBytes)
Allocate a slab of memory of the given size in bytes, and return a pointer to that memory.
|
static int |
arrayBaseOffset(Class klass) |
static int |
arrayIndexScale(Class klass) |
static int |
arrayOffset(int index,
int base,
int scale) |
static void |
assertHasUnsafe() |
static boolean |
compareAndSwapLong(Object obj,
long offset,
long expected,
long update)
Atomically compare the current value of the given long field with the expected value, and if they are the
equal, set the field to the updated value and return true.
|
static boolean |
compareAndSwapObject(Object obj,
long offset,
Object expected,
Object update)
Same as compareAndSwapLong, but for object references.
|
static void |
copyMemory(long srcAddress,
long destAddress,
long bytes)
Copy the given number of bytes from the source address to the destination address.
|
static boolean |
exchangeNativeAccessCheckEnabled(boolean newSetting)
Change if native access checking is enabled by setting it to the given new setting, and returning the old
setting.
|
static void |
free(long pointer)
Free the memory that was allocated with
allocateMemory(long). |
static int |
getAndAddInt(Object obj,
long offset,
int delta)
Atomically add the given delta to the int field, and return its previous value.
|
static long |
getAndAddLong(Object obj,
long offset,
long delta)
Atomically add the given delta to the long field, and return its previous value.
|
static Object |
getAndSetObject(Object obj,
long offset,
Object newValue)
Same as getAndAddInt, but for object references.
|
static boolean |
getBoolean(Object obj,
long offset) |
static boolean |
getBooleanVolatile(Object obj,
long offset) |
static byte |
getByte(long address) |
static byte |
getByte(Object obj,
long offset) |
static byte |
getByteVolatile(long address) |
static byte |
getByteVolatile(Object obj,
long offset) |
static char |
getChar(long address) |
static char |
getChar(Object obj,
long offset) |
static char |
getCharVolatile(long address) |
static char |
getCharVolatile(Object obj,
long offset) |
static double |
getDouble(long address) |
static double |
getDouble(Object obj,
long offset) |
static double |
getDoubleVolatile(long address) |
static double |
getDoubleVolatile(Object obj,
long offset) |
static long |
getFieldOffset(Class<?> type,
String field)
Get the object-relative field offset.
|
static float |
getFloat(long address) |
static float |
getFloat(Object obj,
long offset) |
static float |
getFloatVolatile(long address) |
static float |
getFloatVolatile(Object obj,
long offset) |
static int |
getInt(long address) |
static int |
getInt(Object obj,
long offset) |
static int |
getIntByteWiseLittleEndian(long p)
Gets a
int at memory address p by reading byte for byte, instead of the whole value
in one go. |
static int |
getIntVolatile(long address) |
static int |
getIntVolatile(Object obj,
long offset) |
static long |
getLong(long address) |
static long |
getLong(Object obj,
long offset) |
static long |
getLongByteWiseLittleEndian(long p)
Gets a
long at memory address p by reading byte for byte, instead of the whole value
in one go. |
static long |
getLongVolatile(long address) |
static long |
getLongVolatile(Object obj,
long offset) |
static Object |
getObject(Object obj,
long offset) |
static Object |
getObjectVolatile(Object obj,
long offset) |
static short |
getShort(long address) |
static short |
getShort(Object obj,
long offset) |
static short |
getShortByteWiseLittleEndian(long p)
Gets a
short at memory address p by reading byte for byte, instead of the whole value
in one go. |
static short |
getShortVolatile(long address) |
static short |
getShortVolatile(Object obj,
long offset) |
static void |
initDirectByteBuffer(Object dbb,
long addr,
int cap)
Initialize (simulate calling the constructor of) the given DirectByteBuffer.
|
static void |
loadFence()
Orders loads before the fence, with loads and stores after the fence.
|
static ByteBuffer |
newDirectByteBuffer(long addr,
int cap)
Create a new DirectByteBuffer that wraps the given address and has the given capacity.
|
static String |
newSharedArrayString(char[] chars)
Create a string with a char[] that you know is not going to be modified, so avoid the copy constructor.
|
static int |
pageSize()
Return the power-of-2 native memory page size.
|
static void |
putBoolean(Object obj,
long offset,
boolean value) |
static void |
putBooleanVolatile(Object obj,
long offset,
boolean value) |
static void |
putByte(long address,
byte value) |
static void |
putByte(Object obj,
long offset,
byte value) |
static void |
putByteVolatile(long address,
byte value) |
static void |
putByteVolatile(Object obj,
long offset,
byte value) |
static void |
putChar(long address,
char value) |
static void |
putChar(Object obj,
long offset,
char value) |
static void |
putCharVolatile(long address,
char value) |
static void |
putCharVolatile(Object obj,
long offset,
char value) |
static void |
putDouble(long address,
double value) |
static void |
putDouble(Object obj,
long offset,
double value) |
static void |
putDoubleVolatile(long address,
double value) |
static void |
putDoubleVolatile(Object obj,
long offset,
double value) |
static void |
putFloat(long address,
float value) |
static void |
putFloat(Object obj,
long offset,
float value) |
static void |
putFloatVolatile(long address,
float value) |
static void |
putFloatVolatile(Object obj,
long offset,
float value) |
static void |
putInt(long address,
int value) |
static void |
putInt(Object obj,
long offset,
int value) |
static void |
putIntByteWiseLittleEndian(long p,
int value)
Puts a
int at memory address p by writing byte for byte, instead of the whole value
in one go. |
static void |
putIntVolatile(long address,
int value) |
static void |
putIntVolatile(Object obj,
long offset,
int value) |
static void |
putLong(long address,
long value) |
static void |
putLong(Object obj,
long offset,
long value) |
static void |
putLongByteWiseLittleEndian(long p,
long value)
Puts a
long at memory address p by writing byte for byte, instead of the whole value
in one go. |
static void |
putLongVolatile(long address,
long value) |
static void |
putLongVolatile(Object obj,
long offset,
long value) |
static void |
putObject(Object obj,
long offset,
Object value) |
static void |
putObjectVolatile(Object obj,
long offset,
Object value) |
static void |
putShort(long address,
short value) |
static void |
putShort(Object obj,
long offset,
short value) |
static void |
putShortByteWiseLittleEndian(long p,
short value)
Puts a
short at memory address p by writing byte for byte, instead of the whole value
in one go. |
static void |
putShortVolatile(long address,
short value) |
static void |
putShortVolatile(Object obj,
long offset,
short value) |
static void |
setMemory(long address,
long bytes,
byte value)
Set the given number of bytes to the given value, starting from the given address.
|
static void |
storeFence()
Orders stores before the fence, with loads and stores after the fence.
|
public static final boolean allowUnalignedMemoryAccess
public static final boolean storeByteOrderIsNative
public static void assertHasUnsafe()
LinkageError - if the Unsafe tools are not available on in this JVM.public static long getFieldOffset(Class<?> type, String field)
public static int getAndAddInt(Object obj, long offset, int delta)
This has the memory visibility semantics of a volatile read followed by a volatile write.
public static long getAndAddLong(Object obj, long offset, long delta)
This has the memory visibility semantics of a volatile read followed by a volatile write.
public static void loadFence()
public static void storeFence()
public static boolean compareAndSwapLong(Object obj, long offset, long expected, long update)
If this method returns true, then it has the memory visibility semantics of a volatile read followed by a volatile write.
public static boolean compareAndSwapObject(Object obj, long offset, Object expected, Object update)
public static Object getAndSetObject(Object obj, long offset, Object newValue)
public static String newSharedArrayString(char[] chars)
chars - array that will back the new stringpublic static long allocateMemory(long sizeInBytes)
The memory is aligned such that it can be used for any data type. The memory is cleared, so all bytes are zero.
public static long alignedMemory(long pointer,
int alignBy)
pointer.
The returned pointer as aligned with alignBy such that pointer % alignBy == 0.
The given pointer should be allocated with at least the requested size + alignBy - 1,
where the additional bytes will serve as padding for the worst case where the start of the usable
area of the allocated memory will need to be shifted at most alignBy - 1 bytes to the right.
0 4 8 12 16 20 ; 4-byte alignments
|---|---|---|---|---| ; memory
--------=== ; allocated memory (-required, =padding)
^------^ ; used memory
pointer - pointer to allocated memory from allocateMemory(long).alignBy - power-of-two size to align to, e.g. 4 or 8.alignBy.public static void free(long pointer)
allocateMemory(long).public static int pageSize()
public static void putBoolean(Object obj, long offset, boolean value)
public static boolean getBoolean(Object obj, long offset)
public static void putBooleanVolatile(Object obj, long offset, boolean value)
public static boolean getBooleanVolatile(Object obj, long offset)
public static void putByte(long address,
byte value)
public static byte getByte(long address)
public static void putByteVolatile(long address,
byte value)
public static byte getByteVolatile(long address)
public static void putByte(Object obj, long offset, byte value)
public static byte getByte(Object obj, long offset)
public static byte getByteVolatile(Object obj, long offset)
public static void putByteVolatile(Object obj, long offset, byte value)
public static void putShort(long address,
short value)
public static short getShort(long address)
public static void putShortVolatile(long address,
short value)
public static short getShortVolatile(long address)
public static void putShort(Object obj, long offset, short value)
public static short getShort(Object obj, long offset)
public static void putShortVolatile(Object obj, long offset, short value)
public static short getShortVolatile(Object obj, long offset)
public static void putFloat(long address,
float value)
public static float getFloat(long address)
public static void putFloatVolatile(long address,
float value)
public static float getFloatVolatile(long address)
public static void putFloat(Object obj, long offset, float value)
public static float getFloat(Object obj, long offset)
public static void putFloatVolatile(Object obj, long offset, float value)
public static float getFloatVolatile(Object obj, long offset)
public static void putChar(long address,
char value)
public static char getChar(long address)
public static void putCharVolatile(long address,
char value)
public static char getCharVolatile(long address)
public static void putChar(Object obj, long offset, char value)
public static char getChar(Object obj, long offset)
public static void putCharVolatile(Object obj, long offset, char value)
public static char getCharVolatile(Object obj, long offset)
public static void putInt(long address,
int value)
public static int getInt(long address)
public static void putIntVolatile(long address,
int value)
public static int getIntVolatile(long address)
public static void putInt(Object obj, long offset, int value)
public static int getInt(Object obj, long offset)
public static void putIntVolatile(Object obj, long offset, int value)
public static int getIntVolatile(Object obj, long offset)
public static void putLongVolatile(long address,
long value)
public static long getLongVolatile(long address)
public static void putLong(long address,
long value)
public static long getLong(long address)
public static void putLong(Object obj, long offset, long value)
public static long getLong(Object obj, long offset)
public static void putLongVolatile(Object obj, long offset, long value)
public static long getLongVolatile(Object obj, long offset)
public static void putDouble(long address,
double value)
public static double getDouble(long address)
public static void putDoubleVolatile(long address,
double value)
public static double getDoubleVolatile(long address)
public static void putDouble(Object obj, long offset, double value)
public static double getDouble(Object obj, long offset)
public static void putDoubleVolatile(Object obj, long offset, double value)
public static double getDoubleVolatile(Object obj, long offset)
public static int arrayBaseOffset(Class klass)
public static int arrayIndexScale(Class klass)
public static int arrayOffset(int index,
int base,
int scale)
public static void setMemory(long address,
long bytes,
byte value)
public static void copyMemory(long srcAddress,
long destAddress,
long bytes)
public static ByteBuffer newDirectByteBuffer(long addr, int cap) throws Exception
The ByteBuffer does NOT create a Cleaner, or otherwise register the pointer for freeing.
Exceptionpublic static void initDirectByteBuffer(Object dbb, long addr, int cap)
public static boolean exchangeNativeAccessCheckEnabled(boolean newSetting)
This is only useful for speeding up tests when you have a lot of them, and they access native memory a lot. This does not disable the recording of memory allocations or frees.
Remember to restore the old value so other tests in the same JVM get the benefit of native access checks.
The changing of this setting is completely unsynchronised, so you have to order this modification before and after the tests that you want to run without native access checks.
newSetting - The new setting.public static short getShortByteWiseLittleEndian(long p)
short at memory address p by reading byte for byte, instead of the whole value
in one go. This can be useful, even necessary in some scenarios where allowUnalignedMemoryAccess
is false and p isn't aligned properly. Values read with this method should have been
previously put using putShortByteWiseLittleEndian(long, short).p - address pointer to start reading at.public static int getIntByteWiseLittleEndian(long p)
int at memory address p by reading byte for byte, instead of the whole value
in one go. This can be useful, even necessary in some scenarios where allowUnalignedMemoryAccess
is false and p isn't aligned properly. Values read with this method should have been
previously put using putIntByteWiseLittleEndian(long, int).p - address pointer to start reading at.public static long getLongByteWiseLittleEndian(long p)
long at memory address p by reading byte for byte, instead of the whole value
in one go. This can be useful, even necessary in some scenarios where allowUnalignedMemoryAccess
is false and p isn't aligned properly. Values read with this method should have been
previously put using putLongByteWiseLittleEndian(long, long).p - address pointer to start reading at.public static void putShortByteWiseLittleEndian(long p,
short value)
short at memory address p by writing byte for byte, instead of the whole value
in one go. This can be useful, even necessary in some scenarios where allowUnalignedMemoryAccess
is false and p isn't aligned properly. Values written with this method should be
read using getShortByteWiseLittleEndian(long).p - address pointer to start writing at.value - value to write byte for byte.public static void putIntByteWiseLittleEndian(long p,
int value)
int at memory address p by writing byte for byte, instead of the whole value
in one go. This can be useful, even necessary in some scenarios where allowUnalignedMemoryAccess
is false and p isn't aligned properly. Values written with this method should be
read using getIntByteWiseLittleEndian(long).p - address pointer to start writing at.value - value to write byte for byte.public static void putLongByteWiseLittleEndian(long p,
long value)
long at memory address p by writing byte for byte, instead of the whole value
in one go. This can be useful, even necessary in some scenarios where allowUnalignedMemoryAccess
is false and p isn't aligned properly. Values written with this method should be
read using getShortByteWiseLittleEndian(long).p - address pointer to start writing at.value - value to write byte for byte.Copyright © 2002–2017 The Neo4j Graph Database Project. All rights reserved.