s1 := dtypesym(t.Key())
s2 := dtypesym(t.Elem())
s3 := dtypesym(bmap(t))
- s4 := dtypesym(hmap(t))
ot = dcommontype(lsym, t)
ot = dsymptr(lsym, ot, s1, 0)
ot = dsymptr(lsym, ot, s2, 0)
ot = dsymptr(lsym, ot, s3, 0)
- ot = dsymptr(lsym, ot, s4, 0)
if t.Key().Width > MAXKEYSIZE {
ot = duint8(lsym, ot, uint8(Widthptr))
ot = duint8(lsym, ot, 1) // indirect
case kindChan: // reflect.chanType
off += 2 * arch.PtrSize
case kindMap: // reflect.mapType
- off += 4*arch.PtrSize + 8
+ off += 3*arch.PtrSize + 8
case kindInterface: // reflect.interfaceType
off += 3 * arch.PtrSize
default:
key *rtype // map key type
elem *rtype // map element (value) type
bucket *rtype // internal bucket structure
- hmap *rtype // internal map header
keysize uint8 // size of key slot
indirectkey uint8 // store ptr to key instead of key itself
valuesize uint8 // size of value slot
rw.rw.unlock()
}
+const RuntimeHmapSize = unsafe.Sizeof(hmap{})
+
func MapBucketsCount(m map[int]int) int {
h := *(**hmap)(unsafe.Pointer(&m))
return 1 << h.B
// If h != nil, the map can be created directly in h.
// If h.buckets != nil, bucket pointed to can be used as the first bucket.
func makemap(t *maptype, hint int, h *hmap) *hmap {
- // The size of hmap should be 48 bytes on 64 bit
- // and 28 bytes on 32 bit platforms.
- if sz := unsafe.Sizeof(hmap{}); sz != 8+5*sys.PtrSize {
- println("runtime: sizeof(hmap) =", sz, ", t.hmap.size =", t.hmap.size)
- throw("bad hmap size")
- }
-
if hint < 0 || hint > int(maxSliceCap(t.bucket.size)) {
hint = 0
}
//go:linkname reflect_makemap reflect.makemap
func reflect_makemap(t *maptype, cap int) *hmap {
// Check invariants and reflects math.
- if sz := unsafe.Sizeof(hmap{}); sz != t.hmap.size {
- println("runtime: sizeof(hmap) =", sz, ", t.hmap.size =", t.hmap.size)
- throw("bad hmap size")
- }
if !ismapkey(t.key) {
throw("runtime.reflect_makemap: unsupported map key type")
}
"math"
"reflect"
"runtime"
+ "runtime/internal/sys"
"sort"
"strconv"
"strings"
"testing"
)
+func TestHmapSize(t *testing.T) {
+ // The structure of hmap is defined in runtime/map.go
+ // and in cmd/compile/internal/gc/reflect.go and must be in sync.
+ // The size of hmap should be 48 bytes on 64 bit and 28 bytes on 32 bit platforms.
+ var hmapSize = uintptr(8 + 5*sys.PtrSize)
+ if runtime.RuntimeHmapSize != hmapSize {
+ t.Errorf("sizeof(runtime.hmap{})==%d, want %d", runtime.RuntimeHmapSize, hmapSize)
+ }
+
+}
+
// negative zero is a good test because:
// 1) 0 and -0 are equal, yet have distinct representations.
// 2) 0 is represented as all zeros, -0 isn't.
key *_type
elem *_type
bucket *_type // internal type representing a hash bucket
- hmap *_type // internal type representing a hmap
keysize uint8 // size of key slot
indirectkey bool // store ptr to key instead of key itself
valuesize uint8 // size of value slot