aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Memory/BytesReadOnlySequenceSegment.cs
diff options
context:
space:
mode:
authorjhorv <38920027+jhorv@users.noreply.github.com>2024-04-04 21:23:03 -0400
committerGitHub <noreply@github.com>2024-04-04 22:23:03 -0300
commit5def0429f82c795d820b2307a0301b78dfb1e6b7 (patch)
tree8495abf3183132aa411d6b46eb7609d69cd7a335 /src/Ryujinx.Memory/BytesReadOnlySequenceSegment.cs
parent8e74fa34560c4a8c3de234eb3488e1d0fb6f8f6c (diff)
Add support to IVirtualMemoryManager for zero-copy reads (#6251)
* - WritableRegion: enable wrapping IMemoryOwner<byte> - IVirtualMemoryManager impls of GetWritableRegion() use pooled memory when region is non-contiguous. - IVirtualMemoryManager: add GetReadOnlySequence() and impls - ByteMemoryPool: add new method RentCopy() - ByteMemoryPool: make class static, remove ctor and singleton field from earlier impl * - BytesReadOnlySequenceSegment: move from Ryujinx.Common.Memory to Ryujinx.Memory - BytesReadOnlySequenceSegment: add IsContiguousWith() and Replace() methods - VirtualMemoryManagerBase: - remove generic type parameters, instead use ulong for virtual addresses and nuint for host/physical addresses - implement IWritableBlock - add virtual GetReadOnlySequence() with coalescing of contiguous segments - add virtual GetSpan() - add virtual GetWritableRegion() - add abstract IsMapped() - add virtual MapForeign(ulong, nuint, ulong) - add virtual Read<T>() - add virtual Read(ulong, Span<byte>) - add virtual ReadTracked<T>() - add virtual SignalMemoryTracking() - add virtual Write() - add virtual Write<T>() - add virtual WriteUntracked() - add virtual WriteWithRedundancyCheck() - VirtualMemoryManagerRefCountedBase: remove generic type parameters - AddressSpaceManager: remove redundant methods, add required overrides - HvMemoryManager: remove redundant methods, add required overrides, add overrides for _invalidAccessHandler handling - MemoryManager: remove redundant methods, add required overrides, add overrides for _invalidAccessHandler handling - MemoryManagerHostMapped: remove redundant methods, add required overrides, add overrides for _invalidAccessHandler handling - NativeMemoryManager: add get properties for Pointer and Length - throughout: removed invalid <inheritdoc/> comments * make HvMemoryManager class sealed * remove unused method * adjust MemoryManagerHostTracked * let MemoryManagerHostTracked override WriteImpl()
Diffstat (limited to 'src/Ryujinx.Memory/BytesReadOnlySequenceSegment.cs')
-rw-r--r--src/Ryujinx.Memory/BytesReadOnlySequenceSegment.cs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/Ryujinx.Memory/BytesReadOnlySequenceSegment.cs b/src/Ryujinx.Memory/BytesReadOnlySequenceSegment.cs
new file mode 100644
index 00000000..5fe8d936
--- /dev/null
+++ b/src/Ryujinx.Memory/BytesReadOnlySequenceSegment.cs
@@ -0,0 +1,60 @@
+using System;
+using System.Buffers;
+using System.Runtime.InteropServices;
+
+namespace Ryujinx.Memory
+{
+ /// <summary>
+ /// A concrete implementation of <seealso cref="ReadOnlySequence{Byte}"/>,
+ /// with methods to help build a full sequence.
+ /// </summary>
+ public sealed class BytesReadOnlySequenceSegment : ReadOnlySequenceSegment<byte>
+ {
+ public BytesReadOnlySequenceSegment(Memory<byte> memory) => Memory = memory;
+
+ public BytesReadOnlySequenceSegment Append(Memory<byte> memory)
+ {
+ var nextSegment = new BytesReadOnlySequenceSegment(memory)
+ {
+ RunningIndex = RunningIndex + Memory.Length
+ };
+
+ Next = nextSegment;
+
+ return nextSegment;
+ }
+
+ /// <summary>
+ /// Attempts to determine if the current <seealso cref="Memory{Byte}"/> and <paramref name="other"/> are contiguous.
+ /// Only works if both were created by a <seealso cref="NativeMemoryManager{Byte}"/>.
+ /// </summary>
+ /// <param name="other">The segment to check if continuous with the current one</param>
+ /// <param name="contiguousStart">The starting address of the contiguous segment</param>
+ /// <param name="contiguousSize">The size of the contiguous segment</param>
+ /// <returns>True if the segments are contiguous, otherwise false</returns>
+ public unsafe bool IsContiguousWith(Memory<byte> other, out nuint contiguousStart, out int contiguousSize)
+ {
+ if (MemoryMarshal.TryGetMemoryManager<byte, NativeMemoryManager<byte>>(Memory, out var thisMemoryManager) &&
+ MemoryMarshal.TryGetMemoryManager<byte, NativeMemoryManager<byte>>(other, out var otherMemoryManager) &&
+ thisMemoryManager.Pointer + thisMemoryManager.Length == otherMemoryManager.Pointer)
+ {
+ contiguousStart = (nuint)thisMemoryManager.Pointer;
+ contiguousSize = thisMemoryManager.Length + otherMemoryManager.Length;
+ return true;
+ }
+ else
+ {
+ contiguousStart = 0;
+ contiguousSize = 0;
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Replaces the current <seealso cref="Memory{Byte}"/> value with the one provided.
+ /// </summary>
+ /// <param name="memory">The new segment to hold in this <seealso cref="BytesReadOnlySequenceSegment"/></param>
+ public void Replace(Memory<byte> memory)
+ => Memory = memory;
+ }
+}