| 1 |
// Copyright 2018 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 |
import ( |
| 8 |
"internal/abi" |
| 9 |
"internal/goarch" |
| 10 |
"unsafe" |
| 11 |
) |
| 12 |
|
| 13 |
// For gccgo, use go:linkname to export compiler-called functions. |
| 14 |
// |
| 15 |
//go:linkname mapaccess1_fast32 |
| 16 |
//go:linkname mapaccess2_fast32 |
| 17 |
//go:linkname mapassign_fast32 |
| 18 |
//go:linkname mapassign_fast32ptr |
| 19 |
//go:linkname mapdelete_fast32 |
| 20 |
|
| 21 |
func mapaccess1_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer { |
| 22 |
if raceenabled && h != nil { |
| 23 |
callerpc := getcallerpc() |
| 24 |
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess1_fast32)) |
| 25 |
} |
| 26 |
if h == nil || h.count == 0 { |
| 27 |
return unsafe.Pointer(&zeroVal[0]) |
| 28 |
} |
| 29 |
if h.flags&hashWriting != 0 { |
| 30 |
throw("concurrent map read and map write") |
| 31 |
} |
| 32 |
var b *bmap |
| 33 |
if h.B == 0 { |
| 34 |
// One-bucket table. No need to hash. |
| 35 |
b = (*bmap)(h.buckets) |
| 36 |
} else { |
| 37 |
hash := t.hasher(noescape(unsafe.Pointer(&key)), uintptr(h.hash0)) |
| 38 |
m := bucketMask(h.B) |
| 39 |
b = (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
| 40 |
if c := h.oldbuckets; c != nil { |
| 41 |
if !h.sameSizeGrow() { |
| 42 |
// There used to be half as many buckets; mask down one more power of two. |
| 43 |
m >>= 1 |
| 44 |
} |
| 45 |
oldb := (*bmap)(add(c, (hash&m)*uintptr(t.bucketsize))) |
| 46 |
if !evacuated(oldb) { |
| 47 |
b = oldb |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
for ; b != nil; b = b.overflow(t) { |
| 52 |
for i, k := uintptr(0), b.keys(); i < bucketCnt; i, k = i+1, add(k, 4) { |
| 53 |
if *(*uint32)(k) == key && !isEmpty(b.tophash[i]) { |
| 54 |
return add(unsafe.Pointer(b), dataOffset+bucketCnt*4+i*uintptr(t.elemsize)) |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
return unsafe.Pointer(&zeroVal[0]) |
| 59 |
} |
| 60 |
|
| 61 |
func mapaccess2_fast32(t *maptype, h *hmap, key uint32) (unsafe.Pointer, bool) { |
| 62 |
if raceenabled && h != nil { |
| 63 |
callerpc := getcallerpc() |
| 64 |
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess2_fast32)) |
| 65 |
} |
| 66 |
if h == nil || h.count == 0 { |
| 67 |
return unsafe.Pointer(&zeroVal[0]), false |
| 68 |
} |
| 69 |
if h.flags&hashWriting != 0 { |
| 70 |
throw("concurrent map read and map write") |
| 71 |
} |
| 72 |
var b *bmap |
| 73 |
if h.B == 0 { |
| 74 |
// One-bucket table. No need to hash. |
| 75 |
b = (*bmap)(h.buckets) |
| 76 |
} else { |
| 77 |
hash := t.hasher(noescape(unsafe.Pointer(&key)), uintptr(h.hash0)) |
| 78 |
m := bucketMask(h.B) |
| 79 |
b = (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
| 80 |
if c := h.oldbuckets; c != nil { |
| 81 |
if !h.sameSizeGrow() { |
| 82 |
// There used to be half as many buckets; mask down one more power of two. |
| 83 |
m >>= 1 |
| 84 |
} |
| 85 |
oldb := (*bmap)(add(c, (hash&m)*uintptr(t.bucketsize))) |
| 86 |
if !evacuated(oldb) { |
| 87 |
b = oldb |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
for ; b != nil; b = b.overflow(t) { |
| 92 |
for i, k := uintptr(0), b.keys(); i < bucketCnt; i, k = i+1, add(k, 4) { |
| 93 |
if *(*uint32)(k) == key && !isEmpty(b.tophash[i]) { |
| 94 |
return add(unsafe.Pointer(b), dataOffset+bucketCnt*4+i*uintptr(t.elemsize)), true |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
return unsafe.Pointer(&zeroVal[0]), false |
| 99 |
} |
| 100 |
|
| 101 |
func mapassign_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer { |
| 102 |
if h == nil { |
| 103 |
panic(plainError("assignment to entry in nil map")) |
| 104 |
} |
| 105 |
if raceenabled { |
| 106 |
callerpc := getcallerpc() |
| 107 |
racewritepc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapassign_fast32)) |
| 108 |
} |
| 109 |
if h.flags&hashWriting != 0 { |
| 110 |
throw("concurrent map writes") |
| 111 |
} |
| 112 |
hash := t.hasher(noescape(unsafe.Pointer(&key)), uintptr(h.hash0)) |
| 113 |
|
| 114 |
// Set hashWriting after calling t.hasher for consistency with mapassign. |
| 115 |
h.flags ^= hashWriting |
| 116 |
|
| 117 |
if h.buckets == nil { |
| 118 |
h.buckets = newobject(t.bucket) // newarray(t.bucket, 1) |
| 119 |
} |
| 120 |
|
| 121 |
again: |
| 122 |
bucket := hash & bucketMask(h.B) |
| 123 |
if h.growing() { |
| 124 |
growWork_fast32(t, h, bucket) |
| 125 |
} |
| 126 |
b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) |
| 127 |
|
| 128 |
var insertb *bmap |
| 129 |
var inserti uintptr |
| 130 |
var insertk unsafe.Pointer |
| 131 |
|
| 132 |
bucketloop: |
| 133 |
for { |
| 134 |
for i := uintptr(0); i < bucketCnt; i++ { |
| 135 |
if isEmpty(b.tophash[i]) { |
| 136 |
if insertb == nil { |
| 137 |
inserti = i |
| 138 |
insertb = b |
| 139 |
} |
| 140 |
if b.tophash[i] == emptyRest { |
| 141 |
break bucketloop |
| 142 |
} |
| 143 |
continue |
| 144 |
} |
| 145 |
k := *((*uint32)(add(unsafe.Pointer(b), dataOffset+i*4))) |
| 146 |
if k != key { |
| 147 |
continue |
| 148 |
} |
| 149 |
inserti = i |
| 150 |
insertb = b |
| 151 |
goto done |
| 152 |
} |
| 153 |
ovf := b.overflow(t) |
| 154 |
if ovf == nil { |
| 155 |
break |
| 156 |
} |
| 157 |
b = ovf |
| 158 |
} |
| 159 |
|
| 160 |
// Did not find mapping for key. Allocate new cell & add entry. |
| 161 |
|
| 162 |
// If we hit the max load factor or we have too many overflow buckets, |
| 163 |
// and we're not already in the middle of growing, start growing. |
| 164 |
if !h.growing() && (overLoadFactor(h.count+1, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) { |
| 165 |
hashGrow(t, h) |
| 166 |
goto again // Growing the table invalidates everything, so try again |
| 167 |
} |
| 168 |
|
| 169 |
if insertb == nil { |
| 170 |
// The current bucket and all the overflow buckets connected to it are full, allocate a new one. |
| 171 |
insertb = h.newoverflow(t, b) |
| 172 |
inserti = 0 // not necessary, but avoids needlessly spilling inserti |
| 173 |
} |
| 174 |
insertb.tophash[inserti&(bucketCnt-1)] = tophash(hash) // mask inserti to avoid bounds checks |
| 175 |
|
| 176 |
insertk = add(unsafe.Pointer(insertb), dataOffset+inserti*4) |
| 177 |
// store new key at insert position |
| 178 |
*(*uint32)(insertk) = key |
| 179 |
|
| 180 |
h.count++ |
| 181 |
|
| 182 |
done: |
| 183 |
elem := add(unsafe.Pointer(insertb), dataOffset+bucketCnt*4+inserti*uintptr(t.elemsize)) |
| 184 |
if h.flags&hashWriting == 0 { |
| 185 |
throw("concurrent map writes") |
| 186 |
} |
| 187 |
h.flags &^= hashWriting |
| 188 |
return elem |
| 189 |
} |
| 190 |
|
| 191 |
func mapassign_fast32ptr(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer { |
| 192 |
if h == nil { |
| 193 |
panic(plainError("assignment to entry in nil map")) |
| 194 |
} |
| 195 |
if raceenabled { |
| 196 |
callerpc := getcallerpc() |
| 197 |
racewritepc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapassign_fast32)) |
| 198 |
} |
| 199 |
if h.flags&hashWriting != 0 { |
| 200 |
throw("concurrent map writes") |
| 201 |
} |
| 202 |
hash := t.hasher(noescape(unsafe.Pointer(&key)), uintptr(h.hash0)) |
| 203 |
|
| 204 |
// Set hashWriting after calling t.hasher for consistency with mapassign. |
| 205 |
h.flags ^= hashWriting |
| 206 |
|
| 207 |
if h.buckets == nil { |
| 208 |
h.buckets = newobject(t.bucket) // newarray(t.bucket, 1) |
| 209 |
} |
| 210 |
|
| 211 |
again: |
| 212 |
bucket := hash & bucketMask(h.B) |
| 213 |
if h.growing() { |
| 214 |
growWork_fast32(t, h, bucket) |
| 215 |
} |
| 216 |
b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) |
| 217 |
|
| 218 |
var insertb *bmap |
| 219 |
var inserti uintptr |
| 220 |
var insertk unsafe.Pointer |
| 221 |
|
| 222 |
bucketloop: |
| 223 |
for { |
| 224 |
for i := uintptr(0); i < bucketCnt; i++ { |
| 225 |
if isEmpty(b.tophash[i]) { |
| 226 |
if insertb == nil { |
| 227 |
inserti = i |
| 228 |
insertb = b |
| 229 |
} |
| 230 |
if b.tophash[i] == emptyRest { |
| 231 |
break bucketloop |
| 232 |
} |
| 233 |
continue |
| 234 |
} |
| 235 |
k := *((*unsafe.Pointer)(add(unsafe.Pointer(b), dataOffset+i*4))) |
| 236 |
if k != key { |
| 237 |
continue |
| 238 |
} |
| 239 |
inserti = i |
| 240 |
insertb = b |
| 241 |
goto done |
| 242 |
} |
| 243 |
ovf := b.overflow(t) |
| 244 |
if ovf == nil { |
| 245 |
break |
| 246 |
} |
| 247 |
b = ovf |
| 248 |
} |
| 249 |
|
| 250 |
// Did not find mapping for key. Allocate new cell & add entry. |
| 251 |
|
| 252 |
// If we hit the max load factor or we have too many overflow buckets, |
| 253 |
// and we're not already in the middle of growing, start growing. |
| 254 |
if !h.growing() && (overLoadFactor(h.count+1, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) { |
| 255 |
hashGrow(t, h) |
| 256 |
goto again // Growing the table invalidates everything, so try again |
| 257 |
} |
| 258 |
|
| 259 |
if insertb == nil { |
| 260 |
// The current bucket and all the overflow buckets connected to it are full, allocate a new one. |
| 261 |
insertb = h.newoverflow(t, b) |
| 262 |
inserti = 0 // not necessary, but avoids needlessly spilling inserti |
| 263 |
} |
| 264 |
insertb.tophash[inserti&(bucketCnt-1)] = tophash(hash) // mask inserti to avoid bounds checks |
| 265 |
|
| 266 |
insertk = add(unsafe.Pointer(insertb), dataOffset+inserti*4) |
| 267 |
// store new key at insert position |
| 268 |
*(*unsafe.Pointer)(insertk) = key |
| 269 |
|
| 270 |
h.count++ |
| 271 |
|
| 272 |
done: |
| 273 |
elem := add(unsafe.Pointer(insertb), dataOffset+bucketCnt*4+inserti*uintptr(t.elemsize)) |
| 274 |
if h.flags&hashWriting == 0 { |
| 275 |
throw("concurrent map writes") |
| 276 |
} |
| 277 |
h.flags &^= hashWriting |
| 278 |
return elem |
| 279 |
} |
| 280 |
|
| 281 |
func mapdelete_fast32(t *maptype, h *hmap, key uint32) { |
| 282 |
if raceenabled && h != nil { |
| 283 |
callerpc := getcallerpc() |
| 284 |
racewritepc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapdelete_fast32)) |
| 285 |
} |
| 286 |
if h == nil || h.count == 0 { |
| 287 |
return |
| 288 |
} |
| 289 |
if h.flags&hashWriting != 0 { |
| 290 |
throw("concurrent map writes") |
| 291 |
} |
| 292 |
|
| 293 |
hash := t.hasher(noescape(unsafe.Pointer(&key)), uintptr(h.hash0)) |
| 294 |
|
| 295 |
// Set hashWriting after calling t.hasher for consistency with mapdelete |
| 296 |
h.flags ^= hashWriting |
| 297 |
|
| 298 |
bucket := hash & bucketMask(h.B) |
| 299 |
if h.growing() { |
| 300 |
growWork_fast32(t, h, bucket) |
| 301 |
} |
| 302 |
b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) |
| 303 |
bOrig := b |
| 304 |
search: |
| 305 |
for ; b != nil; b = b.overflow(t) { |
| 306 |
for i, k := uintptr(0), b.keys(); i < bucketCnt; i, k = i+1, add(k, 4) { |
| 307 |
if key != *(*uint32)(k) || isEmpty(b.tophash[i]) { |
| 308 |
continue |
| 309 |
} |
| 310 |
// Only clear key if there are pointers in it. |
| 311 |
// This can only happen if pointers are 32 bit |
| 312 |
// wide as 64 bit pointers do not fit into a 32 bit key. |
| 313 |
if goarch.PtrSize == 4 && t.key.ptrdata != 0 { |
| 314 |
// The key must be a pointer as we checked pointers are |
| 315 |
// 32 bits wide and the key is 32 bits wide also. |
| 316 |
*(*unsafe.Pointer)(k) = nil |
| 317 |
} |
| 318 |
e := add(unsafe.Pointer(b), dataOffset+bucketCnt*4+i*uintptr(t.elemsize)) |
| 319 |
if t.elem.ptrdata != 0 { |
| 320 |
memclrHasPointers(e, t.elem.size) |
| 321 |
} else { |
| 322 |
memclrNoHeapPointers(e, t.elem.size) |
| 323 |
} |
| 324 |
b.tophash[i] = emptyOne |
| 325 |
// If the bucket now ends in a bunch of emptyOne states, |
| 326 |
// change those to emptyRest states. |
| 327 |
if i == bucketCnt-1 { |
| 328 |
if b.overflow(t) != nil && b.overflow(t).tophash[0] != emptyRest { |
| 329 |
goto notLast |
| 330 |
} |
| 331 |
} else { |
| 332 |
if b.tophash[i+1] != emptyRest { |
| 333 |
goto notLast |
| 334 |
} |
| 335 |
} |
| 336 |
for { |
| 337 |
b.tophash[i] = emptyRest |
| 338 |
if i == 0 { |
| 339 |
if b == bOrig { |
| 340 |
break // beginning of initial bucket, we're done. |
| 341 |
} |
| 342 |
// Find previous bucket, continue at its last entry. |
| 343 |
c := b |
| 344 |
for b = bOrig; b.overflow(t) != c; b = b.overflow(t) { |
| 345 |
} |
| 346 |
i = bucketCnt - 1 |
| 347 |
} else { |
| 348 |
i-- |
| 349 |
} |
| 350 |
if b.tophash[i] != emptyOne { |
| 351 |
break |
| 352 |
} |
| 353 |
} |
| 354 |
notLast: |
| 355 |
h.count-- |
| 356 |
// Reset the hash seed to make it more difficult for attackers to |
| 357 |
// repeatedly trigger hash collisions. See issue 25237. |
| 358 |
if h.count == 0 { |
| 359 |
h.hash0 = fastrand() |
| 360 |
} |
| 361 |
break search |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if h.flags&hashWriting == 0 { |
| 366 |
throw("concurrent map writes") |
| 367 |
} |
| 368 |
h.flags &^= hashWriting |
| 369 |
} |
| 370 |
|
| 371 |
func growWork_fast32(t *maptype, h *hmap, bucket uintptr) { |
| 372 |
// make sure we evacuate the oldbucket corresponding |
| 373 |
// to the bucket we're about to use |
| 374 |
evacuate_fast32(t, h, bucket&h.oldbucketmask()) |
| 375 |
|
| 376 |
// evacuate one more oldbucket to make progress on growing |
| 377 |
if h.growing() { |
| 378 |
evacuate_fast32(t, h, h.nevacuate) |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
func evacuate_fast32(t *maptype, h *hmap, oldbucket uintptr) { |
| 383 |
b := (*bmap)(add(h.oldbuckets, oldbucket*uintptr(t.bucketsize))) |
| 384 |
newbit := h.noldbuckets() |
| 385 |
if !evacuated(b) { |
| 386 |
// TODO: reuse overflow buckets instead of using new ones, if there |
| 387 |
// is no iterator using the old buckets. (If !oldIterator.) |
| 388 |
|
| 389 |
// xy contains the x and y (low and high) evacuation destinations. |
| 390 |
var xy [2]evacDst |
| 391 |
x := &xy[0] |
| 392 |
x.b = (*bmap)(add(h.buckets, oldbucket*uintptr(t.bucketsize))) |
| 393 |
x.k = add(unsafe.Pointer(x.b), dataOffset) |
| 394 |
x.e = add(x.k, bucketCnt*4) |
| 395 |
|
| 396 |
if !h.sameSizeGrow() { |
| 397 |
// Only calculate y pointers if we're growing bigger. |
| 398 |
// Otherwise GC can see bad pointers. |
| 399 |
y := &xy[1] |
| 400 |
y.b = (*bmap)(add(h.buckets, (oldbucket+newbit)*uintptr(t.bucketsize))) |
| 401 |
y.k = add(unsafe.Pointer(y.b), dataOffset) |
| 402 |
y.e = add(y.k, bucketCnt*4) |
| 403 |
} |
| 404 |
|
| 405 |
for ; b != nil; b = b.overflow(t) { |
| 406 |
k := add(unsafe.Pointer(b), dataOffset) |
| 407 |
e := add(k, bucketCnt*4) |
| 408 |
for i := 0; i < bucketCnt; i, k, e = i+1, add(k, 4), add(e, uintptr(t.elemsize)) { |
| 409 |
top := b.tophash[i] |
| 410 |
if isEmpty(top) { |
| 411 |
b.tophash[i] = evacuatedEmpty |
| 412 |
continue |
| 413 |
} |
| 414 |
if top < minTopHash { |
| 415 |
throw("bad map state") |
| 416 |
} |
| 417 |
var useY uint8 |
| 418 |
if !h.sameSizeGrow() { |
| 419 |
// Compute hash to make our evacuation decision (whether we need |
| 420 |
// to send this key/elem to bucket x or bucket y). |
| 421 |
hash := t.hasher(k, uintptr(h.hash0)) |
| 422 |
if hash&newbit != 0 { |
| 423 |
useY = 1 |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
b.tophash[i] = evacuatedX + useY // evacuatedX + 1 == evacuatedY, enforced in makemap |
| 428 |
dst := &xy[useY] // evacuation destination |
| 429 |
|
| 430 |
if dst.i == bucketCnt { |
| 431 |
dst.b = h.newoverflow(t, dst.b) |
| 432 |
dst.i = 0 |
| 433 |
dst.k = add(unsafe.Pointer(dst.b), dataOffset) |
| 434 |
dst.e = add(dst.k, bucketCnt*4) |
| 435 |
} |
| 436 |
dst.b.tophash[dst.i&(bucketCnt-1)] = top // mask dst.i as an optimization, to avoid a bounds check |
| 437 |
|
| 438 |
// Copy key. |
| 439 |
if goarch.PtrSize == 4 && t.key.ptrdata != 0 && writeBarrier.enabled { |
| 440 |
// Write with a write barrier. |
| 441 |
*(*unsafe.Pointer)(dst.k) = *(*unsafe.Pointer)(k) |
| 442 |
} else { |
| 443 |
*(*uint32)(dst.k) = *(*uint32)(k) |
| 444 |
} |
| 445 |
|
| 446 |
typedmemmove(t.elem, dst.e, e) |
| 447 |
dst.i++ |
| 448 |
// These updates might push these pointers past the end of the |
| 449 |
// key or elem arrays. That's ok, as we have the overflow pointer |
| 450 |
// at the end of the bucket to protect against pointing past the |
| 451 |
// end of the bucket. |
| 452 |
dst.k = add(dst.k, 4) |
| 453 |
dst.e = add(dst.e, uintptr(t.elemsize)) |
| 454 |
} |
| 455 |
} |
| 456 |
// Unlink the overflow buckets & clear key/elem to help GC. |
| 457 |
if h.flags&oldIterator == 0 && t.bucket.ptrdata != 0 { |
| 458 |
b := add(h.oldbuckets, oldbucket*uintptr(t.bucketsize)) |
| 459 |
// Preserve b.tophash because the evacuation |
| 460 |
// state is maintained there. |
| 461 |
ptr := add(b, dataOffset) |
| 462 |
n := uintptr(t.bucketsize) - dataOffset |
| 463 |
memclrHasPointers(ptr, n) |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
if oldbucket == h.nevacuate { |
| 468 |
advanceEvacuationMark(h, t, newbit) |
| 469 |
} |
| 470 |
} |