2018-08-15 20:59:51 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Memory
|
|
|
|
{
|
|
|
|
class ArenaAllocator
|
|
|
|
{
|
|
|
|
private class Region
|
|
|
|
{
|
|
|
|
public long Position { get; set; }
|
|
|
|
public long Size { get; set; }
|
|
|
|
|
|
|
|
public Region(long Position, long Size)
|
|
|
|
{
|
|
|
|
this.Position = Position;
|
|
|
|
this.Size = Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private LinkedList<Region> FreeRegions;
|
|
|
|
|
|
|
|
public long TotalAvailableSize { get; private set; }
|
|
|
|
public long TotalUsedSize { get; private set; }
|
|
|
|
|
|
|
|
public ArenaAllocator(long ArenaSize)
|
|
|
|
{
|
|
|
|
TotalAvailableSize = ArenaSize;
|
|
|
|
|
|
|
|
FreeRegions = new LinkedList<Region>();
|
|
|
|
|
|
|
|
FreeRegions.AddFirst(new Region(0, ArenaSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool TryAllocate(long Size, out long Position)
|
|
|
|
{
|
|
|
|
LinkedListNode<Region> Node = FreeRegions.First;
|
|
|
|
|
|
|
|
while (Node != null)
|
|
|
|
{
|
|
|
|
Region Rg = Node.Value;
|
|
|
|
|
|
|
|
if ((ulong)Rg.Size >= (ulong)Size)
|
|
|
|
{
|
|
|
|
Position = Rg.Position;
|
|
|
|
|
|
|
|
Rg.Position += Size;
|
|
|
|
Rg.Size -= Size;
|
|
|
|
|
2018-09-18 17:28:28 +02:00
|
|
|
if (Rg.Size == 0)
|
|
|
|
{
|
|
|
|
//Region is empty, just remove it.
|
|
|
|
FreeRegions.Remove(Node);
|
|
|
|
}
|
|
|
|
else if (Node.Previous != null)
|
|
|
|
{
|
|
|
|
//Re-sort based on size (smaller first).
|
|
|
|
Node = Node.Previous;
|
|
|
|
|
|
|
|
FreeRegions.Remove(Node.Next);
|
|
|
|
|
|
|
|
while (Node != null && (ulong)Node.Value.Size > (ulong)Rg.Size)
|
|
|
|
{
|
|
|
|
Node = Node.Previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Node != null)
|
|
|
|
{
|
|
|
|
FreeRegions.AddAfter(Node, Rg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FreeRegions.AddFirst(Rg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
TotalUsedSize += Size;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node = Node.Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
Position = 0;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Free(long Position, long Size)
|
|
|
|
{
|
|
|
|
long End = Position + Size;
|
|
|
|
|
|
|
|
Region NewRg = new Region(Position, Size);
|
|
|
|
|
|
|
|
LinkedListNode<Region> Node = FreeRegions.First;
|
2018-09-18 17:28:28 +02:00
|
|
|
LinkedListNode<Region> PrevSz = null;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
while (Node != null)
|
|
|
|
{
|
|
|
|
LinkedListNode<Region> NextNode = Node.Next;
|
|
|
|
|
|
|
|
Region Rg = Node.Value;
|
|
|
|
|
|
|
|
long RgEnd = Rg.Position + Rg.Size;
|
|
|
|
|
|
|
|
if (Rg.Position == End)
|
|
|
|
{
|
2018-09-18 17:28:28 +02:00
|
|
|
//Current region position matches the end of the freed region,
|
|
|
|
//just merge the two and remove the current region from the list.
|
2018-08-15 20:59:51 +02:00
|
|
|
NewRg.Size += Rg.Size;
|
|
|
|
|
|
|
|
FreeRegions.Remove(Node);
|
|
|
|
}
|
|
|
|
else if (RgEnd == Position)
|
|
|
|
{
|
2018-09-18 17:28:28 +02:00
|
|
|
//End of the current region matches the position of the freed region,
|
|
|
|
//just merge the two and remove the current region from the list.
|
2018-08-15 20:59:51 +02:00
|
|
|
NewRg.Position = Rg.Position;
|
|
|
|
NewRg.Size += Rg.Size;
|
|
|
|
|
|
|
|
FreeRegions.Remove(Node);
|
|
|
|
}
|
2018-09-18 17:28:28 +02:00
|
|
|
else
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-09-18 17:28:28 +02:00
|
|
|
if (PrevSz == null)
|
|
|
|
{
|
|
|
|
PrevSz = Node;
|
|
|
|
}
|
|
|
|
else if ((ulong)Rg.Size < (ulong)NewRg.Size &&
|
|
|
|
(ulong)Rg.Size > (ulong)PrevSz.Value.Size)
|
|
|
|
{
|
|
|
|
PrevSz = Node;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Node = NextNode;
|
|
|
|
}
|
|
|
|
|
2018-09-18 17:28:28 +02:00
|
|
|
if (PrevSz != null && (ulong)PrevSz.Value.Size < (ulong)Size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
FreeRegions.AddAfter(PrevSz, NewRg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FreeRegions.AddFirst(NewRg);
|
|
|
|
}
|
|
|
|
|
|
|
|
TotalUsedSize -= Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|