Skip to content

Commit 2f5b420

Browse files
qiulaidongfengrandall77
authored andcommitted
runtime,reflect: move zeroVal to internal/abi
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Joedian Reid <[email protected]>
1 parent a3eb55c commit 2f5b420

File tree

8 files changed

+32
-35
lines changed

8 files changed

+32
-35
lines changed

src/internal/abi/runtime.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44

55
package abi
66

7-
// ZeroValSize is the size in bytes of runtime.zeroVal.
7+
// ZeroValSize is the size in bytes of [ZeroVal].
88
const ZeroValSize = 1024
9+
10+
// ZeroVal is a region containing all zero bytes.
11+
var ZeroVal [ZeroValSize]byte

src/reflect/value.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ func (v Value) IsZero() bool {
15911591
// v.ptr doesn't escape, as Equal functions are compiler generated
15921592
// and never escape. The escape analysis doesn't know, as it is a
15931593
// function pointer call.
1594-
return typ.Equal(abi.NoEscape(v.ptr), unsafe.Pointer(&zeroVal[0]))
1594+
return typ.Equal(abi.NoEscape(v.ptr), unsafe.Pointer(&abi.ZeroVal[0]))
15951595
}
15961596
if typ.TFlag&abi.TFlagRegularMemory != 0 {
15971597
// 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 {
16171617
// If the type is comparable, then compare directly with zero.
16181618
if typ.Equal != nil && typ.Size() <= abi.ZeroValSize {
16191619
// See noescape justification above.
1620-
return typ.Equal(abi.NoEscape(v.ptr), unsafe.Pointer(&zeroVal[0]))
1620+
return typ.Equal(abi.NoEscape(v.ptr), unsafe.Pointer(&abi.ZeroVal[0]))
16211621
}
16221622
if typ.TFlag&abi.TFlagRegularMemory != 0 {
16231623
// 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) {
23122312
}
23132313
x = x.assignTo("reflect.Set", v.typ(), target)
23142314
if x.flag&flagIndir != 0 {
2315-
if x.ptr == unsafe.Pointer(&zeroVal[0]) {
2315+
if x.ptr == unsafe.Pointer(&abi.ZeroVal[0]) {
23162316
typedmemclr(v.typ(), v.ptr)
23172317
} else {
23182318
typedmemmove(v.typ(), v.ptr, x.ptr)
@@ -3280,7 +3280,7 @@ func Zero(typ Type) Value {
32803280
if t.IfaceIndir() {
32813281
var p unsafe.Pointer
32823282
if t.Size() <= abi.ZeroValSize {
3283-
p = unsafe.Pointer(&zeroVal[0])
3283+
p = unsafe.Pointer(&abi.ZeroVal[0])
32843284
} else {
32853285
p = unsafe_New(t)
32863286
}
@@ -3289,9 +3289,6 @@ func Zero(typ Type) Value {
32893289
return Value{t, nil, fl}
32903290
}
32913291

3292-
//go:linkname zeroVal runtime.zeroVal
3293-
var zeroVal [abi.ZeroValSize]byte
3294-
32953292
// New returns a Value representing a pointer to a new zero value
32963293
// for the specified type. That is, the returned Value's Type is [PointerTo](typ).
32973294
func New(typ Type) Value {

src/runtime/iface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ func convT64(val uint64) (x unsafe.Pointer) {
391391

392392
func convTstring(val string) (x unsafe.Pointer) {
393393
if val == "" {
394-
x = unsafe.Pointer(&zeroVal[0])
394+
x = unsafe.Pointer(&abi.ZeroVal[0])
395395
} else {
396396
x = mallocgc(unsafe.Sizeof(val), stringType, true)
397397
*(*string)(x) = val
@@ -402,7 +402,7 @@ func convTstring(val string) (x unsafe.Pointer) {
402402
func convTslice(val []byte) (x unsafe.Pointer) {
403403
// Note: this must work for any element type, not just byte.
404404
if (*slice)(unsafe.Pointer(&val)).array == nil {
405-
x = unsafe.Pointer(&zeroVal[0])
405+
x = unsafe.Pointer(&abi.ZeroVal[0])
406406
} else {
407407
x = mallocgc(unsafe.Sizeof(val), sliceType, true)
408408
*(*[]byte)(x) = val

src/runtime/map.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func mapaccess1(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
402402
if err := mapKeyError(t, key); err != nil {
403403
panic(err) // see issue 23734
404404
}
405-
return unsafe.Pointer(&zeroVal[0])
405+
return unsafe.Pointer(&abi.ZeroVal[0])
406406
}
407407
if h.flags&hashWriting != 0 {
408408
fatal("concurrent map read and map write")
@@ -443,7 +443,7 @@ bucketloop:
443443
}
444444
}
445445
}
446-
return unsafe.Pointer(&zeroVal[0])
446+
return unsafe.Pointer(&abi.ZeroVal[0])
447447
}
448448

449449
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)
463463
if err := mapKeyError(t, key); err != nil {
464464
panic(err) // see issue 23734
465465
}
466-
return unsafe.Pointer(&zeroVal[0]), false
466+
return unsafe.Pointer(&abi.ZeroVal[0]), false
467467
}
468468
if h.flags&hashWriting != 0 {
469469
fatal("concurrent map read and map write")
@@ -504,7 +504,7 @@ bucketloop:
504504
}
505505
}
506506
}
507-
return unsafe.Pointer(&zeroVal[0]), false
507+
return unsafe.Pointer(&abi.ZeroVal[0]), false
508508
}
509509

510510
// returns both key and elem. Used by map iterator.
@@ -553,15 +553,15 @@ bucketloop:
553553

554554
func mapaccess1_fat(t *maptype, h *hmap, key, zero unsafe.Pointer) unsafe.Pointer {
555555
e := mapaccess1(t, h, key)
556-
if e == unsafe.Pointer(&zeroVal[0]) {
556+
if e == unsafe.Pointer(&abi.ZeroVal[0]) {
557557
return zero
558558
}
559559
return e
560560
}
561561

562562
func mapaccess2_fat(t *maptype, h *hmap, key, zero unsafe.Pointer) (unsafe.Pointer, bool) {
563563
e := mapaccess1(t, h, key)
564-
if e == unsafe.Pointer(&zeroVal[0]) {
564+
if e == unsafe.Pointer(&abi.ZeroVal[0]) {
565565
return zero, false
566566
}
567567
return e, true

src/runtime/map_fast32.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func mapaccess1_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer {
1616
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess1_fast32))
1717
}
1818
if h == nil || h.count == 0 {
19-
return unsafe.Pointer(&zeroVal[0])
19+
return unsafe.Pointer(&abi.ZeroVal[0])
2020
}
2121
if h.flags&hashWriting != 0 {
2222
fatal("concurrent map read and map write")
@@ -47,7 +47,7 @@ func mapaccess1_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer {
4747
}
4848
}
4949
}
50-
return unsafe.Pointer(&zeroVal[0])
50+
return unsafe.Pointer(&abi.ZeroVal[0])
5151
}
5252

5353
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) {
5656
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess2_fast32))
5757
}
5858
if h == nil || h.count == 0 {
59-
return unsafe.Pointer(&zeroVal[0]), false
59+
return unsafe.Pointer(&abi.ZeroVal[0]), false
6060
}
6161
if h.flags&hashWriting != 0 {
6262
fatal("concurrent map read and map write")
@@ -87,7 +87,7 @@ func mapaccess2_fast32(t *maptype, h *hmap, key uint32) (unsafe.Pointer, bool) {
8787
}
8888
}
8989
}
90-
return unsafe.Pointer(&zeroVal[0]), false
90+
return unsafe.Pointer(&abi.ZeroVal[0]), false
9191
}
9292

