MemoryRange.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. namespace Ryujinx.Memory.Range
  2. {
  3. /// <summary>
  4. /// Range of memory composed of an address and size.
  5. /// </summary>
  6. public readonly record struct MemoryRange
  7. {
  8. /// <summary>
  9. /// An empty memory range, with a null address and zero size.
  10. /// </summary>
  11. public static MemoryRange Empty => new MemoryRange(0UL, 0);
  12. /// <summary>
  13. /// Start address of the range.
  14. /// </summary>
  15. public ulong Address { get; }
  16. /// <summary>
  17. /// Size of the range in bytes.
  18. /// </summary>
  19. public ulong Size { get; }
  20. /// <summary>
  21. /// Address where the range ends (exclusive).
  22. /// </summary>
  23. public ulong EndAddress => Address + Size;
  24. /// <summary>
  25. /// Creates a new memory range with the specified address and size.
  26. /// </summary>
  27. /// <param name="address">Start address</param>
  28. /// <param name="size">Size in bytes</param>
  29. public MemoryRange(ulong address, ulong size)
  30. {
  31. Address = address;
  32. Size = size;
  33. }
  34. /// <summary>
  35. /// Checks if the range overlaps with another.
  36. /// </summary>
  37. /// <param name="other">The other range to check for overlap</param>
  38. /// <returns>True if the ranges overlap, false otherwise</returns>
  39. public bool OverlapsWith(MemoryRange other)
  40. {
  41. ulong thisAddress = Address;
  42. ulong thisEndAddress = EndAddress;
  43. ulong otherAddress = other.Address;
  44. ulong otherEndAddress = other.EndAddress;
  45. // If any of the ranges if invalid (address + size overflows),
  46. // then they are never considered to overlap.
  47. if (thisEndAddress < thisAddress || otherEndAddress < otherAddress)
  48. {
  49. return false;
  50. }
  51. return thisAddress < otherEndAddress && otherAddress < thisEndAddress;
  52. }
  53. }
  54. }