0
0
Fork 0

Optimize kernel memory block lookup and consolidate RBTree implementations (#3410)

* Implement intrusive red-black tree, use it for HLE kernel block manager

* Implement TreeDictionary using IntrusiveRedBlackTree

* Implement IntervalTree using IntrusiveRedBlackTree

* Implement IntervalTree (on Ryujinx.Memory) using IntrusiveRedBlackTree

* Make PredecessorOf and SuccessorOf internal, expose Predecessor and Successor properties on the node itself

* Allocation free tree node lookup
This commit is contained in:
gdkchan 2022-08-26 15:21:48 -03:00 committed by GitHub
parent 6592d64751
commit 6922862db8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 860 additions and 1121 deletions

View file

@ -19,8 +19,6 @@ namespace ARMeilleure.Translation
public int Count => _count;
public IntervalTree() { }
#region Public Methods
/// <summary>

View file

@ -9,19 +9,10 @@ namespace Ryujinx.Common.Collections
/// </summary>
/// <typeparam name="K">Key</typeparam>
/// <typeparam name="V">Value</typeparam>
public class IntervalTree<K, V> where K : IComparable<K>
public class IntervalTree<K, V> : IntrusiveRedBlackTreeImpl<IntervalTreeNode<K, V>> where K : IComparable<K>
{
private const int ArrayGrowthSize = 32;
private const bool Black = true;
private const bool Red = false;
private IntervalTreeNode<K, V> _root = null;
private int _count = 0;
public int Count => _count;
public IntervalTree() { }
#region Public Methods
/// <summary>
@ -80,7 +71,7 @@ namespace Ryujinx.Common.Collections
throw new ArgumentNullException(nameof(end));
}
GetValues(_root, start, end, ref overlaps, ref overlapCount);
GetValues(Root, start, end, ref overlaps, ref overlapCount);
return overlapCount;
}
@ -128,7 +119,7 @@ namespace Ryujinx.Common.Collections
int removed = Delete(key, value);
_count -= removed;
Count -= removed;
return removed;
}
@ -141,7 +132,7 @@ namespace Ryujinx.Common.Collections
{
List<RangeNode<K, V>> list = new List<RangeNode<K, V>>();
AddToList(_root, list);
AddToList(Root, list);
return list;
}
@ -182,7 +173,7 @@ namespace Ryujinx.Common.Collections
throw new ArgumentNullException(nameof(key));
}
IntervalTreeNode<K, V> node = _root;
IntervalTreeNode<K, V> node = Root;
while (node != null)
{
int cmp = key.CompareTo(node.Start);
@ -317,7 +308,7 @@ namespace Ryujinx.Common.Collections
private IntervalTreeNode<K, V> BSTInsert(K start, K end, V value)
{
IntervalTreeNode<K, V> parent = null;
IntervalTreeNode<K, V> node = _root;
IntervalTreeNode<K, V> node = Root;
while (node != null)
{
@ -345,14 +336,14 @@ namespace Ryujinx.Common.Collections
}
}
_count++;
Count++;
return node;
}
}
IntervalTreeNode<K, V> newNode = new IntervalTreeNode<K, V>(start, end, value, parent);
if (newNode.Parent == null)
{
_root = newNode;
Root = newNode;
}
else if (start.CompareTo(parent.Start) < 0)
{
@ -364,7 +355,7 @@ namespace Ryujinx.Common.Collections
}
PropagateIncrease(newNode);
_count++;
Count++;
return newNode;
}
@ -418,7 +409,7 @@ namespace Ryujinx.Common.Collections
if (ParentOf(replacementNode) == null)
{
_root = tmp;
Root = tmp;
}
else if (replacementNode == LeftOf(ParentOf(replacementNode)))
{
@ -447,295 +438,27 @@ namespace Ryujinx.Common.Collections
return removed;
}
/// <summary>
/// Returns the node with the largest key where <paramref name="node"/> is considered the root node.
/// </summary>
/// <param name="node">Root Node</param>
/// <returns>Node with the maximum key in the tree of <paramref name="node"/></returns>
private static IntervalTreeNode<K, V> Maximum(IntervalTreeNode<K, V> node)
{
IntervalTreeNode<K, V> tmp = node;
while (tmp.Right != null)
{
tmp = tmp.Right;
}
return tmp;
}
/// <summary>
/// Finds the node whose key is immediately less than <paramref name="node"/>.
/// </summary>
/// <param name="node">Node to find the predecessor of</param>
/// <returns>Predecessor of <paramref name="node"/></returns>
private static IntervalTreeNode<K, V> PredecessorOf(IntervalTreeNode<K, V> node)
{
if (node.Left != null)
{
return Maximum(node.Left);
}
IntervalTreeNode<K, V> parent = node.Parent;
while (parent != null && node == parent.Left)
{
node = parent;
parent = parent.Parent;
}
return parent;
}
#endregion
#region Private Methods (RBL)
private void RestoreBalanceAfterRemoval(IntervalTreeNode<K, V> balanceNode)
{
IntervalTreeNode<K, V> ptr = balanceNode;
while (ptr != _root && ColorOf(ptr) == Black)
{
if (ptr == LeftOf(ParentOf(ptr)))
{
IntervalTreeNode<K, V> sibling = RightOf(ParentOf(ptr));
if (ColorOf(sibling) == Red)
{
SetColor(sibling, Black);
SetColor(ParentOf(ptr), Red);
RotateLeft(ParentOf(ptr));
sibling = RightOf(ParentOf(ptr));
}
if (ColorOf(LeftOf(sibling)) == Black && ColorOf(RightOf(sibling)) == Black)
{
SetColor(sibling, Red);
ptr = ParentOf(ptr);
}
else
{
if (ColorOf(RightOf(sibling)) == Black)
{
SetColor(LeftOf(sibling), Black);
SetColor(sibling, Red);
RotateRight(sibling);
sibling = RightOf(ParentOf(ptr));
}
SetColor(sibling, ColorOf(ParentOf(ptr)));
SetColor(ParentOf(ptr), Black);
SetColor(RightOf(sibling), Black);
RotateLeft(ParentOf(ptr));
ptr = _root;
}
}
else
{
IntervalTreeNode<K, V> sibling = LeftOf(ParentOf(ptr));
if (ColorOf(sibling) == Red)
{
SetColor(sibling, Black);
SetColor(ParentOf(ptr), Red);
RotateRight(ParentOf(ptr));
sibling = LeftOf(ParentOf(ptr));
}
if (ColorOf(RightOf(sibling)) == Black && ColorOf(LeftOf(sibling)) == Black)
{
SetColor(sibling, Red);
ptr = ParentOf(ptr);
}
else
{
if (ColorOf(LeftOf(sibling)) == Black)
{
SetColor(RightOf(sibling), Black);
SetColor(sibling, Red);
RotateLeft(sibling);
sibling = LeftOf(ParentOf(ptr));
}
SetColor(sibling, ColorOf(ParentOf(ptr)));
SetColor(ParentOf(ptr), Black);
SetColor(LeftOf(sibling), Black);
RotateRight(ParentOf(ptr));
ptr = _root;
}
}
}
SetColor(ptr, Black);
}
private void RestoreBalanceAfterInsertion(IntervalTreeNode<K, V> balanceNode)
{
SetColor(balanceNode, Red);
while (balanceNode != null && balanceNode != _root && ColorOf(ParentOf(balanceNode)) == Red)
{
if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode))))
{
IntervalTreeNode<K, V> sibling = RightOf(ParentOf(ParentOf(balanceNode)));
if (ColorOf(sibling) == Red)
{
SetColor(ParentOf(balanceNode), Black);
SetColor(sibling, Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
balanceNode = ParentOf(ParentOf(balanceNode));
}
else
{
if (balanceNode == RightOf(ParentOf(balanceNode)))
{
balanceNode = ParentOf(balanceNode);
RotateLeft(balanceNode);
}
SetColor(ParentOf(balanceNode), Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
RotateRight(ParentOf(ParentOf(balanceNode)));
}
}
else
{
IntervalTreeNode<K, V> sibling = LeftOf(ParentOf(ParentOf(balanceNode)));
if (ColorOf(sibling) == Red)
{
SetColor(ParentOf(balanceNode), Black);
SetColor(sibling, Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
balanceNode = ParentOf(ParentOf(balanceNode));
}
else
{
if (balanceNode == LeftOf(ParentOf(balanceNode)))
{
balanceNode = ParentOf(balanceNode);
RotateRight(balanceNode);
}
SetColor(ParentOf(balanceNode), Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
RotateLeft(ParentOf(ParentOf(balanceNode)));
}
}
}
SetColor(_root, Black);
}
private void RotateLeft(IntervalTreeNode<K, V> node)
protected override void RotateLeft(IntervalTreeNode<K, V> node)
{
if (node != null)
{
IntervalTreeNode<K, V> right = RightOf(node);
node.Right = LeftOf(right);
if (node.Right != null)
{
node.Right.Parent = node;
}
IntervalTreeNode<K, V> nodeParent = ParentOf(node);
right.Parent = nodeParent;
if (nodeParent == null)
{
_root = right;
}
else if (node == LeftOf(nodeParent))
{
nodeParent.Left = right;
}
else
{
nodeParent.Right = right;
}
right.Left = node;
node.Parent = right;
base.RotateLeft(node);
PropagateFull(node);
}
}
private void RotateRight(IntervalTreeNode<K, V> node)
protected override void RotateRight(IntervalTreeNode<K, V> node)
{
if (node != null)
{
IntervalTreeNode<K, V> left = LeftOf(node);
node.Left = RightOf(left);
if (node.Left != null)
{
node.Left.Parent = node;
}
IntervalTreeNode<K, V> nodeParent = ParentOf(node);
left.Parent = nodeParent;
if (nodeParent == null)
{
_root = left;
}
else if (node == RightOf(nodeParent))
{
nodeParent.Right = left;
}
else
{
nodeParent.Left = left;
}
left.Right = node;
node.Parent = left;
base.RotateRight(node);
PropagateFull(node);
}
}
#endregion
#region Safety-Methods
// These methods save memory by allowing us to forego sentinel nil nodes, as well as serve as protection against NullReferenceExceptions.
/// <summary>
/// Returns the color of <paramref name="node"/>, or Black if it is null.
/// </summary>
/// <param name="node">Node</param>
/// <returns>The boolean color of <paramref name="node"/>, or black if null</returns>
private static bool ColorOf(IntervalTreeNode<K, V> node)
{
return node == null || node.Color;
}
/// <summary>
/// Sets the color of <paramref name="node"/> node to <paramref name="color"/>.
/// <br></br>
/// This method does nothing if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to set the color of</param>
/// <param name="color">Color (Boolean)</param>
private static void SetColor(IntervalTreeNode<K, V> node, bool color)
{
if (node != null)
{
node.Color = color;
}
}
/// <summary>
/// This method returns the left node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the left child from</param>
/// <returns>Left child of <paramref name="node"/></returns>
private static IntervalTreeNode<K, V> LeftOf(IntervalTreeNode<K, V> node)
{
return node?.Left;
}
/// <summary>
/// This method returns the right node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the right child from</param>
/// <returns>Right child of <paramref name="node"/></returns>
private static IntervalTreeNode<K, V> RightOf(IntervalTreeNode<K, V> node)
{
return node?.Right;
}
/// <summary>
/// Returns the parent node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the parent from</param>
/// <returns>Parent of <paramref name="node"/></returns>
private static IntervalTreeNode<K, V> ParentOf(IntervalTreeNode<K, V> node)
{
return node?.Parent;
}
#endregion
public bool ContainsKey(K key)
{
@ -745,12 +468,6 @@ namespace Ryujinx.Common.Collections
}
return GetNode(key) != null;
}
public void Clear()
{
_root = null;
_count = 0;
}
}
/// <summary>
@ -777,31 +494,29 @@ namespace Ryujinx.Common.Collections
/// </summary>
/// <typeparam name="K">Key type of the node</typeparam>
/// <typeparam name="V">Value type of the node</typeparam>
class IntervalTreeNode<K, V>
public class IntervalTreeNode<K, V> : IntrusiveRedBlackTreeNode<IntervalTreeNode<K, V>>
{
public bool Color = true;
public IntervalTreeNode<K, V> Left = null;
public IntervalTreeNode<K, V> Right = null;
public IntervalTreeNode<K, V> Parent = null;
/// <summary>
/// The start of the range.
/// </summary>
public K Start;
internal K Start;
/// <summary>
/// The end of the range - maximum of all in the Values list.
/// </summary>
public K End;
internal K End;
/// <summary>
/// The maximum end value of this node and all its children.
/// </summary>
public K Max;
internal K Max;
public List<RangeNode<K, V>> Values;
/// <summary>
/// Values contained on the node that shares a common Start value.
/// </summary>
internal List<RangeNode<K, V>> Values;
public IntervalTreeNode(K start, K end, V value, IntervalTreeNode<K, V> parent)
internal IntervalTreeNode(K start, K end, V value, IntervalTreeNode<K, V> parent)
{
Start = start;
End = end;

View file

@ -0,0 +1,293 @@
using System;
namespace Ryujinx.Common.Collections
{
/// <summary>
/// Tree that provides the ability for O(logN) lookups for keys that exist in the tree, and O(logN) lookups for keys immediately greater than or less than a specified key.
/// </summary>
/// <typeparam name="T">Derived node type</typeparam>
public class IntrusiveRedBlackTree<T> : IntrusiveRedBlackTreeImpl<T> where T : IntrusiveRedBlackTreeNode<T>, IComparable<T>
{
#region Public Methods
/// <summary>
/// Adds a new node into the tree.
/// </summary>
/// <param name="node">Node to be added</param>
/// <exception cref="ArgumentNullException"><paramref name="node"/> is null</exception>
public void Add(T node)
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
Insert(node);
}
/// <summary>
/// Removes a node from the tree.
/// </summary>
/// <param name="node">Note to be removed</param>
/// <exception cref="ArgumentNullException"><paramref name="node"/> is null</exception>
public void Remove(T node)
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
if (Delete(node) != null)
{
Count--;
}
}
/// <summary>
/// Retrieve the node that is considered equal to the specified node by the comparator.
/// </summary>
/// <param name="searchNode">Node to compare with</param>
/// <returns>Node that is equal to <paramref name="searchNode"/></returns>
/// <exception cref="ArgumentNullException"><paramref name="searchNode"/> is null</exception>
public T GetNode(T searchNode)
{
if (searchNode == null)
{
throw new ArgumentNullException(nameof(searchNode));
}
T node = Root;
while (node != null)
{
int cmp = searchNode.CompareTo(node);
if (cmp < 0)
{
node = node.Left;
}
else if (cmp > 0)
{
node = node.Right;
}
else
{
return node;
}
}
return null;
}
#endregion
#region Private Methods (BST)
/// <summary>
/// Inserts a new node into the tree.
/// </summary>
/// <param name="node">Node to be inserted</param>
private void Insert(T node)
{
T newNode = BSTInsert(node);
RestoreBalanceAfterInsertion(newNode);
}
/// <summary>
/// Insertion Mechanism for a Binary Search Tree (BST).
/// <br></br>
/// Iterates the tree starting from the root and inserts a new node
/// where all children in the left subtree are less than <paramref name="newNode"/>,
/// and all children in the right subtree are greater than <paramref name="newNode"/>.
/// </summary>
/// <param name="newNode">Node to be inserted</param>
/// <returns>The inserted Node</returns>
private T BSTInsert(T newNode)
{
T parent = null;
T node = Root;
while (node != null)
{
parent = node;
int cmp = newNode.CompareTo(node);
if (cmp < 0)
{
node = node.Left;
}
else if (cmp > 0)
{
node = node.Right;
}
else
{
return node;
}
}
newNode.Parent = parent;
if (parent == null)
{
Root = newNode;
}
else if (newNode.CompareTo(parent) < 0)
{
parent.Left = newNode;
}
else
{
parent.Right = newNode;
}
Count++;
return newNode;
}
/// <summary>
/// Removes <paramref name="nodeToDelete"/> from the tree, if it exists.
/// </summary>
/// <param name="nodeToDelete">Node to be removed</param>
/// <returns>The deleted Node</returns>
private T Delete(T nodeToDelete)
{
if (nodeToDelete == null)
{
return null;
}
T old = nodeToDelete;
T child;
T parent;
bool color;
if (LeftOf(nodeToDelete) == null)
{
child = RightOf(nodeToDelete);
}
else if (RightOf(nodeToDelete) == null)
{
child = LeftOf(nodeToDelete);
}
else
{
T element = Minimum(RightOf(nodeToDelete));
child = RightOf(element);
parent = ParentOf(element);
color = ColorOf(element);
if (child != null)
{
child.Parent = parent;
}
if (parent == null)
{
Root = child;
}
else if (element == LeftOf(parent))
{
parent.Left = child;
}
else
{
parent.Right = child;
}
if (ParentOf(element) == old)
{
parent = element;
}
element.Color = old.Color;
element.Left = old.Left;
element.Right = old.Right;
element.Parent = old.Parent;
if (ParentOf(old) == null)
{
Root = element;
}
else if (old == LeftOf(ParentOf(old)))
{
ParentOf(old).Left = element;
}
else
{
ParentOf(old).Right = element;
}
LeftOf(old).Parent = element;
if (RightOf(old) != null)
{
RightOf(old).Parent = element;
}
if (child != null && color == Black)
{
RestoreBalanceAfterRemoval(child);
}
return old;
}
parent = ParentOf(nodeToDelete);
color = ColorOf(nodeToDelete);
if (child != null)
{
child.Parent = parent;
}
if (parent == null)
{
Root = child;
}
else if (nodeToDelete == LeftOf(parent))
{
parent.Left = child;
}
else
{
parent.Right = child;
}
if (child != null && color == Black)
{
RestoreBalanceAfterRemoval(child);
}
return old;
}
#endregion
}
public static class IntrusiveRedBlackTreeExtensions
{
/// <summary>
/// Retrieve the node that is considered equal to the key by the comparator.
/// </summary>
/// <param name="tree">Tree to search at</param>
/// <param name="key">Key of the node to be found</param>
/// <returns>Node that is equal to <paramref name="key"/></returns>
public static N GetNodeByKey<N, K>(this IntrusiveRedBlackTree<N> tree, K key)
where N : IntrusiveRedBlackTreeNode<N>, IComparable<N>, IComparable<K>
where K : struct
{
N node = tree.RootNode;
while (node != null)
{
int cmp = node.CompareTo(key);
if (cmp < 0)
{
node = node.Right;
}
else if (cmp > 0)
{
node = node.Left;
}
else
{
return node;
}
}
return null;
}
}
}

