diff options
Diffstat (limited to 'vendor/github.com/bytedance/sonic/loader/internal/rt')
3 files changed, 426 insertions, 0 deletions
diff --git a/vendor/github.com/bytedance/sonic/loader/internal/rt/fastmem.go b/vendor/github.com/bytedance/sonic/loader/internal/rt/fastmem.go new file mode 100644 index 000000000..3bc24c4e4 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/loader/internal/rt/fastmem.go @@ -0,0 +1,62 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package rt + +import ( + `unsafe` + `reflect` +) + +//go:nosplit +func Mem2Str(v []byte) (s string) { + (*GoString)(unsafe.Pointer(&s)).Len = (*GoSlice)(unsafe.Pointer(&v)).Len + (*GoString)(unsafe.Pointer(&s)).Ptr = (*GoSlice)(unsafe.Pointer(&v)).Ptr + return +} + +//go:nosplit +func Str2Mem(s string) (v []byte) { + (*GoSlice)(unsafe.Pointer(&v)).Cap = (*GoString)(unsafe.Pointer(&s)).Len + (*GoSlice)(unsafe.Pointer(&v)).Len = (*GoString)(unsafe.Pointer(&s)).Len + (*GoSlice)(unsafe.Pointer(&v)).Ptr = (*GoString)(unsafe.Pointer(&s)).Ptr + return +} + +func BytesFrom(p unsafe.Pointer, n int, c int) (r []byte) { + (*GoSlice)(unsafe.Pointer(&r)).Ptr = p + (*GoSlice)(unsafe.Pointer(&r)).Len = n + (*GoSlice)(unsafe.Pointer(&r)).Cap = c + return +} + +func FuncAddr(f interface{}) unsafe.Pointer { + if vv := UnpackEface(f); vv.Type.Kind() != reflect.Func { + panic("f is not a function") + } else { + return *(*unsafe.Pointer)(vv.Value) + } +} + +//go:nocheckptr +func IndexChar(src string, index int) unsafe.Pointer { + return unsafe.Pointer(uintptr((*GoString)(unsafe.Pointer(&src)).Ptr) + uintptr(index)) +} + +//go:nocheckptr +func IndexByte(ptr []byte, index int) unsafe.Pointer { + return unsafe.Pointer(uintptr((*GoSlice)(unsafe.Pointer(&ptr)).Ptr) + uintptr(index)) +} diff --git a/vendor/github.com/bytedance/sonic/loader/internal/rt/fastvalue.go b/vendor/github.com/bytedance/sonic/loader/internal/rt/fastvalue.go new file mode 100644 index 000000000..e6c5bc869 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/loader/internal/rt/fastvalue.go @@ -0,0 +1,183 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package rt + +import ( + `reflect` + `unsafe` +) + +var ( + reflectRtypeItab = findReflectRtypeItab() +) + +// GoType.KindFlags const +const ( + F_direct = 1 << 5 + F_kind_mask = (1 << 5) - 1 +) + +// GoType.Flags const +const ( + tflagUncommon uint8 = 1 << 0 + tflagExtraStar uint8 = 1 << 1 + tflagNamed uint8 = 1 << 2 + tflagRegularMemory uint8 = 1 << 3 +) + +type GoType struct { + Size uintptr + PtrData uintptr + Hash uint32 + Flags uint8 + Align uint8 + FieldAlign uint8 + KindFlags uint8 + Traits unsafe.Pointer + GCData *byte + Str int32 + PtrToSelf int32 +} + +func (self *GoType) IsNamed() bool { + return (self.Flags & tflagNamed) != 0 +} + +func (self *GoType) Kind() reflect.Kind { + return reflect.Kind(self.KindFlags & F_kind_mask) +} + +func (self *GoType) Pack() (t reflect.Type) { + (*GoIface)(unsafe.Pointer(&t)).Itab = reflectRtypeItab + (*GoIface)(unsafe.Pointer(&t)).Value = unsafe.Pointer(self) + return +} + +func (self *GoType) String() string { + return self.Pack().String() +} + +func (self *GoType) Indirect() bool { + return self.KindFlags & F_direct == 0 +} + +type GoItab struct { + it unsafe.Pointer + Vt *GoType + hv uint32 + _ [4]byte + fn [1]uintptr +} + +type GoIface struct { + Itab *GoItab + Value unsafe.Pointer +} + +type GoEface struct { + Type *GoType + Value unsafe.Pointer +} + +func (self GoEface) Pack() (v interface{}) { + *(*GoEface)(unsafe.Pointer(&v)) = self + return +} + +type GoPtrType struct { + GoType + Elem *GoType +} + +type GoMapType struct { + GoType + Key *GoType + Elem *GoType + Bucket *GoType + Hasher func(unsafe.Pointer, uintptr) uintptr + KeySize uint8 + ElemSize uint8 + BucketSize uint16 + Flags uint32 +} + +func (self *GoMapType) IndirectElem() bool { + return self.Flags & 2 != 0 +} + +type GoStructType struct { + GoType + Pkg *byte + Fields []GoStructField +} + +type GoStructField struct { + Name *byte + Type *GoType + OffEmbed uintptr +} + +type GoInterfaceType struct { + GoType + PkgPath *byte + Methods []GoInterfaceMethod +} + +type GoInterfaceMethod struct { + Name int32 + Type int32 +} + +type GoSlice struct { + Ptr unsafe.Pointer + Len int + Cap int +} + +type GoString struct { + Ptr unsafe.Pointer + Len int +} + +func PtrElem(t *GoType) *GoType { + return (*GoPtrType)(unsafe.Pointer(t)).Elem +} + +func MapType(t *GoType) *GoMapType { + return (*GoMapType)(unsafe.Pointer(t)) +} + +func IfaceType(t *GoType) *GoInterfaceType { + return (*GoInterfaceType)(unsafe.Pointer(t)) +} + +func UnpackType(t reflect.Type) *GoType { + return (*GoType)((*GoIface)(unsafe.Pointer(&t)).Value) +} + +func UnpackEface(v interface{}) GoEface { + return *(*GoEface)(unsafe.Pointer(&v)) +} + +func UnpackIface(v interface{}) GoIface { + return *(*GoIface)(unsafe.Pointer(&v)) +} + +func findReflectRtypeItab() *GoItab { + v := reflect.TypeOf(struct{}{}) + return (*GoIface)(unsafe.Pointer(&v)).Itab +} diff --git a/vendor/github.com/bytedance/sonic/loader/internal/rt/stackmap.go b/vendor/github.com/bytedance/sonic/loader/internal/rt/stackmap.go new file mode 100644 index 000000000..84ed9a95f --- /dev/null +++ b/vendor/github.com/bytedance/sonic/loader/internal/rt/stackmap.go @@ -0,0 +1,181 @@ +/** + * Copyright 2023 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package rt + +import ( + `fmt` + `strings` + `unsafe` + +) + +type Bitmap struct { + N int + B []byte +} + +func (self *Bitmap) grow() { + if self.N >= len(self.B) * 8 { + self.B = append(self.B, 0) + } +} + +func (self *Bitmap) mark(i int, bv int) { + if bv != 0 { + self.B[i / 8] |= 1 << (i % 8) + } else { + self.B[i / 8] &^= 1 << (i % 8) + } +} + +func (self *Bitmap) Set(i int, bv int) { + if i >= self.N { + panic("bitmap: invalid bit position") + } else { + self.mark(i, bv) + } +} + +func (self *Bitmap) Append(bv int) { + self.grow() + self.mark(self.N, bv) + self.N++ +} + +func (self *Bitmap) AppendMany(n int, bv int) { + for i := 0; i < n; i++ { + self.Append(bv) + } +} + +// var ( +// _stackMapLock = sync.Mutex{} +// _stackMapCache = make(map[*StackMap]struct{}) +// ) + +type BitVec struct { + N uintptr + B unsafe.Pointer +} + +func (self BitVec) Bit(i uintptr) byte { + return (*(*byte)(unsafe.Pointer(uintptr(self.B) + i / 8)) >> (i % 8)) & 1 +} + +func (self BitVec) String() string { + var i uintptr + var v []string + + /* add each bit */ + for i = 0; i < self.N; i++ { + v = append(v, fmt.Sprintf("%d", self.Bit(i))) + } + + /* join them together */ + return fmt.Sprintf( + "BitVec { %s }", + strings.Join(v, ", "), + ) +} + +type StackMap struct { + N int32 + L int32 + B [1]byte +} + +// func (self *StackMap) add() { +// _stackMapLock.Lock() +// _stackMapCache[self] = struct{}{} +// _stackMapLock.Unlock() +// } + +func (self *StackMap) Pin() uintptr { + // self.add() + return uintptr(unsafe.Pointer(self)) +} + +func (self *StackMap) Get(i int32) BitVec { + return BitVec { + N: uintptr(self.L), + B: unsafe.Pointer(uintptr(unsafe.Pointer(&self.B)) + uintptr(i * ((self.L + 7) >> 3))), + } +} + +func (self *StackMap) String() string { + sb := strings.Builder{} + sb.WriteString("StackMap {") + + /* dump every stack map */ + for i := int32(0); i < self.N; i++ { + sb.WriteRune('\n') + sb.WriteString(" " + self.Get(i).String()) + } + + /* close the stackmap */ + sb.WriteString("\n}") + return sb.String() +} + +func (self *StackMap) MarshalBinary() ([]byte, error) { + size := int(self.N) * int(self.L) + int(unsafe.Sizeof(self.L)) + int(unsafe.Sizeof(self.N)) + return BytesFrom(unsafe.Pointer(self), size, size), nil +} + +var ( + byteType = UnpackEface(byte(0)).Type +) + +const ( + _StackMapSize = unsafe.Sizeof(StackMap{}) +) + +//go:linkname mallocgc runtime.mallocgc +//goland:noinspection GoUnusedParameter +func mallocgc(nb uintptr, vt *GoType, zero bool) unsafe.Pointer + +type StackMapBuilder struct { + b Bitmap +} + +//go:nocheckptr +func (self *StackMapBuilder) Build() (p *StackMap) { + nb := len(self.b.B) + bm := mallocgc(_StackMapSize + uintptr(nb) - 1, byteType, false) + + /* initialize as 1 bitmap of N bits */ + p = (*StackMap)(bm) + p.N, p.L = 1, int32(self.b.N) + copy(BytesFrom(unsafe.Pointer(&p.B), nb, nb), self.b.B) + return +} + +func (self *StackMapBuilder) AddField(ptr bool) { + if ptr { + self.b.Append(1) + } else { + self.b.Append(0) + } +} + +func (self *StackMapBuilder) AddFields(n int, ptr bool) { + if ptr { + self.b.AppendMany(n, 1) + } else { + self.b.AppendMany(n, 0) + } +}
\ No newline at end of file |
