Class GridUnsafeMemory


  • public class GridUnsafeMemory
    extends Object
    Unsafe memory.
    • Constructor Detail

      • GridUnsafeMemory

        public GridUnsafeMemory​(long total)
        Parameters:
        total - Total size, 0 for unlimited.
    • 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:
        True if memory is under allowed size, false otherwise.
      • allocate

        public long allocate​(long size,
                             boolean init)
                      throws GridOffHeapOutOfMemoryException
        Allocates 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 GridOffHeapOutOfMemoryException
        Allocates 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 GridOffHeapOutOfMemoryException
        Allocates 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 by allocateSystem(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:
        true If 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:
        true If 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 is 0, 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.
      • 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:
        True if equals.
      • compare

        public static boolean compare​(long ptr,
                                      byte[] bytes)
        Compares memory.
        Parameters:
        ptr - Pointer.
        bytes - Bytes to compare.
        Returns:
        True if 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:
        True if 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:
        True if 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.