View file

@ -0,0 +1,356 @@
using System;
namespace Ryujinx.Common.Collections
{
/// <summary>
/// Tree that provides the ability for O(logN) lookups for keys that exist in the tree, and O(logN) lookups for keys immediately greater than or less than a specified key.
/// </summary>
/// <typeparam name="T">Derived node type</typeparam>
public class IntrusiveRedBlackTreeImpl<T> where T : IntrusiveRedBlackTreeNode<T>
{
protected const bool Black = true;
protected const bool Red = false;
protected T Root = null;
internal T RootNode => Root;
/// <summary>
/// Number of nodes on the tree.
/// </summary>
public int Count { get; protected set; }
/// <summary>
/// Removes all nodes on the tree.
/// </summary>
public void Clear()
{
Root = null;
Count = 0;
}
/// <summary>
/// Finds the node whose key is immediately greater than <paramref name="node"/>.
/// </summary>
/// <param name="node">Node to find the successor of</param>
/// <returns>Successor of <paramref name="node"/></returns>
internal static T SuccessorOf(T node)
{
if (node.Right != null)
{
return Minimum(node.Right);
}
T parent = node.Parent;
while (parent != null && node == parent.Right)
{
node = parent;
parent = parent.Parent;
}
return parent;
}
/// <summary>
/// Finds the node whose key is immediately less than <paramref name="node"/>.
/// </summary>
/// <param name="node">Node to find the predecessor of</param>
/// <returns>Predecessor of <paramref name="node"/></returns>
internal static T PredecessorOf(T node)
{
if (node.Left != null)
{
return Maximum(node.Left);
}
T parent = node.Parent;
while (parent != null && node == parent.Left)
{
node = parent;
parent = parent.Parent;
}
return parent;
}
/// <summary>
/// Returns the node with the largest key where <paramref name="node"/> is considered the root node.
/// </summary>
/// <param name="node">Root node</param>
/// <returns>Node with the maximum key in the tree of <paramref name="node"/></returns>
protected static T Maximum(T node)
{
T tmp = node;
while (tmp.Right != null)
{
tmp = tmp.Right;
}
return tmp;
}
/// <summary>
/// Returns the node with the smallest key where <paramref name="node"/> is considered the root node.
/// </summary>
/// <param name="node">Root node</param>
/// <returns>Node with the minimum key in the tree of <paramref name="node"/></returns>
/// <exception cref="ArgumentNullException"><paramref name="node"/> is null</exception>
protected static T Minimum(T node)
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
T tmp = node;
while (tmp.Left != null)
{
tmp = tmp.Left;
}
return tmp;
}
protected void RestoreBalanceAfterRemoval(T balanceNode)
{
T ptr = balanceNode;
while (ptr != Root && ColorOf(ptr) == Black)
{
if (ptr == LeftOf(ParentOf(ptr)))
{
T sibling = RightOf(ParentOf(ptr));
if (ColorOf(sibling) == Red)
{
SetColor(sibling, Black);
SetColor(ParentOf(ptr), Red);
RotateLeft(ParentOf(ptr));
sibling = RightOf(ParentOf(ptr));
}
if (ColorOf(LeftOf(sibling)) == Black && ColorOf(RightOf(sibling)) == Black)
{
SetColor(sibling, Red);
ptr = ParentOf(ptr);
}
else
{
if (ColorOf(RightOf(sibling)) == Black)
{
SetColor(LeftOf(sibling), Black);
SetColor(sibling, Red);
RotateRight(sibling);
sibling = RightOf(ParentOf(ptr));
}
SetColor(sibling, ColorOf(ParentOf(ptr)));
SetColor(ParentOf(ptr), Black);
SetColor(RightOf(sibling), Black);
RotateLeft(ParentOf(ptr));
ptr = Root;
}
}
else
{
T sibling = LeftOf(ParentOf(ptr));
if (ColorOf(sibling) == Red)
{
SetColor(sibling, Black);
SetColor(ParentOf(ptr), Red);
RotateRight(ParentOf(ptr));
sibling = LeftOf(ParentOf(ptr));
}
if (ColorOf(RightOf(sibling)) == Black && ColorOf(LeftOf(sibling)) == Black)
{
SetColor(sibling, Red);
ptr = ParentOf(ptr);
}
else
{
if (ColorOf(LeftOf(sibling)) == Black)
{
SetColor(RightOf(sibling), Black);
SetColor(sibling, Red);
RotateLeft(sibling);
sibling = LeftOf(ParentOf(ptr));
}
SetColor(sibling, ColorOf(ParentOf(ptr)));
SetColor(ParentOf(ptr), Black);
SetColor(LeftOf(sibling), Black);
RotateRight(ParentOf(ptr));
ptr = Root;
}
}
}
SetColor(ptr, Black);
}
protected void RestoreBalanceAfterInsertion(T balanceNode)
{
SetColor(balanceNode, Red);
while (balanceNode != null && balanceNode != Root && ColorOf(ParentOf(balanceNode)) == Red)
{
if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode))))
{
T sibling = RightOf(ParentOf(ParentOf(balanceNode)));
if (ColorOf(sibling) == Red)
{
SetColor(ParentOf(balanceNode), Black);
SetColor(sibling, Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
balanceNode = ParentOf(ParentOf(balanceNode));
}
else
{
if (balanceNode == RightOf(ParentOf(balanceNode)))
{
balanceNode = ParentOf(balanceNode);
RotateLeft(balanceNode);
}
SetColor(ParentOf(balanceNode), Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
RotateRight(ParentOf(ParentOf(balanceNode)));
}
}
else
{
T sibling = LeftOf(ParentOf(ParentOf(balanceNode)));
if (ColorOf(sibling) == Red)
{
SetColor(ParentOf(balanceNode), Black);
SetColor(sibling, Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
balanceNode = ParentOf(ParentOf(balanceNode));
}
else
{
if (balanceNode == LeftOf(ParentOf(balanceNode)))
{
balanceNode = ParentOf(balanceNode);
RotateRight(balanceNode);
}
SetColor(ParentOf(balanceNode), Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
RotateLeft(ParentOf(ParentOf(balanceNode)));
}
}
}
SetColor(Root, Black);
}
protected virtual void RotateLeft(T node)
{
if (node != null)
{
T right = RightOf(node);
node.Right = LeftOf(right);
if (node.Right != null)
{
node.Right.Parent = node;
}
T nodeParent = ParentOf(node);
right.Parent = nodeParent;
if (nodeParent == null)
{
Root = right;
}
else if (node == LeftOf(nodeParent))
{
nodeParent.Left = right;
}
else
{
nodeParent.Right = right;
}
right.Left = node;
node.Parent = right;
}
}
protected virtual void RotateRight(T node)
{
if (node != null)
{
T left = LeftOf(node);
node.Left = RightOf(left);
if (node.Left != null)
{
node.Left.Parent = node;
}
T nodeParent = ParentOf(node);
left.Parent = nodeParent;
if (nodeParent == null)
{
Root = left;
}
else if (node == RightOf(nodeParent))
{
nodeParent.Right = left;
}
else
{
nodeParent.Left = left;
}
left.Right = node;
node.Parent = left;
}
}
#region Safety-Methods
// These methods save memory by allowing us to forego sentinel nil nodes, as well as serve as protection against NullReferenceExceptions.
/// <summary>
/// Returns the color of <paramref name="node"/>, or Black if it is null.
/// </summary>
/// <param name="node">Node</param>
/// <returns>The boolean color of <paramref name="node"/>, or black if null</returns>
protected static bool ColorOf(T node)
{
return node == null || node.Color;
}
/// <summary>
/// Sets the color of <paramref name="node"/> node to <paramref name="color"/>.
/// <br></br>
/// This method does nothing if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to set the color of</param>
/// <param name="color">Color (Boolean)</param>
protected static void SetColor(T node, bool color)
{
if (node != null)
{
node.Color = color;
}
}
/// <summary>
/// This method returns the left node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the left child from</param>
/// <returns>Left child of <paramref name="node"/></returns>
protected static T LeftOf(T node)
{
return node?.Left;
}
/// <summary>
/// This method returns the right node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the right child from</param>
/// <returns>Right child of <paramref name="node"/></returns>
protected static T RightOf(T node)
{
return node?.Right;
}
/// <summary>
/// Returns the parent node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the parent from</param>
/// <returns>Parent of <paramref name="node"/></returns>
protected static T ParentOf(T node)
{
return node?.Parent;
}
#endregion
}
}

