From 6922862db8677fb8067a3e35d2433b1f12f8329c Mon Sep 17 00:00:00 2001 From: gdkchan Date: Fri, 26 Aug 2022 15:21:48 -0300 Subject: [PATCH] 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 --- ARMeilleure/Translation/IntervalTree.cs | 2 - Ryujinx.Common/Collections/IntervalTree.cs | 331 +-------------- .../Collections/IntrusiveRedBlackTree.cs | 293 +++++++++++++ .../Collections/IntrusiveRedBlackTreeImpl.cs | 356 ++++++++++++++++ .../Collections/IntrusiveRedBlackTreeNode.cs | 16 + Ryujinx.Common/Collections/TreeDictionary.cs | 398 ++---------------- Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlock.cs | 73 +++- .../HOS/Kernel/Memory/KMemoryBlockManager.cs | 169 +++----- .../HOS/Kernel/Memory/KPageTableBase.cs | 24 +- Ryujinx.Memory/WindowsShared/IntervalTree.cs | 319 +------------- 10 files changed, 860 insertions(+), 1121 deletions(-) create mode 100644 Ryujinx.Common/Collections/IntrusiveRedBlackTree.cs create mode 100644 Ryujinx.Common/Collections/IntrusiveRedBlackTreeImpl.cs create mode 100644 Ryujinx.Common/Collections/IntrusiveRedBlackTreeNode.cs diff --git a/ARMeilleure/Translation/IntervalTree.cs b/ARMeilleure/Translation/IntervalTree.cs index 10bab7ae..79662bc9 100644 --- a/ARMeilleure/Translation/IntervalTree.cs +++ b/ARMeilleure/Translation/IntervalTree.cs @@ -19,8 +19,6 @@ namespace ARMeilleure.Translation public int Count => _count; - public IntervalTree() { } - #region Public Methods /// diff --git a/Ryujinx.Common/Collections/IntervalTree.cs b/Ryujinx.Common/Collections/IntervalTree.cs index 514fd534..c829caba 100644 --- a/Ryujinx.Common/Collections/IntervalTree.cs +++ b/Ryujinx.Common/Collections/IntervalTree.cs @@ -9,19 +9,10 @@ namespace Ryujinx.Common.Collections /// /// Key /// Value - public class IntervalTree where K : IComparable + public class IntervalTree : IntrusiveRedBlackTreeImpl> where K : IComparable { private const int ArrayGrowthSize = 32; - private const bool Black = true; - private const bool Red = false; - private IntervalTreeNode _root = null; - private int _count = 0; - - public int Count => _count; - - public IntervalTree() { } - #region Public Methods /// @@ -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> list = new List>(); - AddToList(_root, list); + AddToList(Root, list); return list; } @@ -182,7 +173,7 @@ namespace Ryujinx.Common.Collections throw new ArgumentNullException(nameof(key)); } - IntervalTreeNode node = _root; + IntervalTreeNode node = Root; while (node != null) { int cmp = key.CompareTo(node.Start); @@ -317,7 +308,7 @@ namespace Ryujinx.Common.Collections private IntervalTreeNode BSTInsert(K start, K end, V value) { IntervalTreeNode parent = null; - IntervalTreeNode node = _root; + IntervalTreeNode node = Root; while (node != null) { @@ -345,14 +336,14 @@ namespace Ryujinx.Common.Collections } } - _count++; + Count++; return node; } } IntervalTreeNode newNode = new IntervalTreeNode(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; } - /// - /// Returns the node with the largest key where is considered the root node. - /// - /// Root Node - /// Node with the maximum key in the tree of - private static IntervalTreeNode Maximum(IntervalTreeNode node) - { - IntervalTreeNode tmp = node; - while (tmp.Right != null) - { - tmp = tmp.Right; - } - - return tmp; - } - - /// - /// Finds the node whose key is immediately less than . - /// - /// Node to find the predecessor of - /// Predecessor of - private static IntervalTreeNode PredecessorOf(IntervalTreeNode node) - { - if (node.Left != null) - { - return Maximum(node.Left); - } - IntervalTreeNode 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 balanceNode) - { - IntervalTreeNode ptr = balanceNode; - - while (ptr != _root && ColorOf(ptr) == Black) - { - if (ptr == LeftOf(ParentOf(ptr))) - { - IntervalTreeNode 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 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 balanceNode) - { - SetColor(balanceNode, Red); - while (balanceNode != null && balanceNode != _root && ColorOf(ParentOf(balanceNode)) == Red) - { - if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode)))) - { - IntervalTreeNode 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 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 node) + protected override void RotateLeft(IntervalTreeNode node) { if (node != null) { - IntervalTreeNode right = RightOf(node); - node.Right = LeftOf(right); - if (node.Right != null) - { - node.Right.Parent = node; - } - IntervalTreeNode 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 node) + protected override void RotateRight(IntervalTreeNode node) { if (node != null) { - IntervalTreeNode left = LeftOf(node); - node.Left = RightOf(left); - if (node.Left != null) - { - node.Left.Parent = node; - } - IntervalTreeNode 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. - - /// - /// Returns the color of , or Black if it is null. - /// - /// Node - /// The boolean color of , or black if null - private static bool ColorOf(IntervalTreeNode node) - { - return node == null || node.Color; - } - - /// - /// Sets the color of node to . - ///