9393
func mapassign_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer {

src/runtime/map_fast64.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func mapaccess1_fast64(t *maptype, h *hmap, key uint64) unsafe.Pointer {
1616
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess1_fast64))
1717
}
1818
if h == nil || h.count == 0 {
19-
return unsafe.Pointer(&zeroVal[0])
19+
return unsafe.Pointer(&abi.ZeroVal[0])
2020
}
2121
if h.flags&hashWriting != 0 {
2222
fatal("concurrent map read and map write")
@@ -47,7 +47,7 @@ func mapaccess1_fast64(t *maptype, h *hmap, key uint64) unsafe.Pointer {
4747
}
4848
}
4949
}
50-
return unsafe.Pointer(&zeroVal[0])
50+
return unsafe.Pointer(&abi.ZeroVal[0])
5151
}
5252

5353
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) {
5656
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess2_fast64))
5757
}
5858
if h == nil || h.count == 0 {
59-
return unsafe.Pointer(&zeroVal[0]), false
59+
return unsafe.Pointer(&abi.ZeroVal[0]), false
6060
}
6161
if h.flags&hashWriting != 0 {
6262
fatal("concurrent map read and map write")
@@ -87,7 +87,7 @@ func mapaccess2_fast64(t *maptype, h *hmap, key uint64) (unsafe.Pointer, bool) {
8787
}
8888
}
8989
}
90-
return unsafe.Pointer(&zeroVal[0]), false
90+
return unsafe.Pointer(&abi.ZeroVal[0]), false
9191
}
9292