View file

@ -0,0 +1,16 @@
namespace Ryujinx.Common.Collections
{
/// <summary>
/// Represents a node in the Red-Black Tree.
/// </summary>
public class IntrusiveRedBlackTreeNode<T> where T : IntrusiveRedBlackTreeNode<T>
{
public bool Color = true;
public T Left;
public T Right;
public T Parent;
public T Predecessor => IntrusiveRedBlackTreeImpl<T>.PredecessorOf((T)this);
public T Successor => IntrusiveRedBlackTreeImpl<T>.SuccessorOf((T)this);
}
}

View file

@ -10,14 +10,8 @@ namespace Ryujinx.Common.Collections
/// </summary>
/// <typeparam name="K">Key</typeparam>
/// <typeparam name="V">Value</typeparam>
public class TreeDictionary<K, V> : IDictionary<K, V> where K : IComparable<K>
public class TreeDictionary<K, V> : IntrusiveRedBlackTreeImpl<Node<K, V>>, IDictionary<K, V> where K : IComparable<K>
{
private const bool Black = true;
private const bool Red = false;
private Node<K, V> _root = null;
private int _count = 0;
public TreeDictionary() { }
#region Public Methods
/// <summary>
@ -57,7 +51,7 @@ namespace Ryujinx.Common.Collections
{
throw new ArgumentNullException(nameof(key));
}
if (null == value)
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
@ -78,7 +72,7 @@ namespace Ryujinx.Common.Collections
}
if (Delete(key) != null)
{
_count--;
Count--;
}
}
@ -160,13 +154,12 @@ namespace Ryujinx.Common.Collections
Queue<Node<K, V>> nodes = new Queue<Node<K, V>>();
if (this._root != null)
if (this.Root != null)
{
nodes.Enqueue(this._root);
nodes.Enqueue(this.Root);
}
while (nodes.Count > 0)
while (nodes.TryDequeue(out Node<K, V> node))
{
Node<K, V> node = nodes.Dequeue();
list.Add(new KeyValuePair<K, V>(node.Key, node.Value));
if (node.Left != null)
{
@ -188,7 +181,7 @@ namespace Ryujinx.Common.Collections
{
List<KeyValuePair<K, V>> list = new List<KeyValuePair<K, V>>();
AddToList(_root, list);
AddToList(Root, list);
return list;
}
@ -229,7 +222,7 @@ namespace Ryujinx.Common.Collections
throw new ArgumentNullException(nameof(key));
}
Node<K, V> node = _root;
Node<K, V> node = Root;
while (node != null)
{
int cmp = key.CompareTo(node.Key);
@ -275,7 +268,7 @@ namespace Ryujinx.Common.Collections
private Node<K, V> BSTInsert(K key, V value)
{
Node<K, V> parent = null;
Node<K, V> node = _root;
Node<K, V> node = Root;
while (node != null)
{
@ -298,7 +291,7 @@ namespace Ryujinx.Common.Collections
Node<K, V> newNode = new Node<K, V>(key, value, parent);
if (newNode.Parent == null)
{
_root = newNode;
Root = newNode;
}
else if (key.CompareTo(parent.Key) < 0)
{
@ -308,7 +301,7 @@ namespace Ryujinx.Common.Collections
{
parent.Right = newNode;
}
_count++;
Count++;
return newNode;
}
@ -344,9 +337,8 @@ namespace Ryujinx.Common.Collections
if (ParentOf(replacementNode) == null)
{
_root = tmp;
Root = tmp;
}
else if (replacementNode == LeftOf(ParentOf(replacementNode)))
{
ParentOf(replacementNode).Left = tmp;
@ -370,43 +362,6 @@ namespace Ryujinx.Common.Collections
return replacementNode;
}
/// <summary>
/// Returns the node with the largest key where <paramref name="node"/> is considered the root node.
/// </summary>
/// <param name="node">Root Node</param>
/// <returns>Node with the maximum key in the tree of <paramref name="node"/></returns>
private static Node<K, V> Maximum(Node<K, V> node)
{
Node<K, V> tmp = node;
while (tmp.Right != null)
{
tmp = tmp.Right;
}
return tmp;
}
/// <summary>
/// Returns the node with the smallest key where <paramref name="node"/> is considered the root node.
/// </summary>
/// <param name="node">Root Node</param>
/// <returns>Node with the minimum key in the tree of <paramref name="node"/></returns>
///<exception cref="ArgumentNullException"><paramref name="node"/> is null</exception>
private static Node<K, V> Minimum(Node<K, V> node)
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
Node<K, V> tmp = node;
while (tmp.Left != null)
{
tmp = tmp.Left;
}
return tmp;
}
/// <summary>
/// Returns the node whose key immediately less than or equal to <paramref name="key"/>.
/// </summary>
@ -419,7 +374,7 @@ namespace Ryujinx.Common.Collections
{
throw new ArgumentNullException(nameof(key));
}
Node<K, V> tmp = _root;
Node<K, V> tmp = Root;
while (tmp != null)
{
@ -473,7 +428,7 @@ namespace Ryujinx.Common.Collections
{
throw new ArgumentNullException(nameof(key));
}
Node<K, V> tmp = _root;
Node<K, V> tmp = Root;
while (tmp != null)
{
@ -515,294 +470,6 @@ namespace Ryujinx.Common.Collections
return null;
}
/// <summary>
/// Finds the node with the key is immediately greater than <paramref name="node"/>.
/// </summary>
/// <param name="node">Node to find the successor of</param>
/// <returns>Successor of <paramref name="node"/></returns>
private static Node<K, V> SuccessorOf(Node<K, V> node)
{
if (node.Right != null)
{
return Minimum(node.Right);
}
Node<K, V> parent = node.Parent;
while (parent != null && node == parent.Right)
{
node = parent;
parent = parent.Parent;
}
return parent;
}
/// <summary>
/// Finds the node whose key is immediately less than <paramref name="node"/>.
/// </summary>
/// <param name="node">Node to find the predecessor of</param>
/// <returns>Predecessor of <paramref name="node"/></returns>
private static Node<K, V> PredecessorOf(Node<K, V> node)
{
if (node.Left != null)
{
return Maximum(node.Left);
}
Node<K, V> parent = node.Parent;
while (parent != null && node == parent.Left)
{
node = parent;
parent = parent.Parent;
}
return parent;
}
#endregion
#region Private Methods (RBL)
private void RestoreBalanceAfterRemoval(Node<K, V> balanceNode)
{
Node<K, V> ptr = balanceNode;
while (ptr != _root && ColorOf(ptr) == Black)
{
if (ptr == LeftOf(ParentOf(ptr)))
{
Node<K, V> sibling = RightOf(ParentOf(ptr));
if (ColorOf(sibling) == Red)
{
SetColor(sibling, Black);
SetColor(ParentOf(ptr), Red);
RotateLeft(ParentOf(ptr));
sibling = RightOf(ParentOf(ptr));
}
if (ColorOf(LeftOf(sibling)) == Black && ColorOf(RightOf(sibling)) == Black)
{
SetColor(sibling, Red);
ptr = ParentOf(ptr);
}
else
{
if (ColorOf(RightOf(sibling)) == Black)
{
SetColor(LeftOf(sibling), Black);
SetColor(sibling, Red);
RotateRight(sibling);
sibling = RightOf(ParentOf(ptr));
}
SetColor(sibling, ColorOf(ParentOf(ptr)));
SetColor(ParentOf(ptr), Black);
SetColor(RightOf(sibling), Black);
RotateLeft(ParentOf(ptr));
ptr = _root;
}
}
else
{
Node<K, V> sibling = LeftOf(ParentOf(ptr));
if (ColorOf(sibling) == Red)
{
SetColor(sibling, Black);
SetColor(ParentOf(ptr), Red);
RotateRight(ParentOf(ptr));
sibling = LeftOf(ParentOf(ptr));
}
if (ColorOf(RightOf(sibling)) == Black && ColorOf(LeftOf(sibling)) == Black)
{
SetColor(sibling, Red);
ptr = ParentOf(ptr);
}
else
{
if (ColorOf(LeftOf(sibling)) == Black)
{
SetColor(RightOf(sibling), Black);
SetColor(sibling, Red);
RotateLeft(sibling);
sibling = LeftOf(ParentOf(ptr));
}
SetColor(sibling, ColorOf(ParentOf(ptr)));
SetColor(ParentOf(ptr), Black);
SetColor(LeftOf(sibling), Black);
RotateRight(ParentOf(ptr));
ptr = _root;
}
}
}
SetColor(ptr, Black);
}
private void RestoreBalanceAfterInsertion(Node<K, V> balanceNode)
{
SetColor(balanceNode, Red);
while (balanceNode != null && balanceNode != _root && ColorOf(ParentOf(balanceNode)) == Red)
{
if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode))))
{
Node<K, V> sibling = RightOf(ParentOf(ParentOf(balanceNode)));
if (ColorOf(sibling) == Red)
{
SetColor(ParentOf(balanceNode), Black);
SetColor(sibling, Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
balanceNode = ParentOf(ParentOf(balanceNode));
}
else
{
if (balanceNode == RightOf(ParentOf(balanceNode)))
{
balanceNode = ParentOf(balanceNode);
RotateLeft(balanceNode);
}
SetColor(ParentOf(balanceNode), Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
RotateRight(ParentOf(ParentOf(balanceNode)));
}
}
else
{
Node<K, V> sibling = LeftOf(ParentOf(ParentOf(balanceNode)));
if (ColorOf(sibling) == Red)
{
SetColor(ParentOf(balanceNode), Black);
SetColor(sibling, Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
balanceNode = ParentOf(ParentOf(balanceNode));
}
else
{
if (balanceNode == LeftOf(ParentOf(balanceNode)))
{
balanceNode = ParentOf(balanceNode);
RotateRight(balanceNode);
}
SetColor(ParentOf(balanceNode), Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
RotateLeft(ParentOf(ParentOf(balanceNode)));
}
}
}
SetColor(_root, Black);
}
private void RotateLeft(Node<K, V> node)
{
if (node != null)
{
Node<K, V> right = RightOf(node);
node.Right = LeftOf(right);
if (LeftOf(right) != null)
{
LeftOf(right).Parent = node;
}
right.Parent = ParentOf(node);
if (ParentOf(node) == null)
{
_root = right;
}
else if (node == LeftOf(ParentOf(node)))
{
ParentOf(node).Left = right;
}
else
{
ParentOf(node).Right = right;
}
right.Left = node;
node.Parent = right;
}
}
private void RotateRight(Node<K, V> node)
{
if (node != null)
{
Node<K, V> left = LeftOf(node);
node.Left = RightOf(left);
if (RightOf(left) != null)
{
RightOf(left).Parent = node;
}
left.Parent = node.Parent;
if (ParentOf(node) == null)
{
_root = left;
}
else if (node == RightOf(ParentOf(node)))
{
ParentOf(node).Right = left;
}
else
{
ParentOf(node).Left = left;
}
left.Right = node;
node.Parent = left;
}
}
#endregion
#region Safety-Methods
// These methods save memory by allowing us to forego sentinel nil nodes, as well as serve as protection against NullReferenceExceptions.
/// <summary>
/// Returns the color of <paramref name="node"/>, or Black if it is null.
/// </summary>
/// <param name="node">Node</param>
/// <returns>The boolean color of <paramref name="node"/>, or black if null</returns>
private static bool ColorOf(Node<K, V> node)
{
return node == null || node.Color;
}
/// <summary>
/// Sets the color of <paramref name="node"/> node to <paramref name="color"/>.
/// <br></br>
/// This method does nothing if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to set the color of</param>
/// <param name="color">Color (Boolean)</param>
private static void SetColor(Node<K, V> node, bool color)
{
if (node != null)
{
node.Color = color;
}
}
/// <summary>
/// This method returns the left node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the left child from</param>
/// <returns>Left child of <paramref name="node"/></returns>
private static Node<K, V> LeftOf(Node<K, V> node)
{
return node?.Left;
}
/// <summary>
/// This method returns the right node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the right child from</param>
/// <returns>Right child of <paramref name="node"/></returns>
private static Node<K, V> RightOf(Node<K, V> node)
{
return node?.Right;
}
/// <summary>
/// Returns the parent node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the parent from</param>
/// <returns>Parent of <paramref name="node"/></returns>
private static Node<K, V> ParentOf(Node<K, V> node)
{
return node?.Parent;
}
#endregion
#region Interface Implementations
@ -819,9 +486,9 @@ namespace Ryujinx.Common.Collections
bool IDictionary<K, V>.Remove(K key)
{
int count = _count;
int count = Count;
Remove(key);
return count > _count;
return count > Count;
}
public bool TryGetValue(K key, [MaybeNullWhen(false)] out V value)
@ -845,12 +512,6 @@ namespace Ryujinx.Common.Collections
Add(item.Key, item.Value);
}
public void Clear()
{
_root = null;
_count = 0;
}
public bool Contains(KeyValuePair<K, V> item)
{
if (item.Key == null)
@ -895,9 +556,9 @@ namespace Ryujinx.Common.Collections
if (node.Value.Equals(item.Value))
{
int count = _count;
int count = Count;
Remove(item.Key);
return count > _count;
return count > Count;
}
return false;
@ -913,8 +574,6 @@ namespace Ryujinx.Common.Collections
return GetKeyValues().GetEnumerator();
}
public int Count => _count;
public ICollection<K> Keys => GetKeyValues().Keys;
public ICollection<V> Values => GetKeyValues().Values;
@ -928,6 +587,7 @@ namespace Ryujinx.Common.Collections
}
#endregion
#region Private Interface Helper Methods
/// <summary>
@ -938,14 +598,13 @@ namespace Ryujinx.Common.Collections
{
SortedList<K, V> set = new SortedList<K, V>();
Queue<Node<K, V>> queue = new Queue<Node<K, V>>();
if (_root != null)
if (Root != null)
{
queue.Enqueue(_root);
queue.Enqueue(Root);
}
while (queue.Count > 0)
while (queue.TryDequeue(out Node<K, V> node))
{
Node<K, V> node = queue.Dequeue();
set.Add(node.Key, node.Value);
if (null != node.Left)
{
@ -959,6 +618,7 @@ namespace Ryujinx.Common.Collections
return set;
}
#endregion
}
@ -967,16 +627,12 @@ namespace Ryujinx.Common.Collections
/// </summary>
/// <typeparam name="K">Key of the node</typeparam>
/// <typeparam name="V">Value of the node</typeparam>
class Node<K, V>
public class Node<K, V> : IntrusiveRedBlackTreeNode<Node<K, V>> where K : IComparable<K>
{
public bool Color = true;
public Node<K, V> Left = null;
public Node<K, V> Right = null;
public Node<K, V> Parent = null;
public K Key;
public V Value;
internal K Key;
internal V Value;
public Node(K key, V value, Node<K, V> parent)
internal Node(K key, V value, Node<K, V> parent)
{
Key = key;
Value = value;

View file

@ -1,8 +1,9 @@
using Ryujinx.Common.Collections;
using System;
namespace Ryujinx.HLE.HOS.Kernel.Memory
{
class KMemoryBlock
class KMemoryBlock : IntrusiveRedBlackTreeNode<KMemoryBlock>, IComparable<KMemoryBlock>, IComparable<ulong>
{
public ulong BaseAddress { get; private set; }
public ulong PagesCount { get; private set; }
@ -119,5 +120,37 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
IpcRefCount,
DeviceRefCount);
}
public int CompareTo(KMemoryBlock other)
{
if (BaseAddress < other.BaseAddress)
{
return -1;
}
else if (BaseAddress <= other.BaseAddress + other.PagesCount * KPageTableBase.PageSize - 1UL)
{
return 0;
}
else
{
return 1;
}
}
public int CompareTo(ulong address)
{
if (address < BaseAddress)
{
return 1;
}
else if (address <= BaseAddress + PagesCount * KPageTableBase.PageSize - 1UL)
{
return 0;
}
else
{
return -1;
}
}
}
}

View file

@ -1,5 +1,5 @@
using Ryujinx.HLE.HOS.Kernel.Common;
using System.Collections.Generic;
using Ryujinx.Common.Collections;
using Ryujinx.HLE.HOS.Kernel.Common;
using System.Diagnostics;
namespace Ryujinx.HLE.HOS.Kernel.Memory
@ -8,26 +8,27 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{
private const int PageSize = KPageTableBase.PageSize;
private readonly LinkedList<KMemoryBlock> _blocks;
private readonly IntrusiveRedBlackTree<KMemoryBlock> _blockTree;
public int BlocksCount => _blocks.Count;
public int BlocksCount => _blockTree.Count;
private KMemoryBlockSlabManager _slabManager;
private ulong _addrSpaceStart;
private ulong _addrSpaceEnd;
public KMemoryBlockManager()
{
_blocks = new LinkedList<KMemoryBlock>();
_blockTree = new IntrusiveRedBlackTree<KMemoryBlock>();
}
public KernelResult Initialize(ulong addrSpaceStart, ulong addrSpaceEnd, KMemoryBlockSlabManager slabManager)
{
_slabManager = slabManager;
_addrSpaceStart = addrSpaceStart;
_addrSpaceEnd = addrSpaceEnd;
// First insertion will always need only a single block,
// because there's nothing else to split.
// First insertion will always need only a single block, because there's nothing to split.
if (!slabManager.CanAllocate(1))
{
return KernelResult.OutOfResource;
@ -35,7 +36,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
ulong addrSpacePagesCount = (addrSpaceEnd - addrSpaceStart) / PageSize;
_blocks.AddFirst(new KMemoryBlock(
_blockTree.Add(new KMemoryBlock(
addrSpaceStart,
addrSpacePagesCount,
MemoryState.Unmapped,
@ -58,20 +59,17 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
// Insert new block on the list only on areas where the state
// of the block matches the state specified on the old* state
// arguments, otherwise leave it as is.
int oldCount = _blocks.Count;
int oldCount = _blockTree.Count;
oldAttribute |= MemoryAttribute.IpcAndDeviceMapped;
ulong endAddr = baseAddress + pagesCount * PageSize;
LinkedListNode<KMemoryBlock> node = _blocks.First;
KMemoryBlock currBlock = FindBlock(baseAddress);
while (node != null)
while (currBlock != null)
{
LinkedListNode<KMemoryBlock> newNode = node;
KMemoryBlock currBlock = node.Value;
ulong currBaseAddr = currBlock.BaseAddress;
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
@ -83,24 +81,27 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
currBlock.Permission != oldPermission ||
currBlockAttr != oldAttribute)
{
node = node.Next;
currBlock = currBlock.Successor;
continue;
}
if (baseAddress > currBaseAddr)
{
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
KMemoryBlock newBlock = currBlock.SplitRightAtAddress(baseAddress);
_blockTree.Add(newBlock);
}
if (endAddr < currEndAddr)
{
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
KMemoryBlock newBlock = currBlock.SplitRightAtAddress(endAddr);
_blockTree.Add(newBlock);
currBlock = newBlock;
}
newNode.Value.SetState(newPermission, newState, newAttribute);
currBlock.SetState(newPermission, newState, newAttribute);
newNode = MergeEqualStateNeighbors(newNode);
currBlock = MergeEqualStateNeighbors(currBlock);
}
if (currEndAddr - 1 >= endAddr - 1)
@ -108,10 +109,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
break;
}
node = newNode.Next;
currBlock = currBlock.Successor;
}
_slabManager.Count += _blocks.Count - oldCount;
_slabManager.Count += _blockTree.Count - oldCount;
ValidateInternalState();
}
@ -125,18 +126,15 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{
// Inserts new block at the list, replacing and splitting
// existing blocks as needed.
int oldCount = _blocks.Count;
int oldCount = _blockTree.Count;
ulong endAddr = baseAddress + pagesCount * PageSize;
LinkedListNode<KMemoryBlock> node = _blocks.First;
KMemoryBlock currBlock = FindBlock(baseAddress);
while (node != null)
while (currBlock != null)
{
LinkedListNode<KMemoryBlock> newNode = node;
KMemoryBlock currBlock = node.Value;
ulong currBaseAddr = currBlock.BaseAddress;
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
@ -144,17 +142,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{
if (baseAddress > currBaseAddr)
{
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
KMemoryBlock newBlock = currBlock.SplitRightAtAddress(baseAddress);
_blockTree.Add(newBlock);
}
if (endAddr < currEndAddr)
{
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
KMemoryBlock newBlock = currBlock.SplitRightAtAddress(endAddr);
_blockTree.Add(newBlock);
currBlock = newBlock;
}
newNode.Value.SetState(permission, state, attribute);
currBlock.SetState(permission, state, attribute);
newNode = MergeEqualStateNeighbors(newNode);
currBlock = MergeEqualStateNeighbors(currBlock);
}
if (currEndAddr - 1 >= endAddr - 1)
@ -162,10 +163,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
break;
}
node = newNode.Next;
currBlock = currBlock.Successor;
}
_slabManager.Count += _blocks.Count - oldCount;
_slabManager.Count += _blockTree.Count - oldCount;
ValidateInternalState();
}
@ -181,18 +182,15 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
// Inserts new block at the list, replacing and splitting
// existing blocks as needed, then calling the callback
// function on the new block.
int oldCount = _blocks.Count;
int oldCount = _blockTree.Count;
ulong endAddr = baseAddress + pagesCount * PageSize;
LinkedListNode<KMemoryBlock> node = _blocks.First;
KMemoryBlock currBlock = FindBlock(baseAddress);
while (node != null)
while (currBlock != null)
{
LinkedListNode<KMemoryBlock> newNode = node;
KMemoryBlock currBlock = node.Value;
ulong currBaseAddr = currBlock.BaseAddress;
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
@ -200,19 +198,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{
if (baseAddress > currBaseAddr)
{
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
KMemoryBlock newBlock = currBlock.SplitRightAtAddress(baseAddress);
_blockTree.Add(newBlock);
}
if (endAddr < currEndAddr)
{
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
KMemoryBlock newBlock = currBlock.SplitRightAtAddress(endAddr);
_blockTree.Add(newBlock);
currBlock = newBlock;
}
KMemoryBlock newBlock = newNode.Value;
blockMutate(currBlock, permission);
blockMutate(newBlock, permission);
newNode = MergeEqualStateNeighbors(newNode);
currBlock = MergeEqualStateNeighbors(currBlock);
}
if (currEndAddr - 1 >= endAddr - 1)
@ -220,10 +219,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
break;
}
node = newNode.Next;
currBlock = currBlock.Successor;
}
_slabManager.Count += _blocks.Count - oldCount;
_slabManager.Count += _blockTree.Count - oldCount;
ValidateInternalState();
}
@ -233,58 +232,42 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{
ulong expectedAddress = 0;
LinkedListNode<KMemoryBlock> node = _blocks.First;
KMemoryBlock currBlock = FindBlock(_addrSpaceStart);
while (node != null)
while (currBlock != null)
{
LinkedListNode<KMemoryBlock> newNode = node;
KMemoryBlock currBlock = node.Value;
Debug.Assert(currBlock.BaseAddress == expectedAddress);
expectedAddress = currBlock.BaseAddress + currBlock.PagesCount * PageSize;
node = newNode.Next;
currBlock = currBlock.Successor;
}
Debug.Assert(expectedAddress == _addrSpaceEnd);
}
private LinkedListNode<KMemoryBlock> MergeEqualStateNeighbors(LinkedListNode<KMemoryBlock> node)
private KMemoryBlock MergeEqualStateNeighbors(KMemoryBlock block)
{
KMemoryBlock block = node.Value;
KMemoryBlock previousBlock = block.Predecessor;
KMemoryBlock nextBlock = block.Successor;
if (node.Previous != null)
if (previousBlock != null && BlockStateEquals(block, previousBlock))
{
KMemoryBlock previousBlock = node.Previous.Value;
if (BlockStateEquals(block, previousBlock))
{
LinkedListNode<KMemoryBlock> previousNode = node.Previous;
_blocks.Remove(node);
_blockTree.Remove(block);
previousBlock.AddPages(block.PagesCount);
node = previousNode;
block = previousBlock;
}
}
if (node.Next != null)
if (nextBlock != null && BlockStateEquals(block, nextBlock))
{
KMemoryBlock nextBlock = node.Next.Value;
if (BlockStateEquals(block, nextBlock))
{
_blocks.Remove(node.Next);
_blockTree.Remove(nextBlock);
block.AddPages(nextBlock.PagesCount);
}
}
return node;
return block;
}
private static bool BlockStateEquals(KMemoryBlock lhs, KMemoryBlock rhs)
@ -299,31 +282,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
public KMemoryBlock FindBlock(ulong address)
{
return FindBlockNode(address)?.Value;
}
public LinkedListNode<KMemoryBlock> FindBlockNode(ulong address)
{
lock (_blocks)
{
LinkedListNode<KMemoryBlock> node = _blocks.First;
while (node != null)
{
KMemoryBlock block = node.Value;
ulong currEndAddr = block.PagesCount * PageSize + block.BaseAddress;
if (block.BaseAddress <= address && currEndAddr - 1 >= address)
{
return node;
}
node = node.Next;
}
}
return null;
return _blockTree.GetNodeByKey(address);
}
}
}

View file

@ -2447,9 +2447,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{
ulong endAddr = address + size;
LinkedListNode<KMemoryBlock> node = _blockManager.FindBlockNode(address);
KMemoryBlock currBlock = _blockManager.FindBlock(address);
KMemoryInfo info = node.Value.GetInfo();
KMemoryInfo info = currBlock.GetInfo();
MemoryState firstState = info.State;
KMemoryPermission firstPermission = info.Permission;
@ -2457,7 +2457,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
do
{
info = node.Value.GetInfo();
info = currBlock.GetInfo();
// Check if the block state matches what we expect.
if (firstState != info.State ||
@ -2474,7 +2474,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
return false;
}
}
while (info.Address + info.Size - 1 < endAddr - 1 && (node = node.Next) != null);
while (info.Address + info.Size - 1 < endAddr - 1 && (currBlock = currBlock.Successor) != null);
outState = firstState;
outPermission = firstPermission;
@ -2509,17 +2509,17 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
private IEnumerable<KMemoryInfo> IterateOverRange(ulong start, ulong end)
{
LinkedListNode<KMemoryBlock> node = _blockManager.FindBlockNode(start);
KMemoryBlock currBlock = _blockManager.FindBlock(start);
KMemoryInfo info;
do
{
info = node.Value.GetInfo();
info = currBlock.GetInfo();
yield return info;
}
while (info.Address + info.Size - 1 < end - 1 && (node = node.Next) != null);
while (info.Address + info.Size - 1 < end - 1 && (currBlock = currBlock.Successor) != null);
}
private ulong AllocateVa(ulong regionStart, ulong regionPagesCount, ulong neededPagesCount, int alignment)
@ -2605,9 +2605,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
ulong regionEndAddr = regionStart + regionPagesCount * PageSize;
LinkedListNode<KMemoryBlock> node = _blockManager.FindBlockNode(regionStart);
KMemoryBlock currBlock = _blockManager.FindBlock(regionStart);
KMemoryInfo info = node.Value.GetInfo();
KMemoryInfo info = currBlock.GetInfo();
while (regionEndAddr >= info.Address)
{
@ -2636,14 +2636,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
}
}
node = node.Next;
currBlock = currBlock.Successor;
if (node == null)
if (currBlock == null)
{
break;
}
info = node.Value.GetInfo();
info = currBlock.GetInfo();
}
return 0;

