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