- /// This method does nothing if is null. - ///
- /// Node to set the color of - /// Color (Boolean) - private static void SetColor(IntervalTreeNode node, bool color) - { - if (node != null) - { - node.Color = color; - } - } - - /// - /// This method returns the left node of , or null if is null. - /// - /// Node to retrieve the left child from - /// Left child of - private static IntervalTreeNode LeftOf(IntervalTreeNode node) - { - return node?.Left; - } - - /// - /// This method returns the right node of , or null if is null. - /// - /// Node to retrieve the right child from - /// Right child of - private static IntervalTreeNode RightOf(IntervalTreeNode node) - { - return node?.Right; - } - - /// - /// Returns the parent node of , or null if is null. - /// - /// Node to retrieve the parent from - /// Parent of - private static IntervalTreeNode ParentOf(IntervalTreeNode 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; - } } /// @@ -777,31 +494,29 @@ namespace Ryujinx.Common.Collections /// /// Key type of the node /// Value type of the node - class IntervalTreeNode + public class IntervalTreeNode : IntrusiveRedBlackTreeNode> { - public bool Color = true; - public IntervalTreeNode Left = null; - public IntervalTreeNode Right = null; - public IntervalTreeNode Parent = null; - /// /// The start of the range. /// - public K Start; + internal K Start; /// /// The end of the range - maximum of all in the Values list. /// - public K End; + internal K End; /// /// The maximum end value of this node and all its children. /// - public K Max; + internal K Max; - public List> Values; + /// + /// Values contained on the node that shares a common Start value. + /// + internal List> Values; - public IntervalTreeNode(K start, K end, V value, IntervalTreeNode parent) + internal IntervalTreeNode(K start, K end, V value, IntervalTreeNode parent) { Start = start; End = end; diff --git a/Ryujinx.Common/Collections/IntrusiveRedBlackTree.cs b/Ryujinx.Common/Collections/IntrusiveRedBlackTree.cs new file mode 100644 index 00000000..970cab87 --- /dev/null +++ b/Ryujinx.Common/Collections/IntrusiveRedBlackTree.cs @@ -0,0 +1,293 @@ +using System; + +namespace Ryujinx.Common.Collections +{ + /// + /// 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. + /// + /// Derived node type + public class IntrusiveRedBlackTree : IntrusiveRedBlackTreeImpl where T : IntrusiveRedBlackTreeNode, IComparable + { + #region Public Methods + + /// + /// Adds a new node into the tree. + /// + /// Node to be added + /// is null + public void Add(T node) + { + if (node == null) + { + throw new ArgumentNullException(nameof(node)); + } + + Insert(node); + } + + /// + /// Removes a node from the tree. + /// + /// Note to be removed + /// is null + public void Remove(T node) + { + if (node == null) + { + throw new ArgumentNullException(nameof(node)); + } + if (Delete(node) != null) + { + Count--; + } + } + + /// + /// Retrieve the node that is considered equal to the specified node by the comparator. + /// + /// Node to compare with + /// Node that is equal to + /// is null + 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) + + /// + /// Inserts a new node into the tree. + /// + /// Node to be inserted + private void Insert(T node) + { + T newNode = BSTInsert(node); + RestoreBalanceAfterInsertion(newNode); + } + + /// + /// Insertion Mechanism for a Binary Search Tree (BST). + ///

