rtutil.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. //
  24. //go:linkname NanoTime runtime.nanotime
  25. func NanoTime() int64
  26. // CPUTicks is a faster alternative to NanoTime to measure time duration.
  27. //
  28. //go:linkname CPUTicks runtime.cputicks
  29. func CPUTicks() int64
  30. type stringStruct struct {
  31. str unsafe.Pointer
  32. len int
  33. }
  34. //go:noescape
  35. //go:linkname memhash runtime.memhash
  36. func memhash(p unsafe.Pointer, h, s uintptr) uintptr
  37. // MemHash is the hash function used by go map, it utilizes available hardware instructions(behaves
  38. // as aeshash if aes instruction is available).
  39. // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
  40. func MemHash(data []byte) uint64 {
  41. ss := (*stringStruct)(unsafe.Pointer(&data))
  42. return uint64(memhash(ss.str, 0, uintptr(ss.len)))
  43. }
  44. // MemHashString is the hash function used by go map, it utilizes available hardware instructions
  45. // (behaves as aeshash if aes instruction is available).
  46. // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
  47. func MemHashString(str string) uint64 {
  48. ss := (*stringStruct)(unsafe.Pointer(&str))
  49. return uint64(memhash(ss.str, 0, uintptr(ss.len)))
  50. }
  51. // FastRand is a fast thread local random function.
  52. //
  53. //go:linkname FastRand runtime.fastrand
  54. func FastRand() uint32
  55. //go:linkname memclrNoHeapPointers runtime.memclrNoHeapPointers
  56. func memclrNoHeapPointers(p unsafe.Pointer, n uintptr)
  57. func Memclr(b []byte) {
  58. if len(b) == 0 {
  59. return
  60. }
  61. p := unsafe.Pointer(&b[0])
  62. memclrNoHeapPointers(p, uintptr(len(b)))
  63. }