mirror of
https://git.ryujinx.app/ryubing/ryujinx.git
synced 2026-05-13 00:35:48 +00:00
Memory Changes part 2 (ryubing/ryujinx!123)
See merge request ryubing/ryujinx!123
This commit is contained in:
@@ -36,10 +36,12 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
|
||||
class RangeNode<T> : IntrusiveRedBlackTreeNode<RangeNode<T>>, IComparable<RangeNode<T>>, IComparable<ulong>
|
||||
{
|
||||
public ulong Start { get; }
|
||||
public ulong Start { get; private set; }
|
||||
public ulong End { get; private set; }
|
||||
public T Value { get; }
|
||||
public T Value { get; private set; }
|
||||
|
||||
public RangeNode() { }
|
||||
|
||||
public RangeNode(ulong start, ulong end, T value)
|
||||
{
|
||||
Start = start;
|
||||
@@ -47,6 +49,22 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public RangeNode<T> Init(ulong start, ulong end, T value)
|
||||
{
|
||||
Start = start;
|
||||
End = end;
|
||||
Value = value;
|
||||
|
||||
Color = true;
|
||||
Left = null;
|
||||
Right = null;
|
||||
Parent = null;
|
||||
Predecessor = null;
|
||||
Successor = null;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Extend(ulong sizeDelta)
|
||||
{
|
||||
End += sizeDelta;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Ryujinx.Common.Collections;
|
||||
using Ryujinx.Common.Memory.PartialUnmaps;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.Versioning;
|
||||
@@ -8,6 +9,24 @@ using System.Threading;
|
||||
|
||||
namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
public class ObjectPool<T>
|
||||
{
|
||||
private readonly Stack<T> _objects;
|
||||
private readonly Func<T> _objectGenerator;
|
||||
|
||||
public ObjectPool(Func<T> objectGenerator)
|
||||
{
|
||||
_objectGenerator = objectGenerator ?? throw new ArgumentNullException(nameof(objectGenerator));
|
||||
_objects = new Stack<T>();
|
||||
}
|
||||
|
||||
public T Get() => _objects.Count > 0 ? _objects.Pop() : _objectGenerator();
|
||||
|
||||
public void Return(T item) => _objects.Push(item);
|
||||
|
||||
public void Clear() => _objects.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Windows memory placeholder manager.
|
||||
/// </summary>
|
||||
@@ -18,6 +37,7 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
|
||||
private readonly MappingTree<ulong> _mappings;
|
||||
private readonly MappingTree<MemoryPermission> _protections;
|
||||
private readonly ObjectPool<RangeNode<MemoryPermission>> _protectionObjectPool;
|
||||
private readonly nint _partialUnmapStatePtr;
|
||||
private readonly Thread _partialUnmapTrimThread;
|
||||
|
||||
@@ -28,6 +48,8 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
_mappings = new MappingTree<ulong>();
|
||||
_protections = new MappingTree<MemoryPermission>();
|
||||
|
||||
_protectionObjectPool = new ObjectPool<RangeNode<MemoryPermission>>(() => new RangeNode<MemoryPermission>());
|
||||
|
||||
_partialUnmapStatePtr = PartialUnmapState.GlobalState;
|
||||
|
||||
@@ -70,12 +92,12 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
lock (_mappings)
|
||||
{
|
||||
_mappings.Add(new RangeNode<ulong>(address, address + size, ulong.MaxValue));
|
||||
_mappings.Add(new RangeNode<ulong>().Init(address, address + size, ulong.MaxValue));
|
||||
}
|
||||
|
||||
lock (_protections)
|
||||
{
|
||||
_protections.Add(new RangeNode<MemoryPermission>(address, address + size, MemoryPermission.None));
|
||||
_protections.Add(_protectionObjectPool.Get().Init(address, address + size, MemoryPermission.None));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,8 +236,8 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
(nint)size,
|
||||
AllocationType.Release | AllocationType.PreservePlaceholder));
|
||||
|
||||
_mappings.Add(new RangeNode<ulong>(overlapStart, address, overlapValue));
|
||||
_mappings.Add(new RangeNode<ulong>(endAddress, overlapEnd, AddBackingOffset(overlapValue, endAddress - overlapStart)));
|
||||
_mappings.Add(new RangeNode<ulong>().Init(overlapStart, address, overlapValue));
|
||||
_mappings.Add(new RangeNode<ulong>().Init(endAddress, overlapEnd, AddBackingOffset(overlapValue, endAddress - overlapStart)));
|
||||
}
|
||||
else if (overlapStartsBefore)
|
||||
{
|
||||
@@ -226,7 +248,7 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
(nint)overlappedSize,
|
||||
AllocationType.Release | AllocationType.PreservePlaceholder));
|
||||
|
||||
_mappings.Add(new RangeNode<ulong>(overlapStart, address, overlapValue));
|
||||
_mappings.Add(new RangeNode<ulong>().Init(overlapStart, address, overlapValue));
|
||||
}
|
||||
else if (overlapEndsAfter)
|
||||
{
|
||||
@@ -237,10 +259,10 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
(nint)overlappedSize,
|
||||
AllocationType.Release | AllocationType.PreservePlaceholder));
|
||||
|
||||
_mappings.Add(new RangeNode<ulong>(endAddress, overlapEnd, AddBackingOffset(overlapValue, overlappedSize)));
|
||||
_mappings.Add(new RangeNode<ulong>().Init(endAddress, overlapEnd, AddBackingOffset(overlapValue, overlappedSize)));
|
||||
}
|
||||
|
||||
_mappings.Add(new RangeNode<ulong>(address, endAddress, backingOffset));
|
||||
_mappings.Add(new RangeNode<ulong>().Init(address, endAddress, backingOffset));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,7 +328,7 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
lock (_mappings)
|
||||
{
|
||||
_mappings.Remove(overlap);
|
||||
_mappings.Add(new RangeNode<ulong>(overlap.Start, overlap.End, ulong.MaxValue));
|
||||
_mappings.Add(new RangeNode<ulong>().Init(overlap.Start, overlap.End, ulong.MaxValue));
|
||||
}
|
||||
|
||||
bool overlapStartsBefore = overlap.Start < startAddress;
|
||||
@@ -433,7 +455,7 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
unmappedCount++;
|
||||
}
|
||||
|
||||
_mappings.Add(new RangeNode<ulong>(address, endAddress, ulong.MaxValue));
|
||||
_mappings.Add(new RangeNode<ulong>().Init(address, endAddress, ulong.MaxValue));
|
||||
}
|
||||
|
||||
if (unmappedCount > 1)
|
||||
@@ -628,14 +650,16 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
if (startAddress > protAddress)
|
||||
{
|
||||
_protections.Add(new RangeNode<MemoryPermission>(protAddress, startAddress, protPermission));
|
||||
_protections.Add(_protectionObjectPool.Get().Init(protAddress, startAddress, protPermission));
|
||||
}
|
||||
|
||||
if (endAddress < protEndAddress)
|
||||
{
|
||||
_protections.Add(new RangeNode<MemoryPermission>(endAddress, protEndAddress, protPermission));
|
||||
_protections.Add(_protectionObjectPool.Get().Init(endAddress, protEndAddress, protPermission));
|
||||
}
|
||||
}
|
||||
|
||||
_protectionObjectPool.Return(protection);
|
||||
|
||||
if (node.End >= endAddress)
|
||||
{
|
||||
@@ -643,7 +667,7 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
}
|
||||
}
|
||||
|
||||
_protections.Add(new RangeNode<MemoryPermission>(startAddress, endAddress, permission));
|
||||
_protections.Add(_protectionObjectPool.Get().Init(startAddress, endAddress, permission));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -674,14 +698,16 @@ namespace Ryujinx.Memory.WindowsShared
|
||||
|
||||
if (address > protAddress)
|
||||
{
|
||||
_protections.Add(new RangeNode<MemoryPermission>(protAddress, address, protPermission));
|
||||
_protections.Add(_protectionObjectPool.Get().Init(protAddress, address, protPermission));
|
||||
}
|
||||
|
||||
if (endAddress < protEndAddress)
|
||||
{
|
||||
_protections.Add(new RangeNode<MemoryPermission>(endAddress, protEndAddress, protPermission));
|
||||
_protections.Add(_protectionObjectPool.Get().Init(endAddress, protEndAddress, protPermission));
|
||||
}
|
||||
|
||||
_protectionObjectPool.Return(protection);
|
||||
|
||||
if (node.End >= endAddress)
|
||||
{
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user