public void Defragment()
{
GATManagedData chunk;
GATManagedData nextChunk;
int i;
chunk = _firstCursor.next;
if( chunk == _unallocatedCursor )
{
return; //Nothing to defrag
}
nextChunk = chunk.next;
for( i = 0; i < _freeChunksBins.Length; i++ )
{
_freeChunksBins[i].Clear();
}
while( nextChunk != _unallocatedCursor )
{
if( chunk.allocatedSize == 0 )
{
if( nextChunk.allocatedSize == 0 )
{
chunk.next = nextChunk.next;
_pool.Push( nextChunk );
nextChunk = chunk.next;
}
else
{
while( chunk.MaxSize > _maxBinSize ) // fragment and bin
{
GATManagedData subChunk;
subChunk = GetOrMakeChunk();
subChunk.AllocateFree( chunk.MemOffset + _maxBinSize, nextChunk );
chunk.next = subChunk;
AddToFreeChunksBins( chunk );
chunk = subChunk;
}
if( chunk.MaxSize != 0 )
{
AddToFreeChunksBins( chunk );
}
else
{
_pool.Push( chunk );
}
chunk = nextChunk;
nextChunk = nextChunk.next;
}
}
else
{
chunk = nextChunk;
nextChunk = nextChunk.next;
}
}
//Now chunk.next is _unallocatedChunk, concatenate if free
if( chunk.allocatedSize == 0 )
{
_pool.Push( _unallocatedCursor );
_unallocatedCursor = chunk;
_unallocatedCursor.next = _endCursor;
}
}