| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- using System;
- using System.Runtime.CompilerServices;
- using System.Threading;
- namespace Ryujinx.Memory
- {
- /// <summary>
- /// Represents a block of contiguous physical guest memory.
- /// </summary>
- public sealed class MemoryBlock : IDisposable
- {
- private IntPtr _pointer;
- /// <summary>
- /// Pointer to the memory block data.
- /// </summary>
- public IntPtr Pointer => _pointer;
- /// <summary>
- /// Size of the memory block.
- /// </summary>
- public ulong Size { get; }
- /// <summary>
- /// Initializes a new instance of the memory block class.
- /// </summary>
- /// <param name="size">Size of the memory block</param>
- /// <param name="flags">Flags that controls memory block memory allocation</param>
- /// <exception cref="OutOfMemoryException">Throw when there's no enough memory to allocate the requested size</exception>
- /// <exception cref="PlatformNotSupportedException">Throw when the current platform is not supported</exception>
- public MemoryBlock(ulong size, MemoryAllocationFlags flags = MemoryAllocationFlags.None)
- {
- if (flags.HasFlag(MemoryAllocationFlags.Reserve))
- {
- _pointer = MemoryManagement.Reserve(size);
- }
- else
- {
- _pointer = MemoryManagement.Allocate(size);
- }
- Size = size;
- GC.AddMemoryPressure((long)Size);
- }
- /// <summary>
- /// Commits a region of memory that has previously been reserved.
- /// This can be used to allocate memory on demand.
- /// </summary>
- /// <param name="offset">Starting offset of the range to be committed</param>
- /// <param name="size">Size of the range to be committed</param>
- /// <returns>True if the operation was successful, false otherwise</returns>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
- public bool Commit(ulong offset, ulong size)
- {
- return MemoryManagement.Commit(GetPointerInternal(offset, size), size);
- }
- /// <summary>
- /// Reprotects a region of memory.
- /// </summary>
- /// <param name="offset">Starting offset of the range to be reprotected</param>
- /// <param name="size">Size of the range to be reprotected</param>
- /// <param name="permission">New memory permissions</param>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
- /// <exception cref="MemoryProtectionException">Throw when <paramref name="permission"/> is invalid</exception>
- public void Reprotect(ulong offset, ulong size, MemoryPermission permission)
- {
- MemoryManagement.Reprotect(GetPointerInternal(offset, size), size, permission);
- }
- /// <summary>
- /// Reads bytes from the memory block.
- /// </summary>
- /// <param name="offset">Starting offset of the range being read</param>
- /// <param name="data">Span where the bytes being read will be copied to</param>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the the data is out of range</exception>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Read(ulong offset, Span<byte> data)
- {
- GetSpan(offset, data.Length).CopyTo(data);
- }
- /// <summary>
- /// Reads data from the memory block.
- /// </summary>
- /// <typeparam name="T">Type of the data</typeparam>
- /// <param name="offset">Offset where the data is located</param>
- /// <returns>Data at the specified address</returns>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the the data is out of range</exception>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public T Read<T>(ulong offset) where T : unmanaged
- {
- return GetRef<T>(offset);
- }
- /// <summary>
- /// Writes bytes to the memory block.
- /// </summary>
- /// <param name="offset">Starting offset of the range being written</param>
- /// <param name="data">Span where the bytes being written will be copied from</param>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the the data is out of range</exception>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Write(ulong offset, ReadOnlySpan<byte> data)
- {
- data.CopyTo(GetSpan(offset, data.Length));
- }
- /// <summary>
- /// Writes data to the memory block.
- /// </summary>
- /// <typeparam name="T">Type of the data being written</typeparam>
- /// <param name="offset">Offset to write the data into</param>
- /// <param name="data">Data to be written</param>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the the data is out of range</exception>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Write<T>(ulong offset, T data) where T : unmanaged
- {
- GetRef<T>(offset) = data;
- }
- /// <summary>
- /// Copies data from one memory location to another.
- /// </summary>
- /// <param name="dstOffset">Destination offset to write the data into</param>
- /// <param name="srcOffset">Source offset to read the data from</param>
- /// <param name="size">Size of the copy in bytes</param>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when <paramref name="srcOffset"/>, <paramref name="dstOffset"/> or <paramref name="size"/> is out of range</exception>
- public void Copy(ulong dstOffset, ulong srcOffset, ulong size)
- {
- const int MaxChunkSize = 1 << 30;
- for (ulong offset = 0; offset < size; offset += MaxChunkSize)
- {
- int copySize = (int)Math.Min(MaxChunkSize, size - offset);
- Write(dstOffset + offset, GetSpan(srcOffset + offset, copySize));
- }
- }
- /// <summary>
- /// Fills a region of memory with zeros.
- /// </summary>
- /// <param name="offset">Offset of the region to fill with zeros</param>
- /// <param name="size">Size in bytes of the region to fill</param>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
- public void ZeroFill(ulong offset, ulong size)
- {
- const int MaxChunkSize = 1 << 30;
- for (ulong subOffset = 0; subOffset < size; subOffset += MaxChunkSize)
- {
- int copySize = (int)Math.Min(MaxChunkSize, size - subOffset);
- GetSpan(offset + subOffset, copySize).Fill(0);
- }
- }
- /// <summary>
- /// Gets a reference of the data at a given memory block region.
- /// </summary>
- /// <typeparam name="T">Data type</typeparam>
- /// <param name="offset">Offset of the memory region</param>
- /// <returns>A reference to the given memory region data</returns>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public unsafe ref T GetRef<T>(ulong offset) where T : unmanaged
- {
- IntPtr ptr = _pointer;
- if (ptr == IntPtr.Zero)
- {
- ThrowObjectDisposed();
- }
- int size = Unsafe.SizeOf<T>();
- ulong endOffset = offset + (ulong)size;
- if (endOffset > Size || endOffset < offset)
- {
- ThrowInvalidMemoryRegionException();
- }
- return ref Unsafe.AsRef<T>((void*)PtrAddr(ptr, offset));
- }
- /// <summary>
- /// Gets the pointer of a given memory block region.
- /// </summary>
- /// <param name="offset">Start offset of the memory region</param>
- /// <param name="size">Size in bytes of the region</param>
- /// <returns>The pointer to the memory region</returns>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public IntPtr GetPointer(ulong offset, int size) => GetPointerInternal(offset, (ulong)size);
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private IntPtr GetPointerInternal(ulong offset, ulong size)
- {
- IntPtr ptr = _pointer;
- if (ptr == IntPtr.Zero)
- {
- ThrowObjectDisposed();
- }
- ulong endOffset = offset + size;
- if (endOffset > Size || endOffset < offset)
- {
- ThrowInvalidMemoryRegionException();
- }
- return PtrAddr(ptr, offset);
- }
- /// <summary>
- /// Gets the <see cref="Span{T}"/> of a given memory block region.
- /// </summary>
- /// <param name="offset">Start offset of the memory region</param>
- /// <param name="size">Size in bytes of the region</param>
- /// <returns>Span of the memory region</returns>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public unsafe Span<byte> GetSpan(ulong offset, int size)
- {
- return new Span<byte>((void*)GetPointer(offset, size), size);
- }
- /// <summary>
- /// Gets the <see cref="Memory{T}"/> of a given memory block region.
- /// </summary>
- /// <param name="offset">Start offset of the memory region</param>
- /// <param name="size">Size in bytes of the region</param>
- /// <returns>Memory of the memory region</returns>
- /// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
- /// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public unsafe Memory<byte> GetMemory(ulong offset, int size)
- {
- return new NativeMemoryManager<byte>((byte*)GetPointer(offset, size), size).Memory;
- }
- /// <summary>
- /// Adds a 64-bits offset to a native pointer.
- /// </summary>
- /// <param name="pointer">Native pointer</param>
- /// <param name="offset">Offset to add</param>
- /// <returns>Native pointer with the added offset</returns>
- private IntPtr PtrAddr(IntPtr pointer, ulong offset)
- {
- return (IntPtr)(pointer.ToInt64() + (long)offset);
- }
- /// <summary>
- /// Frees the memory allocated for this memory block.
- /// </summary>
- /// <remarks>
- /// It's an error to use the memory block after disposal.
- /// </remarks>
- public void Dispose() => FreeMemory();
- ~MemoryBlock() => FreeMemory();
- private void FreeMemory()
- {
- IntPtr ptr = Interlocked.Exchange(ref _pointer, IntPtr.Zero);
- // If pointer is null, the memory was already freed or never allocated.
- if (ptr != IntPtr.Zero)
- {
- MemoryManagement.Free(ptr);
- GC.RemoveMemoryPressure((long)Size);
- }
- }
- private void ThrowObjectDisposed() => throw new ObjectDisposedException(nameof(MemoryBlock));
- private void ThrowInvalidMemoryRegionException() => throw new InvalidMemoryRegionException();
- }
- }
|