Skip to content

Commit

Permalink
runtime,reflect: move zeroVal to internal/abi
Browse files Browse the repository at this point in the history
Change-Id: I0e19e4aa2ea47a714e27b8d66c23c449e27861f2
GitHub-Last-Rev: 2d59b95
GitHub-Pull-Request: #67014
Reviewed-on: https://go-review.googlesource.com/c/go/+/581395
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Joedian Reid <joedian@google.com>
  • Loading branch information
qiulaidongfeng authored and randall77 committed May 3, 2024
1 parent a3eb55c commit 2f5b420
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 35 deletions.
5 changes: 4 additions & 1 deletion src/internal/abi/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

package abi

// ZeroValSize is the size in bytes of runtime.zeroVal.
// ZeroValSize is the size in bytes of [ZeroVal].
const ZeroValSize = 1024

// ZeroVal is a region containing all zero bytes.
var ZeroVal [ZeroValSize]byte
11 changes: 4 additions & 7 deletions src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ func (v Value) IsZero() bool {
// v.ptr doesn't escape, as Equal functions are compiler generated
// and never escape. The escape analysis doesn't know, as it is a
// function pointer call.
return typ.Equal(abi.NoEscape(v.ptr), unsafe.Pointer(&zeroVal[0]))
return typ.Equal(abi.NoEscape(v.ptr), unsafe.Pointer(&abi.ZeroVal[0]))
}
if typ.TFlag&abi.TFlagRegularMemory != 0 {
// For some types where the zero value is a value where all bits of this type are 0
Expand All @@ -1617,7 +1617,7 @@ func (v Value) IsZero() bool {
// If the type is comparable, then compare directly with zero.
if typ.Equal != nil && typ.Size() <= abi.ZeroValSize {
// See noescape justification above.
return typ.Equal(abi.NoEscape(v.ptr), unsafe.Pointer(&zeroVal[0]))
return typ.Equal(abi.NoEscape(v.ptr), unsafe.Pointer(&abi.ZeroVal[0]))
}
if typ.TFlag&abi.TFlagRegularMemory != 0 {
// For some types where the zero value is a value where all bits of this type are 0
Expand Down Expand Up @@ -2312,7 +2312,7 @@ func (v Value) Set(x Value) {
}
x = x.assignTo("reflect.Set", v.typ(), target)
if x.flag&flagIndir != 0 {
if x.ptr == unsafe.Pointer(&zeroVal[0]) {
if x.ptr == unsafe.Pointer(&abi.ZeroVal[0]) {
typedmemclr(v.typ(), v.ptr)
} else {
typedmemmove(v.typ(), v.ptr, x.ptr)
Expand Down Expand Up @@ -3280,7 +3280,7 @@ func Zero(typ Type) Value {
if t.IfaceIndir() {
var p unsafe.Pointer
if t.Size() <= abi.ZeroValSize {
p = unsafe.Pointer(&zeroVal[0])
p = unsafe.Pointer(&abi.ZeroVal[0])
} else {
p = unsafe_New(t)
}
Expand All @@ -3289,9 +3289,6 @@ func Zero(typ Type) Value {
return Value{t, nil, fl}
}

//go:linkname zeroVal runtime.zeroVal
var zeroVal [abi.ZeroValSize]byte

// New returns a Value representing a pointer to a new zero value
// for the specified type. That is, the returned Value's Type is [PointerTo](typ).
func New(typ Type) Value {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func convT64(val uint64) (x unsafe.Pointer) {

func convTstring(val string) (x unsafe.Pointer) {
if val == "" {
x = unsafe.Pointer(&zeroVal[0])
x = unsafe.Pointer(&abi.ZeroVal[0])
} else {
x = mallocgc(unsafe.Sizeof(val), stringType, true)
*(*string)(x) = val
Expand All @@ -402,7 +402,7 @@ func convTstring(val string) (x unsafe.Pointer) {
func convTslice(val []byte) (x unsafe.Pointer) {
// Note: this must work for any element type, not just byte.
if (*slice)(unsafe.Pointer(&val)).array == nil {
x = unsafe.Pointer(&zeroVal[0])
x = unsafe.Pointer(&abi.ZeroVal[0])
} else {
x = mallocgc(unsafe.Sizeof(val), sliceType, true)
*(*[]byte)(x) = val
Expand Down
12 changes: 6 additions & 6 deletions src/runtime/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func mapaccess1(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
if err := mapKeyError(t, key); err != nil {
panic(err) // see issue 23734
}
return unsafe.Pointer(&zeroVal[0])
return unsafe.Pointer(&abi.ZeroVal[0])
}
if h.flags&hashWriting != 0 {
fatal("concurrent map read and map write")
Expand Down Expand Up @@ -443,7 +443,7 @@ bucketloop:
}
}
}
return unsafe.Pointer(&zeroVal[0])
return unsafe.Pointer(&abi.ZeroVal[0])
}

func mapaccess2(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, bool) {
Expand All @@ -463,7 +463,7 @@ func mapaccess2(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, bool)
if err := mapKeyError(t, key); err != nil {
panic(err) // see issue 23734
}
return unsafe.Pointer(&zeroVal[0]), false
return unsafe.Pointer(&abi.ZeroVal[0]), false
}
if h.flags&hashWriting != 0 {
fatal("concurrent map read and map write")
Expand Down Expand Up @@ -504,7 +504,7 @@ bucketloop:
}
}
}
return unsafe.Pointer(&zeroVal[0]), false
return unsafe.Pointer(&abi.ZeroVal[0]), false
}

// returns both key and elem. Used by map iterator.
Expand Down Expand Up @@ -553,15 +553,15 @@ bucketloop:

func mapaccess1_fat(t *maptype, h *hmap, key, zero unsafe.Pointer) unsafe.Pointer {
e := mapaccess1(t, h, key)
if e == unsafe.Pointer(&zeroVal[0]) {
if e == unsafe.Pointer(&abi.ZeroVal[0]) {
return zero
}
return e
}

func mapaccess2_fat(t *maptype, h *hmap, key, zero unsafe.Pointer) (unsafe.Pointer, bool) {
e := mapaccess1(t, h, key)
if e == unsafe.Pointer(&zeroVal[0]) {
if e == unsafe.Pointer(&abi.ZeroVal[0]) {
return zero, false
}
return e, true
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/map_fast32.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func mapaccess1_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer {
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess1_fast32))
}
if h == nil || h.count == 0 {
return unsafe.Pointer(&zeroVal[0])
return unsafe.Pointer(&abi.ZeroVal[0])
}
if h.flags&hashWriting != 0 {
fatal("concurrent map read and map write")
Expand Down Expand Up @@ -47,7 +47,7 @@ func mapaccess1_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer {
}
}
}
return unsafe.Pointer(&zeroVal[0])
return unsafe.Pointer(&abi.ZeroVal[0])
}

func mapaccess2_fast32(t *maptype, h *hmap, key uint32) (unsafe.Pointer, bool) {
Expand All @@ -56,7 +56,7 @@ func mapaccess2_fast32(t *maptype, h *hmap, key uint32) (unsafe.Pointer, bool) {
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess2_fast32))
}
if h == nil || h.count == 0 {
return unsafe.Pointer(&zeroVal[0]), false
return unsafe.Pointer(&abi.ZeroVal[0]), false
}
if h.flags&hashWriting != 0 {
fatal("concurrent map read and map write")
Expand Down Expand Up @@ -87,7 +87,7 @@ func mapaccess2_fast32(t *maptype, h *hmap, key uint32) (unsafe.Pointer, bool) {
}
}
}
return unsafe.Pointer(&zeroVal[0]), false
return unsafe.Pointer(&abi.ZeroVal[0]), false
}

func mapassign_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer {
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/map_fast64.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func mapaccess1_fast64(t *maptype, h *hmap, key uint64) unsafe.Pointer {
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess1_fast64))
}
if h == nil || h.count == 0 {
return unsafe.Pointer(&zeroVal[0])
return unsafe.Pointer(&abi.ZeroVal[0])
}
if h.flags&hashWriting != 0 {
fatal("concurrent map read and map write")
Expand Down Expand Up @@ -47,7 +47,7 @@ func mapaccess1_fast64(t *maptype, h *hmap, key uint64) unsafe.Pointer {
}
}
}
return unsafe.Pointer(&zeroVal[0])
return unsafe.Pointer(&abi.ZeroVal[0])
}

func mapaccess2_fast64(t *maptype, h *hmap, key uint64) (unsafe.Pointer, bool) {
Expand All @@ -56,7 +56,7 @@ func mapaccess2_fast64(t *maptype, h *hmap, key uint64) (unsafe.Pointer, bool) {
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess2_fast64))
}
if h == nil || h.count == 0 {
return unsafe.Pointer(&zeroVal[0]), false
return unsafe.Pointer(&abi.ZeroVal[0]), false
}
if h.flags&hashWriting != 0 {
fatal("concurrent map read and map write")
Expand Down Expand Up @@ -87,7 +87,7 @@ func mapaccess2_fast64(t *maptype, h *hmap, key uint64) (unsafe.Pointer, bool) {
}
}
}
return unsafe.Pointer(&zeroVal[0]), false
return unsafe.Pointer(&abi.ZeroVal[0]), false
}

func mapassign_fast64(t *maptype, h *hmap, key uint64) unsafe.Pointer {
Expand Down
16 changes: 8 additions & 8 deletions src/runtime/map_faststr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer {
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess1_faststr))
}
if h == nil || h.count == 0 {
return unsafe.Pointer(&zeroVal[0])
return unsafe.Pointer(&abi.ZeroVal[0])
}
if h.flags&hashWriting != 0 {
fatal("concurrent map read and map write")
Expand All @@ -39,7 +39,7 @@ func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer {
return add(unsafe.Pointer(b), dataOffset+abi.MapBucketCount*2*goarch.PtrSize+i*uintptr(t.ValueSize))
}
}
return unsafe.Pointer(&zeroVal[0])
return unsafe.Pointer(&abi.ZeroVal[0])
}
// long key, try not to do more comparisons than necessary
keymaybe := uintptr(abi.MapBucketCount)
Expand Down Expand Up @@ -74,7 +74,7 @@ func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer {
return add(unsafe.Pointer(b), dataOffset+abi.MapBucketCount*2*goarch.PtrSize+keymaybe*uintptr(t.ValueSize))
}
}
return unsafe.Pointer(&zeroVal[0])
return unsafe.Pointer(&abi.ZeroVal[0])
}
dohash:
hash := t.Hasher(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0))
Expand Down Expand Up @@ -102,7 +102,7 @@ dohash:
}
}
}
return unsafe.Pointer(&zeroVal[0])
return unsafe.Pointer(&abi.ZeroVal[0])
}

