abstract class BaseGenericFreeList extends Object
free()
with
the index of the first unit as the argument.Properties/Constraints:
+-+-+-----------+-----------+ |f|m| prev | next/size | +-+-+-----------+-----------+ - single free unit: "free", "single", "prev", "next" - single used unit: "used", "single" - contiguous free units . first unit: "free", "multi", "prev", "next" . second unit: "free", "multi", "size" . last unit: "free", "multi", "size" - contiguous used units . first unit: "used", "multi", "prev", "next" . second unit: "used", "multi", "size" . last unit: "used", "multi", "size" - any other unit: undefined +-+-+-----------+-----------+ top sentinel |0|0| tail | head | [-1] +-+-+-----------+-----------+ .... /-------- +-+-+-----------+-----------+ | |1|1| prev | next | [j] | +-+-+-----------+-----------+ | |1|1| | size | [j+1] free multi +-+-+-----------+-----------+ unit block | ... | ... | +-+-+-----------+-----------+ | |1|1| | size | >-------- +-+-+-----------+-----------+ single free unit |1|0| prev | next | >-------- +-+-+-----------+-----------+ single used unit |0|0| | >-------- +-+-+-----------------------+ | |0|1| | | +-+-+-----------+-----------+ | |0|1| | size | used multi +-+-+-----------+-----------+ unit block | ... | | +-+-+-----------+-----------+ | |0|1| | size | \-------- +-+-+-----------+-----------+ .... +-+-+-----------------------+ bottom sentinel |0|0| | [N] +-+-+-----------------------+The sentinels serve as guards against out of range coalescing because they both appear as "used" blocks and so will never coalesce. The top sentinel also serves as the head and tail of the doubly linked list of free blocks.
Modifier and Type | Field and Description |
---|---|
protected static boolean |
DEBUG |
static int |
FAILURE |
protected int |
head |
protected int |
heads |
protected static int |
MAX_HEADS |
Constructor and Description |
---|
BaseGenericFreeList() |
Modifier and Type | Method and Description |
---|---|
private void |
addToFree(int unit)
Add a lump of units to the free list
|
int |
alloc(int size)
Allocate
size units. |
int |
alloc(int size,
int unit)
Allocate
size units. |
private int |
alloc(int size,
int unit,
int unitSize)
Allocate
size units. |
private void |
coalesce(int start,
int end)
Coalesce two or three contiguous lumps of units, removing start
and end lumps from the free list as necessary.
|
boolean |
couldAlloc(int size)
Would an allocation of
size units succeed? |
(package private) void |
dbgPrintFree()
Print the free list (for debugging purposes)
|
int |
free(int unit)
Free a previously allocated contiguous lump of units.
|
int |
free(int unit,
boolean returnCoalescedSize)
Free a previously allocated contiguous lump of units.
|
(package private) abstract boolean |
getFree(int unit) |
(package private) abstract int |
getLeft(int unit) |
(package private) abstract int |
getNext(int unit) |
(package private) abstract int |
getPrev(int unit) |
private int |
getRight(int unit)
Get the lump to the "right" of the current lump (i.e.
|
(package private) abstract int |
getSize(int unit) |
protected void |
initializeHeap(int units)
Initialize a new heap.
|
protected void |
initializeHeap(int units,
int grain)
Initialize a new heap.
|
(package private) abstract boolean |
isCoalescable(int unit) |
private void |
removeFromFree(int unit)
Remove a lump of units from the free list
|
(package private) abstract void |
setFree(int unit,
boolean isFree) |
(package private) abstract void |
setNext(int unit,
int next) |
(package private) abstract void |
setPrev(int unit,
int prev) |
(package private) abstract void |
setSentinel(int unit) |
(package private) abstract void |
setSize(int unit,
int size) |
int |
size(int unit)
Return the size of the specified lump of units
|
private void |
split(int unit,
int size)
Reduce a lump of units to size, freeing any excess.
|
protected static final boolean DEBUG
public static final int FAILURE
protected static final int MAX_HEADS
protected int heads
protected int head
BaseGenericFreeList()
public final int alloc(int size)
size
units. Return the unit IDsize
- The number of units to be allocatedsize
contiguous units, or -1 if the request can't be satisfiedpublic final boolean couldAlloc(int size)
size
units succeed?size
- The number of units to test forpublic final int alloc(int size, int unit)
size
units. Return the unit IDsize
- The number of units to be allocatedunit
- TODO needs documentationsize
contiguous units, or -1 if the request can't be satisfiedprivate int alloc(int size, int unit, int unitSize)
size
units. Return the unit IDsize
- The number of units to be allocatedunit
- TODO needs documentationunitSize
- TODO needs documentationsize
contiguous units, or -1 if the request can't be satisfiedpublic final int free(int unit)
unit
- The index of the first unit.public final int free(int unit, boolean returnCoalescedSize)
unit
- The index of the first unit.returnCoalescedSize
- if true, return the coalesced sizepublic final int size(int unit)
unit
- The index of the first unit in the lump.protected final void initializeHeap(int units)
units
- The number of units in the heapprotected final void initializeHeap(int units, int grain)
units
- The number of units in the heapgrain
- TODO needs documentationprivate void split(int unit, int size)
unit
- The index of the first unitsize
- The size of the first partprivate void coalesce(int start, int end)
start
- The index of the start of the first lumpend
- The index of the start of the last lumpprivate void addToFree(int unit)
unit
- The first unit in the lump of units to be addedprivate void removeFromFree(int unit)
unit
- The first unit in the lump of units to be removedprivate int getRight(int unit)
unit
- The index of the first unit in the lump in questionvoid dbgPrintFree()
abstract void setSentinel(int unit)
abstract int getSize(int unit)
abstract void setSize(int unit, int size)
abstract boolean getFree(int unit)
abstract void setFree(int unit, boolean isFree)
abstract int getNext(int unit)
abstract void setNext(int unit, int next)
abstract int getPrev(int unit)
abstract void setPrev(int unit, int prev)
abstract int getLeft(int unit)
abstract boolean isCoalescable(int unit)