public GATData GetDataContainer( int size )
{
GATManagedData chunk = null;
int binIndex;
int binSize;
binIndex = GetBinIndexForSize( size );
binSize = _binWidth + binIndex * _binWidth;
if( _freeChunksBins[ binIndex ].Count != 0 )
{
chunk = _freeChunksBins[ binIndex ].Pop ();
}
else
{
if( _unallocatedCursor.MaxSize >= binSize )
{
chunk = _unallocatedCursor;
chunk.allocatedSize = size;
_unallocatedCursor = GetOrMakeChunk();
_unallocatedCursor.AllocateFree( chunk.MemOffset + binSize, _endCursor );
chunk.next = _unallocatedCursor;
}
else
{
if( TryFragmentBins( binIndex + 1, binSize, ref chunk ) == false )
{
Defragment();
if( _freeChunksBins[ binIndex ].Count != 0 )
{
chunk = _freeChunksBins[ binIndex ].Pop ();
}
else
{
if( _unallocatedCursor.MaxSize >= binSize )
{
chunk = _unallocatedCursor;
chunk.allocatedSize = size;
_unallocatedCursor = GetOrMakeChunk();
_unallocatedCursor.AllocateFree( chunk.MemOffset + binSize, _endCursor );
chunk.next = _unallocatedCursor;
}
else if( TryFragmentBins( binIndex + 1, binSize, ref chunk ) == false )
{
throw new GATException( "Out of memory!" );
}
}
}
}
}
chunk.allocatedSize = size;
return chunk;
}