|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.musclecard.CardEdge.MemoryManager
Memory Manager class.
An instance of this class is capable of handling allocation and deallocation of chunks in a large Java byte array that is allocated once during the object instantiation.
The Memory Manager allocates or frees memory chunks in the preallocated byte array on demand.
No defragmentation is done, actually.
Consecutive freed memory chunks are recompacted.
Every allocation takes 2 more bytes to store the allocated block size, just before the allocated offset.
A free memory block starts with a node (NODE_SIZE bytes):
short size; short next;
Field Summary | |
private short |
free_head
|
private static byte |
NODE_SIZE
|
static short |
NULL_OFFSET
Special offset value used as invalid offset |
private byte[] |
ptr
|
Constructor Summary | |
MemoryManager(short mem_size)
Constructor for the MemoryManager class |
Method Summary | |
short |
alloc(short size)
Allocate memory Each allocation takes actually a 2 bytes overhead. |
void |
free(short offset)
Free a memory block Consecutive free blocks are recompacted. |
short |
freemem()
Get available free memory |
short |
getBlockSize(short offset)
Get the size of a memory block |
byte[] |
getBuffer()
Retrieve the Java byte array containing all the memory contents. |
byte |
getByte(short base)
Read a byte value from memory |
byte |
getByte(short base,
short offset)
Read a byte value from memory |
void |
getBytes(byte[] dst_bytes,
short dst_offset,
short src_base,
short src_offset,
short size)
Copy a byte sequence from memory |
short |
getMaxSize()
Gets the size of the greatest chunk of available memory |
short |
getShort(short base)
Read a short value from memory |
short |
getShort(short base,
short offset)
Read a short value from memory |
private void |
Init(short mem_size)
|
boolean |
realloc(short offset,
short new_size)
Resize (only clamping is supported) a previously allocated memory chunk |
void |
setByte(short base,
byte b)
Set a byte value into memory |
void |
setByte(short base,
short offset,
byte b)
Set a byte value into memory |
void |
setBytes(short dst_base,
short dst_offset,
byte[] src_bytes,
short src_offset,
short size)
Copy a byte sequence into memory |
void |
setShort(short base,
short b)
Set a short value into memory |
void |
setShort(short base,
short offset,
short b)
Set a short value into memory |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private short free_head
private static final byte NODE_SIZE
public static final short NULL_OFFSET
private byte[] ptr
Constructor Detail |
public MemoryManager(short mem_size)
mem_size
- Size of the memory are to be allocatedMethod Detail |
public short alloc(short size)
size
- Size of the memory block
alloc(short)
,
freemem()
public void free(short offset)
Consecutive free blocks are recompacted. Recompaction happens on free(). 4 cases are considered: don't recompact, recompact with next only, with previous only and with both of them.
offset
- The offset at which the memory block starts; it was
returned from a previous call to alloc(short)
alloc(short)
,
freemem()
public short freemem()
free(short)
,
alloc(short)
public short getBlockSize(short offset)
offset
- The offset at which the memory block startspublic byte[] getBuffer()
To optimize, we don't use external buffers, but we directly copy from the memory array.
Use this function only if really required.
public byte getByte(short base)
base
- The complete memory location (offset) of the byte to
read
public byte getByte(short base, short offset)
base
- The base memory location (offset) of the byte to readoffset
- The offset of the byte (is added to the base
parameter)
public void getBytes(byte[] dst_bytes, short dst_offset, short src_base, short src_offset, short size)
dst_offset
- The offset at which the sequence will be copied
in dst_bytes[]src_base
- The base memory location (offset) of the source
byte sequencesrc_offset
- The offset of the source byte sequence (is
added to the src_base parameter)size
- The number of bytes to be copiedpublic short getMaxSize()
public short getShort(short base)
base
- The base memory location (offset) of the short to
read
public short getShort(short base, short offset)
base
- The base memory location (offset) of the short to
readoffset
- The offset of the short (is added to the base
parameter)
private void Init(short mem_size)
public boolean realloc(short offset, short new_size)
offset
- Memory offset as returned by alloc()new_size
- ew size of the memory block
alloc(short)
,
free(short)
,
freemem()
public void setByte(short base, byte b)
base
- The complete memory location (offset) of the byte to
setb
- The new byte valuepublic void setByte(short base, short offset, byte b)
base
- The base memory location (offset) of the byte to setoffset
- The offset of the byte (is added to the base
parameter)b
- The new byte valuepublic void setBytes(short dst_base, short dst_offset, byte[] src_bytes, short src_offset, short size)
dst_base
- The base memory location (offset) of the
destination byte sequencedst_offset
- The offset of the destination byte sequence (is
added to the dst_base parameter)src_offset
- The offset at which the source sequence starts
in src_bytes[]size
- The number of bytes to be copiedpublic void setShort(short base, short b)
base
- The complete memory location (offset) of the short to
setb
- The short valuepublic void setShort(short base, short offset, short b)
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |