fastmem.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "reflect"
  19. "unsafe"
  20. "github.com/bytedance/sonic/option"
  21. )
  22. //go:nosplit
  23. func Get16(v []byte) int16 {
  24. return *(*int16)((*GoSlice)(unsafe.Pointer(&v)).Ptr)
  25. }
  26. //go:nosplit
  27. func Get32(v []byte) int32 {
  28. return *(*int32)((*GoSlice)(unsafe.Pointer(&v)).Ptr)
  29. }
  30. //go:nosplit
  31. func Get64(v []byte) int64 {
  32. return *(*int64)((*GoSlice)(unsafe.Pointer(&v)).Ptr)
  33. }
  34. //go:nosplit
  35. func Mem2Str(v []byte) (s string) {
  36. (*GoString)(unsafe.Pointer(&s)).Len = (*GoSlice)(unsafe.Pointer(&v)).Len
  37. (*GoString)(unsafe.Pointer(&s)).Ptr = (*GoSlice)(unsafe.Pointer(&v)).Ptr
  38. return
  39. }
  40. //go:nosplit
  41. func Str2Mem(s string) (v []byte) {
  42. (*GoSlice)(unsafe.Pointer(&v)).Cap = (*GoString)(unsafe.Pointer(&s)).Len
  43. (*GoSlice)(unsafe.Pointer(&v)).Len = (*GoString)(unsafe.Pointer(&s)).Len
  44. (*GoSlice)(unsafe.Pointer(&v)).Ptr = (*GoString)(unsafe.Pointer(&s)).Ptr
  45. return
  46. }
  47. func BytesFrom(p unsafe.Pointer, n int, c int) (r []byte) {
  48. (*GoSlice)(unsafe.Pointer(&r)).Ptr = p
  49. (*GoSlice)(unsafe.Pointer(&r)).Len = n
  50. (*GoSlice)(unsafe.Pointer(&r)).Cap = c
  51. return
  52. }
  53. func FuncAddr(f interface{}) unsafe.Pointer {
  54. if vv := UnpackEface(f); vv.Type.Kind() != reflect.Func {
  55. panic("f is not a function")
  56. } else {
  57. return *(*unsafe.Pointer)(vv.Value)
  58. }
  59. }
  60. //go:nocheckptr
  61. func IndexChar(src string, index int) unsafe.Pointer {
  62. return unsafe.Pointer(uintptr((*GoString)(unsafe.Pointer(&src)).Ptr) + uintptr(index))
  63. }
  64. //go:nocheckptr
  65. func IndexByte(ptr []byte, index int) unsafe.Pointer {
  66. return unsafe.Pointer(uintptr((*GoSlice)(unsafe.Pointer(&ptr)).Ptr) + uintptr(index))
  67. }
  68. func GuardSlice(buf *[]byte, n int) {
  69. c := cap(*buf)
  70. l := len(*buf)
  71. if c-l < n {
  72. c = c>>1 + n + l
  73. if c < 32 {
  74. c = 32
  75. }
  76. tmp := make([]byte, l, c)
  77. copy(tmp, *buf)
  78. *buf = tmp
  79. }
  80. }
  81. func GuardSlice2(buf []byte, n int) []byte {
  82. c := cap(buf)
  83. l := len(buf)
  84. if c-l < n {
  85. c = c>>1 + n + l
  86. if c < 32 {
  87. c = 32
  88. }
  89. tmp := make([]byte, l, c)
  90. copy(tmp, buf)
  91. buf = tmp
  92. }
  93. return buf
  94. }
  95. //go:nosplit
  96. func Ptr2SlicePtr(s unsafe.Pointer, l int, c int) unsafe.Pointer {
  97. slice := &GoSlice{
  98. Ptr: s,
  99. Len: l,
  100. Cap: c,
  101. }
  102. return unsafe.Pointer(slice)
  103. }
  104. //go:nosplit
  105. func StrPtr(s string) unsafe.Pointer {
  106. return (*GoString)(unsafe.Pointer(&s)).Ptr
  107. }
  108. //go:nosplit
  109. func StrFrom(p unsafe.Pointer, n int64) (s string) {
  110. (*GoString)(unsafe.Pointer(&s)).Ptr = p
  111. (*GoString)(unsafe.Pointer(&s)).Len = int(n)
  112. return
  113. }
  114. // NoEscape hides a pointer from escape analysis. NoEscape is
  115. // the identity function but escape analysis doesn't think the
  116. // output depends on the input. NoEscape is inlined and currently
  117. // compiles down to zero instructions.
  118. // USE CAREFULLY!
  119. //go:nosplit
  120. //goland:noinspection GoVetUnsafePointer
  121. func NoEscape(p unsafe.Pointer) unsafe.Pointer {
  122. x := uintptr(p)
  123. return unsafe.Pointer(x ^ 0)
  124. }
  125. //go:nosplit
  126. func MoreStack(size uintptr)
  127. //go:nosplit
  128. func Add(ptr unsafe.Pointer, off uintptr) unsafe.Pointer {
  129. return unsafe.Pointer(uintptr(ptr) + off)
  130. }
  131. // CanSizeResue
  132. func CanSizeResue(cap int) bool {
  133. return cap <= int(option.LimitBufferSize)
  134. }