]> Cypherpunks repositories - gostls13.git/commitdiff
runtime,reflect: move zeroVal to internal/abi
authorqiulaidongfeng <2645477756@qq.com>
Wed, 24 Apr 2024 22:49:31 +0000 (22:49 +0000)
committerKeith Randall <khr@golang.org>
Fri, 3 May 2024 12:00:41 +0000 (12:00 +0000)
Change-Id: I0e19e4aa2ea47a714e27b8d66c23c449e27861f2
GitHub-Last-Rev: 2d59b9589efcf4ade6cfd7c8feffc46bf9ba912c
GitHub-Pull-Request: golang/go#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>
src/internal/abi/runtime.go
src/reflect/value.go
src/runtime/iface.go
src/runtime/map.go
src/runtime/map_fast32.go
src/runtime/map_fast64.go
src/runtime/map_faststr.go
src/runtime/runtime.go

index 9b91cdf5ef60c0136e068a1d3c3f81c271fd6e7c..2a3181a48d343c0672be987e2c1c64f8c58b3bc9 100644 (file)
@@ -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
index 4b936bf5bbf54be3383a5c96c346aa7088b248e1..06f2c2b7dad8754ab1d7825d47b92a566e71dacd 100644 (file)
@@ -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
@@ -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
@@ -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)
@@ -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)
                }
@@ -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 {
index e28018066591c3b44498bb90d512ad7819b245e7..28eb8fb5ec1722f12abcbab63b5075c765e28d1e 100644 (file)
@@ -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
@@ -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
index d97e209deb90008aca5e38a558e186bb63c1c099..9e8ae67a357550ba289aefea66f5b3ca4ba90fc9 100644 (file)
@@ -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")
@@ -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) {
@@ -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")
@@ -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.
@@ -553,7 +553,7 @@ 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
@@ -561,7 +561,7 @@ func mapaccess1_fat(t *maptype, h *hmap, key, zero unsafe.Pointer) unsafe.Pointe
 
 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
index 7e52240e77988acb26b17ad6e028d58b2aa862c2..06dcbcabc461d645f52e1eac78a117f9b612e3dd 100644 (file)
@@ -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")
@@ -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) {
@@ -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")
@@ -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 {
index 2c365183cb74523cf881c96c90722495ff29acf5..c8b34dd41bc219de6e2f9445a90eae4ce46fbc19 100644 (file)
@@ -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")
@@ -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) {
@@ -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")
@@ -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 {
index d989190f710b5f2c3d1d5364dd8da819c342f2e1..38841aee4b5af9f01569cf95da2021fc365c6f8a 100644 (file)
@@ -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")
@@ -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)
@@ -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))
@@ -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) {
@@ -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")
@@ -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)
@@ -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))
@@ -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 {
index 6ec0369a7e59d89d7d002afa6c6c8701f37e83d9..cc6f03d2a086dfd76b80563a0f8c3646247cd55a 100644 (file)
@@ -5,7 +5,6 @@
 package runtime
 
 import (
-       "internal/abi"
        "internal/runtime/atomic"
        "unsafe"
 )
@@ -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