IMemoryManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. namespace ARMeilleure.Memory
  3. {
  4. public interface IMemoryManager
  5. {
  6. int AddressSpaceBits { get; }
  7. IntPtr PageTablePointer { get; }
  8. MemoryManagerType Type { get; }
  9. event Action<ulong, ulong> UnmapEvent;
  10. /// <summary>
  11. /// Reads data from CPU mapped memory.
  12. /// </summary>
  13. /// <typeparam name="T">Type of the data being read</typeparam>
  14. /// <param name="va">Virtual address of the data in memory</param>
  15. /// <returns>The data</returns>
  16. T Read<T>(ulong va) where T : unmanaged;
  17. /// <summary>
  18. /// Reads data from CPU mapped memory, with read tracking
  19. /// </summary>
  20. /// <typeparam name="T">Type of the data being read</typeparam>
  21. /// <param name="va">Virtual address of the data in memory</param>
  22. /// <returns>The data</returns>
  23. T ReadTracked<T>(ulong va) where T : unmanaged;
  24. /// <summary>
  25. /// Writes data to CPU mapped memory.
  26. /// </summary>
  27. /// <typeparam name="T">Type of the data being written</typeparam>
  28. /// <param name="va">Virtual address to write the data into</param>
  29. /// <param name="value">Data to be written</param>
  30. void Write<T>(ulong va, T value) where T : unmanaged;
  31. /// <summary>
  32. /// Gets a read-only span of data from CPU mapped memory.
  33. /// </summary>
  34. /// <param name="va">Virtual address of the data</param>
  35. /// <param name="size">Size of the data</param>
  36. /// <param name="tracked">True if read tracking is triggered on the span</param>
  37. /// <returns>A read-only span of the data</returns>
  38. ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false);
  39. /// <summary>
  40. /// Gets a reference for the given type at the specified virtual memory address.
  41. /// </summary>
  42. /// <remarks>
  43. /// The data must be located at a contiguous memory region.
  44. /// </remarks>
  45. /// <typeparam name="T">Type of the data to get the reference</typeparam>
  46. /// <param name="va">Virtual address of the data</param>
  47. /// <returns>A reference to the data in memory</returns>
  48. ref T GetRef<T>(ulong va) where T : unmanaged;
  49. /// <summary>
  50. /// Checks if the page at a given CPU virtual address is mapped.
  51. /// </summary>
  52. /// <param name="va">Virtual address to check</param>
  53. /// <returns>True if the address is mapped, false otherwise</returns>
  54. bool IsMapped(ulong va);
  55. /// <summary>
  56. /// Alerts the memory tracking that a given region has been read from or written to.
  57. /// This should be called before read/write is performed.
  58. /// </summary>
  59. /// <param name="va">Virtual address of the region</param>
  60. /// <param name="size">Size of the region</param>
  61. /// <param name="write">True if the region was written, false if read</param>
  62. void SignalMemoryTracking(ulong va, ulong size, bool write);
  63. }
  64. }