Debian Codegraph @

codec/helper_unsafe_compiler_gc.go

raw symbols SCIP
1 // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
2 // Use of this source code is governed by a MIT license found in the LICENSE file.
3
4 //go:build !safe && !codec.safe && !appengine && go1.9 && gc
5 // +build !safe,!codec.safe,!appengine,go1.9,gc
6
7 package codec
8
9 import (
10 "reflect"
11 _ "runtime" // needed for go linkname(s)
12 "unsafe"
13 )
14
15 // keep in sync with
16 //
17 // $GOROOT/src/cmd/compile/internal/gc/reflect.go: MAXKEYSIZE, MAXELEMSIZE
18 // $GOROOT/src/runtime/map.go: maxKeySize, maxElemSize
19 // $GOROOT/src/reflect/type.go: maxKeySize, maxElemSize
20 //
21 // We use these to determine whether the type is stored indirectly in the map or not.
22 const (
23 // mapMaxKeySize = 128
24 mapMaxElemSize = 128
25 )
26
27 func unsafeGrowslice(typ unsafe.Pointer, old unsafeSlice, cap, incr int) (v unsafeSlice) {
28 return growslice(typ, old, cap+incr)
29 }
30
31 func rvType(rv reflect.Value) reflect.Type {
32 return rvPtrToType(((*unsafeReflectValue)(unsafe.Pointer(&rv))).typ) // rv.Type()
33 }
34
35 // mapStoresElemIndirect tells if the element type is stored indirectly in the map.
36 //
37 // This is used to determine valIsIndirect which is passed into mapSet/mapGet calls.
38 //
39 // If valIsIndirect doesn't matter, then just return false and ignore the value
40 // passed in mapGet/mapSet calls
41 func mapStoresElemIndirect(elemsize uintptr) bool {
42 return elemsize > mapMaxElemSize
43 }
44
45 func mapSet(m, k, v reflect.Value, keyFastKind mapKeyFastKind, valIsIndirect, valIsRef bool) {
46 var urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
47 var kptr = unsafeMapKVPtr(urv)
48 urv = (*unsafeReflectValue)(unsafe.Pointer(&v))
49 var vtyp = urv.typ
50 var vptr = unsafeMapKVPtr(urv)
51
52 urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
53 mptr := rvRefPtr(urv)
54
55 var vvptr unsafe.Pointer
56
57 // mapassign_fastXXX don't take indirect into account.
58 // It was hard to infer what makes it work all the time.
59 // Sometimes, we got vvptr == nil when we dereferenced vvptr (if valIsIndirect).
60 // Consequently, only use fastXXX functions if !valIsIndirect
61
62 if valIsIndirect {
63 vvptr = mapassign(urv.typ, mptr, kptr)
64 typedmemmove(vtyp, vvptr, vptr)
65 // reflect_mapassign(urv.typ, mptr, kptr, vptr)
66 return
67 }
68
69 switch keyFastKind {
70 case mapKeyFastKind32:
71 vvptr = mapassign_fast32(urv.typ, mptr, *(*uint32)(kptr))
72 case mapKeyFastKind32ptr:
73 vvptr = mapassign_fast32ptr(urv.typ, mptr, *(*unsafe.Pointer)(kptr))
74 case mapKeyFastKind64:
75 vvptr = mapassign_fast64(urv.typ, mptr, *(*uint64)(kptr))
76 case mapKeyFastKind64ptr:
77 vvptr = mapassign_fast64ptr(urv.typ, mptr, *(*unsafe.Pointer)(kptr))
78 case mapKeyFastKindStr:
79 vvptr = mapassign_faststr(urv.typ, mptr, *(*string)(kptr))
80 default:
81 vvptr = mapassign(urv.typ, mptr, kptr)
82 }
83
84 // if keyFastKind != 0 && valIsIndirect {
85 // vvptr = *(*unsafe.Pointer)(vvptr)
86 // }
87
88 typedmemmove(vtyp, vvptr, vptr)
89 }
90
91 func mapGet(m, k, v reflect.Value, keyFastKind mapKeyFastKind, valIsIndirect, valIsRef bool) (_ reflect.Value) {
92 var urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
93 var kptr = unsafeMapKVPtr(urv)
94 urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
95 mptr := rvRefPtr(urv)
96
97 var vvptr unsafe.Pointer
98 var ok bool
99
100 // Note that mapaccess2_fastXXX functions do not check if the value needs to be copied.
101 // if they do, we should dereference the pointer and return that
102
103 switch keyFastKind {
104 case mapKeyFastKind32, mapKeyFastKind32ptr:
105 vvptr, ok = mapaccess2_fast32(urv.typ, mptr, *(*uint32)(kptr))
106 case mapKeyFastKind64, mapKeyFastKind64ptr:
107 vvptr, ok = mapaccess2_fast64(urv.typ, mptr, *(*uint64)(kptr))
108 case mapKeyFastKindStr:
109 vvptr, ok = mapaccess2_faststr(urv.typ, mptr, *(*string)(kptr))
110 default:
111 vvptr, ok = mapaccess2(urv.typ, mptr, kptr)
112 }
113
114 if !ok {
115 return
116 }
117
118 urv = (*unsafeReflectValue)(unsafe.Pointer(&v))
119
120 if keyFastKind != 0 && valIsIndirect {
121 urv.ptr = *(*unsafe.Pointer)(vvptr)
122 } else if helperUnsafeDirectAssignMapEntry || valIsRef {
123 urv.ptr = vvptr
124 } else {
125 typedmemmove(urv.typ, urv.ptr, vvptr)
126 }
127
128 return v
129 }
130
131 //go:linkname unsafeZeroArr runtime.zeroVal
132 var unsafeZeroArr [1024]byte
133
134 //go:linkname rvPtrToType reflect.toType
135 //go:noescape
136 func rvPtrToType(typ unsafe.Pointer) reflect.Type
137
138 //go:linkname mapassign_fast32 runtime.mapassign_fast32
139 //go:noescape
140 func mapassign_fast32(typ unsafe.Pointer, m unsafe.Pointer, key uint32) unsafe.Pointer
141
142 //go:linkname mapassign_fast32ptr runtime.mapassign_fast32ptr
143 //go:noescape
144 func mapassign_fast32ptr(typ unsafe.Pointer, m unsafe.Pointer, key unsafe.Pointer) unsafe.Pointer
145
146 //go:linkname mapassign_fast64 runtime.mapassign_fast64
147 //go:noescape
148 func mapassign_fast64(typ unsafe.Pointer, m unsafe.Pointer, key uint64) unsafe.Pointer
149
150 //go:linkname mapassign_fast64ptr runtime.mapassign_fast64ptr
151 //go:noescape
152 func mapassign_fast64ptr(typ unsafe.Pointer, m unsafe.Pointer, key unsafe.Pointer) unsafe.Pointer
153
154 //go:linkname mapassign_faststr runtime.mapassign_faststr
155 //go:noescape
156 func mapassign_faststr(typ unsafe.Pointer, m unsafe.Pointer, s string) unsafe.Pointer
157
158 //go:linkname mapaccess2_fast32 runtime.mapaccess2_fast32
159 //go:noescape
160 func mapaccess2_fast32(typ unsafe.Pointer, m unsafe.Pointer, key uint32) (val unsafe.Pointer, ok bool)
161
162 //go:linkname mapaccess2_fast64 runtime.mapaccess2_fast64
163 //go:noescape
164 func mapaccess2_fast64(typ unsafe.Pointer, m unsafe.Pointer, key uint64) (val unsafe.Pointer, ok bool)
165
166 //go:linkname mapaccess2_faststr runtime.mapaccess2_faststr
167 //go:noescape
168 func mapaccess2_faststr(typ unsafe.Pointer, m unsafe.Pointer, key string) (val unsafe.Pointer, ok bool)