2014-07-16 15:16:19 -06:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
// This file contains the implementation of Go's map type.
|
|
|
|
//
|
|
|
|
// A map is just a hash table. The data is arranged
|
|
|
|
// into an array of buckets. Each bucket contains up to
|
|
|
|
// 8 key/value pairs. The low-order bits of the hash are
|
|
|
|
// used to select a bucket. Each bucket contains a few
|
|
|
|
// high-order bits of each hash to distinguish the entries
|
|
|
|
// within a single bucket.
|
|
|
|
//
|
|
|
|
// If more than 8 keys hash to a bucket, we chain on
|
|
|
|
// extra buckets.
|
|
|
|
//
|
|
|
|
// When the hashtable grows, we allocate a new array
|
|
|
|
// of buckets twice as big. Buckets are incrementally
|
|
|
|
// copied from the old bucket array to the new bucket array.
|
|
|
|
//
|
|
|
|
// Map iterators walk through the array of buckets and
|
|
|
|
// return the keys in walk order (bucket #, then overflow
|
|
|
|
// chain order, then bucket index). To maintain iteration
|
|
|
|
// semantics, we never move keys within their bucket (if
|
|
|
|
// we did, keys might be returned 0 or 2 times). When
|
|
|
|
// growing the table, iterators remain iterating through the
|
|
|
|
// old table and must check the new table if the bucket
|
|
|
|
// they are iterating through has been moved ("evacuated")
|
|
|
|
// to the new table.
|
|
|
|
|
|
|
|
// Picking loadFactor: too large and we have lots of overflow
|
|
|
|
// buckets, too small and we waste a lot of space. I wrote
|
|
|
|
// a simple program to check some stats for different loads:
|
|
|
|
// (64-bit, 8 byte keys and values)
|
|
|
|
// loadFactor %overflow bytes/entry hitprobe missprobe
|
|
|
|
// 4.00 2.13 20.77 3.00 4.00
|
|
|
|
// 4.50 4.05 17.30 3.25 4.50
|
|
|
|
// 5.00 6.85 14.77 3.50 5.00
|
|
|
|
// 5.50 10.55 12.94 3.75 5.50
|
|
|
|
// 6.00 15.27 11.67 4.00 6.00
|
|
|
|
// 6.50 20.90 10.79 4.25 6.50
|
|
|
|
// 7.00 27.14 10.15 4.50 7.00
|
|
|
|
// 7.50 34.03 9.73 4.75 7.50
|
|
|
|
// 8.00 41.10 9.40 5.00 8.00
|
|
|
|
//
|
|
|
|
// %overflow = percentage of buckets which have an overflow bucket
|
|
|
|
// bytes/entry = overhead bytes used per key/value pair
|
|
|
|
// hitprobe = # of entries to check when looking up a present key
|
|
|
|
// missprobe = # of entries to check when looking up an absent key
|
|
|
|
//
|
|
|
|
// Keep in mind this data is for maximally loaded tables, i.e. just
|
|
|
|
// before the table grows. Typical tables will be somewhat less loaded.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Maximum number of key/value pairs a bucket can hold.
|
2014-09-09 15:22:58 -06:00
|
|
|
bucketCntBits = 3
|
|
|
|
bucketCnt = 1 << bucketCntBits
|
2014-07-16 15:16:19 -06:00
|
|
|
|
|
|
|
// Maximum average load of a bucket that triggers growth.
|
|
|
|
loadFactor = 6.5
|
|
|
|
|
|
|
|
// Maximum key or value size to keep inline (instead of mallocing per element).
|
|
|
|
// Must fit in a uint8.
|
|
|
|
// Fast versions cannot handle big values - the cutoff size for
|
2015-03-11 13:58:47 -06:00
|
|
|
// fast versions in ../../cmd/internal/gc/walk.go must be at most this value.
|
2014-07-16 15:16:19 -06:00
|
|
|
maxKeySize = 128
|
|
|
|
maxValueSize = 128
|
|
|
|
|
|
|
|
// data offset should be the size of the bmap struct, but needs to be
|
|
|
|
// aligned correctly. For amd64p32 this means 64-bit alignment
|
|
|
|
// even though pointers are 32 bit.
|
|
|
|
dataOffset = unsafe.Offsetof(struct {
|
|
|
|
b bmap
|
|
|
|
v int64
|
|
|
|
}{}.v)
|
|
|
|
|
|
|
|
// Possible tophash values. We reserve a few possibilities for special marks.
|
|
|
|
// Each bucket (including its overflow buckets, if any) will have either all or none of its
|
|
|
|
// entries in the evacuated* states (except during the evacuate() method, which only happens
|
|
|
|
// during map writes and thus no one else can observe the map during that time).
|
|
|
|
empty = 0 // cell is empty
|
|
|
|
evacuatedEmpty = 1 // cell is empty, bucket is evacuated.
|
|
|
|
evacuatedX = 2 // key/value is valid. Entry has been evacuated to first half of larger table.
|
|
|
|
evacuatedY = 3 // same as above, but evacuated to second half of larger table.
|
|
|
|
minTopHash = 4 // minimum tophash for a normal filled cell.
|
|
|
|
|
|
|
|
// flags
|
2014-08-01 15:38:56 -06:00
|
|
|
iterator = 1 // there may be an iterator using buckets
|
|
|
|
oldIterator = 2 // there may be an iterator using oldbuckets
|
2014-07-16 15:16:19 -06:00
|
|
|
|
|
|
|
// sentinel bucket ID for iterator checks
|
|
|
|
noCheck = 1<<(8*ptrSize) - 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// A header for a Go map.
|
|
|
|
type hmap struct {
|
2015-03-11 13:58:47 -06:00
|
|
|
// Note: the format of the Hmap is encoded in ../../cmd/internal/gc/reflect.go and
|
2014-07-16 15:16:19 -06:00
|
|
|
// ../reflect/type.go. Don't change this structure without also changing that code!
|
2014-08-01 15:38:56 -06:00
|
|
|
count int // # live cells == size of map. Must be first (used by len() builtin)
|
2015-01-26 11:04:41 -07:00
|
|
|
flags uint8
|
2014-08-01 15:38:56 -06:00
|
|
|
B uint8 // log_2 of # of buckets (can hold up to loadFactor * 2^B items)
|
2015-01-26 11:04:41 -07:00
|
|
|
hash0 uint32 // hash seed
|
2014-07-16 15:16:19 -06:00
|
|
|
|
|
|
|
buckets unsafe.Pointer // array of 2^B Buckets. may be nil if count==0.
|
|
|
|
oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing
|
|
|
|
nevacuate uintptr // progress counter for evacuation (buckets less than this have been evacuated)
|
2015-01-26 11:04:41 -07:00
|
|
|
|
2015-02-14 06:10:06 -07:00
|
|
|
// If both key and value do not contain pointers and are inline, then we mark bucket
|
2015-01-26 11:04:41 -07:00
|
|
|
// type as containing no pointers. This avoids scanning such maps.
|
|
|
|
// However, bmap.overflow is a pointer. In order to keep overflow buckets
|
|
|
|
// alive, we store pointers to all overflow buckets in hmap.overflow.
|
|
|
|
// Overflow is used only if key and value do not contain pointers.
|
|
|
|
// overflow[0] contains overflow buckets for hmap.buckets.
|
|
|
|
// overflow[1] contains overflow buckets for hmap.oldbuckets.
|
|
|
|
// The first indirection allows us to reduce static size of hmap.
|
|
|
|
// The second indirection allows to store a pointer to the slice in hiter.
|
|
|
|
overflow *[2]*[]*bmap
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A bucket for a Go map.
|
|
|
|
type bmap struct {
|
2014-12-25 19:44:55 -07:00
|
|
|
tophash [bucketCnt]uint8
|
2014-07-16 15:16:19 -06:00
|
|
|
// Followed by bucketCnt keys and then bucketCnt values.
|
|
|
|
// NOTE: packing all the keys together and then all the values together makes the
|
|
|
|
// code a bit more complicated than alternating key/value/key/value/... but it allows
|
|
|
|
// us to eliminate padding which would be needed for, e.g., map[int64]int8.
|
2014-12-19 21:44:18 -07:00
|
|
|
// Followed by an overflow pointer.
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A hash iteration structure.
|
2015-03-11 13:58:47 -06:00
|
|
|
// If you modify hiter, also change cmd/internal/gc/reflect.go to indicate
|
2014-07-16 15:16:19 -06:00
|
|
|
// the layout of this structure.
|
|
|
|
type hiter struct {
|
2015-03-11 13:58:47 -06:00
|
|
|
key unsafe.Pointer // Must be in first position. Write nil to indicate iteration end (see cmd/internal/gc/range.go).
|
|
|
|
value unsafe.Pointer // Must be in second position (see cmd/internal/gc/range.go).
|
2014-07-16 15:16:19 -06:00
|
|
|
t *maptype
|
|
|
|
h *hmap
|
|
|
|
buckets unsafe.Pointer // bucket ptr at hash_iter initialization time
|
|
|
|
bptr *bmap // current bucket
|
2015-01-26 11:04:41 -07:00
|
|
|
overflow [2]*[]*bmap // keeps overflow buckets alive
|
2014-09-08 18:42:21 -06:00
|
|
|
startBucket uintptr // bucket iteration started at
|
2014-07-16 15:16:19 -06:00
|
|
|
offset uint8 // intra-bucket offset to start from during iteration (should be big enough to hold bucketCnt-1)
|
2014-09-08 18:42:21 -06:00
|
|
|
wrapped bool // already wrapped around from end of bucket array to beginning
|
2014-07-16 15:16:19 -06:00
|
|
|
B uint8
|
2014-09-09 15:22:58 -06:00
|
|
|
i uint8
|
2014-07-16 15:16:19 -06:00
|
|
|
bucket uintptr
|
|
|
|
checkBucket uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
func evacuated(b *bmap) bool {
|
|
|
|
h := b.tophash[0]
|
|
|
|
return h > empty && h < minTopHash
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:44:18 -07:00
|
|
|
func (b *bmap) overflow(t *maptype) *bmap {
|
cmd/compile, runtime: fix placement of map bucket overflow pointer on nacl
On most systems, a pointer is the worst case alignment, so adding
a pointer field at the end of a struct guarantees there will be no
padding added after that field (to satisfy overall struct alignment
due to some more-aligned field also present).
In the runtime, the map implementation needs a quick way to
get to the overflow pointer, which is last in the bucket struct,
so it uses size - sizeof(pointer) as the offset.
NaCl/amd64p32 is the exception, as always.
The worst case alignment is 64 bits but pointers are 32 bits.
There's a long history that is not worth going into, but when
we moved the overflow pointer to the end of the struct,
we didn't get the padding computation right.
The compiler computed the regular struct size and then
on amd64p32 added another 32-bit field.
And the runtime assumed it could step back two 32-bit fields
(one 64-bit register size) to get to the overflow pointer.
But in fact if the struct needed 64-bit alignment, the computation
of the regular struct size would have added a 32-bit pad already,
and then the code unconditionally added a second 32-bit pad.
This placed the overflow pointer three words from the end, not two.
The last two were padding, and since the runtime was consistent
about using the second-to-last word as the overflow pointer,
no harm done in the sense of overwriting useful memory.
But writing the overflow pointer to a non-pointer word of memory
means that the GC can't see the overflow blocks, so it will
collect them prematurely. Then bad things happen.
Correct all this in a few steps:
1. Add an explicit check at the end of the bucket layout in the
compiler that the overflow field is last in the struct, never
followed by padding.
2. When padding is needed on nacl (not always, just when needed),
insert it before the overflow pointer, to preserve the "last in the struct"
property.
3. Let the compiler have the final word on the width of the struct,
by inserting an explicit padding field instead of overwriting the
results of the width computation it does.
4. For the same reason (tell the truth to the compiler), set the type
of the overflow field when we're trying to pretend its not a pointer
(in this case the runtime maintains a list of the overflow blocks
elsewhere).
5. Make the runtime use "last in the struct" as its location algorithm.
This fixes TestTraceStress on nacl/amd64p32.
The 'bad map state' and 'invalid free list' failures no longer occur.
Fixes #11838.
Change-Id: If918887f8f252d988db0a35159944d2b36512f92
Reviewed-on: https://go-review.googlesource.com/12971
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
2015-07-30 20:05:51 -06:00
|
|
|
return *(**bmap)(add(unsafe.Pointer(b), uintptr(t.bucketsize)-ptrSize))
|
2014-12-19 21:44:18 -07:00
|
|
|
}
|
2015-01-26 11:04:41 -07:00
|
|
|
|
|
|
|
func (h *hmap) setoverflow(t *maptype, b, ovf *bmap) {
|
|
|
|
if t.bucket.kind&kindNoPointers != 0 {
|
|
|
|
h.createOverflow()
|
|
|
|
*h.overflow[0] = append(*h.overflow[0], ovf)
|
|
|
|
}
|
cmd/compile, runtime: fix placement of map bucket overflow pointer on nacl
On most systems, a pointer is the worst case alignment, so adding
a pointer field at the end of a struct guarantees there will be no
padding added after that field (to satisfy overall struct alignment
due to some more-aligned field also present).
In the runtime, the map implementation needs a quick way to
get to the overflow pointer, which is last in the bucket struct,
so it uses size - sizeof(pointer) as the offset.
NaCl/amd64p32 is the exception, as always.
The worst case alignment is 64 bits but pointers are 32 bits.
There's a long history that is not worth going into, but when
we moved the overflow pointer to the end of the struct,
we didn't get the padding computation right.
The compiler computed the regular struct size and then
on amd64p32 added another 32-bit field.
And the runtime assumed it could step back two 32-bit fields
(one 64-bit register size) to get to the overflow pointer.
But in fact if the struct needed 64-bit alignment, the computation
of the regular struct size would have added a 32-bit pad already,
and then the code unconditionally added a second 32-bit pad.
This placed the overflow pointer three words from the end, not two.
The last two were padding, and since the runtime was consistent
about using the second-to-last word as the overflow pointer,
no harm done in the sense of overwriting useful memory.
But writing the overflow pointer to a non-pointer word of memory
means that the GC can't see the overflow blocks, so it will
collect them prematurely. Then bad things happen.
Correct all this in a few steps:
1. Add an explicit check at the end of the bucket layout in the
compiler that the overflow field is last in the struct, never
followed by padding.
2. When padding is needed on nacl (not always, just when needed),
insert it before the overflow pointer, to preserve the "last in the struct"
property.
3. Let the compiler have the final word on the width of the struct,
by inserting an explicit padding field instead of overwriting the
results of the width computation it does.
4. For the same reason (tell the truth to the compiler), set the type
of the overflow field when we're trying to pretend its not a pointer
(in this case the runtime maintains a list of the overflow blocks
elsewhere).
5. Make the runtime use "last in the struct" as its location algorithm.
This fixes TestTraceStress on nacl/amd64p32.
The 'bad map state' and 'invalid free list' failures no longer occur.
Fixes #11838.
Change-Id: If918887f8f252d988db0a35159944d2b36512f92
Reviewed-on: https://go-review.googlesource.com/12971
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
2015-07-30 20:05:51 -06:00
|
|
|
*(**bmap)(add(unsafe.Pointer(b), uintptr(t.bucketsize)-ptrSize)) = ovf
|
2014-12-19 21:44:18 -07:00
|
|
|
}
|
|
|
|
|
2015-01-26 11:04:41 -07:00
|
|
|
func (h *hmap) createOverflow() {
|
|
|
|
if h.overflow == nil {
|
|
|
|
h.overflow = new([2]*[]*bmap)
|
|
|
|
}
|
|
|
|
if h.overflow[0] == nil {
|
|
|
|
h.overflow[0] = new([]*bmap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 09:40:02 -07:00
|
|
|
// makemap implements a Go map creation make(map[k]v, hint)
|
|
|
|
// If the compiler has determined that the map or the first bucket
|
|
|
|
// can be created on the stack, h and/or bucket may be non-nil.
|
|
|
|
// If h != nil, the map can be created directly in h.
|
|
|
|
// If bucket != nil, bucket can be used as the first bucket.
|
|
|
|
func makemap(t *maptype, hint int64, h *hmap, bucket unsafe.Pointer) *hmap {
|
2014-07-29 12:06:20 -06:00
|
|
|
if sz := unsafe.Sizeof(hmap{}); sz > 48 || sz != uintptr(t.hmap.size) {
|
2015-01-29 09:40:02 -07:00
|
|
|
println("runtime: sizeof(hmap) =", sz, ", t.hmap.size =", t.hmap.size)
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("bad hmap size")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if hint < 0 || int64(int32(hint)) != hint {
|
|
|
|
panic("makemap: size out of range")
|
|
|
|
// TODO: make hint an int, then none of this nonsense
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ismapkey(t.key) {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime.makemap: unsupported map key type")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
2014-08-01 15:38:56 -06:00
|
|
|
// check compiler's and reflect's math
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.key.size > maxKeySize && (!t.indirectkey || t.keysize != uint8(ptrSize)) ||
|
|
|
|
t.key.size <= maxKeySize && (t.indirectkey || t.keysize != uint8(t.key.size)) {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("key size wrong")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.elem.size > maxValueSize && (!t.indirectvalue || t.valuesize != uint8(ptrSize)) ||
|
|
|
|
t.elem.size <= maxValueSize && (t.indirectvalue || t.valuesize != uint8(t.elem.size)) {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("value size wrong")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// invariants we depend on. We should probably check these at compile time
|
|
|
|
// somewhere, but for now we'll do it here.
|
|
|
|
if t.key.align > bucketCnt {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("key align too big")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
if t.elem.align > bucketCnt {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("value align too big")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
if uintptr(t.key.size)%uintptr(t.key.align) != 0 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("key size not a multiple of key align")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
if uintptr(t.elem.size)%uintptr(t.elem.align) != 0 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("value size not a multiple of value align")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
if bucketCnt < 8 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("bucketsize too small for proper alignment")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
if dataOffset%uintptr(t.key.align) != 0 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("need padding in bucket (key)")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
if dataOffset%uintptr(t.elem.align) != 0 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("need padding in bucket (value)")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
2015-08-20 20:54:55 -06:00
|
|
|
// make sure zeroptr is large enough
|
2015-05-14 15:27:04 -06:00
|
|
|
mapzero(t.elem)
|
|
|
|
|
2014-07-16 15:16:19 -06:00
|
|
|
// find size parameter which will hold the requested # of elements
|
|
|
|
B := uint8(0)
|
|
|
|
for ; hint > bucketCnt && float32(hint) > loadFactor*float32(uintptr(1)<<B); B++ {
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate initial hash table
|
|
|
|
// if B == 0, the buckets field is allocated lazily later (in mapassign)
|
|
|
|
// If hint is large zeroing this memory could take a while.
|
2015-01-29 09:40:02 -07:00
|
|
|
buckets := bucket
|
2014-07-16 15:16:19 -06:00
|
|
|
if B != 0 {
|
2014-07-30 10:01:52 -06:00
|
|
|
buckets = newarray(t.bucket, uintptr(1)<<B)
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize Hmap
|
2015-01-29 09:40:02 -07:00
|
|
|
if h == nil {
|
|
|
|
h = (*hmap)(newobject(t.hmap))
|
|
|
|
}
|
2014-07-16 15:16:19 -06:00
|
|
|
h.count = 0
|
|
|
|
h.B = B
|
2014-08-01 15:38:56 -06:00
|
|
|
h.flags = 0
|
2014-09-02 15:33:33 -06:00
|
|
|
h.hash0 = fastrand1()
|
2014-07-16 15:16:19 -06:00
|
|
|
h.buckets = buckets
|
|
|
|
h.oldbuckets = nil
|
|
|
|
h.nevacuate = 0
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// mapaccess1 returns a pointer to h[key]. Never returns nil, instead
|
|
|
|
// it will return a reference to the zero object for the value type if
|
|
|
|
// the key is not in the map.
|
|
|
|
// NOTE: The returned pointer may keep the whole map live, so don't
|
|
|
|
// hold onto it for very long.
|
|
|
|
func mapaccess1(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
|
|
|
|
if raceenabled && h != nil {
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
callerpc := getcallerpc(unsafe.Pointer(&t))
|
2014-09-03 09:10:38 -06:00
|
|
|
pc := funcPC(mapaccess1)
|
2014-07-16 15:16:19 -06:00
|
|
|
racereadpc(unsafe.Pointer(h), callerpc, pc)
|
|
|
|
raceReadObjectPC(t.key, key, callerpc, pc)
|
|
|
|
}
|
|
|
|
if h == nil || h.count == 0 {
|
2015-08-20 20:54:55 -06:00
|
|
|
return atomicloadp(unsafe.Pointer(&zeroptr))
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
2014-12-27 21:32:11 -07:00
|
|
|
alg := t.key.alg
|
2015-01-06 17:42:48 -07:00
|
|
|
hash := alg.hash(key, uintptr(h.hash0))
|
2014-07-16 15:16:19 -06:00
|
|
|
m := uintptr(1)<<h.B - 1
|
2014-08-01 15:38:56 -06:00
|
|
|
b := (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
if c := h.oldbuckets; c != nil {
|
2014-08-01 15:38:56 -06:00
|
|
|
oldb := (*bmap)(add(c, (hash&(m>>1))*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
if !evacuated(oldb) {
|
|
|
|
b = oldb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
top := uint8(hash >> (ptrSize*8 - 8))
|
|
|
|
if top < minTopHash {
|
|
|
|
top += minTopHash
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
for i := uintptr(0); i < bucketCnt; i++ {
|
|
|
|
if b.tophash[i] != top {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-01 15:38:56 -06:00
|
|
|
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
k = *((*unsafe.Pointer)(k))
|
|
|
|
}
|
2015-01-06 17:42:48 -07:00
|
|
|
if alg.equal(key, k) {
|
2014-08-01 15:38:56 -06:00
|
|
|
v := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.valuesize))
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectvalue {
|
2014-07-16 15:16:19 -06:00
|
|
|
v = *((*unsafe.Pointer)(v))
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 21:44:18 -07:00
|
|
|
b = b.overflow(t)
|
2014-07-16 15:16:19 -06:00
|
|
|
if b == nil {
|
2015-08-20 20:54:55 -06:00
|
|
|
return atomicloadp(unsafe.Pointer(&zeroptr))
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapaccess2(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, bool) {
|
|
|
|
if raceenabled && h != nil {
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
callerpc := getcallerpc(unsafe.Pointer(&t))
|
2014-09-03 09:10:38 -06:00
|
|
|
pc := funcPC(mapaccess2)
|
2014-07-16 15:16:19 -06:00
|
|
|
racereadpc(unsafe.Pointer(h), callerpc, pc)
|
|
|
|
raceReadObjectPC(t.key, key, callerpc, pc)
|
|
|
|
}
|
|
|
|
if h == nil || h.count == 0 {
|
2015-08-20 20:54:55 -06:00
|
|
|
return atomicloadp(unsafe.Pointer(&zeroptr)), false
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
2014-12-27 21:32:11 -07:00
|
|
|
alg := t.key.alg
|
2015-01-06 17:42:48 -07:00
|
|
|
hash := alg.hash(key, uintptr(h.hash0))
|
2014-07-16 15:16:19 -06:00
|
|
|
m := uintptr(1)<<h.B - 1
|
2014-08-01 15:38:56 -06:00
|
|
|
b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + (hash&m)*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
if c := h.oldbuckets; c != nil {
|
2014-08-01 15:38:56 -06:00
|
|
|
oldb := (*bmap)(unsafe.Pointer(uintptr(c) + (hash&(m>>1))*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
if !evacuated(oldb) {
|
|
|
|
b = oldb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
top := uint8(hash >> (ptrSize*8 - 8))
|
|
|
|
if top < minTopHash {
|
|
|
|
top += minTopHash
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
for i := uintptr(0); i < bucketCnt; i++ {
|
|
|
|
if b.tophash[i] != top {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-01 15:38:56 -06:00
|
|
|
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
k = *((*unsafe.Pointer)(k))
|
|
|
|
}
|
2015-01-06 17:42:48 -07:00
|
|
|
if alg.equal(key, k) {
|
2014-08-01 15:38:56 -06:00
|
|
|
v := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.valuesize))
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectvalue {
|
2014-07-16 15:16:19 -06:00
|
|
|
v = *((*unsafe.Pointer)(v))
|
|
|
|
}
|
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 21:44:18 -07:00
|
|
|
b = b.overflow(t)
|
2014-07-16 15:16:19 -06:00
|
|
|
if b == nil {
|
2015-08-20 20:54:55 -06:00
|
|
|
return atomicloadp(unsafe.Pointer(&zeroptr)), false
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns both key and value. Used by map iterator
|
|
|
|
func mapaccessK(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, unsafe.Pointer) {
|
|
|
|
if h == nil || h.count == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2014-12-27 21:32:11 -07:00
|
|
|
alg := t.key.alg
|
2015-01-06 17:42:48 -07:00
|
|
|
hash := alg.hash(key, uintptr(h.hash0))
|
2014-07-16 15:16:19 -06:00
|
|
|
m := uintptr(1)<<h.B - 1
|
2014-08-01 15:38:56 -06:00
|
|
|
b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + (hash&m)*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
if c := h.oldbuckets; c != nil {
|
2014-08-01 15:38:56 -06:00
|
|
|
oldb := (*bmap)(unsafe.Pointer(uintptr(c) + (hash&(m>>1))*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
if !evacuated(oldb) {
|
|
|
|
b = oldb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
top := uint8(hash >> (ptrSize*8 - 8))
|
|
|
|
if top < minTopHash {
|
|
|
|
top += minTopHash
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
for i := uintptr(0); i < bucketCnt; i++ {
|
|
|
|
if b.tophash[i] != top {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-01 15:38:56 -06:00
|
|
|
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
k = *((*unsafe.Pointer)(k))
|
|
|
|
}
|
2015-01-06 17:42:48 -07:00
|
|
|
if alg.equal(key, k) {
|
2014-08-01 15:38:56 -06:00
|
|
|
v := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.valuesize))
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectvalue {
|
2014-07-16 15:16:19 -06:00
|
|
|
v = *((*unsafe.Pointer)(v))
|
|
|
|
}
|
|
|
|
return k, v
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 21:44:18 -07:00
|
|
|
b = b.overflow(t)
|
2014-07-16 15:16:19 -06:00
|
|
|
if b == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapassign1(t *maptype, h *hmap, key unsafe.Pointer, val unsafe.Pointer) {
|
|
|
|
if h == nil {
|
|
|
|
panic("assignment to entry in nil map")
|
|
|
|
}
|
|
|
|
if raceenabled {
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
callerpc := getcallerpc(unsafe.Pointer(&t))
|
2014-09-03 09:10:38 -06:00
|
|
|
pc := funcPC(mapassign1)
|
2014-07-16 15:16:19 -06:00
|
|
|
racewritepc(unsafe.Pointer(h), callerpc, pc)
|
|
|
|
raceReadObjectPC(t.key, key, callerpc, pc)
|
|
|
|
raceReadObjectPC(t.elem, val, callerpc, pc)
|
|
|
|
}
|
|
|
|
|
2014-12-27 21:32:11 -07:00
|
|
|
alg := t.key.alg
|
2015-01-06 17:42:48 -07:00
|
|
|
hash := alg.hash(key, uintptr(h.hash0))
|
2014-07-16 15:16:19 -06:00
|
|
|
|
|
|
|
if h.buckets == nil {
|
2014-07-30 10:01:52 -06:00
|
|
|
h.buckets = newarray(t.bucket, 1)
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
again:
|
|
|
|
bucket := hash & (uintptr(1)<<h.B - 1)
|
|
|
|
if h.oldbuckets != nil {
|
|
|
|
growWork(t, h, bucket)
|
|
|
|
}
|
2014-08-01 15:38:56 -06:00
|
|
|
b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + bucket*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
top := uint8(hash >> (ptrSize*8 - 8))
|
|
|
|
if top < minTopHash {
|
|
|
|
top += minTopHash
|
|
|
|
}
|
|
|
|
|
|
|
|
var inserti *uint8
|
|
|
|
var insertk unsafe.Pointer
|
|
|
|
var insertv unsafe.Pointer
|
|
|
|
for {
|
|
|
|
for i := uintptr(0); i < bucketCnt; i++ {
|
|
|
|
if b.tophash[i] != top {
|
|
|
|
if b.tophash[i] == empty && inserti == nil {
|
|
|
|
inserti = &b.tophash[i]
|
2014-08-01 15:38:56 -06:00
|
|
|
insertk = add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
|
|
|
|
insertv = add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.valuesize))
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2014-08-01 15:38:56 -06:00
|
|
|
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
|
2014-07-16 15:16:19 -06:00
|
|
|
k2 := k
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
k2 = *((*unsafe.Pointer)(k2))
|
|
|
|
}
|
2015-01-06 17:42:48 -07:00
|
|
|
if !alg.equal(key, k2) {
|
2014-07-16 15:16:19 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// already have a mapping for key. Update it.
|
2015-06-08 09:42:28 -06:00
|
|
|
if t.needkeyupdate {
|
|
|
|
typedmemmove(t.key, k2, key)
|
|
|
|
}
|
2014-08-01 15:38:56 -06:00
|
|
|
v := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.valuesize))
|
2014-07-16 15:16:19 -06:00
|
|
|
v2 := v
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectvalue {
|
2014-07-16 15:16:19 -06:00
|
|
|
v2 = *((*unsafe.Pointer)(v2))
|
|
|
|
}
|
2014-12-29 08:07:47 -07:00
|
|
|
typedmemmove(t.elem, v2, val)
|
2014-07-16 15:16:19 -06:00
|
|
|
return
|
|
|
|
}
|
2014-12-19 21:44:18 -07:00
|
|
|
ovf := b.overflow(t)
|
|
|
|
if ovf == nil {
|
2014-07-16 15:16:19 -06:00
|
|
|
break
|
|
|
|
}
|
2014-12-19 21:44:18 -07:00
|
|
|
b = ovf
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// did not find mapping for key. Allocate new cell & add entry.
|
|
|
|
if float32(h.count) >= loadFactor*float32((uintptr(1)<<h.B)) && h.count >= bucketCnt {
|
|
|
|
hashGrow(t, h)
|
|
|
|
goto again // Growing the table invalidates everything, so try again
|
|
|
|
}
|
|
|
|
|
|
|
|
if inserti == nil {
|
|
|
|
// all current buckets are full, allocate a new one.
|
2014-07-30 10:01:52 -06:00
|
|
|
newb := (*bmap)(newobject(t.bucket))
|
2015-01-26 11:04:41 -07:00
|
|
|
h.setoverflow(t, b, newb)
|
2014-07-16 15:16:19 -06:00
|
|
|
inserti = &newb.tophash[0]
|
|
|
|
insertk = add(unsafe.Pointer(newb), dataOffset)
|
2014-08-01 15:38:56 -06:00
|
|
|
insertv = add(insertk, bucketCnt*uintptr(t.keysize))
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// store new key/value at insert position
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-30 10:01:52 -06:00
|
|
|
kmem := newobject(t.key)
|
2014-07-16 15:16:19 -06:00
|
|
|
*(*unsafe.Pointer)(insertk) = kmem
|
|
|
|
insertk = kmem
|
|
|
|
}
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectvalue {
|
2014-07-30 10:01:52 -06:00
|
|
|
vmem := newobject(t.elem)
|
2014-07-16 15:16:19 -06:00
|
|
|
*(*unsafe.Pointer)(insertv) = vmem
|
|
|
|
insertv = vmem
|
|
|
|
}
|
2014-12-29 08:07:47 -07:00
|
|
|
typedmemmove(t.key, insertk, key)
|
|
|
|
typedmemmove(t.elem, insertv, val)
|
2014-07-16 15:16:19 -06:00
|
|
|
*inserti = top
|
|
|
|
h.count++
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapdelete(t *maptype, h *hmap, key unsafe.Pointer) {
|
|
|
|
if raceenabled && h != nil {
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
callerpc := getcallerpc(unsafe.Pointer(&t))
|
2014-09-03 09:10:38 -06:00
|
|
|
pc := funcPC(mapdelete)
|
2014-07-16 15:16:19 -06:00
|
|
|
racewritepc(unsafe.Pointer(h), callerpc, pc)
|
|
|
|
raceReadObjectPC(t.key, key, callerpc, pc)
|
|
|
|
}
|
|
|
|
if h == nil || h.count == 0 {
|
|
|
|
return
|
|
|
|
}
|
2014-12-27 21:32:11 -07:00
|
|
|
alg := t.key.alg
|
2015-01-06 17:42:48 -07:00
|
|
|
hash := alg.hash(key, uintptr(h.hash0))
|
2014-07-16 15:16:19 -06:00
|
|
|
bucket := hash & (uintptr(1)<<h.B - 1)
|
|
|
|
if h.oldbuckets != nil {
|
|
|
|
growWork(t, h, bucket)
|
|
|
|
}
|
2014-08-01 15:38:56 -06:00
|
|
|
b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + bucket*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
top := uint8(hash >> (ptrSize*8 - 8))
|
|
|
|
if top < minTopHash {
|
|
|
|
top += minTopHash
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
for i := uintptr(0); i < bucketCnt; i++ {
|
|
|
|
if b.tophash[i] != top {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-01 15:38:56 -06:00
|
|
|
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
|
2014-07-16 15:16:19 -06:00
|
|
|
k2 := k
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
k2 = *((*unsafe.Pointer)(k2))
|
|
|
|
}
|
2015-01-06 17:42:48 -07:00
|
|
|
if !alg.equal(key, k2) {
|
2014-07-16 15:16:19 -06:00
|
|
|
continue
|
|
|
|
}
|
2014-08-01 15:38:56 -06:00
|
|
|
memclr(k, uintptr(t.keysize))
|
|
|
|
v := unsafe.Pointer(uintptr(unsafe.Pointer(b)) + dataOffset + bucketCnt*uintptr(t.keysize) + i*uintptr(t.valuesize))
|
|
|
|
memclr(v, uintptr(t.valuesize))
|
2014-07-16 15:16:19 -06:00
|
|
|
b.tophash[i] = empty
|
|
|
|
h.count--
|
|
|
|
return
|
|
|
|
}
|
2014-12-19 21:44:18 -07:00
|
|
|
b = b.overflow(t)
|
2014-07-16 15:16:19 -06:00
|
|
|
if b == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapiterinit(t *maptype, h *hmap, it *hiter) {
|
|
|
|
// Clear pointer fields so garbage collector does not complain.
|
|
|
|
it.key = nil
|
|
|
|
it.value = nil
|
|
|
|
it.t = nil
|
|
|
|
it.h = nil
|
|
|
|
it.buckets = nil
|
|
|
|
it.bptr = nil
|
2015-01-26 11:04:41 -07:00
|
|
|
it.overflow[0] = nil
|
|
|
|
it.overflow[1] = nil
|
2014-07-16 15:16:19 -06:00
|
|
|
|
|
|
|
if raceenabled && h != nil {
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
callerpc := getcallerpc(unsafe.Pointer(&t))
|
2014-09-03 09:10:38 -06:00
|
|
|
racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapiterinit))
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if h == nil || h.count == 0 {
|
|
|
|
it.key = nil
|
|
|
|
it.value = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-26 11:04:41 -07:00
|
|
|
if unsafe.Sizeof(hiter{})/ptrSize != 12 {
|
2015-03-11 13:58:47 -06:00
|
|
|
throw("hash_iter size incorrect") // see ../../cmd/internal/gc/reflect.go
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
it.t = t
|
|
|
|
it.h = h
|
|
|
|
|
|
|
|
// grab snapshot of bucket state
|
|
|
|
it.B = h.B
|
|
|
|
it.buckets = h.buckets
|
2015-01-26 11:04:41 -07:00
|
|
|
if t.bucket.kind&kindNoPointers != 0 {
|
|
|
|
// Allocate the current slice and remember pointers to both current and old.
|
|
|
|
// This preserves all relevant overflow buckets alive even if
|
|
|
|
// the table grows and/or overflow buckets are added to the table
|
|
|
|
// while we are iterating.
|
|
|
|
h.createOverflow()
|
|
|
|
it.overflow = *h.overflow
|
|
|
|
}
|
2014-07-16 15:16:19 -06:00
|
|
|
|
2014-09-08 18:42:21 -06:00
|
|
|
// decide where to start
|
2014-09-09 15:22:58 -06:00
|
|
|
r := uintptr(fastrand1())
|
|
|
|
if h.B > 31-bucketCntBits {
|
|
|
|
r += uintptr(fastrand1()) << 31
|
2014-09-08 18:42:21 -06:00
|
|
|
}
|
2014-09-09 15:22:58 -06:00
|
|
|
it.startBucket = r & (uintptr(1)<<h.B - 1)
|
|
|
|
it.offset = uint8(r >> h.B & (bucketCnt - 1))
|
2014-09-08 18:42:21 -06:00
|
|
|
|
2014-07-16 15:16:19 -06:00
|
|
|
// iterator state
|
2014-09-08 18:42:21 -06:00
|
|
|
it.bucket = it.startBucket
|
|
|
|
it.wrapped = false
|
2014-07-16 15:16:19 -06:00
|
|
|
it.bptr = nil
|
|
|
|
|
|
|
|
// Remember we have an iterator.
|
|
|
|
// Can run concurrently with another hash_iter_init().
|
2015-01-26 11:04:41 -07:00
|
|
|
if old := h.flags; old&(iterator|oldIterator) != iterator|oldIterator {
|
|
|
|
atomicor8(&h.flags, iterator|oldIterator)
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mapiternext(it)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapiternext(it *hiter) {
|
|
|
|
h := it.h
|
|
|
|
if raceenabled {
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
callerpc := getcallerpc(unsafe.Pointer(&it))
|
2014-09-03 09:10:38 -06:00
|
|
|
racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapiternext))
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
t := it.t
|
|
|
|
bucket := it.bucket
|
|
|
|
b := it.bptr
|
|
|
|
i := it.i
|
|
|
|
checkBucket := it.checkBucket
|
2014-12-27 21:32:11 -07:00
|
|
|
alg := t.key.alg
|
2014-07-16 15:16:19 -06:00
|
|
|
|
|
|
|
next:
|
|
|
|
if b == nil {
|
2014-09-08 18:42:21 -06:00
|
|
|
if bucket == it.startBucket && it.wrapped {
|
2014-07-16 15:16:19 -06:00
|
|
|
// end of iteration
|
|
|
|
it.key = nil
|
|
|
|
it.value = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if h.oldbuckets != nil && it.B == h.B {
|
|
|
|
// Iterator was started in the middle of a grow, and the grow isn't done yet.
|
|
|
|
// If the bucket we're looking at hasn't been filled in yet (i.e. the old
|
|
|
|
// bucket hasn't been evacuated) then we need to iterate through the old
|
|
|
|
// bucket and only return the ones that will be migrated to this bucket.
|
|
|
|
oldbucket := bucket & (uintptr(1)<<(it.B-1) - 1)
|
2014-08-01 15:38:56 -06:00
|
|
|
b = (*bmap)(add(h.oldbuckets, oldbucket*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
if !evacuated(b) {
|
|
|
|
checkBucket = bucket
|
|
|
|
} else {
|
2014-08-01 15:38:56 -06:00
|
|
|
b = (*bmap)(add(it.buckets, bucket*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
checkBucket = noCheck
|
|
|
|
}
|
|
|
|
} else {
|
2014-08-01 15:38:56 -06:00
|
|
|
b = (*bmap)(add(it.buckets, bucket*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
checkBucket = noCheck
|
|
|
|
}
|
|
|
|
bucket++
|
|
|
|
if bucket == uintptr(1)<<it.B {
|
|
|
|
bucket = 0
|
2014-09-08 18:42:21 -06:00
|
|
|
it.wrapped = true
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
i = 0
|
|
|
|
}
|
|
|
|
for ; i < bucketCnt; i++ {
|
2014-09-08 18:42:21 -06:00
|
|
|
offi := (i + it.offset) & (bucketCnt - 1)
|
|
|
|
k := add(unsafe.Pointer(b), dataOffset+uintptr(offi)*uintptr(t.keysize))
|
|
|
|
v := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+uintptr(offi)*uintptr(t.valuesize))
|
2014-07-16 15:16:19 -06:00
|
|
|
if b.tophash[offi] != empty && b.tophash[offi] != evacuatedEmpty {
|
|
|
|
if checkBucket != noCheck {
|
|
|
|
// Special case: iterator was started during a grow and the
|
|
|
|
// grow is not done yet. We're working on a bucket whose
|
|
|
|
// oldbucket has not been evacuated yet. Or at least, it wasn't
|
|
|
|
// evacuated when we started the bucket. So we're iterating
|
|
|
|
// through the oldbucket, skipping any keys that will go
|
|
|
|
// to the other new bucket (each oldbucket expands to two
|
|
|
|
// buckets during a grow).
|
|
|
|
k2 := k
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
k2 = *((*unsafe.Pointer)(k2))
|
|
|
|
}
|
2015-01-06 17:42:48 -07:00
|
|
|
if t.reflexivekey || alg.equal(k2, k2) {
|
2014-07-16 15:16:19 -06:00
|
|
|
// If the item in the oldbucket is not destined for
|
|
|
|
// the current new bucket in the iteration, skip it.
|
2015-01-06 17:42:48 -07:00
|
|
|
hash := alg.hash(k2, uintptr(h.hash0))
|
2014-07-16 15:16:19 -06:00
|
|
|
if hash&(uintptr(1)<<it.B-1) != checkBucket {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Hash isn't repeatable if k != k (NaNs). We need a
|
|
|
|
// repeatable and randomish choice of which direction
|
|
|
|
// to send NaNs during evacuation. We'll use the low
|
|
|
|
// bit of tophash to decide which way NaNs go.
|
|
|
|
// NOTE: this case is why we need two evacuate tophash
|
|
|
|
// values, evacuatedX and evacuatedY, that differ in
|
|
|
|
// their low bit.
|
|
|
|
if checkBucket>>(it.B-1) != uintptr(b.tophash[offi]&1) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if b.tophash[offi] != evacuatedX && b.tophash[offi] != evacuatedY {
|
|
|
|
// this is the golden data, we can return it.
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
k = *((*unsafe.Pointer)(k))
|
|
|
|
}
|
|
|
|
it.key = k
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectvalue {
|
2014-07-16 15:16:19 -06:00
|
|
|
v = *((*unsafe.Pointer)(v))
|
|
|
|
}
|
|
|
|
it.value = v
|
|
|
|
} else {
|
|
|
|
// The hash table has grown since the iterator was started.
|
|
|
|
// The golden data for this key is now somewhere else.
|
|
|
|
k2 := k
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
k2 = *((*unsafe.Pointer)(k2))
|
|
|
|
}
|
2015-01-06 17:42:48 -07:00
|
|
|
if t.reflexivekey || alg.equal(k2, k2) {
|
2014-07-16 15:16:19 -06:00
|
|
|
// Check the current hash table for the data.
|
|
|
|
// This code handles the case where the key
|
|
|
|
// has been deleted, updated, or deleted and reinserted.
|
|
|
|
// NOTE: we need to regrab the key as it has potentially been
|
|
|
|
// updated to an equal() but not identical key (e.g. +0.0 vs -0.0).
|
|
|
|
rk, rv := mapaccessK(t, h, k2)
|
|
|
|
if rk == nil {
|
|
|
|
continue // key has been deleted
|
|
|
|
}
|
|
|
|
it.key = rk
|
|
|
|
it.value = rv
|
|
|
|
} else {
|
|
|
|
// if key!=key then the entry can't be deleted or
|
|
|
|
// updated, so we can just return it. That's lucky for
|
|
|
|
// us because when key!=key we can't look it up
|
|
|
|
// successfully in the current table.
|
|
|
|
it.key = k2
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectvalue {
|
2014-07-16 15:16:19 -06:00
|
|
|
v = *((*unsafe.Pointer)(v))
|
|
|
|
}
|
|
|
|
it.value = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
it.bucket = bucket
|
|
|
|
it.bptr = b
|
|
|
|
it.i = i + 1
|
|
|
|
it.checkBucket = checkBucket
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 21:44:18 -07:00
|
|
|
b = b.overflow(t)
|
2014-07-16 15:16:19 -06:00
|
|
|
i = 0
|
|
|
|
goto next
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashGrow(t *maptype, h *hmap) {
|
|
|
|
if h.oldbuckets != nil {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("evacuation not done in time")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
oldbuckets := h.buckets
|
2014-07-30 10:01:52 -06:00
|
|
|
newbuckets := newarray(t.bucket, uintptr(1)<<(h.B+1))
|
2014-07-16 15:16:19 -06:00
|
|
|
flags := h.flags &^ (iterator | oldIterator)
|
|
|
|
if h.flags&iterator != 0 {
|
|
|
|
flags |= oldIterator
|
|
|
|
}
|
|
|
|
// commit the grow (atomic wrt gc)
|
|
|
|
h.B++
|
|
|
|
h.flags = flags
|
|
|
|
h.oldbuckets = oldbuckets
|
|
|
|
h.buckets = newbuckets
|
|
|
|
h.nevacuate = 0
|
|
|
|
|
2015-01-26 11:04:41 -07:00
|
|
|
if h.overflow != nil {
|
|
|
|
// Promote current overflow buckets to the old generation.
|
|
|
|
if h.overflow[1] != nil {
|
|
|
|
throw("overflow is not nil")
|
|
|
|
}
|
|
|
|
h.overflow[1] = h.overflow[0]
|
|
|
|
h.overflow[0] = nil
|
|
|
|
}
|
|
|
|
|
2014-07-16 15:16:19 -06:00
|
|
|
// the actual copying of the hash table data is done incrementally
|
|
|
|
// by growWork() and evacuate().
|
|
|
|
}
|
|
|
|
|
|
|
|
func growWork(t *maptype, h *hmap, bucket uintptr) {
|
|
|
|
noldbuckets := uintptr(1) << (h.B - 1)
|
|
|
|
|
|
|
|
// make sure we evacuate the oldbucket corresponding
|
|
|
|
// to the bucket we're about to use
|
|
|
|
evacuate(t, h, bucket&(noldbuckets-1))
|
|
|
|
|
|
|
|
// evacuate one more oldbucket to make progress on growing
|
|
|
|
if h.oldbuckets != nil {
|
|
|
|
evacuate(t, h, h.nevacuate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func evacuate(t *maptype, h *hmap, oldbucket uintptr) {
|
2014-08-01 15:38:56 -06:00
|
|
|
b := (*bmap)(add(h.oldbuckets, oldbucket*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
newbit := uintptr(1) << (h.B - 1)
|
2014-12-27 21:32:11 -07:00
|
|
|
alg := t.key.alg
|
2014-07-16 15:16:19 -06:00
|
|
|
if !evacuated(b) {
|
|
|
|
// TODO: reuse overflow buckets instead of using new ones, if there
|
|
|
|
// is no iterator using the old buckets. (If !oldIterator.)
|
|
|
|
|
2014-08-01 15:38:56 -06:00
|
|
|
x := (*bmap)(add(h.buckets, oldbucket*uintptr(t.bucketsize)))
|
|
|
|
y := (*bmap)(add(h.buckets, (oldbucket+newbit)*uintptr(t.bucketsize)))
|
2014-07-16 15:16:19 -06:00
|
|
|
xi := 0
|
|
|
|
yi := 0
|
|
|
|
xk := add(unsafe.Pointer(x), dataOffset)
|
|
|
|
yk := add(unsafe.Pointer(y), dataOffset)
|
2014-08-01 15:38:56 -06:00
|
|
|
xv := add(xk, bucketCnt*uintptr(t.keysize))
|
|
|
|
yv := add(yk, bucketCnt*uintptr(t.keysize))
|
2014-12-19 21:44:18 -07:00
|
|
|
for ; b != nil; b = b.overflow(t) {
|
2014-07-16 15:16:19 -06:00
|
|
|
k := add(unsafe.Pointer(b), dataOffset)
|
2014-08-01 15:38:56 -06:00
|
|
|
v := add(k, bucketCnt*uintptr(t.keysize))
|
|
|
|
for i := 0; i < bucketCnt; i, k, v = i+1, add(k, uintptr(t.keysize)), add(v, uintptr(t.valuesize)) {
|
2014-07-16 15:16:19 -06:00
|
|
|
top := b.tophash[i]
|
|
|
|
if top == empty {
|
|
|
|
b.tophash[i] = evacuatedEmpty
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if top < minTopHash {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("bad map state")
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
k2 := k
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
k2 = *((*unsafe.Pointer)(k2))
|
|
|
|
}
|
|
|
|
// Compute hash to make our evacuation decision (whether we need
|
|
|
|
// to send this key/value to bucket x or bucket y).
|
2015-01-06 17:42:48 -07:00
|
|
|
hash := alg.hash(k2, uintptr(h.hash0))
|
2014-07-16 15:16:19 -06:00
|
|
|
if h.flags&iterator != 0 {
|
2015-01-06 17:42:48 -07:00
|
|
|
if !t.reflexivekey && !alg.equal(k2, k2) {
|
2014-07-16 15:16:19 -06:00
|
|
|
// If key != key (NaNs), then the hash could be (and probably
|
|
|
|
// will be) entirely different from the old hash. Moreover,
|
|
|
|
// it isn't reproducible. Reproducibility is required in the
|
|
|
|
// presence of iterators, as our evacuation decision must
|
|
|
|
// match whatever decision the iterator made.
|
|
|
|
// Fortunately, we have the freedom to send these keys either
|
|
|
|
// way. Also, tophash is meaningless for these kinds of keys.
|
|
|
|
// We let the low bit of tophash drive the evacuation decision.
|
|
|
|
// We recompute a new random tophash for the next level so
|
|
|
|
// these keys will get evenly distributed across all buckets
|
|
|
|
// after multiple grows.
|
|
|
|
if (top & 1) != 0 {
|
|
|
|
hash |= newbit
|
|
|
|
} else {
|
|
|
|
hash &^= newbit
|
|
|
|
}
|
|
|
|
top = uint8(hash >> (ptrSize*8 - 8))
|
|
|
|
if top < minTopHash {
|
|
|
|
top += minTopHash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hash & newbit) == 0 {
|
|
|
|
b.tophash[i] = evacuatedX
|
|
|
|
if xi == bucketCnt {
|
2014-07-30 10:01:52 -06:00
|
|
|
newx := (*bmap)(newobject(t.bucket))
|
2015-01-26 11:04:41 -07:00
|
|
|
h.setoverflow(t, x, newx)
|
2014-07-16 15:16:19 -06:00
|
|
|
x = newx
|
|
|
|
xi = 0
|
|
|
|
xk = add(unsafe.Pointer(x), dataOffset)
|
2014-08-01 15:38:56 -06:00
|
|
|
xv = add(xk, bucketCnt*uintptr(t.keysize))
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
x.tophash[xi] = top
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
*(*unsafe.Pointer)(xk) = k2 // copy pointer
|
|
|
|
} else {
|
2014-12-29 08:07:47 -07:00
|
|
|
typedmemmove(t.key, xk, k) // copy value
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectvalue {
|
2014-07-16 15:16:19 -06:00
|
|
|
*(*unsafe.Pointer)(xv) = *(*unsafe.Pointer)(v)
|
|
|
|
} else {
|
2014-12-29 08:07:47 -07:00
|
|
|
typedmemmove(t.elem, xv, v)
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
xi++
|
2014-08-01 15:38:56 -06:00
|
|
|
xk = add(xk, uintptr(t.keysize))
|
|
|
|
xv = add(xv, uintptr(t.valuesize))
|
2014-07-16 15:16:19 -06:00
|
|
|
} else {
|
|
|
|
b.tophash[i] = evacuatedY
|
|
|
|
if yi == bucketCnt {
|
2014-07-30 10:01:52 -06:00
|
|
|
newy := (*bmap)(newobject(t.bucket))
|
2015-01-26 11:04:41 -07:00
|
|
|
h.setoverflow(t, y, newy)
|
2014-07-16 15:16:19 -06:00
|
|
|
y = newy
|
|
|
|
yi = 0
|
|
|
|
yk = add(unsafe.Pointer(y), dataOffset)
|
2014-08-01 15:38:56 -06:00
|
|
|
yv = add(yk, bucketCnt*uintptr(t.keysize))
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
y.tophash[yi] = top
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectkey {
|
2014-07-16 15:16:19 -06:00
|
|
|
*(*unsafe.Pointer)(yk) = k2
|
|
|
|
} else {
|
2014-12-29 08:07:47 -07:00
|
|
|
typedmemmove(t.key, yk, k)
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if t.indirectvalue {
|
2014-07-16 15:16:19 -06:00
|
|
|
*(*unsafe.Pointer)(yv) = *(*unsafe.Pointer)(v)
|
|
|
|
} else {
|
2014-12-29 08:07:47 -07:00
|
|
|
typedmemmove(t.elem, yv, v)
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
yi++
|
2014-08-01 15:38:56 -06:00
|
|
|
yk = add(yk, uintptr(t.keysize))
|
|
|
|
yv = add(yv, uintptr(t.valuesize))
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Unlink the overflow buckets & clear key/value to help GC.
|
|
|
|
if h.flags&oldIterator == 0 {
|
2014-08-01 15:38:56 -06:00
|
|
|
b = (*bmap)(add(h.oldbuckets, oldbucket*uintptr(t.bucketsize)))
|
|
|
|
memclr(add(unsafe.Pointer(b), dataOffset), uintptr(t.bucketsize)-dataOffset)
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance evacuation mark
|
|
|
|
if oldbucket == h.nevacuate {
|
|
|
|
h.nevacuate = oldbucket + 1
|
|
|
|
if oldbucket+1 == newbit { // newbit == # of oldbuckets
|
|
|
|
// Growing is all done. Free old main bucket array.
|
|
|
|
h.oldbuckets = nil
|
2015-01-26 11:04:41 -07:00
|
|
|
// Can discard old overflow buckets as well.
|
|
|
|
// If they are still referenced by an iterator,
|
|
|
|
// then the iterator holds a pointers to the slice.
|
|
|
|
if h.overflow != nil {
|
|
|
|
h.overflow[1] = nil
|
|
|
|
}
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ismapkey(t *_type) bool {
|
2014-12-27 21:32:11 -07:00
|
|
|
return t.alg.hash != nil
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reflect stubs. Called from ../reflect/asm_*.s
|
|
|
|
|
2014-12-22 11:27:53 -07:00
|
|
|
//go:linkname reflect_makemap reflect.makemap
|
2014-07-16 15:16:19 -06:00
|
|
|
func reflect_makemap(t *maptype) *hmap {
|
2015-01-29 09:40:02 -07:00
|
|
|
return makemap(t, 0, nil, nil)
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
|
2014-12-22 11:27:53 -07:00
|
|
|
//go:linkname reflect_mapaccess reflect.mapaccess
|
2014-07-16 15:16:19 -06:00
|
|
|
func reflect_mapaccess(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
|
|
|
|
val, ok := mapaccess2(t, h, key)
|
|
|
|
if !ok {
|
|
|
|
// reflect wants nil for a missing element
|
|
|
|
val = nil
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2014-12-22 11:27:53 -07:00
|
|
|
//go:linkname reflect_mapassign reflect.mapassign
|
2014-07-16 15:16:19 -06:00
|
|
|
func reflect_mapassign(t *maptype, h *hmap, key unsafe.Pointer, val unsafe.Pointer) {
|
|
|
|
mapassign1(t, h, key, val)
|
|
|
|
}
|
|
|
|
|
2014-12-22 11:27:53 -07:00
|
|
|
//go:linkname reflect_mapdelete reflect.mapdelete
|
2014-07-16 15:16:19 -06:00
|
|
|
func reflect_mapdelete(t *maptype, h *hmap, key unsafe.Pointer) {
|
|
|
|
mapdelete(t, h, key)
|
|
|
|
}
|
|
|
|
|
2014-12-22 11:27:53 -07:00
|
|
|
//go:linkname reflect_mapiterinit reflect.mapiterinit
|
2014-07-16 15:16:19 -06:00
|
|
|
func reflect_mapiterinit(t *maptype, h *hmap) *hiter {
|
|
|
|
it := new(hiter)
|
|
|
|
mapiterinit(t, h, it)
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
|
2014-12-22 11:27:53 -07:00
|
|
|
//go:linkname reflect_mapiternext reflect.mapiternext
|
2014-07-16 15:16:19 -06:00
|
|
|
func reflect_mapiternext(it *hiter) {
|
|
|
|
mapiternext(it)
|
|
|
|
}
|
|
|
|
|
2014-12-22 11:27:53 -07:00
|
|
|
//go:linkname reflect_mapiterkey reflect.mapiterkey
|
2014-07-16 15:16:19 -06:00
|
|
|
func reflect_mapiterkey(it *hiter) unsafe.Pointer {
|
|
|
|
return it.key
|
|
|
|
}
|
|
|
|
|
2014-12-22 11:27:53 -07:00
|
|
|
//go:linkname reflect_maplen reflect.maplen
|
2014-07-16 15:16:19 -06:00
|
|
|
func reflect_maplen(h *hmap) int {
|
|
|
|
if h == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if raceenabled {
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
callerpc := getcallerpc(unsafe.Pointer(&h))
|
2014-09-03 09:10:38 -06:00
|
|
|
racereadpc(unsafe.Pointer(h), callerpc, funcPC(reflect_maplen))
|
2014-07-16 15:16:19 -06:00
|
|
|
}
|
|
|
|
return h.count
|
|
|
|
}
|
|
|
|
|
2014-12-22 11:27:53 -07:00
|
|
|
//go:linkname reflect_ismapkey reflect.ismapkey
|
2014-07-16 15:16:19 -06:00
|
|
|
func reflect_ismapkey(t *_type) bool {
|
|
|
|
return ismapkey(t)
|
|
|
|
}
|
2015-05-14 15:27:04 -06:00
|
|
|
|
2015-08-20 20:54:55 -06:00
|
|
|
var zerolock mutex
|
2015-05-14 15:27:04 -06:00
|
|
|
|
2015-08-20 20:54:55 -06:00
|
|
|
const initialZeroSize = 1024
|
2015-05-14 15:27:04 -06:00
|
|
|
|
2015-08-20 20:54:55 -06:00
|
|
|
var zeroinitial [initialZeroSize]byte
|
2015-05-15 14:05:52 -06:00
|
|
|
|
2015-08-20 20:54:55 -06:00
|
|
|
// All accesses to zeroptr and zerosize must be atomic so that they
|
|
|
|
// can be accessed without locks in the common case.
|
|
|
|
var zeroptr unsafe.Pointer = unsafe.Pointer(&zeroinitial)
|
|
|
|
var zerosize uintptr = initialZeroSize
|
2015-05-14 15:27:04 -06:00
|
|
|
|
2015-08-20 20:54:55 -06:00
|
|
|
// mapzero ensures that zeroptr points to a buffer large enough to
|
|
|
|
// serve as the zero value for t.
|
|
|
|
func mapzero(t *_type) {
|
|
|
|
// Is the type small enough for existing buffer?
|
|
|
|
cursize := uintptr(atomicloadp(unsafe.Pointer(&zerosize)))
|
|
|
|
if t.size <= cursize {
|
2015-05-14 15:27:04 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-20 20:54:55 -06:00
|
|
|
// Allocate a new buffer.
|
|
|
|
lock(&zerolock)
|
|
|
|
cursize = uintptr(atomicloadp(unsafe.Pointer(&zerosize)))
|
|
|
|
if cursize < t.size {
|
|
|
|
for cursize < t.size {
|
|
|
|
cursize *= 2
|
|
|
|
if cursize == 0 {
|
2015-05-14 15:27:04 -06:00
|
|
|
// need >2GB zero on 32-bit machine
|
|
|
|
throw("map element too large")
|
|
|
|
}
|
|
|
|
}
|
2015-08-20 20:54:55 -06:00
|
|
|
atomicstorep1(unsafe.Pointer(&zeroptr), persistentalloc(cursize, 64, &memstats.other_sys))
|
|
|
|
atomicstorep1(unsafe.Pointer(&zerosize), unsafe.Pointer(zerosize))
|
2015-05-14 15:27:04 -06:00
|
|
|
}
|
2015-08-20 20:54:55 -06:00
|
|
|
unlock(&zerolock)
|
2015-05-14 15:27:04 -06:00
|
|
|
}
|