github.com/dgraph-io/ristretto@v0.1.2-0.20240116140435-c67e07994f91/z/rtutil.go (about)

     1  // MIT License
     2  
     3  // Copyright (c) 2019 Ewan Chou
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in all
    13  // copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    21  // SOFTWARE.
    22  
    23  package z
    24  
    25  import (
    26  	"unsafe"
    27  )
    28  
    29  // NanoTime returns the current time in nanoseconds from a monotonic clock.
    30  //
    31  //go:linkname NanoTime runtime.nanotime
    32  func NanoTime() int64
    33  
    34  // CPUTicks is a faster alternative to NanoTime to measure time duration.
    35  //
    36  //go:linkname CPUTicks runtime.cputicks
    37  func CPUTicks() int64
    38  
    39  type stringStruct struct {
    40  	str unsafe.Pointer
    41  	len int
    42  }
    43  
    44  //go:noescape
    45  //go:linkname memhash runtime.memhash
    46  func memhash(p unsafe.Pointer, h, s uintptr) uintptr
    47  
    48  // MemHash is the hash function used by go map, it utilizes available hardware instructions(behaves
    49  // as aeshash if aes instruction is available).
    50  // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
    51  func MemHash(data []byte) uint64 {
    52  	ss := (*stringStruct)(unsafe.Pointer(&data))
    53  	return uint64(memhash(ss.str, 0, uintptr(ss.len)))
    54  }
    55  
    56  // MemHashString is the hash function used by go map, it utilizes available hardware instructions
    57  // (behaves as aeshash if aes instruction is available).
    58  // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
    59  func MemHashString(str string) uint64 {
    60  	ss := (*stringStruct)(unsafe.Pointer(&str))
    61  	return uint64(memhash(ss.str, 0, uintptr(ss.len)))
    62  }
    63  
    64  // FastRand is a fast thread local random function.
    65  //
    66  //go:linkname FastRand runtime.fastrand
    67  func FastRand() uint32
    68  
    69  //go:linkname memclrNoHeapPointers runtime.memclrNoHeapPointers
    70  func memclrNoHeapPointers(p unsafe.Pointer, n uintptr)
    71  
    72  func Memclr(b []byte) {
    73  	if len(b) == 0 {
    74  		return
    75  	}
    76  	p := unsafe.Pointer(&b[0])
    77  	memclrNoHeapPointers(p, uintptr(len(b)))
    78  }