View file

@ -1,3 +1,4 @@
using Ryujinx.Common.Collections;
using System;
using System.Collections.Generic;
@ -8,19 +9,10 @@ namespace Ryujinx.Memory.WindowsShared
/// </summary>
/// <typeparam name="K">Key</typeparam>
/// <typeparam name="V">Value</typeparam>
class IntervalTree<K, V> where K : IComparable<K>
class IntervalTree<K, V> : IntrusiveRedBlackTreeImpl<IntervalTreeNode<K, V>> where K : IComparable<K>
{
private const int ArrayGrowthSize = 32;
private const bool Black = true;
private const bool Red = false;
private IntervalTreeNode<K, V> _root = null;
private int _count = 0;
public int Count => _count;
public IntervalTree() { }
#region Public Methods
/// <summary>
@ -53,7 +45,7 @@ namespace Ryujinx.Memory.WindowsShared
/// <returns>Number of intervals found</returns>
public int Get(K start, K end, ref IntervalTreeNode<K, V>[] overlaps, int overlapCount = 0)
{
GetNodes(_root, start, end, ref overlaps, ref overlapCount);
GetNodes(Root, start, end, ref overlaps, ref overlapCount);
return overlapCount;
}
@ -99,7 +91,7 @@ namespace Ryujinx.Memory.WindowsShared
Delete(nodeToDelete);
_count--;
Count--;
return 1;
}
@ -112,7 +104,7 @@ namespace Ryujinx.Memory.WindowsShared
{
List<V> list = new List<V>();
AddToList(_root, list);
AddToList(Root, list);
return list;
}
@ -153,7 +145,7 @@ namespace Ryujinx.Memory.WindowsShared
throw new ArgumentNullException(nameof(key));
}
IntervalTreeNode<K, V> node = _root;
IntervalTreeNode<K, V> node = Root;
while (node != null)
{
int cmp = key.CompareTo(node.Start);
@ -271,7 +263,7 @@ namespace Ryujinx.Memory.WindowsShared
private bool BSTInsert(K start, K end, V value, Func<K, V, V> updateFactoryCallback, out IntervalTreeNode<K, V> outNode)
{
IntervalTreeNode<K, V> parent = null;
IntervalTreeNode<K, V> node = _root;
IntervalTreeNode<K, V> node = Root;
while (node != null)
{
@ -319,7 +311,7 @@ namespace Ryujinx.Memory.WindowsShared
IntervalTreeNode<K, V> newNode = new IntervalTreeNode<K, V>(start, end, value, parent);
if (newNode.Parent == null)
{
_root = newNode;
Root = newNode;
}
else if (start.CompareTo(parent.Start) < 0)
{
@ -331,7 +323,7 @@ namespace Ryujinx.Memory.WindowsShared
}
PropagateIncrease(newNode);
_count++;
Count++;
RestoreBalanceAfterInsertion(newNode);
outNode = newNode;
return true;
@ -351,7 +343,7 @@ namespace Ryujinx.Memory.WindowsShared
}
else
{
replacementNode = PredecessorOf(nodeToDelete);
replacementNode = nodeToDelete.Predecessor;
}
IntervalTreeNode<K, V> tmp = LeftOf(replacementNode) ?? RightOf(replacementNode);
@ -363,7 +355,7 @@ namespace Ryujinx.Memory.WindowsShared
if (ParentOf(replacementNode) == null)
{
_root = tmp;
Root = tmp;
}
else if (replacementNode == LeftOf(ParentOf(replacementNode)))
{
@ -390,232 +382,25 @@ namespace Ryujinx.Memory.WindowsShared
}
}
/// <summary>
/// Returns the node with the largest key where <paramref name="node"/> is considered the root node.
/// </summary>
/// <param name="node">Root Node</param>
/// <returns>Node with the maximum key in the tree of <paramref name="node"/></returns>
private static IntervalTreeNode<K, V> Maximum(IntervalTreeNode<K, V> node)
{
IntervalTreeNode<K, V> tmp = node;
while (tmp.Right != null)
{
tmp = tmp.Right;
}
return tmp;
}
/// <summary>
/// Finds the node whose key is immediately less than <paramref name="node"/>.
/// </summary>
/// <param name="node">Node to find the predecessor of</param>
/// <returns>Predecessor of <paramref name="node"/></returns>
private static IntervalTreeNode<K, V> PredecessorOf(IntervalTreeNode<K, V> node)
{
if (node.Left != null)
{
return Maximum(node.Left);
}
IntervalTreeNode<K, V> parent = node.Parent;
while (parent != null && node == parent.Left)
{
node = parent;
parent = parent.Parent;
}
return parent;
}
#endregion
#region Private Methods (RBL)
private void RestoreBalanceAfterRemoval(IntervalTreeNode<K, V> balanceNode)
{
IntervalTreeNode<K, V> ptr = balanceNode;
while (ptr != _root && ColorOf(ptr) == Black)
{
if (ptr == LeftOf(ParentOf(ptr)))
{
IntervalTreeNode<K, V> sibling = RightOf(ParentOf(ptr));
if (ColorOf(sibling) == Red)
{
SetColor(sibling, Black);
SetColor(ParentOf(ptr), Red);
RotateLeft(ParentOf(ptr));
sibling = RightOf(ParentOf(ptr));
}
if (ColorOf(LeftOf(sibling)) == Black && ColorOf(RightOf(sibling)) == Black)
{
SetColor(sibling, Red);
ptr = ParentOf(ptr);
}
else
{
if (ColorOf(RightOf(sibling)) == Black)
{
SetColor(LeftOf(sibling), Black);
SetColor(sibling, Red);
RotateRight(sibling);
sibling = RightOf(ParentOf(ptr));
}
SetColor(sibling, ColorOf(ParentOf(ptr)));
SetColor(ParentOf(ptr), Black);
SetColor(RightOf(sibling), Black);
RotateLeft(ParentOf(ptr));
ptr = _root;
}
}
else
{
IntervalTreeNode<K, V> sibling = LeftOf(ParentOf(ptr));
if (ColorOf(sibling) == Red)
{
SetColor(sibling, Black);
SetColor(ParentOf(ptr), Red);
RotateRight(ParentOf(ptr));
sibling = LeftOf(ParentOf(ptr));
}
if (ColorOf(RightOf(sibling)) == Black && ColorOf(LeftOf(sibling)) == Black)
{
SetColor(sibling, Red);
ptr = ParentOf(ptr);
}
else
{
if (ColorOf(LeftOf(sibling)) == Black)
{
SetColor(RightOf(sibling), Black);
SetColor(sibling, Red);
RotateLeft(sibling);
sibling = LeftOf(ParentOf(ptr));
}
SetColor(sibling, ColorOf(ParentOf(ptr)));
SetColor(ParentOf(ptr), Black);
SetColor(LeftOf(sibling), Black);
RotateRight(ParentOf(ptr));
ptr = _root;
}
}
}
SetColor(ptr, Black);
}
private void RestoreBalanceAfterInsertion(IntervalTreeNode<K, V> balanceNode)
{
SetColor(balanceNode, Red);
while (balanceNode != null && balanceNode != _root && ColorOf(ParentOf(balanceNode)) == Red)
{
if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode))))
{
IntervalTreeNode<K, V> sibling = RightOf(ParentOf(ParentOf(balanceNode)));
if (ColorOf(sibling) == Red)
{
SetColor(ParentOf(balanceNode), Black);
SetColor(sibling, Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
balanceNode = ParentOf(ParentOf(balanceNode));
}
else
{
if (balanceNode == RightOf(ParentOf(balanceNode)))
{
balanceNode = ParentOf(balanceNode);
RotateLeft(balanceNode);
}
SetColor(ParentOf(balanceNode), Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
RotateRight(ParentOf(ParentOf(balanceNode)));
}
}
else
{
IntervalTreeNode<K, V> sibling = LeftOf(ParentOf(ParentOf(balanceNode)));
if (ColorOf(sibling) == Red)
{
SetColor(ParentOf(balanceNode), Black);
SetColor(sibling, Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
balanceNode = ParentOf(ParentOf(balanceNode));
}
else
{
if (balanceNode == LeftOf(ParentOf(balanceNode)))
{
balanceNode = ParentOf(balanceNode);
RotateRight(balanceNode);
}
SetColor(ParentOf(balanceNode), Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
RotateLeft(ParentOf(ParentOf(balanceNode)));
}
}
}
SetColor(_root, Black);
}
private void RotateLeft(IntervalTreeNode<K, V> node)
protected override void RotateLeft(IntervalTreeNode<K, V> node)
{
if (node != null)
{
IntervalTreeNode<K, V> right = RightOf(node);
node.Right = LeftOf(right);
if (node.Right != null)
{
node.Right.Parent = node;
}
IntervalTreeNode<K, V> nodeParent = ParentOf(node);
right.Parent = nodeParent;
if (nodeParent == null)
{
_root = right;
}
else if (node == LeftOf(nodeParent))
{
nodeParent.Left = right;
}
else
{
nodeParent.Right = right;
}
right.Left = node;
node.Parent = right;
base.RotateLeft(node);
PropagateFull(node);
}
}
private void RotateRight(IntervalTreeNode<K, V> node)
protected override void RotateRight(IntervalTreeNode<K, V> node)
{
if (node != null)
{
IntervalTreeNode<K, V> left = LeftOf(node);
node.Left = RightOf(left);
if (node.Left != null)
{
node.Left.Parent = node;
}
IntervalTreeNode<K, V> nodeParent = ParentOf(node);
left.Parent = nodeParent;
if (nodeParent == null)
{
_root = left;
}
else if (node == RightOf(nodeParent))
{
nodeParent.Right = left;
}
else
{
nodeParent.Left = left;
}
left.Right = node;
node.Parent = left;
base.RotateRight(node);
PropagateFull(node);
}
@ -623,77 +408,10 @@ namespace Ryujinx.Memory.WindowsShared
#endregion
#region Safety-Methods
// These methods save memory by allowing us to forego sentinel nil nodes, as well as serve as protection against NullReferenceExceptions.
/// <summary>
/// Returns the color of <paramref name="node"/>, or Black if it is null.
/// </summary>
/// <param name="node">Node</param>
/// <returns>The boolean color of <paramref name="node"/>, or black if null</returns>
private static bool ColorOf(IntervalTreeNode<K, V> node)
{
return node == null || node.Color;
}
/// <summary>
/// Sets the color of <paramref name="node"/> node to <paramref name="color"/>.
/// <br></br>
/// This method does nothing if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to set the color of</param>
/// <param name="color">Color (Boolean)</param>
private static void SetColor(IntervalTreeNode<K, V> node, bool color)
{
if (node != null)
{
node.Color = color;
}
}
/// <summary>
/// This method returns the left node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the left child from</param>
/// <returns>Left child of <paramref name="node"/></returns>
private static IntervalTreeNode<K, V> LeftOf(IntervalTreeNode<K, V> node)
{
return node?.Left;
}
/// <summary>
/// This method returns the right node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the right child from</param>
/// <returns>Right child of <paramref name="node"/></returns>
private static IntervalTreeNode<K, V> RightOf(IntervalTreeNode<K, V> node)
{
return node?.Right;
}
/// <summary>
/// Returns the parent node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
/// </summary>
/// <param name="node">Node to retrieve the parent from</param>
/// <returns>Parent of <paramref name="node"/></returns>
private static IntervalTreeNode<K, V> ParentOf(IntervalTreeNode<K, V> node)
{
return node?.Parent;
}
#endregion
public bool ContainsKey(K key)
{
return GetNode(key) != null;
}
public void Clear()
{
_root = null;
_count = 0;
}
}
/// <summary>
@ -701,13 +419,8 @@ namespace Ryujinx.Memory.WindowsShared
/// </summary>
/// <typeparam name="K">Key type of the node</typeparam>
/// <typeparam name="V">Value type of the node</typeparam>
class IntervalTreeNode<K, V>
class IntervalTreeNode<K, V> : IntrusiveRedBlackTreeNode<IntervalTreeNode<K, V>>
{
public bool Color = true;
public IntervalTreeNode<K, V> Left = null;
public IntervalTreeNode<K, V> Right = null;
public IntervalTreeNode<K, V> Parent = null;
/// <summary>
/// The start of the range.
/// </summary>