HostMemoryRange.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. namespace Ryujinx.Memory.Range
  3. {
  4. /// <summary>
  5. /// Range of memory composed of an address and size.
  6. /// </summary>
  7. public struct HostMemoryRange : IEquatable<HostMemoryRange>
  8. {
  9. /// <summary>
  10. /// An empty memory range, with a null address and zero size.
  11. /// </summary>
  12. public static HostMemoryRange Empty => new HostMemoryRange(0, 0);
  13. /// <summary>
  14. /// Start address of the range.
  15. /// </summary>
  16. public nuint Address { get; }
  17. /// <summary>
  18. /// Size of the range in bytes.
  19. /// </summary>
  20. public ulong Size { get; }
  21. /// <summary>
  22. /// Address where the range ends (exclusive).
  23. /// </summary>
  24. public nuint EndAddress => Address + (nuint)Size;
  25. /// <summary>
  26. /// Creates a new memory range with the specified address and size.
  27. /// </summary>
  28. /// <param name="address">Start address</param>
  29. /// <param name="size">Size in bytes</param>
  30. public HostMemoryRange(nuint address, ulong size)
  31. {
  32. Address = address;
  33. Size = size;
  34. }
  35. /// <summary>
  36. /// Checks if the range overlaps with another.
  37. /// </summary>
  38. /// <param name="other">The other range to check for overlap</param>
  39. /// <returns>True if the ranges overlap, false otherwise</returns>
  40. public bool OverlapsWith(HostMemoryRange other)
  41. {
  42. nuint thisAddress = Address;
  43. nuint thisEndAddress = EndAddress;
  44. nuint otherAddress = other.Address;
  45. nuint otherEndAddress = other.EndAddress;
  46. return thisAddress < otherEndAddress && otherAddress < thisEndAddress;
  47. }
  48. public override bool Equals(object obj)
  49. {
  50. return obj is HostMemoryRange other && Equals(other);
  51. }
  52. public bool Equals(HostMemoryRange other)
  53. {
  54. return Address == other.Address && Size == other.Size;
  55. }
  56. public override int GetHashCode()
  57. {
  58. return HashCode.Combine(Address, Size);
  59. }
  60. }
  61. }