+ /// Iterates the tree starting from the root and inserts a new node + /// where all children in the left subtree are less than , + /// and all children in the right subtree are greater than . + ///
+ /// Node to be inserted + /// The inserted Node + 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; + } + + /// + /// Removes from the tree, if it exists. + /// + /// Node to be removed + /// The deleted Node + 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 + { + /// + /// Retrieve the node that is considered equal to the key by the comparator. + /// + /// Tree to search at + /// Key of the node to be found + /// Node that is equal to + public static N GetNodeByKey(this IntrusiveRedBlackTree tree, K key) + where N : IntrusiveRedBlackTreeNode, IComparable, IComparable + 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; + } + } +} diff --git a/Ryujinx.Common/Collections/IntrusiveRedBlackTreeImpl.cs b/Ryujinx.Common/Collections/IntrusiveRedBlackTreeImpl.cs new file mode 100644 index 00000000..267aeec3 --- /dev/null +++ b/Ryujinx.Common/Collections/IntrusiveRedBlackTreeImpl.cs @@ -0,0 +1,356 @@ +using System; + +namespace Ryujinx.Common.Collections +{ + /// + /// 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. + /// + /// Derived node type + public class IntrusiveRedBlackTreeImpl where T : IntrusiveRedBlackTreeNode + { + protected const bool Black = true; + protected const bool Red = false; + protected T Root = null; + + internal T RootNode => Root; + + /// + /// Number of nodes on the tree. + /// + public int Count { get; protected set; } + + /// + /// Removes all nodes on the tree. + /// + public void Clear() + { + Root = null; + Count = 0; + } + + /// + /// Finds the node whose key is immediately greater than . + /// + /// Node to find the successor of + /// Successor of + 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; + } + + /// + /// Finds the node whose key is immediately less than . + /// + /// Node to find the predecessor of + /// Predecessor of + 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; + } + + /// + /// Returns the node with the largest key where is considered the root node. + /// + /// Root node + /// Node with the maximum key in the tree of + protected static T Maximum(T node) + { + T tmp = node; + while (tmp.Right != null) + { + tmp = tmp.Right; + } + + return tmp; + } + + /// + /// Returns the node with the smallest key where is considered the root node. + /// + /// Root node + /// Node with the minimum key in the tree of + /// is null + 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. + + /// + /// Returns the color of , or Black if it is null. + /// + /// Node + /// The boolean color of , or black if null + protected static bool ColorOf(T node) + { + return node == null || node.Color; + } + + /// + /// Sets the color of node to . + ///

