public void GetLargestContiguousSize(out long largestContiguous, out long totalFreeSpace)
{
if (allocations.Count == 0)
{
totalFreeSpace = memoryPoolSize;
largestContiguous = memoryPoolSize;
return;
}
largestContiguous = 0;
totalFreeSpace = 0;
for (int i = 0; i < allocations.Count; ++i)
{
Allocation nextAllocation;
allocations.TryGetValue(allocations.Values[i].Next, out nextAllocation);
var toNext = nextAllocation.Start - allocations.Values[i].End;
if (toNext < 0)
{
//The next allocation requires a wrap, so the actual contiguous area is only from our end to the end of the pool,
//and then a second region from 0 to the next allocation.
var adjacent = memoryPoolSize - allocations.Values[i].End;
var wrapped = nextAllocation.Start;
if (largestContiguous < adjacent)
largestContiguous = adjacent;
if (largestContiguous < wrapped)
largestContiguous = wrapped;
totalFreeSpace += adjacent + wrapped;
}
else
{
if (largestContiguous < toNext)
largestContiguous = toNext;
totalFreeSpace += toNext;
}
}
}