| 1 |
// Copyright 2024 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 |
//go:build !goexperiment.swissmap |
| 6 |
|
| 7 |
package runtime |
| 8 |
|
| 9 |
import ( |
| 10 |
"internal/abi" |
| 11 |
"unsafe" |
| 12 |
) |
| 13 |
|
| 14 |
const RuntimeHmapSize = unsafe.Sizeof(hmap{}) |
| 15 |
|
| 16 |
func OverLoadFactor(count int, B uint8) bool { |
| 17 |
return overLoadFactor(count, B) |
| 18 |
} |
| 19 |
|
| 20 |
func MapBucketsCount(m map[int]int) int { |
| 21 |
h := *(**hmap)(unsafe.Pointer(&m)) |
| 22 |
return 1 << h.B |
| 23 |
} |
| 24 |
|
| 25 |
func MapBucketsPointerIsNil(m map[int]int) bool { |
| 26 |
h := *(**hmap)(unsafe.Pointer(&m)) |
| 27 |
return h.buckets == nil |
| 28 |
} |
| 29 |
|
| 30 |
func MapTombstoneCheck(m map[int]int) { |
| 31 |
// Make sure emptyOne and emptyRest are distributed correctly. |
| 32 |
// We should have a series of filled and emptyOne cells, followed by |
| 33 |
// a series of emptyRest cells. |
| 34 |
h := *(**hmap)(unsafe.Pointer(&m)) |
| 35 |
i := any(m) |
| 36 |
t := *(**maptype)(unsafe.Pointer(&i)) |
| 37 |
|
| 38 |
for x := 0; x < 1<<h.B; x++ { |
| 39 |
b0 := (*bmap)(add(h.buckets, uintptr(x)*uintptr(t.BucketSize))) |
| 40 |
n := 0 |
| 41 |
for b := b0; b != nil; b = b.overflow(t) { |
| 42 |
for i := 0; i < abi.OldMapBucketCount; i++ { |
| 43 |
if b.tophash[i] != emptyRest { |
| 44 |
n++ |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
k := 0 |
| 49 |
for b := b0; b != nil; b = b.overflow(t) { |
| 50 |
for i := 0; i < abi.OldMapBucketCount; i++ { |
| 51 |
if k < n && b.tophash[i] == emptyRest { |
| 52 |
panic("early emptyRest") |
| 53 |
} |
| 54 |
if k >= n && b.tophash[i] != emptyRest { |
| 55 |
panic("late non-emptyRest") |
| 56 |
} |
| 57 |
if k == n-1 && b.tophash[i] == emptyOne { |
| 58 |
panic("last non-emptyRest entry is emptyOne") |
| 59 |
} |
| 60 |
k++ |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |