fastmem.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2021 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package rt
  17. import (
  18. `unsafe`
  19. `reflect`
  20. )
  21. //go:nosplit
  22. func Get16(v []byte) int16 {
  23. return *(*int16)((*GoSlice)(unsafe.Pointer(&v)).Ptr)
  24. }
  25. //go:nosplit
  26. func Get32(v []byte) int32 {
  27. return *(*int32)((*GoSlice)(unsafe.Pointer(&v)).Ptr)
  28. }
  29. //go:nosplit
  30. func Get64(v []byte) int64 {
  31. return *(*int64)((*GoSlice)(unsafe.Pointer(&v)).Ptr)
  32. }
  33. //go:nosplit
  34. func Mem2Str(v []byte) (s string) {
  35. (*GoString)(unsafe.Pointer(&s)).Len = (*GoSlice)(unsafe.Pointer(&v)).Len
  36. (*GoString)(unsafe.Pointer(&s)).Ptr = (*GoSlice)(unsafe.Pointer(&v)).Ptr
  37. return
  38. }
  39. //go:nosplit
  40. func Str2Mem(s string) (v []byte) {
  41. (*GoSlice)(unsafe.Pointer(&v)).Cap = (*GoString)(unsafe.Pointer(&s)).Len
  42. (*GoSlice)(unsafe.Pointer(&v)).Len = (*GoString)(unsafe.Pointer(&s)).Len
  43. (*GoSlice)(unsafe.Pointer(&v)).Ptr = (*GoString)(unsafe.Pointer(&s)).Ptr
  44. return
  45. }
  46. func BytesFrom(p unsafe.Pointer, n int, c int) (r []byte) {
  47. (*GoSlice)(unsafe.Pointer(&r)).Ptr = p
  48. (*GoSlice)(unsafe.Pointer(&r)).Len = n
  49. (*GoSlice)(unsafe.Pointer(&r)).Cap = c
  50. return
  51. }
  52. func FuncAddr(f interface{}) unsafe.Pointer {
  53. if vv := UnpackEface(f); vv.Type.Kind() != reflect.Func {
  54. panic("f is not a function")
  55. } else {
  56. return *(*unsafe.Pointer)(vv.Value)
  57. }
  58. }
  59. func IndexChar(src string, index int) unsafe.Pointer {
  60. return unsafe.Pointer(uintptr((*GoString)(unsafe.Pointer(&src)).Ptr) + uintptr(index))
  61. }
  62. func IndexByte(ptr []byte, index int) unsafe.Pointer {
  63. return unsafe.Pointer(uintptr((*GoSlice)(unsafe.Pointer(&ptr)).Ptr) + uintptr(index))
  64. }
  65. //go:nosplit
  66. func GuardSlice(buf *[]byte, n int) {
  67. c := cap(*buf)
  68. l := len(*buf)
  69. if c-l < n {
  70. c = c>>1 + n + l
  71. if c < 32 {
  72. c = 32
  73. }
  74. tmp := make([]byte, l, c)
  75. copy(tmp, *buf)
  76. *buf = tmp
  77. }
  78. }
  79. //go:nosplit
  80. func Ptr2SlicePtr(s unsafe.Pointer, l int, c int) unsafe.Pointer {
  81. slice := &GoSlice{
  82. Ptr: s,
  83. Len: l,
  84. Cap: c,
  85. }
  86. return unsafe.Pointer(slice)
  87. }
  88. //go:nosplit
  89. func StrPtr(s string) unsafe.Pointer {
  90. return (*GoString)(unsafe.Pointer(&s)).Ptr
  91. }
  92. //go:nosplit
  93. func StrFrom(p unsafe.Pointer, n int64) (s string) {
  94. (*GoString)(unsafe.Pointer(&s)).Ptr = p
  95. (*GoString)(unsafe.Pointer(&s)).Len = int(n)
  96. return
  97. }
  98. // NoEscape hides a pointer from escape analysis. NoEscape is
  99. // the identity function but escape analysis doesn't think the
  100. // output depends on the input. NoEscape is inlined and currently
  101. // compiles down to zero instructions.
  102. // USE CAREFULLY!
  103. //go:nosplit
  104. //goland:noinspection GoVetUnsafePointer
  105. func NoEscape(p unsafe.Pointer) unsafe.Pointer {
  106. x := uintptr(p)
  107. return unsafe.Pointer(x ^ 0)
  108. }