com.alexkasko.unsafe.offheap
public abstract class OffHeapMemory extends Object
Allocates an area of off-heap memory that is not a subject to GC.
Default implementation uses sun.misc.Unsafe
, with all operations guarded with assert
boundary checks.
With assert enabled in runtime (-ea
java switch) AssertionError
will be thrown on illegal input. Without assertions illegal input will crash JVM.
Fallback implementation uses ByteBuffer.allocateDirect(int)
that may (OpenJDK)
or may not (Dalvik) use sun.misc.Unsafe
under the hood.
Note: only unsafe implementation (allocateMemoryUnsafe(long)
) may use memory areas longer than Integer.MAX_VALUE
Unsafe implementation uses native endianness (LE on most platforms). Fallback implementation uses Little Endian byte order.
Provides methods to access allocated memory. More complex data structures (off-heap quasi-objects) may be
constructed wrapping this class, see OffHeapLongArray
.
Memory may be freed manually calling free()
(thread-safe and may be called safely more than one time),
or it will be freed when OffHeapMemory
instance itself will be garbage collected.
Constructor and Description |
---|
OffHeapMemory() |
Modifier and Type | Method and Description |
---|---|
static OffHeapMemory |
allocateMemory(long bytes)
Allocates memory using
sun.misc.Unsafe if it's proper implementation is available. |
static OffHeapMemory |
allocateMemoryDirect(long bytes)
Allocates memory using
ByteBuffer.allocateDirect(int) |
static OffHeapMemory |
allocateMemoryOnHeap(ByteArrayTool bt,
long bytes)
Allocates memory using byte array
|
static OffHeapMemory |
allocateMemoryUnsafe(long bytes)
Allocates memory using
sun.misc.Unsafe |
abstract OffHeapMemory |
clone()
Creates new instance of
OffHeapMemory
and copies there current instance contents |
abstract void |
copy(long offset,
OffHeapMemory destination,
long destOffset,
long bytes)
Copies memory from this instance's area into another instance's area
|
abstract void |
free()
Frees allocated memory, may be called multiple times from any thread
|
abstract void |
get(long offset,
byte[] buffer)
Copies part of memory area into byte array
|
abstract void |
get(long offset,
byte[] buffer,
int bufferOffset,
int bytes)
Copies part of memory area into byte array
|
abstract byte |
getByte(long offset)
Gets byte from memory area
|
abstract int |
getInt(long offset)
Gets four bytes from memory area as int
|
abstract long |
getLong(long offset)
Gets eight bytes from memory area as long
|
abstract short |
getShort(long offset)
Gets two bytes from memory area as short
|
abstract short |
getUnsignedByte(long offset)
Gets one byte (stored as one signed byte), converts it to unsigned
and returns it as short
|
abstract long |
getUnsignedInt(long offset)
Gets unsigned int (stored as 4 bytes) and returns it as long
|
abstract int |
getUnsignedShort(long offset)
Gets unsigned short from memory area (stored as two bytes) and returns it as int
|
abstract boolean |
isUnsafe()
Whether current implementation uses
sun.misc.Unsafe |
abstract long |
length()
Returns length of allocated memory area
|
abstract void |
put(long offset,
byte[] buffer)
Copies byte array contents into memory area
|
abstract void |
put(long offset,
byte[] buffer,
int bufferOffset,
int bytes)
Copies byte array contents into memory area
|
abstract void |
putByte(long offset,
byte value)
Puts byte into memory area
|
abstract void |
putInt(long offset,
int value)
Puts int into memory area as four bytes
|
abstract void |
putLong(long offset,
long value)
Puts long into memory area as eight bytes
|
abstract void |
putShort(long offset,
short value)
Puts short into memory area as two bytes
|
abstract void |
putUnsignedByte(long offset,
short value)
Puts short with value from 0 to 255 inclusive into memory area as one
signed byte
|
abstract void |
putUnsignedInt(long offset,
long value)
Puts long value from 0 to 4294967295 inclusive into memory area as four bytes
|
abstract void |
putUnsignedShort(long offset,
int value)
Puts int with value from 0 to 65535 inclusive into memory are as two bytes
|
public OffHeapMemory()
public static OffHeapMemory allocateMemory(long bytes)
sun.misc.Unsafe
if it's proper implementation is available.
Allocates using ByteBuffer.allocateDirect(int)
otherwise.bytes
- amount of memory to allocateOffHeapMemory
instancepublic static OffHeapMemory allocateMemoryUnsafe(long bytes) throws Exception
sun.misc.Unsafe
bytes
- amount of memory to allocateOffHeapMemory
instanceException
- if proper sun.misc.Unsafe
not availablepublic static OffHeapMemory allocateMemoryDirect(long bytes)
ByteBuffer.allocateDirect(int)
bytes
- amount of memory to allocateOffHeapMemory
instancepublic static OffHeapMemory allocateMemoryOnHeap(ByteArrayTool bt, long bytes)
bytes
- amount of memory to allocatebt
- byte array tool to use for on-heap memory managementOffHeapMemory
instancepublic abstract boolean isUnsafe()
sun.misc.Unsafe
public abstract long length()
public abstract void free()
public abstract void put(long offset, byte[] buffer, int bufferOffset, int bytes)
offset
- memory area offsetbuffer
- source byte arraybufferOffset
- position in byte arraybytes
- bytes length to copypublic abstract void put(long offset, byte[] buffer)
offset
- memory area offsetbuffer
- source byte arraypublic abstract void get(long offset, byte[] buffer, int bufferOffset, int bytes)
offset
- memory area offsetbuffer
- destination byte arraybufferOffset
- position in byte arraybytes
- bytes length to copypublic abstract void get(long offset, byte[] buffer)
offset
- memory area offsetbuffer
- destination byte arraypublic abstract byte getByte(long offset)
offset
- memory area offsetpublic abstract void putByte(long offset, byte value)
offset
- memory area offsetvalue
- byte valuepublic abstract short getUnsignedByte(long offset)
offset
- memory area offsetpublic abstract void putUnsignedByte(long offset, short value)
offset
- memory area offsetvalue
- unsigned byte as shortpublic abstract short getShort(long offset)
offset
- memory area offsetpublic abstract void putShort(long offset, short value)
offset
- memory area offsetvalue
- short valuepublic abstract int getUnsignedShort(long offset)
offset
- memory area offsetpublic abstract void putUnsignedShort(long offset, int value)
offset
- memory area offsetvalue
- unsigned short as intpublic abstract int getInt(long offset)
offset
- memory area offsetpublic abstract void putInt(long offset, int value)
offset
- memory area offsetvalue
- int valuepublic abstract long getUnsignedInt(long offset)
offset
- memory area offsetpublic abstract void putUnsignedInt(long offset, long value)
offset
- memory area offsetvalue
- unsigned int as longpublic abstract long getLong(long offset)
offset
- memory area offsetpublic abstract void putLong(long offset, long value)
offset
- memory area offsetvalue
- long valuepublic abstract void copy(long offset, OffHeapMemory destination, long destOffset, long bytes)
offset
- this memory area offsetdestination
- destination memory areadestOffset
- destination memory area offsetbytes
- memory length in bytes to copypublic abstract OffHeapMemory clone()
OffHeapMemory
and copies there current instance contentsclone
in class Object
OffHeapMemory
Copyright © 2014. All Rights Reserved.