Class Unsafe
@Inclubating public class Unsafe extends Object
- Author:
- Ardika Rommy Sanjaya
-
Field Summary
Fields Modifier and Type Field Description static boolean
HAS_UNSAFE
-
Constructor Summary
Constructors Constructor Description Unsafe()
-
Method Summary
Modifier and Type Method Description long
allocateMemory(long bytes)
int
arrayBaseOffset(Class<?> arrayClass)
Method
bufferCleaner()
Get direct ByteBuffer cleaner.void
copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes)
Sets all bytes in a given block of memory to a copy of another block.void
freeMemory(long address)
Disposes of a block of native memory, as obtained fromallocateMemory(long)
orreallocateMemory(long, long)
.byte
getByte(long address)
Fetches a value from a given memory address.char
getChar(long address)
double
getDouble(long address)
float
getFloat(long address)
int
getInt(long address)
long
getLong(long address)
short
getShort(long address)
void
putByte(long address, byte x)
Stores a value into a given memory address.void
putChar(long address, char x)
void
putDouble(long address, double x)
void
putFloat(long address, float x)
void
putInt(long address, int x)
void
putLong(long address, long x)
void
putShort(long address, short x)
long
reallocateMemory(long address, long bytes)
Resizes a new block of native memory, to the given size in bytes.
-
Field Details
-
HAS_UNSAFE
public static final boolean HAS_UNSAFE
-
-
Constructor Details
-
Unsafe
public Unsafe()
-
-
Method Details
-
arrayBaseOffset
-
allocateMemory
public long allocateMemory(long bytes) -
reallocateMemory
public long reallocateMemory(long address, long bytes)Resizes a new block of native memory, to the given size in bytes. The contents of the new block past the size of the old block are uninitialized; they will generally be garbage. The resulting native pointer will be zero if and only if the requested size is zero. The resulting native pointer will be aligned for all value types. Dispose of this memory by callingfreeMemory(long)
, or resize it withreallocateMemory(long, long)
. The address passed to this method may be null, in which case an allocation will be performed.Note: It is the resposibility of the caller to make sure arguments are checked before the methods are called. While some rudimentary checks are performed on the input, the checks are best effort and when performance is an overriding priority, as when methods of this class are optimized by the runtime compiler, some or all checks (if any) may be elided. Hence, the caller must not rely on the checks and corresponding exceptions!
- Parameters:
address
- memory address.bytes
- size of new memory block.- Returns:
- returns memory address.
- Throws:
RuntimeException
- if the size is negative or too large for the native size_t typeOutOfMemoryError
- if the allocation is refused by the system- See Also:
allocateMemory(long)
-
freeMemory
public void freeMemory(long address)Disposes of a block of native memory, as obtained fromallocateMemory(long)
orreallocateMemory(long, long)
. The address passed to this method may be null, in which case no action is taken.Note: It is the resposibility of the caller to make sure arguments are checked before the methods are called. While some rudimentary checks are performed on the input, the checks are best effort and when performance is an overriding priority, as when methods of this class are optimized by the runtime compiler, some or all checks (if any) may be elided. Hence, the caller must not rely on the checks and corresponding exceptions!
- Parameters:
address
- memory address.- Throws:
RuntimeException
- if any of the arguments is invalid- See Also:
allocateMemory(long)
-
copyMemory
public void copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes)Sets all bytes in a given block of memory to a copy of another block.- Parameters:
srcBase
- src base.srcOffset
- src offset.destBase
- dst base.destOffset
- dst offset.bytes
- size.
-
getByte
public byte getByte(long address)Fetches a value from a given memory address. If the address is zero, or does not point into a block obtained fromallocateMemory(long)
, the results are undefined.- Parameters:
address
- memory address.- See Also:
allocateMemory(long)
-
putByte
public void putByte(long address, byte x)Stores a value into a given memory address. If the address is zero, or does not point into a block obtained fromallocateMemory(long)
, the results are undefined.- Parameters:
address
- memory address.x
- value.- See Also:
getByte(long)
-
getShort
public short getShort(long address)- Parameters:
address
- memory address.- See Also:
getByte(long)
-
putShort
public void putShort(long address, short x)- Parameters:
address
- memory address.x
- value.- See Also:
putByte(long, byte)
-
getChar
public char getChar(long address)- Parameters:
address
- memory address.- See Also:
getByte(long)
-
putChar
public void putChar(long address, char x)- Parameters:
address
- memory address.x
- value.- See Also:
putByte(long, byte)
-
getInt
public int getInt(long address)- Parameters:
address
- memory address.- See Also:
getByte(long)
-
putInt
public void putInt(long address, int x)- Parameters:
address
- memory address.x
- value.- See Also:
putByte(long, byte)
-
getLong
public long getLong(long address)- Parameters:
address
- memory address.- See Also:
getByte(long)
-
putLong
public void putLong(long address, long x)- Parameters:
address
- memory address.x
- value.- See Also:
putByte(long, byte)
-
getFloat
public float getFloat(long address)- Parameters:
address
- memory address.- See Also:
getByte(long)
-
putFloat
public void putFloat(long address, float x)- Parameters:
address
- memory address.x
- value.- See Also:
putByte(long, byte)
-
getDouble
public double getDouble(long address)- Parameters:
address
- memory address.- See Also:
getByte(long)
-
putDouble
public void putDouble(long address, double x)- Parameters:
address
- memory address.x
- value.- See Also:
putByte(long, byte)
-
bufferCleaner
Get direct ByteBuffer cleaner.- Returns:
- cleaner method.
-