org.apache.ignite.internal.util.offheap.unsafe

## Class GridUnsafeMemory

- java.lang.Object
- - org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory

    public class GridUnsafeMemory
    extends Object

Unsafe memory.

### Field Summary

Fields
`static long` `BYTE_ARR_OFF` Byte array offset.
`static sun.misc.Unsafe` `UNSAFE` Unsafe handle.

### Constructor Summary

Constructors
Constructor and Description
`GridUnsafeMemory(long total)`

### Method Summary

Methods
`long` `allocate(long size)` Allocates memory of given size in bytes.
`long` `allocate(long size, boolean init)` Allocates memory of given size in bytes.
`long` `allocate(long size, boolean init, boolean reserved)` Allocates memory of given size in bytes.
`long` `allocatedSize()`
`long` `allocateSystem(long size, boolean init)` Allocates memory of given size in bytes, adds to system memory counter.
`boolean` `casInt(long ptr, int exp, int v)`
`boolean` `casLong(long ptr, long exp, long v)`
`static boolean` `compare(long ptr, byte[] bytes)` Compares memory.
`static boolean` `compare(long ptr1, long ptr2, int size)`
`void` `copyMemory(long srcPtr, long destPtr, long len)` Copy memory.
`void` `fill(long ptr, long size, byte b)`
`long` `freeSize()`
`IgniteBiTuple<byte[],Byte>` `get(long ptr)` Get value stored in offheap along with a value type.
`void` `listen(GridOffHeapEventListener lsnr)` Sets event listener.
`long` `putOffHeap(long ptr, byte[] val, byte type)` Stores value to the specified memory location.
`byte` `readByte(long ptr)`
`byte[]` `readBytes(long ptr, byte[] arr)`
`byte[]` `readBytes(long ptr, byte[] arr, int off, int len)`
`byte[]` `readBytes(long ptr, int cnt)`
`byte` `readByteVolatile(long ptr)`
`double` `readDouble(long ptr)`
`float` `readFloat(long ptr)`
`int` `readInt(long ptr)`
`int` `readIntVolatile(long ptr)`
`long` `readLong(long ptr)`
`long` `readLongVolatile(long ptr)`
`short` `readShort(long ptr)`
`void` `release(long ptr, long size)` Releases memory at the given address.
`void` `releaseSystem(long ptr, long size)` Releases memory allocated by `allocateSystem(long, boolean)`.
`void` `removeOffHeap(long ptr)` Releases off-heap memory allocated by `putOffHeap(long, byte[], byte)` method.
`boolean` `reserve(long size)` Reserves memory.
`long` `systemAllocatedSize()`
`String` `toString()`
`long` `totalSize()`
`boolean` `unlimited()` Checks if direct memory allocation is limited to some value.
`void` `writeByte(long ptr, byte v)`
`void` `writeBytes(long ptr, byte[] arr)` Writes byte array into memory location.
`void` `writeBytes(long ptr, byte[] arr, int off, int len)` Writes part of byte array into memory location.
`void` `writeByteVolatile(long ptr, byte v)`
`void` `writeDouble(long ptr, double v)`
`void` `writeFloat(long ptr, float v)`
`void` `writeInt(long ptr, int v)`
`void` `writeIntVolatile(long ptr, int v)`
`void` `writeLong(long ptr, long v)`
`void` `writeLongVolatile(long ptr, long v)`
`void` `writeShort(long ptr, short v)`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Field Detail

#### UNSAFE

    public static final sun.misc.Unsafe UNSAFE

Unsafe handle.

#### BYTE_ARR_OFF

    public static final long BYTE_ARR_OFF

Byte array offset.

### 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)
                  throws GridOffHeapOutOfMemoryException

Allocates 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 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.

#### removeOffHeap

    public void removeOffHeap(long ptr)

Releases off-heap memory allocated by `putOffHeap(long, byte[], byte)` method.

Parameters:
  `ptr` - Optional pointer returned by `putOffHeap(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:
  `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.

#### 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.

#### systemAllocatedSize

    public long systemAllocatedSize()

Returns:
  Size of memory allocated with `allocateSystem(long, boolean)`.

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`

2015 Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Fabric | : ver. 1.0.0
Release Date | : March 31 2015