public bool CanFit(long size, QuickList<ulong>? ignoredIds = null)
{
if (allocations.Count == 0)
{
return size <= memoryPoolSize;
}
int allocationIndex = searchStartIndex;
var initialId = allocations.Keys[allocationIndex];
while (true)
{
var allocation = allocations.Values[allocationIndex];
int nextAllocationIndex;
Allocation nextAllocation;
//Skip any subsequent allocations that are ignored.
ulong nextAllocationId = allocation.Next;
do
{
nextAllocationIndex = allocations.IndexOf(nextAllocationId);
nextAllocation = allocations.Values[nextAllocationIndex];
nextAllocationId = nextAllocation.Next;
} while (ignoredIds != null && ignoredIds.Value.Contains(nextAllocationId));
if (nextAllocation.Start < allocation.End)
{
//Wrapped around, so the gap goes from here to the end of the memory block, and from the beginning of the memory block to the next allocation.
//But we need contiguous space so the two areas have to be tested independently.
if (memoryPoolSize - allocation.End >= size)
{
return true;
}
else
{
if (nextAllocation.Start >= size)
{
return true;
}
}
}
else
{
//The next allocation is in order.
if (nextAllocation.Start - allocation.End >= size)
{
return true;
}
}
//If we get here, no open space was found.
//Move on to the next spot.
allocationIndex = nextAllocationIndex;
//Have we already wrapped around?
if (allocations.Keys[allocationIndex] == initialId)
{
//Wrapped around without finding any space.
return false;
}
}
}