github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/memmap/file_range.go (about)

     1  package memmap
     2  
     3  // A Range represents a contiguous range of T.
     4  //
     5  // +stateify savable
     6  type FileRange struct {
     7  	// Start is the inclusive start of the range.
     8  	Start uint64
     9  
    10  	// End is the exclusive end of the range.
    11  	End uint64
    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 FileRange) 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 FileRange) Length() uint64 {
    26  	return r.End - r.Start
    27  }
    28  
    29  // Contains returns true if r contains x.
    30  //
    31  //go:nosplit
    32  func (r FileRange) Contains(x uint64) 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 FileRange) Overlaps(r2 FileRange) 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 FileRange) IsSupersetOf(r2 FileRange) 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 FileRange) Intersect(r2 FileRange) FileRange {
    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 FileRange) CanSplitAt(x uint64) bool {
    75  	return r.Contains(x) && r.Start < x
    76  }