+ /// This method does nothing if is null. + ///
+ /// Node to set the color of + /// Color (Boolean) + protected static void SetColor(T node, bool color) + { + if (node != null) + { + node.Color = color; + } + } + + /// + /// This method returns the left node of , or null if is null. + /// + /// Node to retrieve the left child from + /// Left child of + protected static T LeftOf(T node) + { + return node?.Left; + } + + /// + /// This method returns the right node of , or null if is null. + /// + /// Node to retrieve the right child from + /// Right child of + protected static T RightOf(T node) + { + return node?.Right; + } + + /// + /// Returns the parent node of , or null if is null. + /// + /// Node to retrieve the parent from + /// Parent of + protected static T ParentOf(T node) + { + return node?.Parent; + } + + #endregion + } +} diff --git a/Ryujinx.Common/Collections/IntrusiveRedBlackTreeNode.cs b/Ryujinx.Common/Collections/IntrusiveRedBlackTreeNode.cs new file mode 100644 index 00000000..c6c01a06 --- /dev/null +++ b/Ryujinx.Common/Collections/IntrusiveRedBlackTreeNode.cs @@ -0,0 +1,16 @@ +namespace Ryujinx.Common.Collections +{ + /// + /// Represents a node in the Red-Black Tree. + /// + public class IntrusiveRedBlackTreeNode where T : IntrusiveRedBlackTreeNode + { + public bool Color = true; + public T Left; + public T Right; + public T Parent; + + public T Predecessor => IntrusiveRedBlackTreeImpl.PredecessorOf((T)this); + public T Successor => IntrusiveRedBlackTreeImpl.SuccessorOf((T)this); + } +} \ No newline at end of file diff --git a/Ryujinx.Common/Collections/TreeDictionary.cs b/Ryujinx.Common/Collections/TreeDictionary.cs index 108fa773..a5a3b818 100644 --- a/Ryujinx.Common/Collections/TreeDictionary.cs +++ b/Ryujinx.Common/Collections/TreeDictionary.cs @@ -10,14 +10,8 @@ namespace Ryujinx.Common.Collections ///
/// Key /// Value - public class TreeDictionary : IDictionary where K : IComparable + public class TreeDictionary : IntrusiveRedBlackTreeImpl>, IDictionary where K : IComparable { - private const bool Black = true; - private const bool Red = false; - private Node _root = null; - private int _count = 0; - public TreeDictionary() { } - #region Public Methods /// @@ -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> nodes = new Queue>(); - 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 node)) { - Node node = nodes.Dequeue(); list.Add(new KeyValuePair(node.Key, node.Value)); if (node.Left != null) { @@ -188,7 +181,7 @@ namespace Ryujinx.Common.Collections { List> list = new List>(); - AddToList(_root, list); + AddToList(Root, list); return list; } @@ -229,7 +222,7 @@ namespace Ryujinx.Common.Collections throw new ArgumentNullException(nameof(key)); } - Node node = _root; + Node node = Root; while (node != null) { int cmp = key.CompareTo(node.Key); @@ -275,7 +268,7 @@ namespace Ryujinx.Common.Collections private Node BSTInsert(K key, V value) { Node parent = null; - Node node = _root; + Node node = Root; while (node != null) { @@ -298,7 +291,7 @@ namespace Ryujinx.Common.Collections Node newNode = new Node(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; } - /// - /// Returns the node with the largest key where is considered the root node. - /// - /// Root Node - /// Node with the maximum key in the tree of - private static Node Maximum(Node node) - { - Node tmp = node; - while (tmp.Right != null) - { - tmp = tmp.Right; - } - - return tmp; - } - - /// - /// Returns the node with the smallest key where is considered the root node. - /// - /// Root Node - /// Node with the minimum key in the tree of - /// is null - private static Node Minimum(Node node) - { - if (node == null) - { - throw new ArgumentNullException(nameof(node)); - } - Node tmp = node; - while (tmp.Left != null) - { - tmp = tmp.Left; - } - - return tmp; - } - /// /// Returns the node whose key immediately less than or equal to . /// @@ -419,7 +374,7 @@ namespace Ryujinx.Common.Collections { throw new ArgumentNullException(nameof(key)); } - Node tmp = _root; + Node tmp = Root; while (tmp != null) { @@ -473,7 +428,7 @@ namespace Ryujinx.Common.Collections { throw new ArgumentNullException(nameof(key)); } - Node tmp = _root; + Node tmp = Root; while (tmp != null) { @@ -515,294 +470,6 @@ namespace Ryujinx.Common.Collections return null; } - /// - /// Finds the node with the key is immediately greater than . - /// - /// Node to find the successor of - /// Successor of - private static Node SuccessorOf(Node node) - { - if (node.Right != null) - { - return Minimum(node.Right); - } - Node parent = node.Parent; - while (parent != null && node == parent.Right) - { - node = parent; - parent = parent.Parent; - } - return parent; - } - - /// - /// Finds the node whose key is immediately less than . - /// - /// Node to find the predecessor of - /// Predecessor of - private static Node PredecessorOf(Node node) - { - if (node.Left != null) - { - return Maximum(node.Left); - } - Node 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 balanceNode) - { - Node ptr = balanceNode; - - while (ptr != _root && ColorOf(ptr) == Black) - { - if (ptr == LeftOf(ParentOf(ptr))) - { - Node 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 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 balanceNode) - { - SetColor(balanceNode, Red); - while (balanceNode != null && balanceNode != _root && ColorOf(ParentOf(balanceNode)) == Red) - { - if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode)))) - { - Node 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 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 node) - { - if (node != null) - { - Node 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 node) - { - if (node != null) - { - Node 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. - - /// - /// Returns the color of , or Black if it is null. - /// - /// Node - /// The boolean color of , or black if null - private static bool ColorOf(Node node) - { - return node == null || node.Color; - } - - /// - /// Sets the color of node to . - ///