9393
func mapassign_fast64(t *maptype, h *hmap, key uint64) unsafe.Pointer {

src/runtime/map_faststr.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer {
1616
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess1_faststr))
1717
}
1818
if h == nil || h.count == 0 {
19-
return unsafe.Pointer(&zeroVal[0])
19+
return unsafe.Pointer(&abi.ZeroVal[0])
2020
}
2121
if h.flags&hashWriting != 0 {
2222
fatal("concurrent map read and map write")
@@ -39,7 +39,7 @@ func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer {
3939
return add(unsafe.Pointer(b), dataOffset+abi.MapBucketCount*2*goarch.PtrSize+i*uintptr(t.ValueSize))
4040
}
4141
}
42-
return unsafe.Pointer(&zeroVal[0])
42+
return unsafe.Pointer(&abi.ZeroVal[0])
4343
}
4444
// long key, try not to do more comparisons than necessary
4545
keymaybe := uintptr(abi.MapBucketCount)
@@ -74,7 +74,7 @@ func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer {
7474
return add(unsafe.Pointer(b), dataOffset+abi.MapBucketCount*2*goarch.PtrSize+keymaybe*uintptr(t.ValueSize))
7575
}
7676
}
77-
return unsafe.Pointer(&zeroVal[0])
77+
return unsafe.Pointer(&abi.ZeroVal[0])
7878
}
7979
dohash:
8080
hash := t.Hasher(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0))
@@ -102,7 +102,7 @@ dohash:
102102
}
103103
}
104104
}
105-
return unsafe.Pointer(&zeroVal[0])
105+
return unsafe.Pointer(&abi.ZeroVal[0])
106106
}
107107

108108
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) {
111111
racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess2_faststr))
112112
}
113113
if h == nil || h.count == 0 {
114-
return unsafe.Pointer(&zeroVal[0]), false
114+
return unsafe.Pointer(&abi.ZeroVal[0]), false
115115
}
116116
if h.flags&hashWriting != 0 {
117117
fatal("concurrent map read and map write")
@@ -134,7 +134,7 @@ func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) {
134134
return add(unsafe.Pointer(b), dataOffset+abi.MapBucketCount*2*goarch.PtrSize+i*uintptr(t.ValueSize)), true
135135
}
136136
}
137-
return unsafe.Pointer(&zeroVal[0]), false
137+
return unsafe.Pointer(&abi.ZeroVal[0]), false
138138
}
139139
// long key, try not to do more comparisons than necessary
140140
keymaybe := uintptr(abi.MapBucketCount)
@@ -169,7 +169,7 @@ func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) {
169169
return add(unsafe.Pointer(b), dataOffset+abi.MapBucketCount*2*goarch.PtrSize+keymaybe*uintptr(t.ValueSize)), true
170170
}
171171
}
172-
return unsafe.Pointer(&zeroVal[0]), false
172+
return unsafe.Pointer(&abi.ZeroVal[0]), false
173173
}
174174
dohash:
175175
hash := t.Hasher(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0))
@@ -197,7 +197,7 @@ dohash:
197197
}
198198
}
199199
}
200-
return unsafe.Pointer(&zeroVal[0]), false
200+
return unsafe.Pointer(&abi.ZeroVal[0]), false
201201
}
202202

203203
func mapassign_faststr(t *maptype, h *hmap, s string) unsafe.Pointer {

src/runtime/runtime.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package runtime
66

77
import (
8-
"internal/abi"
98
"internal/runtime/atomic"
109
"unsafe"
1110
)
@@ -297,5 +296,3 @@ func setCrashFD(fd uintptr) uintptr {
297296
var auxv []uintptr
298297

299298
func getAuxv() []uintptr { return auxv } // accessed from x/sys/cpu; see issue 57336
300-
301-
var zeroVal [abi.ZeroValSize]byte

0 commit comments

Comments
 (0)