github.com/sagernet/gvisor@v0.0.0-20240428053021-e691de28565f/pkg/state/addr_range.go (about) 1 package state 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 uintptr 9 10 // End is the exclusive end of the range. 11 End uintptr 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() uintptr { 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 uintptr) 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 uintptr) bool { 75 return r.Contains(x) && r.Start < x 76 }