com.alexkasko.unsafe.offheapstruct
public class OffHeapStructArrayList extends Object implements OffHeapStructCollection, OffHeapDisposable
Implementation of off-heap array list 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 ByteArrayTool |
bt |
private long |
capacity |
private static int |
MIN_CAPACITY_INCREMENT |
private OffHeapMemory |
ohm |
private long |
size |
private int |
structLength |
Constructor and Description |
---|
OffHeapStructArrayList(ByteArrayTool bt,
int capacity,
int structLength)
Constructor, uses
OnHeapMemory underneath
effectively making this instance an OnHeap collection |
OffHeapStructArrayList(int structLength)
Constructor with default capacity =
12 |
OffHeapStructArrayList(long capacity,
int structLength)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
add(byte[] struct)
Adds element to the end of this list.
|
void |
add(byte[] struct,
int structPos)
Adds element to the end of this list.
|
long |
capacity()
Returns max number of possible elements in this list without memory reallocation
|
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 |
reset()
Resets the collection setting size to 0.
|
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
|
void |
shrinkToFit()
Shrinks array list capacity to current size
|
long |
size()
Returns number of elements in this list
|
int |
structLength()
Returns length of the single struct in bytes
|
String |
toString() |
private static final int MIN_CAPACITY_INCREMENT
private final ByteArrayTool bt
private final int structLength
private OffHeapMemory ohm
private long size
private long capacity
public OffHeapStructArrayList(int structLength)
12
structLength
- length of the single struct in bytes, must be >= 8
public OffHeapStructArrayList(long capacity, int structLength)
capacity
- initial capacity for this liststructLength
- length of the single struct in bytes, must be >= 8
public OffHeapStructArrayList(ByteArrayTool bt, int capacity, int structLength)
OnHeapMemory
underneath
effectively making this instance an OnHeap collectionbt
- byte array tool to manage on-heap memory of this collectioncapacity
- initial capacity for this liststructLength
- length of struct in bytes, must be >= 8
public int structLength()
structLength
in interface OffHeapStructCollection
public long size()
size
in interface OffHeapStructCollection
public long capacity()
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 void add(byte[] struct)
struct
- to addpublic void add(byte[] struct, int structPos)
struct
- struct to addstructPos
- struct offsetpublic void reset()
public void shrinkToFit()
Copyright © 2014. All Rights Reserved.