com.sun.javacard.samples.CardEdge
Class MemoryManager

java.lang.Object
  |
  +--com.sun.javacard.samples.CardEdge.MemoryManager

public class MemoryManager
extends java.lang.Object

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;
 

Version:
0.9.9
Author:
Tommaso Cucinotta, David Corcoran, Ludovic Rousseau

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, registerNatives, toString, wait, wait, wait
 

Field Detail

NULL_OFFSET

public static final short NULL_OFFSET
Special offset value used as invalid offset

NODE_SIZE

private static final byte NODE_SIZE

ptr

private byte[] ptr

free_head

private short free_head
Constructor Detail

MemoryManager

public MemoryManager(short mem_size)
Constructor for the MemoryManager class
Parameters:
mem_size - Size of the memory are to be allocated
Method Detail

Init

private void Init(short mem_size)

alloc

public short alloc(short size)
Allocate memory Each allocation takes actually a 2 bytes overhead.
Parameters:
size - Size of the memory block
Returns:
The offset at which allocated memory starts or NULL_OFFSET if an error occurred.
See Also:
alloc(short), freemem()

free

public void free(short offset)
Free a memory block

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.

Parameters:
offset - The offset at which the memory block starts; it was returned from a previous call to alloc(short)
See Also:
alloc(short), freemem()

freemem

public short freemem()
Get available free memory
Returns:
The total amount of available free memory, equal to the sum of all free fragments' sizes.
See Also:
free(short), alloc(short)

getBlockSize

public short getBlockSize(short offset)
Get the size of a memory block
Parameters:
offset - The offset at which the memory block starts

getBuffer

public byte[] getBuffer()
Retrieve the Java byte array containing all the memory contents.

To optimize, we don't use external buffers, but we directly copy from the memory array.

Use this function only if really required.

Returns:
The Java byte array containing all memory contents

getByte

public byte getByte(short base)
Read a byte value from memory
Parameters:
base - The complete memory location (offset) of the byte to read
Returns:
The byte value

getByte

public byte getByte(short base,
                    short offset)
Read a byte value from memory
Parameters:
base - The base memory location (offset) of the byte to read
offset - The offset of the byte (is added to the base parameter)
Returns:
The byte value

getBytes

public void getBytes(byte[] dst_bytes,
                     short dst_offset,
                     short src_base,
                     short src_offset,
                     short size)
Copy a byte sequence from memory
Parameters:
dst_bytes[] - The destination byte array
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 sequence
src_offset - The offset of the source byte sequence (is added to the src_base parameter)
size - The number of bytes to be copied

getMaxSize

public short getMaxSize()
Gets the size of the greatest chunk of available memory
Returns:
The size of the greatest free memory chunk, or zero if there is no free mem left

getShort

public short getShort(short base)
Read a short value from memory
Parameters:
base - The base memory location (offset) of the short to read
Returns:
The short value

getShort

public short getShort(short base,
                      short offset)
Read a short value from memory
Parameters:
base - The base memory location (offset) of the short to read
offset - The offset of the short (is added to the base parameter)
Returns:
The short value

realloc

public boolean realloc(short offset,
                       short new_size)
Resize (only clamping is supported) a previously allocated memory chunk
Parameters:
offset - Memory offset as returned by alloc()
new_size - ew size of the memory block
Returns:
True if it was possible to realloc(), False otherwise
See Also:
alloc(short), free(short), freemem()

setByte

public void setByte(short base,
                    byte b)
Set a byte value into memory
Parameters:
base - The complete memory location (offset) of the byte to set
b - The new byte value

setByte

public void setByte(short base,
                    short offset,
                    byte b)
Set a byte value into memory
Parameters:
base - The base memory location (offset) of the byte to set
offset - The offset of the byte (is added to the base parameter)
b - The new byte value

setBytes

public void setBytes(short dst_base,
                     short dst_offset,
                     byte[] src_bytes,
                     short src_offset,
                     short size)
Copy a byte sequence into memory
Parameters:
dst_base - The base memory location (offset) of the destination byte sequence
dst_offset - The offset of the destination byte sequence (is added to the dst_base parameter)
src_bytes[] - The source byte array
src_offset - The offset at which the source sequence starts in src_bytes[]
size - The number of bytes to be copied

setShort

public void setShort(short base,
                     short b)
Set a short value into memory
Parameters:
base - The complete memory location (offset) of the short to set
b - The short value

setShort

public void setShort(short base,
                     short offset,
                     short b)
Set a short value into memory