rtutil.go 2.7 KB

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