src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/mods/unix/rlim_t_uint64.go (about)

     1  //go:build unix && !freebsd
     2  
     3  package unix
     4  
     5  import (
     6  	"math"
     7  	"math/big"
     8  	"strconv"
     9  )
    10  
    11  type rlimT = uint64
    12  
    13  var rlimTValid = "number between 0 and " + strconv.FormatUint(math.MaxUint64, 10)
    14  
    15  const maxInt = uint64(^uint(0) >> 1)
    16  
    17  func convertRlimT(x uint64) any {
    18  	if x <= maxInt {
    19  		return int(x)
    20  	}
    21  	if x <= math.MaxInt64 {
    22  		return big.NewInt(int64(x))
    23  	}
    24  	z := big.NewInt(int64(x / 2))
    25  	z.Lsh(z, 1)
    26  	if x%2 == 1 {
    27  		z.Bits()[0] |= 1
    28  	}
    29  	return z
    30  
    31  }
    32  
    33  func parseRlimT(val any) (uint64, bool) {
    34  	switch val := val.(type) {
    35  	case int:
    36  		if val >= 0 {
    37  			return uint64(val), true
    38  		}
    39  	case *big.Int:
    40  		if val.IsUint64() {
    41  			return val.Uint64(), true
    42  		}
    43  	case string:
    44  		num, err := strconv.ParseUint(val, 0, 64)
    45  		if err == nil {
    46  			return num, true
    47  		}
    48  	}
    49  	return 0, false
    50  }