stubs.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. )
  21. //go:noescape
  22. //go:linkname Memmove runtime.memmove
  23. func Memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)
  24. //go:noescape
  25. //go:linkname MemEqual runtime.memequal
  26. //goland:noinspection GoUnusedParameter
  27. func MemEqual(a unsafe.Pointer, b unsafe.Pointer, size uintptr) bool
  28. //go:linkname Mapiternext runtime.mapiternext
  29. func Mapiternext(it *GoMapIterator)
  30. //go:linkname Mapiterinit runtime.mapiterinit
  31. func Mapiterinit(t *GoMapType, m unsafe.Pointer, it *GoMapIterator)
  32. //go:linkname Maplen reflect.maplen
  33. func Maplen(h unsafe.Pointer) int
  34. //go:linkname IsValidNumber encoding/json.isValidNumber
  35. func IsValidNumber(s string) bool
  36. //go:nosplit
  37. //go:linkname MemclrHasPointers runtime.memclrHasPointers
  38. //goland:noinspection GoUnusedParameter
  39. func MemclrHasPointers(ptr unsafe.Pointer, n uintptr)
  40. //go:linkname MemclrNoHeapPointers runtime.memclrNoHeapPointers
  41. //goland:noinspection GoUnusedParameter
  42. func MemclrNoHeapPointers(ptr unsafe.Pointer, n uintptr)
  43. //go:linkname newarray runtime.newarray
  44. func newarray(typ *GoType, n int) unsafe.Pointer
  45. func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
  46. return unsafe.Pointer(uintptr(p) + x)
  47. }
  48. func ClearMemory(et *GoType, ptr unsafe.Pointer, size uintptr) {
  49. if et.PtrData == 0 {
  50. MemclrNoHeapPointers(ptr, size)
  51. } else {
  52. MemclrHasPointers(ptr, size)
  53. }
  54. }
  55. // runtime.maxElementSize
  56. const _max_map_element_size uintptr = 128
  57. func IsMapfast(vt reflect.Type) bool {
  58. return vt.Elem().Size() <= _max_map_element_size
  59. }
  60. //go:linkname Mallocgc runtime.mallocgc
  61. //goland:noinspection GoUnusedParameter
  62. func Mallocgc(size uintptr, typ *GoType, needzero bool) unsafe.Pointer
  63. //go:linkname Makemap reflect.makemap
  64. func Makemap(*GoType, int) unsafe.Pointer
  65. //go:linkname MakemapSmall runtime.makemap_small
  66. func MakemapSmall() unsafe.Pointer
  67. //go:linkname Mapassign runtime.mapassign
  68. //goland:noinspection GoUnusedParameter
  69. func Mapassign(t *GoMapType, h unsafe.Pointer, k unsafe.Pointer) unsafe.Pointer
  70. //go:linkname Mapassign_fast32 runtime.mapassign_fast32
  71. //goland:noinspection GoUnusedParameter
  72. func Mapassign_fast32(t *GoMapType, h unsafe.Pointer, k uint32) unsafe.Pointer
  73. //go:linkname Mapassign_fast64 runtime.mapassign_fast64
  74. //goland:noinspection GoUnusedParameter
  75. func Mapassign_fast64(t *GoMapType, h unsafe.Pointer, k uint64) unsafe.Pointer
  76. //go:linkname Mapassign_faststr runtime.mapassign_faststr
  77. //goland:noinspection GoUnusedParameter
  78. func Mapassign_faststr(t *GoMapType, h unsafe.Pointer, s string) unsafe.Pointer
  79. type MapStrAssign func (t *GoMapType, h unsafe.Pointer, s string) unsafe.Pointer
  80. func GetMapStrAssign(vt reflect.Type) MapStrAssign {
  81. if IsMapfast(vt) {
  82. return Mapassign_faststr
  83. } else {
  84. return func (t *GoMapType, h unsafe.Pointer, s string) unsafe.Pointer {
  85. return Mapassign(t, h, unsafe.Pointer(&s))
  86. }
  87. }
  88. }
  89. type Map32Assign func(t *GoMapType, h unsafe.Pointer, k uint32) unsafe.Pointer
  90. func GetMap32Assign(vt reflect.Type) Map32Assign {
  91. if IsMapfast(vt) {
  92. return Mapassign_fast32
  93. } else {
  94. return func (t *GoMapType, h unsafe.Pointer, s uint32) unsafe.Pointer {
  95. return Mapassign(t, h, unsafe.Pointer(&s))
  96. }
  97. }
  98. }
  99. type Map64Assign func(t *GoMapType, h unsafe.Pointer, k uint64) unsafe.Pointer
  100. func GetMap64Assign(vt reflect.Type) Map64Assign {
  101. if IsMapfast(vt) {
  102. return Mapassign_fast64
  103. } else {
  104. return func (t *GoMapType, h unsafe.Pointer, s uint64) unsafe.Pointer {
  105. return Mapassign(t, h, unsafe.Pointer(&s))
  106. }
  107. }
  108. }
  109. var emptyBytes = make([]byte, 0, 0)
  110. var EmptySlice = *(*GoSlice)(unsafe.Pointer(&emptyBytes))
  111. //go:linkname MakeSliceStd runtime.makeslice
  112. //goland:noinspection GoUnusedParameter
  113. func MakeSliceStd(et *GoType, len int, cap int) unsafe.Pointer
  114. func MakeSlice(oldPtr unsafe.Pointer, et *GoType, newLen int) *GoSlice {
  115. if newLen == 0 {
  116. return &EmptySlice
  117. }
  118. if *(*unsafe.Pointer)(oldPtr) == nil {
  119. return &GoSlice{
  120. Ptr: MakeSliceStd(et, newLen, newLen),
  121. Len: newLen,
  122. Cap: newLen,
  123. }
  124. }
  125. old := (*GoSlice)(oldPtr)
  126. if old.Cap >= newLen {
  127. old.Len = newLen
  128. return old
  129. }
  130. new := GrowSlice(et, *old, newLen)
  131. // we should clear the memory from [oldLen:newLen]
  132. if et.PtrData == 0 {
  133. oldlenmem := uintptr(old.Len) * et.Size
  134. newlenmem := uintptr(newLen) * et.Size
  135. MemclrNoHeapPointers(add(new.Ptr, oldlenmem), newlenmem-oldlenmem)
  136. }
  137. new.Len = newLen
  138. return &new
  139. }
  140. //go:nosplit
  141. //go:linkname Throw runtime.throw
  142. //goland:noinspection GoUnusedParameter
  143. func Throw(s string)
  144. //go:linkname ConvT64 runtime.convT64
  145. //goland:noinspection GoUnusedParameter
  146. func ConvT64(v uint64) unsafe.Pointer
  147. //go:linkname ConvTslice runtime.convTslice
  148. //goland:noinspection GoUnusedParameter
  149. func ConvTslice(v []byte) unsafe.Pointer
  150. //go:linkname ConvTstring runtime.convTstring
  151. //goland:noinspection GoUnusedParameter
  152. func ConvTstring(v string) unsafe.Pointer
  153. //go:linkname Mapassign_fast64ptr runtime.mapassign_fast64ptr
  154. //goland:noinspection GoUnusedParameter
  155. func Mapassign_fast64ptr(t *GoMapType, h unsafe.Pointer, k unsafe.Pointer) unsafe.Pointer
  156. //go:noescape
  157. //go:linkname Strhash runtime.strhash
  158. func Strhash(_ unsafe.Pointer, _ uintptr) uintptr