Source file
src/runtime/map_swiss.go
1
2
3
4
5
6
7 package runtime
8
9 import (
10 "internal/abi"
11 "internal/runtime/maps"
12 "internal/runtime/sys"
13 "unsafe"
14 )
15
16 const (
17
18 loadFactorNum = 7
19 loadFactorDen = 8
20 )
21
22 type maptype = abi.SwissMapType
23
24
25 var maps_errNilAssign error = plainError("assignment to entry in nil map")
26
27
28 func maps_mapKeyError(t *abi.SwissMapType, p unsafe.Pointer) error {
29 return mapKeyError(t, p)
30 }
31
32 func makemap64(t *abi.SwissMapType, hint int64, m *maps.Map) *maps.Map {
33 if int64(int(hint)) != hint {
34 hint = 0
35 }
36 return makemap(t, int(hint), m)
37 }
38
39
40
41
42 func makemap_small() *maps.Map {
43 return maps.NewEmptyMap()
44 }
45
46
47
48
49
50
51 func makemap(t *abi.SwissMapType, hint int, m *maps.Map) *maps.Map {
52 if hint < 0 {
53 hint = 0
54 }
55
56 return maps.NewMap(t, uintptr(hint), m, maxAlloc)
57 }
58
59
60
61
62
63
64
65
66
67
68
69 func mapaccess1(t *abi.SwissMapType, m *maps.Map, key unsafe.Pointer) unsafe.Pointer
70
71 func mapaccess2(t *abi.SwissMapType, m *maps.Map, key unsafe.Pointer) (unsafe.Pointer, bool)
72
73 func mapaccess1_fat(t *abi.SwissMapType, m *maps.Map, key, zero unsafe.Pointer) unsafe.Pointer {
74 e := mapaccess1(t, m, key)
75 if e == unsafe.Pointer(&zeroVal[0]) {
76 return zero
77 }
78 return e
79 }
80
81 func mapaccess2_fat(t *abi.SwissMapType, m *maps.Map, key, zero unsafe.Pointer) (unsafe.Pointer, bool) {
82 e := mapaccess1(t, m, key)
83 if e == unsafe.Pointer(&zeroVal[0]) {
84 return zero, false
85 }
86 return e, true
87 }
88
89
90
91
92
93 func mapassign(t *abi.SwissMapType, m *maps.Map, key unsafe.Pointer) unsafe.Pointer
94
95 func mapdelete(t *abi.SwissMapType, m *maps.Map, key unsafe.Pointer) {
96 if raceenabled && m != nil {
97 callerpc := sys.GetCallerPC()
98 pc := abi.FuncPCABIInternal(mapdelete)
99 racewritepc(unsafe.Pointer(m), callerpc, pc)
100 raceReadObjectPC(t.Key, key, callerpc, pc)
101 }
102 if msanenabled && m != nil {
103 msanread(key, t.Key.Size_)
104 }
105 if asanenabled && m != nil {
106 asanread(key, t.Key.Size_)
107 }
108
109 m.Delete(t, key)
110 }
111
112
113
114
115
116 func mapiterinit(t *abi.SwissMapType, m *maps.Map, it *maps.Iter) {
117 if raceenabled && m != nil {
118 callerpc := sys.GetCallerPC()
119 racereadpc(unsafe.Pointer(m), callerpc, abi.FuncPCABIInternal(mapiterinit))
120 }
121
122 it.Init(t, m)
123 it.Next()
124 }
125
126 func mapiternext(it *maps.Iter) {
127 if raceenabled {
128 callerpc := sys.GetCallerPC()
129 racereadpc(unsafe.Pointer(it.Map()), callerpc, abi.FuncPCABIInternal(mapiternext))
130 }
131
132 it.Next()
133 }
134
135
136 func mapclear(t *abi.SwissMapType, m *maps.Map) {
137 if raceenabled && m != nil {
138 callerpc := sys.GetCallerPC()
139 pc := abi.FuncPCABIInternal(mapclear)
140 racewritepc(unsafe.Pointer(m), callerpc, pc)
141 }
142
143 m.Clear(t)
144 }
145
146
147
148
149 func reflect_makemap(t *abi.SwissMapType, cap int) *maps.Map {
150
151 if t.Key.Equal == nil {
152 throw("runtime.reflect_makemap: unsupported map key type")
153 }
154
155
156 return makemap(t, cap, nil)
157 }
158
159
160 func reflect_mapaccess(t *abi.SwissMapType, m *maps.Map, key unsafe.Pointer) unsafe.Pointer {
161 elem, ok := mapaccess2(t, m, key)
162 if !ok {
163
164 elem = nil
165 }
166 return elem
167 }
168
169
170 func reflect_mapaccess_faststr(t *abi.SwissMapType, m *maps.Map, key string) unsafe.Pointer {
171 elem, ok := mapaccess2_faststr(t, m, key)
172 if !ok {
173
174 elem = nil
175 }
176 return elem
177 }
178
179
180 func reflect_mapassign(t *abi.SwissMapType, m *maps.Map, key unsafe.Pointer, elem unsafe.Pointer) {
181 p := mapassign(t, m, key)
182 typedmemmove(t.Elem, p, elem)
183 }
184
185
186 func reflect_mapassign_faststr(t *abi.SwissMapType, m *maps.Map, key string, elem unsafe.Pointer) {
187 p := mapassign_faststr(t, m, key)
188 typedmemmove(t.Elem, p, elem)
189 }
190
191
192 func reflect_mapdelete(t *abi.SwissMapType, m *maps.Map, key unsafe.Pointer) {
193 mapdelete(t, m, key)
194 }
195
196
197 func reflect_mapdelete_faststr(t *abi.SwissMapType, m *maps.Map, key string) {
198 mapdelete_faststr(t, m, key)
199 }
200
201
202 func reflect_mapiterinit(t *abi.SwissMapType, m *maps.Map, it *maps.Iter) {
203 mapiterinit(t, m, it)
204 }
205
206
207 func reflect_mapiternext(it *maps.Iter) {
208 mapiternext(it)
209 }
210
211
212 func reflect_mapiterkey(it *maps.Iter) unsafe.Pointer {
213 return it.Key()
214 }
215
216
217 func reflect_mapiterelem(it *maps.Iter) unsafe.Pointer {
218 return it.Elem()
219 }
220
221
222 func reflect_maplen(m *maps.Map) int {
223 if m == nil {
224 return 0
225 }
226 if raceenabled {
227 callerpc := sys.GetCallerPC()
228 racereadpc(unsafe.Pointer(m), callerpc, abi.FuncPCABIInternal(reflect_maplen))
229 }
230 return int(m.Used())
231 }
232
233
234 func reflect_mapclear(t *abi.SwissMapType, m *maps.Map) {
235 mapclear(t, m)
236 }
237
238
239 func reflectlite_maplen(m *maps.Map) int {
240 if m == nil {
241 return 0
242 }
243 if raceenabled {
244 callerpc := sys.GetCallerPC()
245 racereadpc(unsafe.Pointer(m), callerpc, abi.FuncPCABIInternal(reflect_maplen))
246 }
247 return int(m.Used())
248 }
249
250
251
252
253
254
255 func mapinitnoop()
256
257
258
259
260 func mapclone(m any) any {
261 e := efaceOf(&m)
262 e.data = unsafe.Pointer(mapclone2((*abi.SwissMapType)(unsafe.Pointer(e._type)), (*maps.Map)(e.data)))
263 return m
264 }
265
266 func mapclone2(t *abi.SwissMapType, src *maps.Map) *maps.Map {
267 dst := makemap(t, int(src.Used()), nil)
268
269 var iter maps.Iter
270 iter.Init(t, src)
271 for iter.Next(); iter.Key() != nil; iter.Next() {
272 dst.Put(t, iter.Key(), iter.Elem())
273 }
274
275 return dst
276 }
277
278
279
280
281 func keys(m any, p unsafe.Pointer) {
282
283 panic("unimplemented")
284 }
285
286
287
288
289 func values(m any, p unsafe.Pointer) {
290
291 panic("unimplemented")
292 }
293
View as plain text