- /// This method does nothing if is null. - ///
- /// Node to set the color of - /// Color (Boolean) - private static void SetColor(Node node, bool color) - { - if (node != null) - { - node.Color = color; - } - } - - /// - /// This method returns the left node of , or null if is null. - /// - /// Node to retrieve the left child from - /// Left child of - private static Node LeftOf(Node node) - { - return node?.Left; - } - - /// - /// This method returns the right node of , or null if is null. - /// - /// Node to retrieve the right child from - /// Right child of - private static Node RightOf(Node node) - { - return node?.Right; - } - - /// - /// Returns the parent node of , or null if is null. - /// - /// Node to retrieve the parent from - /// Parent of - private static Node ParentOf(Node node) - { - return node?.Parent; - } #endregion #region Interface Implementations @@ -819,9 +486,9 @@ namespace Ryujinx.Common.Collections bool IDictionary.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 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 Keys => GetKeyValues().Keys; public ICollection Values => GetKeyValues().Values; @@ -928,6 +587,7 @@ namespace Ryujinx.Common.Collections } #endregion + #region Private Interface Helper Methods /// @@ -938,14 +598,13 @@ namespace Ryujinx.Common.Collections { SortedList set = new SortedList(); Queue> queue = new Queue>(); - if (_root != null) + if (Root != null) { - queue.Enqueue(_root); + queue.Enqueue(Root); } - while (queue.Count > 0) + while (queue.TryDequeue(out Node node)) { - Node 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 /// /// Key of the node /// Value of the node - class Node + public class Node : IntrusiveRedBlackTreeNode> where K : IComparable { - public bool Color = true; - public Node Left = null; - public Node Right = null; - public Node Parent = null; - public K Key; - public V Value; + internal K Key; + internal V Value; - public Node(K key, V value, Node parent) + internal Node(K key, V value, Node parent) { Key = key; Value = value; diff --git a/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlock.cs b/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlock.cs index b612022c..e082105b 100644 --- a/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlock.cs +++ b/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlock.cs @@ -1,42 +1,43 @@ +using Ryujinx.Common.Collections; using System; namespace Ryujinx.HLE.HOS.Kernel.Memory { - class KMemoryBlock + class KMemoryBlock : IntrusiveRedBlackTreeNode, IComparable, IComparable { public ulong BaseAddress { get; private set; } - public ulong PagesCount { get; private set; } + public ulong PagesCount { get; private set; } - public MemoryState State { get; private set; } - public KMemoryPermission Permission { get; private set; } - public MemoryAttribute Attribute { get; private set; } + public MemoryState State { get; private set; } + public KMemoryPermission Permission { get; private set; } + public MemoryAttribute Attribute { get; private set; } public KMemoryPermission SourcePermission { get; private set; } - public int IpcRefCount { get; private set; } + public int IpcRefCount { get; private set; } public int DeviceRefCount { get; private set; } public KMemoryBlock( - ulong baseAddress, - ulong pagesCount, - MemoryState state, + ulong baseAddress, + ulong pagesCount, + MemoryState state, KMemoryPermission permission, - MemoryAttribute attribute, - int ipcRefCount = 0, - int deviceRefCount = 0) + MemoryAttribute attribute, + int ipcRefCount = 0, + int deviceRefCount = 0) { - BaseAddress = baseAddress; - PagesCount = pagesCount; - State = state; - Attribute = attribute; - Permission = permission; - IpcRefCount = ipcRefCount; + BaseAddress = baseAddress; + PagesCount = pagesCount; + State = state; + Attribute = attribute; + Permission = permission; + IpcRefCount = ipcRefCount; DeviceRefCount = deviceRefCount; } public void SetState(KMemoryPermission permission, MemoryState state, MemoryAttribute attribute) { Permission = permission; - State = state; + State = state; Attribute &= MemoryAttribute.IpcAndDeviceMapped; Attribute |= attribute; } @@ -55,7 +56,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory SourcePermission = Permission; Permission &= ~KMemoryPermission.ReadAndWrite; - Permission |= KMemoryPermission.ReadAndWrite & newPermission; + Permission |= KMemoryPermission.ReadAndWrite & newPermission; } Attribute |= MemoryAttribute.IpcMapped; @@ -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; + } + } } } \ No newline at end of file diff --git a/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlockManager.cs b/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlockManager.cs index c0d11a95..9cfdfda3 100644 --- a/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlockManager.cs +++ b/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryBlockManager.cs @@ -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 _blocks; + private readonly IntrusiveRedBlackTree _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(); + _blockTree = new IntrusiveRedBlackTree(); } 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 node = _blocks.First; + KMemoryBlock currBlock = FindBlock(baseAddress); - while (node != null) + while (currBlock != null) { - LinkedListNode 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 node = _blocks.First; + KMemoryBlock currBlock = FindBlock(baseAddress); - while (node != null) + while (currBlock != null) { - LinkedListNode 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 node = _blocks.First; + KMemoryBlock currBlock = FindBlock(baseAddress); - while (node != null) + while (currBlock != null) { - LinkedListNode 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 node = _blocks.First; + KMemoryBlock currBlock = FindBlock(_addrSpaceStart); - while (node != null) + while (currBlock != null) { - LinkedListNode 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 MergeEqualStateNeighbors(LinkedListNode 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; + _blockTree.Remove(block); - if (BlockStateEquals(block, previousBlock)) - { - LinkedListNode previousNode = node.Previous; + previousBlock.AddPages(block.PagesCount); - _blocks.Remove(node); - - previousBlock.AddPages(block.PagesCount); - - node = previousNode; - block = previousBlock; - } + block = previousBlock; } - if (node.Next != null) + if (nextBlock != null && BlockStateEquals(block, nextBlock)) { - KMemoryBlock nextBlock = node.Next.Value; + _blockTree.Remove(nextBlock); - if (BlockStateEquals(block, nextBlock)) - { - _blocks.Remove(node.Next); - - block.AddPages(nextBlock.PagesCount); - } + 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 FindBlockNode(ulong address) - { - lock (_blocks) - { - LinkedListNode 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); } } } diff --git a/Ryujinx.HLE/HOS/Kernel/Memory/KPageTableBase.cs b/Ryujinx.HLE/HOS/Kernel/Memory/KPageTableBase.cs index ab43b477..857be7a6 100644 --- a/Ryujinx.HLE/HOS/Kernel/Memory/KPageTableBase.cs +++ b/Ryujinx.HLE/HOS/Kernel/Memory/KPageTableBase.cs @@ -2447,9 +2447,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory { ulong endAddr = address + size; - LinkedListNode 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 IterateOverRange(ulong start, ulong end) { - LinkedListNode 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 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; diff --git a/Ryujinx.Memory/WindowsShared/IntervalTree.cs b/Ryujinx.Memory/WindowsShared/IntervalTree.cs index eac5c545..fe12e8b8 100644 --- a/Ryujinx.Memory/WindowsShared/IntervalTree.cs +++ b/Ryujinx.Memory/WindowsShared/IntervalTree.cs @@ -1,3 +1,4 @@ +using Ryujinx.Common.Collections; using System; using System.Collections.Generic; @@ -8,19 +9,10 @@ namespace Ryujinx.Memory.WindowsShared ///
/// Key /// Value - class IntervalTree where K : IComparable + class IntervalTree : IntrusiveRedBlackTreeImpl> where K : IComparable { private const int ArrayGrowthSize = 32; - private const bool Black = true; - private const bool Red = false; - private IntervalTreeNode _root = null; - private int _count = 0; - - public int Count => _count; - - public IntervalTree() { } - #region Public Methods /// @@ -53,7 +45,7 @@ namespace Ryujinx.Memory.WindowsShared /// Number of intervals found public int Get(K start, K end, ref IntervalTreeNode[] 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 list = new List(); - AddToList(_root, list); + AddToList(Root, list); return list; } @@ -153,7 +145,7 @@ namespace Ryujinx.Memory.WindowsShared throw new ArgumentNullException(nameof(key)); } - IntervalTreeNode node = _root; + IntervalTreeNode 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 updateFactoryCallback, out IntervalTreeNode outNode) { IntervalTreeNode parent = null; - IntervalTreeNode node = _root; + IntervalTreeNode node = Root; while (node != null) { @@ -319,7 +311,7 @@ namespace Ryujinx.Memory.WindowsShared IntervalTreeNode newNode = new IntervalTreeNode(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 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 } } - /// - /// Returns the node with the largest key where is considered the root node. - /// - /// Root Node - /// Node with the maximum key in the tree of - private static IntervalTreeNode Maximum(IntervalTreeNode node) - { - IntervalTreeNode tmp = node; - while (tmp.Right != null) - { - tmp = tmp.Right; - } - - return tmp; - } - - /// - /// Finds the node whose key is immediately less than . - /// - /// Node to find the predecessor of - /// Predecessor of - private static IntervalTreeNode PredecessorOf(IntervalTreeNode node) - { - if (node.Left != null) - { - return Maximum(node.Left); - } - IntervalTreeNode 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 balanceNode) - { - IntervalTreeNode ptr = balanceNode; - - while (ptr != _root && ColorOf(ptr) == Black) - { - if (ptr == LeftOf(ParentOf(ptr))) - { - IntervalTreeNode 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 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 balanceNode) - { - SetColor(balanceNode, Red); - while (balanceNode != null && balanceNode != _root && ColorOf(ParentOf(balanceNode)) == Red) - { - if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode)))) - { - IntervalTreeNode 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 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 node) + protected override void RotateLeft(IntervalTreeNode node) { if (node != null) { - IntervalTreeNode right = RightOf(node); - node.Right = LeftOf(right); - if (node.Right != null) - { - node.Right.Parent = node; - } - IntervalTreeNode 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 node) + protected override void RotateRight(IntervalTreeNode node) { if (node != null) { - IntervalTreeNode left = LeftOf(node); - node.Left = RightOf(left); - if (node.Left != null) - { - node.Left.Parent = node; - } - IntervalTreeNode 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. - - /// - /// Returns the color of , or Black if it is null. - /// - /// Node - /// The boolean color of , or black if null - private static bool ColorOf(IntervalTreeNode node) - { - return node == null || node.Color; - } - - /// - /// Sets the color of node to . - ///