func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) {
Expand All @@ -111,7 +111,7 @@ func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) {
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess2_faststr))
}
if h == nil || h.count == 0 {
return unsafe.Pointer(&zeroVal[0]), false
return unsafe.Pointer(&abi.ZeroVal[0]), false
}
if h.flags&hashWriting != 0 {
fatal("concurrent map read and map write")
Expand All @@ -134,7 +134,7 @@ func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) {
return add(unsafe.Pointer(b), dataOffset+abi.MapBucketCount*2*goarch.PtrSize+i*uintptr(t.ValueSize)), true
}
}
return unsafe.Pointer(&zeroVal[0]), false
return unsafe.Pointer(&abi.ZeroVal[0]), false
}
// long key, try not to do more comparisons than necessary
keymaybe := uintptr(abi.MapBucketCount)
Expand Down Expand Up @@ -169,7 +169,7 @@ func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) {
return add(unsafe.Pointer(b), dataOffset+abi.MapBucketCount*2*goarch.PtrSize+keymaybe*uintptr(t.ValueSize)), true
}
}
return unsafe.Pointer(&zeroVal[0]), false
return unsafe.Pointer(&abi.ZeroVal[0]), false
}
dohash:
hash := t.Hasher(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0))
Expand Down Expand Up @@ -197,7 +197,7 @@ dohash:
}
}
}
return unsafe.Pointer(&zeroVal[0]), false
return unsafe.Pointer(&abi.ZeroVal[0]), false
}

func mapassign_faststr(t *maptype, h *hmap, s string) unsafe.Pointer {
Expand Down
3 changes: 0 additions & 3 deletions src/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package runtime

import (
"internal/abi"
"internal/runtime/atomic"
"unsafe"
)
Expand Down Expand Up @@ -297,5 +296,3 @@ func setCrashFD(fd uintptr) uintptr {
var auxv []uintptr

func getAuxv() []uintptr { return auxv } // accessed from x/sys/cpu; see issue 57336

var zeroVal [abi.ZeroValSize]byte

0 comments on commit 2f5b420

Please sign in to comment.