Class GridUnsafeMemory
- java.lang.Object
-
- org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory
-
public class GridUnsafeMemory extends Object
Unsafe memory.
-
-
Constructor Summary
Constructors Constructor Description GridUnsafeMemory(long total)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description longallocate(long size)Allocates memory of given size in bytes.longallocate(long size, boolean init)Allocates memory of given size in bytes.longallocate(long size, boolean init, boolean reserved)Allocates memory of given size in bytes.longallocatedSize()longallocateSystem(long size, boolean init)Allocates memory of given size in bytes, adds to system memory counter.booleancasInt(long ptr, int exp, int v)booleancasLong(long ptr, long exp, long v)static booleancompare(long ptr, byte[] bytes)Compares memory.static booleancompare(long ptr, byte[] bytes, int bytesOff, int len)Compares memory.static booleancompare(long ptr1, long ptr2, int size)voidcopyMemory(long srcPtr, long destPtr, long len)Copy memory.voidfill(long ptr, long size, byte b)longfreeSize()IgniteBiTuple<byte[],Byte>get(long ptr)Get value stored in offheap along with a value type.voidlisten(GridOffHeapEventListener lsnr)Sets event listener.longputOffHeap(long ptr, byte[] val, byte type)Stores value to the specified memory location.bytereadByte(long ptr)byte[]readBytes(long ptr, byte[] arr)byte[]readBytes(long ptr, byte[] arr, int off, int len)byte[]readBytes(long ptr, int cnt)bytereadByteVolatile(long ptr)doublereadDouble(long ptr)floatreadFloat(long ptr)intreadInt(long ptr)intreadIntVolatile(long ptr)longreadLong(long ptr)longreadLongVolatile(long ptr)shortreadShort(long ptr)voidrelease(long ptr, long size)Releases memory at the given address.voidreleaseSystem(long ptr, long size)Releases memory allocated byallocateSystem(long, boolean).voidremoveOffHeap(long ptr)Releases off-heap memory allocated byputOffHeap(long, byte[], byte)method.booleanreserve(long size)Reserves memory.longsystemAllocatedSize()StringtoString()longtotalSize()booleanunlimited()Checks if direct memory allocation is limited to some value.voidwriteByte(long ptr, byte v)voidwriteBytes(long ptr, byte[] arr)Writes byte array into memory location.voidwriteBytes(long ptr, byte[] arr, int off, int len)Writes part of byte array into memory location.voidwriteByteVolatile(long ptr, byte v)voidwriteDouble(long ptr, double v)voidwriteFloat(long ptr, float v)voidwriteInt(long ptr, int v)voidwriteIntVolatile(long ptr, int v)voidwriteLong(long ptr, long v)voidwriteLongVolatile(long ptr, long v)voidwriteShort(long ptr, short v)
-
-
-
Method Detail
-
listen
public void listen(GridOffHeapEventListener lsnr)
Sets event listener.- Parameters:
lsnr- Event listener.
-
reserve
public boolean reserve(long size)
Reserves memory.- Parameters:
size- Size to reserve.- Returns:
Trueif memory is under allowed size,falseotherwise.
-
allocate
public long allocate(long size) throws GridOffHeapOutOfMemoryExceptionAllocates memory of given size in bytes.- Parameters:
size- Size of allocated block.- Returns:
- Allocated block address.
- Throws:
GridOffHeapOutOfMemoryException- If Memory could not be allocated.
-
allocate
public long allocate(long size, boolean init) throws GridOffHeapOutOfMemoryExceptionAllocates memory of given size in bytes.- Parameters:
size- Size of allocated block.init- Flag to zero-out the initialized memory or not.- Returns:
- Allocated block address.
- Throws:
GridOffHeapOutOfMemoryException- If Memory could not be allocated.
-
allocate
public long allocate(long size, boolean init, boolean reserved) throws GridOffHeapOutOfMemoryExceptionAllocates memory of given size in bytes.- Parameters:
size- Size of allocated block.init- Flag to zero-out the initialized memory or not.reserved- Flag indicating that memory being allocated was reserved before.- Returns:
- Allocated block address.
- Throws:
GridOffHeapOutOfMemoryException- If memory could not be allocated.
-
allocateSystem
public long allocateSystem(long size, boolean init) throws GridOffHeapOutOfMemoryExceptionAllocates memory of given size in bytes, adds to system memory counter.- Parameters:
size- Size of allocated block.init- Whether or not allocated block is zeroed upon return.- Returns:
- Allocated block address.
- Throws:
GridOffHeapOutOfMemoryException- If memory could not be allocated.
-
fill
public void fill(long ptr, long size, byte b)- Parameters:
ptr- Pointer.size- Count of long values to fill.b- Value.
-
release
public void release(long ptr, long size)Releases memory at the given address.- Parameters:
ptr- Pointer to memory.size- Memory region size.
-
releaseSystem
public void releaseSystem(long ptr, long size)Releases memory allocated byallocateSystem(long, boolean).- Parameters:
ptr- Address of memory block to deallocate.size- Size of allocated block.
-
readLong
public long readLong(long ptr)
- Parameters:
ptr- Pointer.- Returns:
- Long value.
-
writeLong
public void writeLong(long ptr, long v)- Parameters:
ptr- Pointer.v- Long value.
-
readLongVolatile
public long readLongVolatile(long ptr)
- Parameters:
ptr- Pointer.- Returns:
- Long value.
-
writeLongVolatile
public void writeLongVolatile(long ptr, long v)- Parameters:
ptr- Pointer.v- Long value.
-
casLong
public boolean casLong(long ptr, long exp, long v)- Parameters:
ptr- Pointer.exp- Expected.v- New value.- Returns:
trueIf operation succeeded.
-
readInt
public int readInt(long ptr)
- Parameters:
ptr- Pointer.- Returns:
- Integer value.
-
writeInt
public void writeInt(long ptr, int v)- Parameters:
ptr- Pointer.v- Integer value.
-
readIntVolatile
public int readIntVolatile(long ptr)
- Parameters:
ptr- Pointer.- Returns:
- Integer value.
-
writeIntVolatile
public void writeIntVolatile(long ptr, int v)- Parameters:
ptr- Pointer.v- Integer value.
-
casInt
public boolean casInt(long ptr, int exp, int v)- Parameters:
ptr- Pointer.exp- Expected.v- New value.- Returns:
trueIf operation succeeded.
-
readFloat
public float readFloat(long ptr)
- Parameters:
ptr- Pointer.- Returns:
- Float value.
-
writeFloat
public void writeFloat(long ptr, float v)- Parameters:
ptr- Pointer.v- Value.
-
readDouble
public double readDouble(long ptr)
- Parameters:
ptr- Pointer.- Returns:
- Double value.
-
writeDouble
public void writeDouble(long ptr, double v)- Parameters:
ptr- Pointer.v- Value.
-
readShort
public short readShort(long ptr)
- Parameters:
ptr- Pointer.- Returns:
- Short value.
-
writeShort
public void writeShort(long ptr, short v)- Parameters:
ptr- Pointer.v- Short value.
-
readByte
public byte readByte(long ptr)
- Parameters:
ptr- Pointer.- Returns:
- Integer value.
-
readByteVolatile
public byte readByteVolatile(long ptr)
- Parameters:
ptr- Pointer.- Returns:
- Integer value.
-
writeByte
public void writeByte(long ptr, byte v)- Parameters:
ptr- Pointer.v- Integer value.
-
writeByteVolatile
public void writeByteVolatile(long ptr, byte v)- Parameters:
ptr- Pointer.v- Integer value.
-
putOffHeap
public long putOffHeap(long ptr, byte[] val, byte type)Stores value to the specified memory location. If specified pointer is0, then will allocate required space. If size of allocated space is not enough to hold given values, will reallocate memory.- Parameters:
ptr- Optional pointer to allocated memory. First 4 bytes in allocated region must contain size of allocated chunk.val- Value to store.type- Value type.- Returns:
- Pointer.
-
removeOffHeap
public void removeOffHeap(long ptr)
Releases off-heap memory allocated byputOffHeap(long, byte[], byte)method.- Parameters:
ptr- Optional pointer returned byputOffHeap(long, byte[], byte).
-
get
public IgniteBiTuple<byte[],Byte> get(long ptr)
Get value stored in offheap along with a value type.- Parameters:
ptr- Pointer to read.- Returns:
- Stored byte array and "raw bytes" flag.
-
compare
public static boolean compare(long ptr1, long ptr2, int size)- Parameters:
ptr1- First pointer.ptr2- Second pointer.size- Memory size.- Returns:
Trueif equals.
-
compare
public static boolean compare(long ptr, byte[] bytes)Compares memory.- Parameters:
ptr- Pointer.bytes- Bytes to compare.- Returns:
Trueif equals.
-
compare
public static boolean compare(long ptr, byte[] bytes, int bytesOff, int len)Compares memory.- Parameters:
ptr- Pointer.bytes- Bytes to compare.bytesOff- Offset in the bytes array.len- Count of compared bytes.- Returns:
Trueif equals.
-
readBytes
public byte[] readBytes(long ptr, int cnt)- Parameters:
ptr- Pointer.cnt- Count.- Returns:
- Byte array.
-
readBytes
public byte[] readBytes(long ptr, byte[] arr)- Parameters:
ptr- Pointer.arr- Array.- Returns:
- The same array as passed in one.
-
readBytes
public byte[] readBytes(long ptr, byte[] arr, int off, int len)- Parameters:
ptr- Pointer.arr- Array.off- Offset.len- Length.- Returns:
- The same array as passed in one.
-
writeBytes
public void writeBytes(long ptr, byte[] arr)Writes byte array into memory location.- Parameters:
ptr- Pointer.arr- Array.
-
writeBytes
public void writeBytes(long ptr, byte[] arr, int off, int len)Writes part of byte array into memory location.- Parameters:
ptr- Pointer.arr- Array.off- Offset.len- Length.
-
copyMemory
public void copyMemory(long srcPtr, long destPtr, long len)Copy memory.- Parameters:
srcPtr- Source pointer.destPtr- Destination pointer.len- Length in bytes.
-
unlimited
public boolean unlimited()
Checks if direct memory allocation is limited to some value.- Returns:
Trueif memory allocation is limited.
-
totalSize
public long totalSize()
- Returns:
- Total size.
-
freeSize
public long freeSize()
- Returns:
- Free size.
-
allocatedSize
public long allocatedSize()
- Returns:
- Allocated size.
-
systemAllocatedSize
public long systemAllocatedSize()
- Returns:
- Size of memory allocated with
allocateSystem(long, boolean).
-
-