- /// This method does nothing if is null. - ///
- /// Node to set the color of - /// Color (Boolean) - private static void SetColor(IntervalTreeNode node, bool color) - { - if (node != null) - { - node.Color = color; - } - } - - /// - /// This method returns the left node of , or null if is null. - /// - /// Node to retrieve the left child from - /// Left child of - private static IntervalTreeNode LeftOf(IntervalTreeNode node) - { - return node?.Left; - } - - /// - /// This method returns the right node of , or null if is null. - /// - /// Node to retrieve the right child from - /// Right child of - private static IntervalTreeNode RightOf(IntervalTreeNode node) - { - return node?.Right; - } - - /// - /// Returns the parent node of , or null if is null. - /// - /// Node to retrieve the parent from - /// Parent of - private static IntervalTreeNode ParentOf(IntervalTreeNode node) - { - return node?.Parent; - } - - #endregion - public bool ContainsKey(K key) { return GetNode(key) != null; } - - public void Clear() - { - _root = null; - _count = 0; - } } /// @@ -701,13 +419,8 @@ namespace Ryujinx.Memory.WindowsShared /// /// Key type of the node /// Value type of the node - class IntervalTreeNode + class IntervalTreeNode : IntrusiveRedBlackTreeNode> { - public bool Color = true; - public IntervalTreeNode Left = null; - public IntervalTreeNode Right = null; - public IntervalTreeNode Parent = null; - /// /// The start of the range. ///