2023-12-04 13:17:13 +00:00
|
|
|
using System;
|
2023-04-24 02:06:23 +00:00
|
|
|
using System.Buffers;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.Common.Memory
|
|
|
|
{
|
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()
2024-04-05 01:23:03 +00:00
|
|
|
public partial class ByteMemoryPool
|
2023-04-24 02:06:23 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Represents a <see cref="IMemoryOwner{Byte}"/> that wraps an array rented from
|
|
|
|
/// <see cref="ArrayPool{Byte}.Shared"/> and exposes it as <see cref="Memory{Byte}"/>
|
|
|
|
/// with a length of the requested size.
|
|
|
|
/// </summary>
|
|
|
|
private sealed class ByteMemoryPoolBuffer : IMemoryOwner<byte>
|
|
|
|
{
|
|
|
|
private byte[] _array;
|
|
|
|
private readonly int _length;
|
|
|
|
|
|
|
|
public ByteMemoryPoolBuffer(int length)
|
|
|
|
{
|
|
|
|
_array = ArrayPool<byte>.Shared.Rent(length);
|
|
|
|
_length = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a <see cref="Memory{Byte}"/> belonging to this owner.
|
|
|
|
/// </summary>
|
|
|
|
public Memory<byte> Memory
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
byte[] array = _array;
|
2023-06-28 16:41:38 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
ObjectDisposedException.ThrowIf(array is null, this);
|
|
|
|
|
|
|
|
return new Memory<byte>(array, 0, _length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
var array = Interlocked.Exchange(ref _array, null);
|
2023-06-28 16:41:38 +00:00
|
|
|
|
2023-04-24 02:06:23 +00:00
|
|
|
if (array != null)
|
|
|
|
{
|
|
|
|
ArrayPool<byte>.Shared.Return(array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|