| 1 | package hostarch |
| 2 | |
| 3 | // A Range represents a contiguous range of T. |
| 4 | // |
| 5 | // +stateify savable |
| 6 | type AddrRange struct { |
| 7 | // Start is the inclusive start of the range. |
| 8 | Start Addr |
| 9 | |
| 10 | // End is the exclusive end of the range. |
| 11 | End Addr |
| 12 | } |
| 13 | |
| 14 | // WellFormed returns true if r.Start <= r.End. All other methods on a Range |
| 15 | // require that the Range is well-formed. |
| 16 | // |
| 17 | //go:nosplit |
| 18 | func (r AddrRange) WellFormed() bool { |
| 19 | return r.Start <= r.End |
| 20 | } |
| 21 | |
| 22 | // Length returns the length of the range. |
| 23 | // |
| 24 | //go:nosplit |
| 25 | func (r AddrRange) Length() Addr { |
| 26 | return r.End - r.Start |
| 27 | } |
| 28 | |
| 29 | // Contains returns true if r contains x. |
| 30 | // |
| 31 | //go:nosplit |
| 32 | func (r AddrRange) Contains(x Addr) bool { |
| 33 | return r.Start <= x && x < r.End |
| 34 | } |
| 35 | |
| 36 | // Overlaps returns true if r and r2 overlap. |
| 37 | // |
| 38 | //go:nosplit |
| 39 | func (r AddrRange) Overlaps(r2 AddrRange) bool { |
| 40 | return r.Start < r2.End && r2.Start < r.End |
| 41 | } |
| 42 | |
| 43 | // IsSupersetOf returns true if r is a superset of r2; that is, the range r2 is |
| 44 | // contained within r. |
| 45 | // |
| 46 | //go:nosplit |
| 47 | func (r AddrRange) IsSupersetOf(r2 AddrRange) bool { |
| 48 | return r.Start <= r2.Start && r.End >= r2.End |
| 49 | } |
| 50 | |
| 51 | // Intersect returns a range consisting of the intersection between r and r2. |
| 52 | // If r and r2 do not overlap, Intersect returns a range with unspecified |
| 53 | // bounds, but for which Length() == 0. |
| 54 | // |
| 55 | //go:nosplit |
| 56 | func (r AddrRange) Intersect(r2 AddrRange) AddrRange { |
| 57 | if r.Start < r2.Start { |
| 58 | r.Start = r2.Start |
| 59 | } |
| 60 | if r.End > r2.End { |
| 61 | r.End = r2.End |
| 62 | } |
| 63 | if r.End < r.Start { |
| 64 | r.End = r.Start |
| 65 | } |
| 66 | return r |
| 67 | } |
| 68 | |
| 69 | // CanSplitAt returns true if it is legal to split a segment spanning the range |
| 70 | // r at x; that is, splitting at x would produce two ranges, both of which have |
| 71 | // non-zero length. |
| 72 | // |
| 73 | //go:nosplit |
| 74 | func (r AddrRange) CanSplitAt(x Addr) bool { |
| 75 | return r.Contains(x) && r.Start < x |
| 76 | } |