| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package runtime |
| 6 |
|
| 7 |
// This file contains the implementation of Go's map type. |
| 8 |
// |
| 9 |
// A map is just a hash table. The data is arranged |
| 10 |
// into an array of buckets. Each bucket contains up to |
| 11 |
// 8 key/elem pairs. The low-order bits of the hash are |
| 12 |
// used to select a bucket. Each bucket contains a few |
| 13 |
// high-order bits of each hash to distinguish the entries |
| 14 |
// within a single bucket. |
| 15 |
// |
| 16 |
// If more than 8 keys hash to a bucket, we chain on |
| 17 |
// extra buckets. |
| 18 |
// |
| 19 |
// When the hashtable grows, we allocate a new array |
| 20 |
// of buckets twice as big. Buckets are incrementally |
| 21 |
// copied from the old bucket array to the new bucket array. |
| 22 |
// |
| 23 |
// Map iterators walk through the array of buckets and |
| 24 |
// return the keys in walk order (bucket #, then overflow |
| 25 |
// chain order, then bucket index). To maintain iteration |
| 26 |
// semantics, we never move keys within their bucket (if |
| 27 |
// we did, keys might be returned 0 or 2 times). When |
| 28 |
// growing the table, iterators remain iterating through the |
| 29 |
// old table and must check the new table if the bucket |
| 30 |
// they are iterating through has been moved ("evacuated") |
| 31 |
// to the new table. |
| 32 |
|
| 33 |
// Picking loadFactor: too large and we have lots of overflow |
| 34 |
// buckets, too small and we waste a lot of space. I wrote |
| 35 |
// a simple program to check some stats for different loads: |
| 36 |
// (64-bit, 8 byte keys and elems) |
| 37 |
// loadFactor %overflow bytes/entry hitprobe missprobe |
| 38 |
// 4.00 2.13 20.77 3.00 4.00 |
| 39 |
// 4.50 4.05 17.30 3.25 4.50 |
| 40 |
// 5.00 6.85 14.77 3.50 5.00 |
| 41 |
// 5.50 10.55 12.94 3.75 5.50 |
| 42 |
// 6.00 15.27 11.67 4.00 6.00 |
| 43 |
// 6.50 20.90 10.79 4.25 6.50 |
| 44 |
// 7.00 27.14 10.15 4.50 7.00 |
| 45 |
// 7.50 34.03 9.73 4.75 7.50 |
| 46 |
// 8.00 41.10 9.40 5.00 8.00 |
| 47 |
// |
| 48 |
// %overflow = percentage of buckets which have an overflow bucket |
| 49 |
// bytes/entry = overhead bytes used per key/elem pair |
| 50 |
// hitprobe = # of entries to check when looking up a present key |
| 51 |
// missprobe = # of entries to check when looking up an absent key |
| 52 |
// |
| 53 |
// Keep in mind this data is for maximally loaded tables, i.e. just |
| 54 |
// before the table grows. Typical tables will be somewhat less loaded. |
| 55 |
|
| 56 |
import ( |
| 57 |
"internal/abi" |
| 58 |
"internal/goarch" |
| 59 |
"runtime/internal/atomic" |
| 60 |
"runtime/internal/math" |
| 61 |
"unsafe" |
| 62 |
) |
| 63 |
|
| 64 |
// For gccgo, use go:linkname to export compiler-called functions. |
| 65 |
// |
| 66 |
//go:linkname makemap |
| 67 |
//go:linkname makemap64 |
| 68 |
//go:linkname makemap_small |
| 69 |
//go:linkname mapaccess1 |
| 70 |
//go:linkname mapaccess2 |
| 71 |
//go:linkname mapaccess1_fat |
| 72 |
//go:linkname mapaccess2_fat |
| 73 |
//go:linkname mapassign |
| 74 |
//go:linkname mapdelete |
| 75 |
//go:linkname mapclear |
| 76 |
//go:linkname mapiterinit |
| 77 |
//go:linkname mapiternext |
| 78 |
|
| 79 |
const ( |
| 80 |
// Maximum number of key/elem pairs a bucket can hold. |
| 81 |
bucketCntBits = 3 |
| 82 |
bucketCnt = 1 << bucketCntBits |
| 83 |
|
| 84 |
// Maximum average load of a bucket that triggers growth is 6.5. |
| 85 |
// Represent as loadFactorNum/loadFactorDen, to allow integer math. |
| 86 |
loadFactorNum = 13 |
| 87 |
loadFactorDen = 2 |
| 88 |
|
| 89 |
// Maximum key or elem size to keep inline (instead of mallocing per element). |
| 90 |
// Must fit in a uint8. |
| 91 |
// Fast versions cannot handle big elems - the cutoff size for |
| 92 |
// fast versions in cmd/compile/internal/gc/walk.go must be at most this elem. |
| 93 |
maxKeySize = 128 |
| 94 |
maxElemSize = 128 |
| 95 |
|
| 96 |
// data offset should be the size of the bmap struct, but needs to be |
| 97 |
// aligned correctly. For amd64p32 this means 64-bit alignment |
| 98 |
// even though pointers are 32 bit. |
| 99 |
dataOffset = unsafe.Offsetof(struct { |
| 100 |
b bmap |
| 101 |
v int64 |
| 102 |
}{}.v) |
| 103 |
|
| 104 |
// Possible tophash values. We reserve a few possibilities for special marks. |
| 105 |
// Each bucket (including its overflow buckets, if any) will have either all or none of its |
| 106 |
// entries in the evacuated* states (except during the evacuate() method, which only happens |
| 107 |
// during map writes and thus no one else can observe the map during that time). |
| 108 |
emptyRest = 0 // this cell is empty, and there are no more non-empty cells at higher indexes or overflows. |
| 109 |
emptyOne = 1 // this cell is empty |
| 110 |
evacuatedX = 2 // key/elem is valid. Entry has been evacuated to first half of larger table. |
| 111 |
evacuatedY = 3 // same as above, but evacuated to second half of larger table. |
| 112 |
evacuatedEmpty = 4 // cell is empty, bucket is evacuated. |
| 113 |
minTopHash = 5 // minimum tophash for a normal filled cell. |
| 114 |
|
| 115 |
// flags |
| 116 |
iterator = 1 // there may be an iterator using buckets |
| 117 |
oldIterator = 2 // there may be an iterator using oldbuckets |
| 118 |
hashWriting = 4 // a goroutine is writing to the map |
| 119 |
sameSizeGrow = 8 // the current map growth is to a new map of the same size |
| 120 |
|
| 121 |
// sentinel bucket ID for iterator checks |
| 122 |
noCheck = 1<<(8*goarch.PtrSize) - 1 |
| 123 |
) |
| 124 |
|
| 125 |
// isEmpty reports whether the given tophash array entry represents an empty bucket entry. |
| 126 |
func isEmpty(x uint8) bool { |
| 127 |
return x <= emptyOne |
| 128 |
} |
| 129 |
|
| 130 |
// A header for a Go map. |
| 131 |
type hmap struct { |
| 132 |
// Note: the format of the hmap is also encoded in cmd/compile/internal/reflectdata/reflect.go. |
| 133 |
// Make sure this stays in sync with the compiler's definition. |
| 134 |
count int // # live cells == size of map. Must be first (used by len() builtin) |
| 135 |
flags uint8 |
| 136 |
B uint8 // log_2 of # of buckets (can hold up to loadFactor * 2^B items) |
| 137 |
noverflow uint16 // approximate number of overflow buckets; see incrnoverflow for details |
| 138 |
hash0 uint32 // hash seed |
| 139 |
|
| 140 |
buckets unsafe.Pointer // array of 2^B Buckets. may be nil if count==0. |
| 141 |
oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing |
| 142 |
nevacuate uintptr // progress counter for evacuation (buckets less than this have been evacuated) |
| 143 |
|
| 144 |
extra *mapextra // optional fields |
| 145 |
} |
| 146 |
|
| 147 |
// mapextra holds fields that are not present on all maps. |
| 148 |
type mapextra struct { |
| 149 |
// If both key and elem do not contain pointers and are inline, then we mark bucket |
| 150 |
// type as containing no pointers. This avoids scanning such maps. |
| 151 |
// However, bmap.overflow is a pointer. In order to keep overflow buckets |
| 152 |
// alive, we store pointers to all overflow buckets in hmap.extra.overflow and hmap.extra.oldoverflow. |
| 153 |
// overflow and oldoverflow are only used if key and elem do not contain pointers. |
| 154 |
// overflow contains overflow buckets for hmap.buckets. |
| 155 |
// oldoverflow contains overflow buckets for hmap.oldbuckets. |
| 156 |
// The indirection allows to store a pointer to the slice in hiter. |
| 157 |
overflow *[]*bmap |
| 158 |
oldoverflow *[]*bmap |
| 159 |
|
| 160 |
// nextOverflow holds a pointer to a free overflow bucket. |
| 161 |
nextOverflow *bmap |
| 162 |
} |
| 163 |
|
| 164 |
// A bucket for a Go map. |
| 165 |
type bmap struct { |
| 166 |
// tophash generally contains the top byte of the hash value |
| 167 |
// for each key in this bucket. If tophash[0] < minTopHash, |
| 168 |
// tophash[0] is a bucket evacuation state instead. |
| 169 |
tophash [bucketCnt]uint8 |
| 170 |
// Followed by bucketCnt keys and then bucketCnt elems. |
| 171 |
// NOTE: packing all the keys together and then all the elems together makes the |
| 172 |
// code a bit more complicated than alternating key/elem/key/elem/... but it allows |
| 173 |
// us to eliminate padding which would be needed for, e.g., map[int64]int8. |
| 174 |
// Followed by an overflow pointer. |
| 175 |
} |
| 176 |
|
| 177 |
// A hash iteration structure. |
| 178 |
// If you modify hiter, also change cmd/compile/internal/reflectdata/reflect.go |
| 179 |
// and reflect/value.go to match the layout of this structure. |
| 180 |
type hiter struct { |
| 181 |
key unsafe.Pointer // Must be in first position. Write nil to indicate iteration end (see cmd/compile/internal/walk/range.go). |
| 182 |
elem unsafe.Pointer // Must be in second position (see cmd/compile/internal/walk/range.go). |
| 183 |
t *maptype |
| 184 |
h *hmap |
| 185 |
buckets unsafe.Pointer // bucket ptr at hash_iter initialization time |
| 186 |
bptr *bmap // current bucket |
| 187 |
overflow *[]*bmap // keeps overflow buckets of hmap.buckets alive |
| 188 |
oldoverflow *[]*bmap // keeps overflow buckets of hmap.oldbuckets alive |
| 189 |
startBucket uintptr // bucket iteration started at |
| 190 |
offset uint8 // intra-bucket offset to start from during iteration (should be big enough to hold bucketCnt-1) |
| 191 |
wrapped bool // already wrapped around from end of bucket array to beginning |
| 192 |
B uint8 |
| 193 |
i uint8 |
| 194 |
bucket uintptr |
| 195 |
checkBucket uintptr |
| 196 |
} |
| 197 |
|
| 198 |
// bucketShift returns 1<<b, optimized for code generation. |
| 199 |
func bucketShift(b uint8) uintptr { |
| 200 |
// Masking the shift amount allows overflow checks to be elided. |
| 201 |
return uintptr(1) << (b & (goarch.PtrSize*8 - 1)) |
| 202 |
} |
| 203 |
|
| 204 |
// bucketMask returns 1<<b - 1, optimized for code generation. |
| 205 |
func bucketMask(b uint8) uintptr { |
| 206 |
return bucketShift(b) - 1 |
| 207 |
} |
| 208 |
|
| 209 |
// tophash calculates the tophash value for hash. |
| 210 |
func tophash(hash uintptr) uint8 { |
| 211 |
top := uint8(hash >> (goarch.PtrSize*8 - 8)) |
| 212 |
if top < minTopHash { |
| 213 |
top += minTopHash |
| 214 |
} |
| 215 |
return top |
| 216 |
} |
| 217 |
|
| 218 |
func evacuated(b *bmap) bool { |
| 219 |
h := b.tophash[0] |
| 220 |
return h > emptyOne && h < minTopHash |
| 221 |
} |
| 222 |
|
| 223 |
func (b *bmap) overflow(t *maptype) *bmap { |
| 224 |
return *(**bmap)(add(unsafe.Pointer(b), uintptr(t.bucketsize)-goarch.PtrSize)) |
| 225 |
} |
| 226 |
|
| 227 |
func (b *bmap) setoverflow(t *maptype, ovf *bmap) { |
| 228 |
*(**bmap)(add(unsafe.Pointer(b), uintptr(t.bucketsize)-goarch.PtrSize)) = ovf |
| 229 |
} |
| 230 |
|
| 231 |
func (b *bmap) keys() unsafe.Pointer { |
| 232 |
return add(unsafe.Pointer(b), dataOffset) |
| 233 |
} |
| 234 |
|
| 235 |
// incrnoverflow increments h.noverflow. |
| 236 |
// noverflow counts the number of overflow buckets. |
| 237 |
// This is used to trigger same-size map growth. |
| 238 |
// See also tooManyOverflowBuckets. |
| 239 |
// To keep hmap small, noverflow is a uint16. |
| 240 |
// When there are few buckets, noverflow is an exact count. |
| 241 |
// When there are many buckets, noverflow is an approximate count. |
| 242 |
func (h *hmap) incrnoverflow() { |
| 243 |
// We trigger same-size map growth if there are |
| 244 |
// as many overflow buckets as buckets. |
| 245 |
// We need to be able to count to 1<<h.B. |
| 246 |
if h.B < 16 { |
| 247 |
h.noverflow++ |
| 248 |
return |
| 249 |
} |
| 250 |
// Increment with probability 1/(1<<(h.B-15)). |
| 251 |
// When we reach 1<<15 - 1, we will have approximately |
| 252 |
// as many overflow buckets as buckets. |
| 253 |
mask := uint32(1)<<(h.B-15) - 1 |
| 254 |
// Example: if h.B == 18, then mask == 7, |
| 255 |
// and fastrand & 7 == 0 with probability 1/8. |
| 256 |
if fastrand()&mask == 0 { |
| 257 |
h.noverflow++ |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
func (h *hmap) newoverflow(t *maptype, b *bmap) *bmap { |
| 262 |
var ovf *bmap |
| 263 |
if h.extra != nil && h.extra.nextOverflow != nil { |
| 264 |
// We have preallocated overflow buckets available. |
| 265 |
// See makeBucketArray for more details. |
| 266 |
ovf = h.extra.nextOverflow |
| 267 |
if ovf.overflow(t) == nil { |
| 268 |
// We're not at the end of the preallocated overflow buckets. Bump the pointer. |
| 269 |
h.extra.nextOverflow = (*bmap)(add(unsafe.Pointer(ovf), uintptr(t.bucketsize))) |
| 270 |
} else { |
| 271 |
// This is the last preallocated overflow bucket. |
| 272 |
// Reset the overflow pointer on this bucket, |
| 273 |
// which was set to a non-nil sentinel value. |
| 274 |
ovf.setoverflow(t, nil) |
| 275 |
h.extra.nextOverflow = nil |
| 276 |
} |
| 277 |
} else { |
| 278 |
ovf = (*bmap)(newobject(t.bucket)) |
| 279 |
} |
| 280 |
h.incrnoverflow() |
| 281 |
if t.bucket.ptrdata == 0 { |
| 282 |
h.createOverflow() |
| 283 |
*h.extra.overflow = append(*h.extra.overflow, ovf) |
| 284 |
} |
| 285 |
b.setoverflow(t, ovf) |
| 286 |
return ovf |
| 287 |
} |
| 288 |
|
| 289 |
func (h *hmap) createOverflow() { |
| 290 |
if h.extra == nil { |
| 291 |
h.extra = new(mapextra) |
| 292 |
} |
| 293 |
if h.extra.overflow == nil { |
| 294 |
h.extra.overflow = new([]*bmap) |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func makemap64(t *maptype, hint int64, h *hmap) *hmap { |
| 299 |
if int64(int(hint)) != hint { |
| 300 |
hint = 0 |
| 301 |
} |
| 302 |
return makemap(t, int(hint), h) |
| 303 |
} |
| 304 |
|
| 305 |
// makemap_small implements Go map creation for make(map[k]v) and |
| 306 |
// make(map[k]v, hint) when hint is known to be at most bucketCnt |
| 307 |
// at compile time and the map needs to be allocated on the heap. |
| 308 |
func makemap_small() *hmap { |
| 309 |
h := new(hmap) |
| 310 |
h.hash0 = fastrand() |
| 311 |
return h |
| 312 |
} |
| 313 |
|
| 314 |
// makemap implements Go map creation for make(map[k]v, hint). |
| 315 |
// If the compiler has determined that the map or the first bucket |
| 316 |
// can be created on the stack, h and/or bucket may be non-nil. |
| 317 |
// If h != nil, the map can be created directly in h. |
| 318 |
// If h.buckets != nil, bucket pointed to can be used as the first bucket. |
| 319 |
func makemap(t *maptype, hint int, h *hmap) *hmap { |
| 320 |
mem, overflow := math.MulUintptr(uintptr(hint), t.bucket.size) |
| 321 |
if overflow || mem > maxAlloc { |
| 322 |
hint = 0 |
| 323 |
} |
| 324 |
|
| 325 |
// initialize Hmap |
| 326 |
if h == nil { |
| 327 |
h = new(hmap) |
| 328 |
} |
| 329 |
h.hash0 = fastrand() |
| 330 |
|
| 331 |
// Find the size parameter B which will hold the requested # of elements. |
| 332 |
// For hint < 0 overLoadFactor returns false since hint < bucketCnt. |
| 333 |
B := uint8(0) |
| 334 |
for overLoadFactor(hint, B) { |
| 335 |
B++ |
| 336 |
} |
| 337 |
h.B = B |
| 338 |
|
| 339 |
// allocate initial hash table |
| 340 |
// if B == 0, the buckets field is allocated lazily later (in mapassign) |
| 341 |
// If hint is large zeroing this memory could take a while. |
| 342 |
if h.B != 0 { |
| 343 |
var nextOverflow *bmap |
| 344 |
h.buckets, nextOverflow = makeBucketArray(t, h.B, nil) |
| 345 |
if nextOverflow != nil { |
| 346 |
h.extra = new(mapextra) |
| 347 |
h.extra.nextOverflow = nextOverflow |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
return h |
| 352 |
} |
| 353 |
|
| 354 |
// makeBucketArray initializes a backing array for map buckets. |
| 355 |
// 1<<b is the minimum number of buckets to allocate. |
| 356 |
// dirtyalloc should either be nil or a bucket array previously |
| 357 |
// allocated by makeBucketArray with the same t and b parameters. |
| 358 |
// If dirtyalloc is nil a new backing array will be alloced and |
| 359 |
// otherwise dirtyalloc will be cleared and reused as backing array. |
| 360 |
func makeBucketArray(t *maptype, b uint8, dirtyalloc unsafe.Pointer) (buckets unsafe.Pointer, nextOverflow *bmap) { |
| 361 |
base := bucketShift(b) |
| 362 |
nbuckets := base |
| 363 |
// For small b, overflow buckets are unlikely. |
| 364 |
// Avoid the overhead of the calculation. |
| 365 |
if b >= 4 { |
| 366 |
// Add on the estimated number of overflow buckets |
| 367 |
// required to insert the median number of elements |
| 368 |
// used with this value of b. |
| 369 |
nbuckets += bucketShift(b - 4) |
| 370 |
sz := t.bucket.size * nbuckets |
| 371 |
up := roundupsize(sz) |
| 372 |
if up != sz { |
| 373 |
nbuckets = up / t.bucket.size |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
if dirtyalloc == nil { |
| 378 |
buckets = newarray(t.bucket, int(nbuckets)) |
| 379 |
} else { |
| 380 |
// dirtyalloc was previously generated by |
| 381 |
// the above newarray(t.bucket, int(nbuckets)) |
| 382 |
// but may not be empty. |
| 383 |
buckets = dirtyalloc |
| 384 |
size := t.bucket.size * nbuckets |
| 385 |
if t.bucket.ptrdata != 0 { |
| 386 |
memclrHasPointers(buckets, size) |
| 387 |
} else { |
| 388 |
memclrNoHeapPointers(buckets, size) |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
if base != nbuckets { |
| 393 |
// We preallocated some overflow buckets. |
| 394 |
// To keep the overhead of tracking these overflow buckets to a minimum, |
| 395 |
// we use the convention that if a preallocated overflow bucket's overflow |
| 396 |
// pointer is nil, then there are more available by bumping the pointer. |
| 397 |
// We need a safe non-nil pointer for the last overflow bucket; just use buckets. |
| 398 |
nextOverflow = (*bmap)(add(buckets, base*uintptr(t.bucketsize))) |
| 399 |
last := (*bmap)(add(buckets, (nbuckets-1)*uintptr(t.bucketsize))) |
| 400 |
last.setoverflow(t, (*bmap)(buckets)) |
| 401 |
} |
| 402 |
return buckets, nextOverflow |
| 403 |
} |
| 404 |
|
| 405 |
// mapaccess1 returns a pointer to h[key]. Never returns nil, instead |
| 406 |
// it will return a reference to the zero object for the elem type if |
| 407 |
// the key is not in the map. |
| 408 |
// NOTE: The returned pointer may keep the whole map live, so don't |
| 409 |
// hold onto it for very long. |
| 410 |
func mapaccess1(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer { |
| 411 |
// Check preemption, since unlike gc we don't check on every call. |
| 412 |
if getg().preempt { |
| 413 |
checkPreempt() |
| 414 |
} |
| 415 |
|
| 416 |
if raceenabled && h != nil { |
| 417 |
callerpc := getcallerpc() |
| 418 |
pc := abi.FuncPCABIInternal(mapaccess1) |
| 419 |
racereadpc(unsafe.Pointer(h), callerpc, pc) |
| 420 |
raceReadObjectPC(t.key, key, callerpc, pc) |
| 421 |
} |
| 422 |
if msanenabled && h != nil { |
| 423 |
msanread(key, t.key.size) |
| 424 |
} |
| 425 |
if asanenabled && h != nil { |
| 426 |
asanread(key, t.key.size) |
| 427 |
} |
| 428 |
if h == nil || h.count == 0 { |
| 429 |
if t.hashMightPanic() { |
| 430 |
t.hasher(key, 0) // see issue 23734 |
| 431 |
} |
| 432 |
return unsafe.Pointer(&zeroVal[0]) |
| 433 |
} |
| 434 |
if h.flags&hashWriting != 0 { |
| 435 |
throw("concurrent map read and map write") |
| 436 |
} |
| 437 |
hash := t.hasher(key, uintptr(h.hash0)) |
| 438 |
m := bucketMask(h.B) |
| 439 |
b := (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
| 440 |
if c := h.oldbuckets; c != nil { |
| 441 |
if !h.sameSizeGrow() { |
| 442 |
// There used to be half as many buckets; mask down one more power of two. |
| 443 |
m >>= 1 |
| 444 |
} |
| 445 |
oldb := (*bmap)(add(c, (hash&m)*uintptr(t.bucketsize))) |
| 446 |
if !evacuated(oldb) { |
| 447 |
b = oldb |
| 448 |
} |
| 449 |
} |
| 450 |
top := tophash(hash) |
| 451 |
bucketloop: |
| 452 |
for ; b != nil; b = b.overflow(t) { |
| 453 |
for i := uintptr(0); i < bucketCnt; i++ { |
| 454 |
if b.tophash[i] != top { |
| 455 |
if b.tophash[i] == emptyRest { |
| 456 |
break bucketloop |
| 457 |
} |
| 458 |
continue |
| 459 |
} |
| 460 |
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize)) |
| 461 |
if t.indirectkey() { |
| 462 |
k = *((*unsafe.Pointer)(k)) |
| 463 |
} |
| 464 |
if t.key.equal(key, k) { |
| 465 |
e := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.elemsize)) |
| 466 |
if t.indirectelem() { |
| 467 |
e = *((*unsafe.Pointer)(e)) |
| 468 |
} |
| 469 |
return e |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
return unsafe.Pointer(&zeroVal[0]) |
| 474 |
} |
| 475 |
|
| 476 |
func mapaccess2(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, bool) { |
| 477 |
// Check preemption, since unlike gc we don't check on every call. |
| 478 |
if getg().preempt { |
| 479 |
checkPreempt() |
| 480 |
} |
| 481 |
|
| 482 |
if raceenabled && h != nil { |
| 483 |
callerpc := getcallerpc() |
| 484 |
pc := abi.FuncPCABIInternal(mapaccess2) |
| 485 |
racereadpc(unsafe.Pointer(h), callerpc, pc) |
| 486 |
raceReadObjectPC(t.key, key, callerpc, pc) |
| 487 |
} |
| 488 |
if msanenabled && h != nil { |
| 489 |
msanread(key, t.key.size) |
| 490 |
} |
| 491 |
if asanenabled && h != nil { |
| 492 |
asanread(key, t.key.size) |
| 493 |
} |
| 494 |
if h == nil || h.count == 0 { |
| 495 |
if t.hashMightPanic() { |
| 496 |
t.hasher(key, 0) // see issue 23734 |
| 497 |
} |
| 498 |
return unsafe.Pointer(&zeroVal[0]), false |
| 499 |
} |
| 500 |
if h.flags&hashWriting != 0 { |
| 501 |
throw("concurrent map read and map write") |
| 502 |
} |
| 503 |
hash := t.hasher(key, uintptr(h.hash0)) |
| 504 |
m := bucketMask(h.B) |
| 505 |
b := (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
| 506 |
if c := h.oldbuckets; c != nil { |
| 507 |
if !h.sameSizeGrow() { |
| 508 |
// There used to be half as many buckets; mask down one more power of two. |
| 509 |
m >>= 1 |
| 510 |
} |
| 511 |
oldb := (*bmap)(add(c, (hash&m)*uintptr(t.bucketsize))) |
| 512 |
if !evacuated(oldb) { |
| 513 |
b = oldb |
| 514 |
} |
| 515 |
} |
| 516 |
top := tophash(hash) |
| 517 |
bucketloop: |
| 518 |
for ; b != nil; b = b.overflow(t) { |
| 519 |
for i := uintptr(0); i < bucketCnt; i++ { |
| 520 |
if b.tophash[i] != top { |
| 521 |
if b.tophash[i] == emptyRest { |
| 522 |
break bucketloop |
| 523 |
} |
| 524 |
continue |
| 525 |
} |
| 526 |
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize)) |
| 527 |
if t.indirectkey() { |
| 528 |
k = *((*unsafe.Pointer)(k)) |
| 529 |
} |
| 530 |
if t.key.equal(key, k) { |
| 531 |
e := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.elemsize)) |
| 532 |
if t.indirectelem() { |
| 533 |
e = *((*unsafe.Pointer)(e)) |
| 534 |
} |
| 535 |
return e, true |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
return unsafe.Pointer(&zeroVal[0]), false |
| 540 |
} |
| 541 |
|
| 542 |
// returns both key and elem. Used by map iterator |
| 543 |
func mapaccessK(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, unsafe.Pointer) { |
| 544 |
// Check preemption, since unlike gc we don't check on every call. |
| 545 |
if getg().preempt { |
| 546 |
checkPreempt() |
| 547 |
} |
| 548 |
|
| 549 |
if h == nil || h.count == 0 { |
| 550 |
return nil, nil |
| 551 |
} |
| 552 |
hash := t.hasher(key, uintptr(h.hash0)) |
| 553 |
m := bucketMask(h.B) |
| 554 |
b := (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
| 555 |
if c := h.oldbuckets; c != nil { |
| 556 |
if !h.sameSizeGrow() { |
| 557 |
// There used to be half as many buckets; mask down one more power of two. |
| 558 |
m >>= 1 |
| 559 |
} |
| 560 |
oldb := (*bmap)(add(c, (hash&m)*uintptr(t.bucketsize))) |
| 561 |
if !evacuated(oldb) { |
| 562 |
b = oldb |
| 563 |
} |
| 564 |
} |
| 565 |
top := tophash(hash) |
| 566 |
bucketloop: |
| 567 |
for ; b != nil; b = b.overflow(t) { |
| 568 |
for i := uintptr(0); i < bucketCnt; i++ { |
| 569 |
if b.tophash[i] != top { |
| 570 |
if b.tophash[i] == emptyRest { |
| 571 |
break bucketloop |
| 572 |
} |
| 573 |
continue |
| 574 |
} |
| 575 |
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize)) |
| 576 |
if t.indirectkey() { |
| 577 |
k = *((*unsafe.Pointer)(k)) |
| 578 |
} |
| 579 |
if t.key.equal(key, k) { |
| 580 |
e := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.elemsize)) |
| 581 |
if t.indirectelem() { |
| 582 |
e = *((*unsafe.Pointer)(e)) |
| 583 |
} |
| 584 |
return k, e |
| 585 |
} |
| 586 |
} |
| 587 |
} |
| 588 |
return nil, nil |
| 589 |
} |
| 590 |
|
| 591 |
func mapaccess1_fat(t *maptype, h *hmap, key, zero unsafe.Pointer) unsafe.Pointer { |
| 592 |
e := mapaccess1(t, h, key) |
| 593 |
if e == unsafe.Pointer(&zeroVal[0]) { |
| 594 |
return zero |
| 595 |
} |
| 596 |
return e |
| 597 |
} |
| 598 |
|
| 599 |
func mapaccess2_fat(t *maptype, h *hmap, key, zero unsafe.Pointer) (unsafe.Pointer, bool) { |
| 600 |
e := mapaccess1(t, h, key) |
| 601 |
if e == unsafe.Pointer(&zeroVal[0]) { |
| 602 |
return zero, false |
| 603 |
} |
| 604 |
return e, true |
| 605 |
} |
| 606 |
|
| 607 |
// Like mapaccess, but allocates a slot for the key if it is not present in the map. |
| 608 |
func mapassign(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer { |
| 609 |
// Check preemption, since unlike gc we don't check on every call. |
| 610 |
if getg().preempt { |
| 611 |
checkPreempt() |
| 612 |
} |
| 613 |
|
| 614 |
if h == nil { |
| 615 |
panic(plainError("assignment to entry in nil map")) |
| 616 |
} |
| 617 |
if raceenabled { |
| 618 |
callerpc := getcallerpc() |
| 619 |
pc := abi.FuncPCABIInternal(mapassign) |
| 620 |
racewritepc(unsafe.Pointer(h), callerpc, pc) |
| 621 |
raceReadObjectPC(t.key, key, callerpc, pc) |
| 622 |
} |
| 623 |
if msanenabled { |
| 624 |
msanread(key, t.key.size) |
| 625 |
} |
| 626 |
if asanenabled { |
| 627 |
asanread(key, t.key.size) |
| 628 |
} |
| 629 |
if h.flags&hashWriting != 0 { |
| 630 |
throw("concurrent map writes") |
| 631 |
} |
| 632 |
hash := t.hasher(key, uintptr(h.hash0)) |
| 633 |
|
| 634 |
// Set hashWriting after calling t.hasher, since t.hasher may panic, |
| 635 |
// in which case we have not actually done a write. |
| 636 |
h.flags ^= hashWriting |
| 637 |
|
| 638 |
if h.buckets == nil { |
| 639 |
h.buckets = newobject(t.bucket) // newarray(t.bucket, 1) |
| 640 |
} |
| 641 |
|
| 642 |
again: |
| 643 |
bucket := hash & bucketMask(h.B) |
| 644 |
if h.growing() { |
| 645 |
growWork(t, h, bucket) |
| 646 |
} |
| 647 |
b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) |
| 648 |
top := tophash(hash) |
| 649 |
|
| 650 |
var inserti *uint8 |
| 651 |
var insertk unsafe.Pointer |
| 652 |
var elem unsafe.Pointer |
| 653 |
bucketloop: |
| 654 |
for { |
| 655 |
for i := uintptr(0); i < bucketCnt; i++ { |
| 656 |
if b.tophash[i] != top { |
| 657 |
if isEmpty(b.tophash[i]) && inserti == nil { |
| 658 |
inserti = &b.tophash[i] |
| 659 |
insertk = add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize)) |
| 660 |
elem = add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.elemsize)) |
| 661 |
} |
| 662 |
if b.tophash[i] == emptyRest { |
| 663 |
break bucketloop |
| 664 |
} |
| 665 |
continue |
| 666 |
} |
| 667 |
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize)) |
| 668 |
if t.indirectkey() { |
| 669 |
k = *((*unsafe.Pointer)(k)) |
| 670 |
} |
| 671 |
if !t.key.equal(key, k) { |
| 672 |
continue |
| 673 |
} |
| 674 |
// already have a mapping for key. Update it. |
| 675 |
if t.needkeyupdate() { |
| 676 |
typedmemmove(t.key, k, key) |
| 677 |
} |
| 678 |
elem = add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.elemsize)) |
| 679 |
goto done |
| 680 |
} |
| 681 |
ovf := b.overflow(t) |
| 682 |
if ovf == nil { |
| 683 |
break |
| 684 |
} |
| 685 |
b = ovf |
| 686 |
} |
| 687 |
|
| 688 |
// Did not find mapping for key. Allocate new cell & add entry. |
| 689 |
|
| 690 |
// If we hit the max load factor or we have too many overflow buckets, |
| 691 |
// and we're not already in the middle of growing, start growing. |
| 692 |
if !h.growing() && (overLoadFactor(h.count+1, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) { |
| 693 |
hashGrow(t, h) |
| 694 |
goto again // Growing the table invalidates everything, so try again |
| 695 |
} |
| 696 |
|
| 697 |
if inserti == nil { |
| 698 |
// The current bucket and all the overflow buckets connected to it are full, allocate a new one. |
| 699 |
newb := h.newoverflow(t, b) |
| 700 |
inserti = &newb.tophash[0] |
| 701 |
insertk = add(unsafe.Pointer(newb), dataOffset) |
| 702 |
elem = add(insertk, bucketCnt*uintptr(t.keysize)) |
| 703 |
} |
| 704 |
|
| 705 |
// store new key/elem at insert position |
| 706 |
if t.indirectkey() { |
| 707 |
kmem := newobject(t.key) |
| 708 |
*(*unsafe.Pointer)(insertk) = kmem |
| 709 |
insertk = kmem |
| 710 |
} |
| 711 |
if t.indirectelem() { |
| 712 |
vmem := newobject(t.elem) |
| 713 |
*(*unsafe.Pointer)(elem) = vmem |
| 714 |
} |
| 715 |
typedmemmove(t.key, insertk, key) |
| 716 |
*inserti = top |
| 717 |
h.count++ |
| 718 |
|
| 719 |
done: |
| 720 |
if h.flags&hashWriting == 0 { |
| 721 |
throw("concurrent map writes") |
| 722 |
} |
| 723 |
h.flags &^= hashWriting |
| 724 |
if t.indirectelem() { |
| 725 |
elem = *((*unsafe.Pointer)(elem)) |
| 726 |
} |
| 727 |
return elem |
| 728 |
} |
| 729 |
|
| 730 |
func mapdelete(t *maptype, h *hmap, key unsafe.Pointer) { |
| 731 |
if raceenabled && h != nil { |
| 732 |
callerpc := getcallerpc() |
| 733 |
pc := abi.FuncPCABIInternal(mapdelete) |
| 734 |
racewritepc(unsafe.Pointer(h), callerpc, pc) |
| 735 |
raceReadObjectPC(t.key, key, callerpc, pc) |
| 736 |
} |
| 737 |
if msanenabled && h != nil { |
| 738 |
msanread(key, t.key.size) |
| 739 |
} |
| 740 |
if asanenabled && h != nil { |
| 741 |
asanread(key, t.key.size) |
| 742 |
} |
| 743 |
if h == nil || h.count == 0 { |
| 744 |
if t.hashMightPanic() { |
| 745 |
t.hasher(key, 0) // see issue 23734 |
| 746 |
} |
| 747 |
return |
| 748 |
} |
| 749 |
if h.flags&hashWriting != 0 { |
| 750 |
throw("concurrent map writes") |
| 751 |
} |
| 752 |
|
| 753 |
hash := t.hasher(key, uintptr(h.hash0)) |
| 754 |
|
| 755 |
// Set hashWriting after calling t.hasher, since t.hasher may panic, |
| 756 |
// in which case we have not actually done a write (delete). |
| 757 |
h.flags ^= hashWriting |
| 758 |
|
| 759 |
bucket := hash & bucketMask(h.B) |
| 760 |
if h.growing() { |
| 761 |
growWork(t, h, bucket) |
| 762 |
} |
| 763 |
b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) |
| 764 |
bOrig := b |
| 765 |
top := tophash(hash) |
| 766 |
search: |
| 767 |
for ; b != nil; b = b.overflow(t) { |
| 768 |
for i := uintptr(0); i < bucketCnt; i++ { |
| 769 |
if b.tophash[i] != top { |
| 770 |
if b.tophash[i] == emptyRest { |
| 771 |
break search |
| 772 |
} |
| 773 |
continue |
| 774 |
} |
| 775 |
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize)) |
| 776 |
k2 := k |
| 777 |
if t.indirectkey() { |
| 778 |
k2 = *((*unsafe.Pointer)(k2)) |
| 779 |
} |
| 780 |
if !t.key.equal(key, k2) { |
| 781 |
continue |
| 782 |
} |
| 783 |
// Only clear key if there are pointers in it. |
| 784 |
if t.indirectkey() { |
| 785 |
*(*unsafe.Pointer)(k) = nil |
| 786 |
} else if t.key.ptrdata != 0 { |
| 787 |
memclrHasPointers(k, t.key.size) |
| 788 |
} |
| 789 |
e := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.elemsize)) |
| 790 |
if t.indirectelem() { |
| 791 |
*(*unsafe.Pointer)(e) = nil |
| 792 |
} else if t.elem.ptrdata != 0 { |
| 793 |
memclrHasPointers(e, t.elem.size) |
| 794 |
} else { |
| 795 |
memclrNoHeapPointers(e, t.elem.size) |
| 796 |
} |
| 797 |
b.tophash[i] = emptyOne |
| 798 |
// If the bucket now ends in a bunch of emptyOne states, |
| 799 |
// change those to emptyRest states. |
| 800 |
// It would be nice to make this a separate function, but |
| 801 |
// for loops are not currently inlineable. |
| 802 |
if i == bucketCnt-1 { |
| 803 |
if b.overflow(t) != nil && b.overflow(t).tophash[0] != emptyRest { |
| 804 |
goto notLast |
| 805 |
} |
| 806 |
} else { |
| 807 |
if b.tophash[i+1] != emptyRest { |
| 808 |
goto notLast |
| 809 |
} |
| 810 |
} |
| 811 |
for { |
| 812 |
b.tophash[i] = emptyRest |
| 813 |
if i == 0 { |
| 814 |
if b == bOrig { |
| 815 |
break // beginning of initial bucket, we're done. |
| 816 |
} |
| 817 |
// Find previous bucket, continue at its last entry. |
| 818 |
c := b |
| 819 |
for b = bOrig; b.overflow(t) != c; b = b.overflow(t) { |
| 820 |
} |
| 821 |
i = bucketCnt - 1 |
| 822 |
} else { |
| 823 |
i-- |
| 824 |
} |
| 825 |
if b.tophash[i] != emptyOne { |
| 826 |
break |
| 827 |
} |
| 828 |
} |
| 829 |
notLast: |
| 830 |
h.count-- |
| 831 |
// Reset the hash seed to make it more difficult for attackers to |
| 832 |
// repeatedly trigger hash collisions. See issue 25237. |
| 833 |
if h.count == 0 { |
| 834 |
h.hash0 = fastrand() |
| 835 |
} |
| 836 |
break search |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
if h.flags&hashWriting == 0 { |
| 841 |
throw("concurrent map writes") |
| 842 |
} |
| 843 |
h.flags &^= hashWriting |
| 844 |
} |
| 845 |
|
| 846 |
// mapiterinit initializes the hiter struct used for ranging over maps. |
| 847 |
// The hiter struct pointed to by 'it' is allocated on the stack |
| 848 |
// by the compilers order pass or on the heap by reflect_mapiterinit. |
| 849 |
// Both need to have zeroed hiter since the struct contains pointers. |
| 850 |
// Gccgo-specific: *it need not be zeroed by the compiler, |
| 851 |
// and it's cheaper to zero it here. |
| 852 |
func mapiterinit(t *maptype, h *hmap, it *hiter) { |
| 853 |
it.key = nil |
| 854 |
it.elem = nil |
| 855 |
it.t = nil |
| 856 |
it.h = nil |
| 857 |
it.buckets = nil |
| 858 |
it.bptr = nil |
| 859 |
it.overflow = nil |
| 860 |
it.oldoverflow = nil |
| 861 |
it.startBucket = 0 |
| 862 |
it.offset = 0 |
| 863 |
it.wrapped = false |
| 864 |
it.B = 0 |
| 865 |
it.i = 0 |
| 866 |
it.bucket = 0 |
| 867 |
it.checkBucket = 0 |
| 868 |
|
| 869 |
if raceenabled && h != nil { |
| 870 |
callerpc := getcallerpc() |
| 871 |
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapiterinit)) |
| 872 |
} |
| 873 |
|
| 874 |
it.t = t |
| 875 |
if h == nil || h.count == 0 { |
| 876 |
return |
| 877 |
} |
| 878 |
|
| 879 |
if unsafe.Sizeof(hiter{})/goarch.PtrSize != 12 { |
| 880 |
throw("hash_iter size incorrect") // see cmd/compile/internal/reflectdata/reflect.go |
| 881 |
} |
| 882 |
it.h = h |
| 883 |
|
| 884 |
// grab snapshot of bucket state |
| 885 |
it.B = h.B |
| 886 |
it.buckets = h.buckets |
| 887 |
if t.bucket.ptrdata == 0 { |
| 888 |
// Allocate the current slice and remember pointers to both current and old. |
| 889 |
// This preserves all relevant overflow buckets alive even if |
| 890 |
// the table grows and/or overflow buckets are added to the table |
| 891 |
// while we are iterating. |
| 892 |
h.createOverflow() |
| 893 |
it.overflow = h.extra.overflow |
| 894 |
it.oldoverflow = h.extra.oldoverflow |
| 895 |
} |
| 896 |
|
| 897 |
// decide where to start |
| 898 |
r := uintptr(fastrand()) |
| 899 |
if h.B > 31-bucketCntBits { |
| 900 |
r += uintptr(fastrand()) << 31 |
| 901 |
} |
| 902 |
it.startBucket = r & bucketMask(h.B) |
| 903 |
it.offset = uint8(r >> h.B & (bucketCnt - 1)) |
| 904 |
|
| 905 |
// iterator state |
| 906 |
it.bucket = it.startBucket |
| 907 |
|
| 908 |
// Remember we have an iterator. |
| 909 |
// Can run concurrently with another mapiterinit(). |
| 910 |
if old := h.flags; old&(iterator|oldIterator) != iterator|oldIterator { |
| 911 |
atomic.Or8(&h.flags, iterator|oldIterator) |
| 912 |
} |
| 913 |
|
| 914 |
mapiternext(it) |
| 915 |
} |
| 916 |
|
| 917 |
func mapiternext(it *hiter) { |
| 918 |
// Check preemption, since unlike gc we don't check on every call. |
| 919 |
if getg().preempt { |
| 920 |
checkPreempt() |
| 921 |
} |
| 922 |
|
| 923 |
h := it.h |
| 924 |
if raceenabled { |
| 925 |
callerpc := getcallerpc() |
| 926 |
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapiternext)) |
| 927 |
} |
| 928 |
if h.flags&hashWriting != 0 { |
| 929 |
throw("concurrent map iteration and map write") |
| 930 |
} |
| 931 |
t := it.t |
| 932 |
bucket := it.bucket |
| 933 |
b := it.bptr |
| 934 |
i := it.i |
| 935 |
checkBucket := it.checkBucket |
| 936 |
|
| 937 |
next: |
| 938 |
if b == nil { |
| 939 |
if bucket == it.startBucket && it.wrapped { |
| 940 |
// end of iteration |
| 941 |
it.key = nil |
| 942 |
it.elem = nil |
| 943 |
return |
| 944 |
} |
| 945 |
if h.growing() && it.B == h.B { |
| 946 |
// Iterator was started in the middle of a grow, and the grow isn't done yet. |
| 947 |
// If the bucket we're looking at hasn't been filled in yet (i.e. the old |
| 948 |
// bucket hasn't been evacuated) then we need to iterate through the old |
| 949 |
// bucket and only return the ones that will be migrated to this bucket. |
| 950 |
oldbucket := bucket & it.h.oldbucketmask() |
| 951 |
b = (*bmap)(add(h.oldbuckets, oldbucket*uintptr(t.bucketsize))) |
| 952 |
if !evacuated(b) { |
| 953 |
checkBucket = bucket |
| 954 |
} else { |
| 955 |
b = (*bmap)(add(it.buckets, bucket*uintptr(t.bucketsize))) |
| 956 |
checkBucket = noCheck |
| 957 |
} |
| 958 |
} else { |
| 959 |
b = (*bmap)(add(it.buckets, bucket*uintptr(t.bucketsize))) |
| 960 |
checkBucket = noCheck |
| 961 |
} |
| 962 |
bucket++ |
| 963 |
if bucket == bucketShift(it.B) { |
| 964 |
bucket = 0 |
| 965 |
it.wrapped = true |
| 966 |
} |
| 967 |
i = 0 |
| 968 |
} |
| 969 |
for ; i < bucketCnt; i++ { |
| 970 |
offi := (i + it.offset) & (bucketCnt - 1) |
| 971 |
if isEmpty(b.tophash[offi]) || b.tophash[offi] == evacuatedEmpty { |
| 972 |
// TODO: emptyRest is hard to use here, as we start iterating |
| 973 |
// in the middle of a bucket. It's feasible, just tricky. |
| 974 |
continue |
| 975 |
} |
| 976 |
k := add(unsafe.Pointer(b), dataOffset+uintptr(offi)*uintptr(t.keysize)) |
| 977 |
if t.indirectkey() { |
| 978 |
k = *((*unsafe.Pointer)(k)) |
| 979 |
} |
| 980 |
e := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+uintptr(offi)*uintptr(t.elemsize)) |
| 981 |
if checkBucket != noCheck && !h.sameSizeGrow() { |
| 982 |
// Special case: iterator was started during a grow to a larger size |
| 983 |
// and the grow is not done yet. We're working on a bucket whose |
| 984 |
// oldbucket has not been evacuated yet. Or at least, it wasn't |
| 985 |
// evacuated when we started the bucket. So we're iterating |
| 986 |
// through the oldbucket, skipping any keys that will go |
| 987 |
// to the other new bucket (each oldbucket expands to two |
| 988 |
// buckets during a grow). |
| 989 |
if t.reflexivekey() || t.key.equal(k, k) { |
| 990 |
// If the item in the oldbucket is not destined for |
| 991 |
// the current new bucket in the iteration, skip it. |
| 992 |
hash := t.hasher(k, uintptr(h.hash0)) |
| 993 |
if hash&bucketMask(it.B) != checkBucket { |
| 994 |
continue |
| 995 |
} |
| 996 |
} else { |
| 997 |
// Hash isn't repeatable if k != k (NaNs). We need a |
| 998 |
// repeatable and randomish choice of which direction |
| 999 |
// to send NaNs during evacuation. We'll use the low |
| 1000 |
// bit of tophash to decide which way NaNs go. |
| 1001 |
// NOTE: this case is why we need two evacuate tophash |
| 1002 |
// values, evacuatedX and evacuatedY, that differ in |
| 1003 |
// their low bit. |
| 1004 |
if checkBucket>>(it.B-1) != uintptr(b.tophash[offi]&1) { |
| 1005 |
continue |
| 1006 |
} |
| 1007 |
} |
| 1008 |
} |
| 1009 |
if (b.tophash[offi] != evacuatedX && b.tophash[offi] != evacuatedY) || |
| 1010 |
!(t.reflexivekey() || t.key.equal(k, k)) { |
| 1011 |
// This is the golden data, we can return it. |
| 1012 |
// OR |
| 1013 |
// key!=key, so the entry can't be deleted or updated, so we can just return it. |
| 1014 |
// That's lucky for us because when key!=key we can't look it up successfully. |
| 1015 |
it.key = k |
| 1016 |
if t.indirectelem() { |
| 1017 |
e = *((*unsafe.Pointer)(e)) |
| 1018 |
} |
| 1019 |
it.elem = e |
| 1020 |
} else { |
| 1021 |
// The hash table has grown since the iterator was started. |
| 1022 |
// The golden data for this key is now somewhere else. |
| 1023 |
// Check the current hash table for the data. |
| 1024 |
// This code handles the case where the key |
| 1025 |
// has been deleted, updated, or deleted and reinserted. |
| 1026 |
// NOTE: we need to regrab the key as it has potentially been |
| 1027 |
// updated to an equal() but not identical key (e.g. +0.0 vs -0.0). |
| 1028 |
rk, re := mapaccessK(t, h, k) |
| 1029 |
if rk == nil { |
| 1030 |
continue // key has been deleted |
| 1031 |
} |
| 1032 |
it.key = rk |
| 1033 |
it.elem = re |
| 1034 |
} |
| 1035 |
it.bucket = bucket |
| 1036 |
if it.bptr != b { // avoid unnecessary write barrier; see issue 14921 |
| 1037 |
it.bptr = b |
| 1038 |
} |
| 1039 |
it.i = i + 1 |
| 1040 |
it.checkBucket = checkBucket |
| 1041 |
return |
| 1042 |
} |
| 1043 |
b = b.overflow(t) |
| 1044 |
i = 0 |
| 1045 |
goto next |
| 1046 |
} |
| 1047 |
|
| 1048 |
// mapclear deletes all keys from a map. |
| 1049 |
func mapclear(t *maptype, h *hmap) { |
| 1050 |
if raceenabled && h != nil { |
| 1051 |
callerpc := getcallerpc() |
| 1052 |
pc := abi.FuncPCABIInternal(mapclear) |
| 1053 |
racewritepc(unsafe.Pointer(h), callerpc, pc) |
| 1054 |
} |
| 1055 |
|
| 1056 |
if h == nil || h.count == 0 { |
| 1057 |
return |
| 1058 |
} |
| 1059 |
|
| 1060 |
if h.flags&hashWriting != 0 { |
| 1061 |
throw("concurrent map writes") |
| 1062 |
} |
| 1063 |
|
| 1064 |
h.flags ^= hashWriting |
| 1065 |
|
| 1066 |
h.flags &^= sameSizeGrow |
| 1067 |
h.oldbuckets = nil |
| 1068 |
h.nevacuate = 0 |
| 1069 |
h.noverflow = 0 |
| 1070 |
h.count = 0 |
| 1071 |
|
| 1072 |
// Reset the hash seed to make it more difficult for attackers to |
| 1073 |
// repeatedly trigger hash collisions. See issue 25237. |
| 1074 |
h.hash0 = fastrand() |
| 1075 |
|
| 1076 |
// Keep the mapextra allocation but clear any extra information. |
| 1077 |
if h.extra != nil { |
| 1078 |
*h.extra = mapextra{} |
| 1079 |
} |
| 1080 |
|
| 1081 |
// makeBucketArray clears the memory pointed to by h.buckets |
| 1082 |
// and recovers any overflow buckets by generating them |
| 1083 |
// as if h.buckets was newly alloced. |
| 1084 |
_, nextOverflow := makeBucketArray(t, h.B, h.buckets) |
| 1085 |
if nextOverflow != nil { |
| 1086 |
// If overflow buckets are created then h.extra |
| 1087 |
// will have been allocated during initial bucket creation. |
| 1088 |
h.extra.nextOverflow = nextOverflow |
| 1089 |
} |
| 1090 |
|
| 1091 |
if h.flags&hashWriting == 0 { |
| 1092 |
throw("concurrent map writes") |
| 1093 |
} |
| 1094 |
h.flags &^= hashWriting |
| 1095 |
} |
| 1096 |
|
| 1097 |
func hashGrow(t *maptype, h *hmap) { |
| 1098 |
// If we've hit the load factor, get bigger. |
| 1099 |
// Otherwise, there are too many overflow buckets, |
| 1100 |
// so keep the same number of buckets and "grow" laterally. |
| 1101 |
bigger := uint8(1) |
| 1102 |
if !overLoadFactor(h.count+1, h.B) { |
| 1103 |
bigger = 0 |
| 1104 |
h.flags |= sameSizeGrow |
| 1105 |
} |
| 1106 |
oldbuckets := h.buckets |
| 1107 |
newbuckets, nextOverflow := makeBucketArray(t, h.B+bigger, nil) |
| 1108 |
|
| 1109 |
flags := h.flags &^ (iterator | oldIterator) |
| 1110 |
if h.flags&iterator != 0 { |
| 1111 |
flags |= oldIterator |
| 1112 |
} |
| 1113 |
// commit the grow (atomic wrt gc) |
| 1114 |
h.B += bigger |
| 1115 |
h.flags = flags |
| 1116 |
h.oldbuckets = oldbuckets |
| 1117 |
h.buckets = newbuckets |
| 1118 |
h.nevacuate = 0 |
| 1119 |
h.noverflow = 0 |
| 1120 |
|
| 1121 |
if h.extra != nil && h.extra.overflow != nil { |
| 1122 |
// Promote current overflow buckets to the old generation. |
| 1123 |
if h.extra.oldoverflow != nil { |
| 1124 |
throw("oldoverflow is not nil") |
| 1125 |
} |
| 1126 |
h.extra.oldoverflow = h.extra.overflow |
| 1127 |
h.extra.overflow = nil |
| 1128 |
} |
| 1129 |
if nextOverflow != nil { |
| 1130 |
if h.extra == nil { |
| 1131 |
h.extra = new(mapextra) |
| 1132 |
} |
| 1133 |
h.extra.nextOverflow = nextOverflow |
| 1134 |
} |
| 1135 |
|
| 1136 |
// the actual copying of the hash table data is done incrementally |
| 1137 |
// by growWork() and evacuate(). |
| 1138 |
} |
| 1139 |
|
| 1140 |
// overLoadFactor reports whether count items placed in 1<<B buckets is over loadFactor. |
| 1141 |
func overLoadFactor(count int, B uint8) bool { |
| 1142 |
return count > bucketCnt && uintptr(count) > loadFactorNum*(bucketShift(B)/loadFactorDen) |
| 1143 |
} |
| 1144 |
|
| 1145 |
// tooManyOverflowBuckets reports whether noverflow buckets is too many for a map with 1<<B buckets. |
| 1146 |
// Note that most of these overflow buckets must be in sparse use; |
| 1147 |
// if use was dense, then we'd have already triggered regular map growth. |
| 1148 |
func tooManyOverflowBuckets(noverflow uint16, B uint8) bool { |
| 1149 |
// If the threshold is too low, we do extraneous work. |
| 1150 |
// If the threshold is too high, maps that grow and shrink can hold on to lots of unused memory. |
| 1151 |
// "too many" means (approximately) as many overflow buckets as regular buckets. |
| 1152 |
// See incrnoverflow for more details. |
| 1153 |
if B > 15 { |
| 1154 |
B = 15 |
| 1155 |
} |
| 1156 |
// The compiler doesn't see here that B < 16; mask B to generate shorter shift code. |
| 1157 |
return noverflow >= uint16(1)<<(B&15) |
| 1158 |
} |
| 1159 |
|
| 1160 |
// growing reports whether h is growing. The growth may be to the same size or bigger. |
| 1161 |
func (h *hmap) growing() bool { |
| 1162 |
return h.oldbuckets != nil |
| 1163 |
} |
| 1164 |
|
| 1165 |
// sameSizeGrow reports whether the current growth is to a map of the same size. |
| 1166 |
func (h *hmap) sameSizeGrow() bool { |
| 1167 |
return h.flags&sameSizeGrow != 0 |
| 1168 |
} |
| 1169 |
|
| 1170 |
// noldbuckets calculates the number of buckets prior to the current map growth. |
| 1171 |
func (h *hmap) noldbuckets() uintptr { |
| 1172 |
oldB := h.B |
| 1173 |
if !h.sameSizeGrow() { |
| 1174 |
oldB-- |
| 1175 |
} |
| 1176 |
return bucketShift(oldB) |
| 1177 |
} |
| 1178 |
|
| 1179 |
// oldbucketmask provides a mask that can be applied to calculate n % noldbuckets(). |
| 1180 |
func (h *hmap) oldbucketmask() uintptr { |
| 1181 |
return h.noldbuckets() - 1 |
| 1182 |
} |
| 1183 |
|
| 1184 |
func growWork(t *maptype, h *hmap, bucket uintptr) { |
| 1185 |
// make sure we evacuate the oldbucket corresponding |
| 1186 |
// to the bucket we're about to use |
| 1187 |
evacuate(t, h, bucket&h.oldbucketmask()) |
| 1188 |
|
| 1189 |
// evacuate one more oldbucket to make progress on growing |
| 1190 |
if h.growing() { |
| 1191 |
evacuate(t, h, h.nevacuate) |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
func bucketEvacuated(t *maptype, h *hmap, bucket uintptr) bool { |
| 1196 |
b := (*bmap)(add(h.oldbuckets, bucket*uintptr(t.bucketsize))) |
| 1197 |
return evacuated(b) |
| 1198 |
} |
| 1199 |
|
| 1200 |
// evacDst is an evacuation destination. |
| 1201 |
type evacDst struct { |
| 1202 |
b *bmap // current destination bucket |
| 1203 |
i int // key/elem index into b |
| 1204 |
k unsafe.Pointer // pointer to current key storage |
| 1205 |
e unsafe.Pointer // pointer to current elem storage |
| 1206 |
} |
| 1207 |
|
| 1208 |
func evacuate(t *maptype, h *hmap, oldbucket uintptr) { |
| 1209 |
b := (*bmap)(add(h.oldbuckets, oldbucket*uintptr(t.bucketsize))) |
| 1210 |
newbit := h.noldbuckets() |
| 1211 |
if !evacuated(b) { |
| 1212 |
// TODO: reuse overflow buckets instead of using new ones, if there |
| 1213 |
// is no iterator using the old buckets. (If !oldIterator.) |
| 1214 |
|
| 1215 |
// xy contains the x and y (low and high) evacuation destinations. |
| 1216 |
var xy [2]evacDst |
| 1217 |
x := &xy[0] |
| 1218 |
x.b = (*bmap)(add(h.buckets, oldbucket*uintptr(t.bucketsize))) |
| 1219 |
x.k = add(unsafe.Pointer(x.b), dataOffset) |
| 1220 |
x.e = add(x.k, bucketCnt*uintptr(t.keysize)) |
| 1221 |
|
| 1222 |
if !h.sameSizeGrow() { |
| 1223 |
// Only calculate y pointers if we're growing bigger. |
| 1224 |
// Otherwise GC can see bad pointers. |
| 1225 |
y := &xy[1] |
| 1226 |
y.b = (*bmap)(add(h.buckets, (oldbucket+newbit)*uintptr(t.bucketsize))) |
| 1227 |
y.k = add(unsafe.Pointer(y.b), dataOffset) |
| 1228 |
y.e = add(y.k, bucketCnt*uintptr(t.keysize)) |
| 1229 |
} |
| 1230 |
|
| 1231 |
for ; b != nil; b = b.overflow(t) { |
| 1232 |
k := add(unsafe.Pointer(b), dataOffset) |
| 1233 |
e := add(k, bucketCnt*uintptr(t.keysize)) |
| 1234 |
for i := 0; i < bucketCnt; i, k, e = i+1, add(k, uintptr(t.keysize)), add(e, uintptr(t.elemsize)) { |
| 1235 |
top := b.tophash[i] |
| 1236 |
if isEmpty(top) { |
| 1237 |
b.tophash[i] = evacuatedEmpty |
| 1238 |
continue |
| 1239 |
} |
| 1240 |
if top < minTopHash { |
| 1241 |
throw("bad map state") |
| 1242 |
} |
| 1243 |
k2 := k |
| 1244 |
if t.indirectkey() { |
| 1245 |
k2 = *((*unsafe.Pointer)(k2)) |
| 1246 |
} |
| 1247 |
var useY uint8 |
| 1248 |
if !h.sameSizeGrow() { |
| 1249 |
// Compute hash to make our evacuation decision (whether we need |
| 1250 |
// to send this key/elem to bucket x or bucket y). |
| 1251 |
hash := t.hasher(k2, uintptr(h.hash0)) |
| 1252 |
if h.flags&iterator != 0 && !t.reflexivekey() && !t.key.equal(k2, k2) { |
| 1253 |
// If key != key (NaNs), then the hash could be (and probably |
| 1254 |
// will be) entirely different from the old hash. Moreover, |
| 1255 |
// it isn't reproducible. Reproducibility is required in the |
| 1256 |
// presence of iterators, as our evacuation decision must |
| 1257 |
// match whatever decision the iterator made. |
| 1258 |
// Fortunately, we have the freedom to send these keys either |
| 1259 |
// way. Also, tophash is meaningless for these kinds of keys. |
| 1260 |
// We let the low bit of tophash drive the evacuation decision. |
| 1261 |
// We recompute a new random tophash for the next level so |
| 1262 |
// these keys will get evenly distributed across all buckets |
| 1263 |
// after multiple grows. |
| 1264 |
useY = top & 1 |
| 1265 |
top = tophash(hash) |
| 1266 |
} else { |
| 1267 |
if hash&newbit != 0 { |
| 1268 |
useY = 1 |
| 1269 |
} |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
if evacuatedX+1 != evacuatedY || evacuatedX^1 != evacuatedY { |
| 1274 |
throw("bad evacuatedN") |
| 1275 |
} |
| 1276 |
|
| 1277 |
b.tophash[i] = evacuatedX + useY // evacuatedX + 1 == evacuatedY |
| 1278 |
dst := &xy[useY] // evacuation destination |
| 1279 |
|
| 1280 |
if dst.i == bucketCnt { |
| 1281 |
dst.b = h.newoverflow(t, dst.b) |
| 1282 |
dst.i = 0 |
| 1283 |
dst.k = add(unsafe.Pointer(dst.b), dataOffset) |
| 1284 |
dst.e = add(dst.k, bucketCnt*uintptr(t.keysize)) |
| 1285 |
} |
| 1286 |
dst.b.tophash[dst.i&(bucketCnt-1)] = top // mask dst.i as an optimization, to avoid a bounds check |
| 1287 |
if t.indirectkey() { |
| 1288 |
*(*unsafe.Pointer)(dst.k) = k2 // copy pointer |
| 1289 |
} else { |
| 1290 |
typedmemmove(t.key, dst.k, k) // copy elem |
| 1291 |
} |
| 1292 |
if t.indirectelem() { |
| 1293 |
*(*unsafe.Pointer)(dst.e) = *(*unsafe.Pointer)(e) |
| 1294 |
} else { |
| 1295 |
typedmemmove(t.elem, dst.e, e) |
| 1296 |
} |
| 1297 |
dst.i++ |
| 1298 |
// These updates might push these pointers past the end of the |
| 1299 |
// key or elem arrays. That's ok, as we have the overflow pointer |
| 1300 |
// at the end of the bucket to protect against pointing past the |
| 1301 |
// end of the bucket. |
| 1302 |
dst.k = add(dst.k, uintptr(t.keysize)) |
| 1303 |
dst.e = add(dst.e, uintptr(t.elemsize)) |
| 1304 |
} |
| 1305 |
} |
| 1306 |
// Unlink the overflow buckets & clear key/elem to help GC. |
| 1307 |
if h.flags&oldIterator == 0 && t.bucket.ptrdata != 0 { |
| 1308 |
b := add(h.oldbuckets, oldbucket*uintptr(t.bucketsize)) |
| 1309 |
// Preserve b.tophash because the evacuation |
| 1310 |
// state is maintained there. |
| 1311 |
ptr := add(b, dataOffset) |
| 1312 |
n := uintptr(t.bucketsize) - dataOffset |
| 1313 |
memclrHasPointers(ptr, n) |
| 1314 |
} |
| 1315 |
} |
| 1316 |
|
| 1317 |
if oldbucket == h.nevacuate { |
| 1318 |
advanceEvacuationMark(h, t, newbit) |
| 1319 |
} |
| 1320 |
} |
| 1321 |
|
| 1322 |
func advanceEvacuationMark(h *hmap, t *maptype, newbit uintptr) { |
| 1323 |
h.nevacuate++ |
| 1324 |
// Experiments suggest that 1024 is overkill by at least an order of magnitude. |
| 1325 |
// Put it in there as a safeguard anyway, to ensure O(1) behavior. |
| 1326 |
stop := h.nevacuate + 1024 |
| 1327 |
if stop > newbit { |
| 1328 |
stop = newbit |
| 1329 |
} |
| 1330 |
for h.nevacuate != stop && bucketEvacuated(t, h, h.nevacuate) { |
| 1331 |
h.nevacuate++ |
| 1332 |
} |
| 1333 |
if h.nevacuate == newbit { // newbit == # of oldbuckets |
| 1334 |
// Growing is all done. Free old main bucket array. |
| 1335 |
h.oldbuckets = nil |
| 1336 |
// Can discard old overflow buckets as well. |
| 1337 |
// If they are still referenced by an iterator, |
| 1338 |
// then the iterator holds a pointers to the slice. |
| 1339 |
if h.extra != nil { |
| 1340 |
h.extra.oldoverflow = nil |
| 1341 |
} |
| 1342 |
h.flags &^= sameSizeGrow |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
// Reflect stubs. Called from ../reflect/asm_*.s |
| 1347 |
|
| 1348 |
//go:linkname reflect_makemap reflect.makemap |
| 1349 |
func reflect_makemap(t *maptype, cap int) *hmap { |
| 1350 |
// Check invariants and reflects math. |
| 1351 |
if t.key.equal == nil { |
| 1352 |
throw("runtime.reflect_makemap: unsupported map key type") |
| 1353 |
} |
| 1354 |
if t.key.size > maxKeySize && (!t.indirectkey() || t.keysize != uint8(goarch.PtrSize)) || |
| 1355 |
t.key.size <= maxKeySize && (t.indirectkey() || t.keysize != uint8(t.key.size)) { |
| 1356 |
throw("key size wrong") |
| 1357 |
} |
| 1358 |
if t.elem.size > maxElemSize && (!t.indirectelem() || t.elemsize != uint8(goarch.PtrSize)) || |
| 1359 |
t.elem.size <= maxElemSize && (t.indirectelem() || t.elemsize != uint8(t.elem.size)) { |
| 1360 |
throw("elem size wrong") |
| 1361 |
} |
| 1362 |
if t.key.align > bucketCnt { |
| 1363 |
throw("key align too big") |
| 1364 |
} |
| 1365 |
if t.elem.align > bucketCnt { |
| 1366 |
throw("elem align too big") |
| 1367 |
} |
| 1368 |
if t.key.size%uintptr(t.key.align) != 0 { |
| 1369 |
throw("key size not a multiple of key align") |
| 1370 |
} |
| 1371 |
if t.elem.size%uintptr(t.elem.align) != 0 { |
| 1372 |
throw("elem size not a multiple of elem align") |
| 1373 |
} |
| 1374 |
if bucketCnt < 8 { |
| 1375 |
throw("bucketsize too small for proper alignment") |
| 1376 |
} |
| 1377 |
if dataOffset%uintptr(t.key.align) != 0 { |
| 1378 |
throw("need padding in bucket (key)") |
| 1379 |
} |
| 1380 |
if dataOffset%uintptr(t.elem.align) != 0 { |
| 1381 |
throw("need padding in bucket (elem)") |
| 1382 |
} |
| 1383 |
|
| 1384 |
return makemap(t, cap, nil) |
| 1385 |
} |
| 1386 |
|
| 1387 |
//go:linkname reflect_mapaccess reflect.mapaccess |
| 1388 |
func reflect_mapaccess(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer { |
| 1389 |
elem, ok := mapaccess2(t, h, key) |
| 1390 |
if !ok { |
| 1391 |
// reflect wants nil for a missing element |
| 1392 |
elem = nil |
| 1393 |
} |
| 1394 |
return elem |
| 1395 |
} |
| 1396 |
|
| 1397 |
//go:linkname reflect_mapaccess_faststr reflect.mapaccess__faststr |
| 1398 |
func reflect_mapaccess_faststr(t *maptype, h *hmap, key string) unsafe.Pointer { |
| 1399 |
elem, ok := mapaccess2_faststr(t, h, key) |
| 1400 |
if !ok { |
| 1401 |
// reflect wants nil for a missing element |
| 1402 |
elem = nil |
| 1403 |
} |
| 1404 |
return elem |
| 1405 |
} |
| 1406 |
|
| 1407 |
//go:linkname reflect_mapassign reflect.mapassign |
| 1408 |
func reflect_mapassign(t *maptype, h *hmap, key unsafe.Pointer, elem unsafe.Pointer) { |
| 1409 |
p := mapassign(t, h, key) |
| 1410 |
typedmemmove(t.elem, p, elem) |
| 1411 |
} |
| 1412 |
|
| 1413 |
//go:linkname reflect_mapassign_faststr reflect.mapassign__faststr |
| 1414 |
func reflect_mapassign_faststr(t *maptype, h *hmap, key string, elem unsafe.Pointer) { |
| 1415 |
p := mapassign_faststr(t, h, key) |
| 1416 |
typedmemmove(t.elem, p, elem) |
| 1417 |
} |
| 1418 |
|
| 1419 |
//go:linkname reflect_mapdelete reflect.mapdelete |
| 1420 |
func reflect_mapdelete(t *maptype, h *hmap, key unsafe.Pointer) { |
| 1421 |
mapdelete(t, h, key) |
| 1422 |
} |
| 1423 |
|
| 1424 |
//go:linkname reflect_mapdelete_faststr reflect.mapdelete__faststr |
| 1425 |
func reflect_mapdelete_faststr(t *maptype, h *hmap, key string) { |
| 1426 |
mapdelete_faststr(t, h, key) |
| 1427 |
} |
| 1428 |
|
| 1429 |
//go:linkname reflect_mapiterinit reflect.mapiterinit |
| 1430 |
func reflect_mapiterinit(t *maptype, h *hmap, it *hiter) { |
| 1431 |
mapiterinit(t, h, it) |
| 1432 |
} |
| 1433 |
|
| 1434 |
//go:linkname reflect_mapiternext reflect.mapiternext |
| 1435 |
func reflect_mapiternext(it *hiter) { |
| 1436 |
mapiternext(it) |
| 1437 |
} |
| 1438 |
|
| 1439 |
//go:linkname reflect_mapiterkey reflect.mapiterkey |
| 1440 |
func reflect_mapiterkey(it *hiter) unsafe.Pointer { |
| 1441 |
return it.key |
| 1442 |
} |
| 1443 |
|
| 1444 |
//go:linkname reflect_mapiterelem reflect.mapiterelem |
| 1445 |
func reflect_mapiterelem(it *hiter) unsafe.Pointer { |
| 1446 |
return it.elem |
| 1447 |
} |
| 1448 |
|
| 1449 |
//go:linkname reflect_maplen reflect.maplen |
| 1450 |
func reflect_maplen(h *hmap) int { |
| 1451 |
if h == nil { |
| 1452 |
return 0 |
| 1453 |
} |
| 1454 |
if raceenabled { |
| 1455 |
callerpc := getcallerpc() |
| 1456 |
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(reflect_maplen)) |
| 1457 |
} |
| 1458 |
return h.count |
| 1459 |
} |
| 1460 |
|
| 1461 |
//go:linkname reflectlite_maplen internal_1reflectlite.maplen |
| 1462 |
func reflectlite_maplen(h *hmap) int { |
| 1463 |
if h == nil { |
| 1464 |
return 0 |
| 1465 |
} |
| 1466 |
if raceenabled { |
| 1467 |
callerpc := getcallerpc() |
| 1468 |
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(reflect_maplen)) |
| 1469 |
} |
| 1470 |
return h.count |
| 1471 |
} |
| 1472 |
|
| 1473 |
const maxZero = 1024 // must match value in reflect/value.go:maxZero cmd/compile/internal/gc/walk.go:zeroValSize |
| 1474 |
var zeroVal [maxZero]byte |