com.alexkasko.unsafe.offheapstruct
public class OffHeapStructArray extends Object implements OffHeapStructCollection, OffHeapDisposable
Implementation of off-heap array of structs (memory areas of equal sizes).
Default implementation uses sun.misc.Unsafe
, with all operations guarded with assert
keyword.
With assertions enabled in runtime (-ea
java switch) AssertionError
will be thrown on illegal index access. Without assertions illegal index will crash JVM.
Array won't be zeroed after creation (will contain garbage by default).
Allocated memory may be freed manually using free()
(thread-safe
and may be called multiple times) or it will be freed after OffHeapStructArray
instance will be garbage collected.
Modifier and Type | Field and Description |
---|---|
private OffHeapMemory |
ohm |
private int |
structLength |
Modifier | Constructor and Description |
---|---|
|
OffHeapStructArray(ByteArrayTool bt,
int size,
int structLength)
Constructor, uses
OnHeapMemory underneath
effectively making this instance an OnHeap collection |
|
OffHeapStructArray(long size,
int structLength)
Constructor
|
private |
OffHeapStructArray(OffHeapMemory ohm,
int structLength)
Private constructor for
clone() support |
Modifier and Type | Method and Description |
---|---|
OffHeapStructArray |
clone() |
void |
free()
Frees allocated memory, may be called multiple times from any thread
|
void |
get(long index,
byte[] buffer)
Copies struct on specified index into specified buffer
|
void |
get(long index,
byte[] buffer,
int bufferPos)
Copies struct on specified index into specified buffer
|
void |
get(long index,
int pos,
byte[] buffer,
int bufferPos,
int length)
Copies part of struct on specified index into specified buffer
|
byte |
getByte(long index,
int offset)
Gets byte from struct on specified index with specified offset
|
int |
getInt(long index,
int offset)
Gets four bytes as int from struct on specified index with specified offset
|
long |
getLong(long index,
int offset)
Gets long from struct on specified index with specified offset
|
short |
getShort(long index,
int offset)
Gets two bytes as short from struct on specified index with specified offset
|
short |
getUnsignedByte(long index,
int offset)
Gets one byte (stored as one signed byte) from struct on specified index
with specified offset, converts it to unsigned and returns it as short
|
long |
getUnsignedInt(long index,
int offset)
Gets unsigned int (stored as 4 bytes) and returns it as long
from struct on specified index with specified offset
|
int |
getUnsignedShort(long index,
int offset)
Gets unsigned short (stored as two bytes) from struct on specified index
with specified offset and returns it as int
|
boolean |
isUnsafe()
Whether unsafe implementation of
OffHeapMemory is used |
OffHeapDisposableIterator<byte[]> |
iterator() |
void |
putByte(long index,
int offset,
byte value)
Puts byte into struct onto specified index with specified offset
|
void |
putInt(long index,
int offset,
int value)
Puts int into struct onto specified index with specified offset as four bytes
|
void |
putLong(long index,
int offset,
long value)
Puts long into struct onto specified index with specified offset as eight bytes
|
void |
putShort(long index,
int offset,
short value)
Puts short into struct onto specified index with specified offset as two bytes
|
void |
putUnsignedByte(long index,
int offset,
short value)
Puts short with value from 0 to 255 inclusive into struct onto specified
index with specified offset as one signed byte
|
void |
putUnsignedInt(long index,
int offset,
long value)
Puts long value from 0 to 4294967295 inclusive into struct onto specified index
with specified offset as four bytes
|
void |
putUnsignedShort(long index,
int offset,
int value)
Puts int with value from 0 to 65535 inclusive into struct onto specified
index with specified offset as two bytes
|
void |
set(long index,
byte[] struct)
Copies specified struct contents onto specified index
|
void |
set(long index,
byte[] struct,
int structPos)
Copies specified struct contents onto specified index
|
long |
size()
Returns number of elements in this array
|
int |
structLength()
Returns length of the single struct in bytes
|
String |
toString() |
private final OffHeapMemory ohm
private final int structLength
public OffHeapStructArray(long size, int structLength)
size
- array sizestructLength
- length of struct in bytes, must be >= 8
public OffHeapStructArray(ByteArrayTool bt, int size, int structLength)
OnHeapMemory
underneath
effectively making this instance an OnHeap collectionbt
- byte array tool to manage on-heap memory of this collectionsize
- array sizestructLength
- length of struct in bytes, must be >= 8
private OffHeapStructArray(OffHeapMemory ohm, int structLength)
clone()
supportohm
- cloned memorystructLength
- struct lengthpublic int structLength()
structLength
in interface OffHeapStructCollection
public long size()
size
in interface OffHeapStructCollection
public void free()
free
in interface OffHeapDisposable
public OffHeapDisposableIterator<byte[]> iterator()
iterator
in interface OffHeapDisposableIterable<byte[]>
iterator
in interface Iterable<byte[]>
public boolean isUnsafe()
OffHeapMemory
is usedOffHeapMemory
is usedpublic void get(long index, byte[] buffer)
get
in interface OffHeapStructCollection
index
- array indexbuffer
- buffer to copy struct intopublic void get(long index, byte[] buffer, int bufferPos)
index
- array indexbuffer
- buffer to copy struct intobufferPos
- start position in specified bufferpublic void get(long index, int pos, byte[] buffer, int bufferPos, int length)
get
in interface OffHeapStructCollection
index
- array indexpos
- position in structbuffer
- buffer to copy struct intobufferPos
- start position in specified bufferlength
- number of bytes to copypublic void set(long index, byte[] struct)
set
in interface OffHeapStructCollection
index
- array indexstruct
- struct to copy into arraypublic void set(long index, byte[] struct, int structPos)
index
- array indexstruct
- struct to copy into arraystructPos
- start position in specified structpublic byte getByte(long index, int offset)
getByte
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetpublic void putByte(long index, int offset, byte value)
putByte
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetvalue
- valuepublic short getUnsignedByte(long index, int offset)
getUnsignedByte
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetpublic void putUnsignedByte(long index, int offset, short value)
putUnsignedByte
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetvalue
- unsigned bytepublic short getShort(long index, int offset)
getShort
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetpublic void putShort(long index, int offset, short value)
putShort
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetvalue
- short valuepublic int getUnsignedShort(long index, int offset)
getUnsignedShort
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetpublic void putUnsignedShort(long index, int offset, int value)
putUnsignedShort
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetvalue
- unsigned short as intpublic int getInt(long index, int offset)
getInt
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetpublic void putInt(long index, int offset, int value)
putInt
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetvalue
- int valuepublic long getUnsignedInt(long index, int offset)
getUnsignedInt
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetpublic void putUnsignedInt(long index, int offset, long value)
putUnsignedInt
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetvalue
- unsigned int as longpublic long getLong(long index, int offset)
getLong
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetpublic void putLong(long index, int offset, long value)
putLong
in interface OffHeapStructCollection
index
- array indexoffset
- struct offsetvalue
- long valuepublic OffHeapStructArray clone()
Copyright © 2014. All Rights Reserved.