stubs.go raw

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