mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
runtime: simplify needzero logic
We always need to zero allocations with pointers in them. So we don't need some of the mallocs to take a needzero argument. Change-Id: Ideaa7b738873ba6a93addb5169791b42e2baad7c Reviewed-on: https://go-review.googlesource.com/c/go/+/660455 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Carlos Amedee <carlos@golang.org>
This commit is contained in:
parent
83bbf47863
commit
144d4e5d5f
@ -1047,14 +1047,19 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
|
|||||||
if size <= maxSmallSize-mallocHeaderSize {
|
if size <= maxSmallSize-mallocHeaderSize {
|
||||||
if typ == nil || !typ.Pointers() {
|
if typ == nil || !typ.Pointers() {
|
||||||
if size < maxTinySize {
|
if size < maxTinySize {
|
||||||
x, elemsize = mallocgcTiny(size, typ, needzero)
|
x, elemsize = mallocgcTiny(size, typ)
|
||||||
} else {
|
} else {
|
||||||
x, elemsize = mallocgcSmallNoscan(size, typ, needzero)
|
x, elemsize = mallocgcSmallNoscan(size, typ, needzero)
|
||||||
}
|
}
|
||||||
} else if heapBitsInSpan(size) {
|
|
||||||
x, elemsize = mallocgcSmallScanNoHeader(size, typ, needzero)
|
|
||||||
} else {
|
} else {
|
||||||
x, elemsize = mallocgcSmallScanHeader(size, typ, needzero)
|
if !needzero {
|
||||||
|
throw("objects with pointers must be zeroed")
|
||||||
|
}
|
||||||
|
if heapBitsInSpan(size) {
|
||||||
|
x, elemsize = mallocgcSmallScanNoHeader(size, typ)
|
||||||
|
} else {
|
||||||
|
x, elemsize = mallocgcSmallScanHeader(size, typ)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
x, elemsize = mallocgcLarge(size, typ, needzero)
|
x, elemsize = mallocgcLarge(size, typ, needzero)
|
||||||
@ -1092,7 +1097,7 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
|
|||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
func mallocgcTiny(size uintptr, typ *_type, needzero bool) (unsafe.Pointer, uintptr) {
|
func mallocgcTiny(size uintptr, typ *_type) (unsafe.Pointer, uintptr) {
|
||||||
// Set mp.mallocing to keep from being preempted by GC.
|
// Set mp.mallocing to keep from being preempted by GC.
|
||||||
mp := acquirem()
|
mp := acquirem()
|
||||||
if doubleCheckMalloc {
|
if doubleCheckMalloc {
|
||||||
@ -1172,7 +1177,7 @@ func mallocgcTiny(size uintptr, typ *_type, needzero bool) (unsafe.Pointer, uint
|
|||||||
v, span, checkGCTrigger = c.nextFree(tinySpanClass)
|
v, span, checkGCTrigger = c.nextFree(tinySpanClass)
|
||||||
}
|
}
|
||||||
x := unsafe.Pointer(v)
|
x := unsafe.Pointer(v)
|
||||||
(*[2]uint64)(x)[0] = 0
|
(*[2]uint64)(x)[0] = 0 // Always zero
|
||||||
(*[2]uint64)(x)[1] = 0
|
(*[2]uint64)(x)[1] = 0
|
||||||
// See if we need to replace the existing tiny block with the new one
|
// See if we need to replace the existing tiny block with the new one
|
||||||
// based on amount of remaining free space.
|
// based on amount of remaining free space.
|
||||||
@ -1334,7 +1339,7 @@ func mallocgcSmallNoscan(size uintptr, typ *_type, needzero bool) (unsafe.Pointe
|
|||||||
return x, size
|
return x, size
|
||||||
}
|
}
|
||||||
|
|
||||||
func mallocgcSmallScanNoHeader(size uintptr, typ *_type, needzero bool) (unsafe.Pointer, uintptr) {
|
func mallocgcSmallScanNoHeader(size uintptr, typ *_type) (unsafe.Pointer, uintptr) {
|
||||||
// Set mp.mallocing to keep from being preempted by GC.
|
// Set mp.mallocing to keep from being preempted by GC.
|
||||||
mp := acquirem()
|
mp := acquirem()
|
||||||
if doubleCheckMalloc {
|
if doubleCheckMalloc {
|
||||||
@ -1363,7 +1368,7 @@ func mallocgcSmallScanNoHeader(size uintptr, typ *_type, needzero bool) (unsafe.
|
|||||||
v, span, checkGCTrigger = c.nextFree(spc)
|
v, span, checkGCTrigger = c.nextFree(spc)
|
||||||
}
|
}
|
||||||
x := unsafe.Pointer(v)
|
x := unsafe.Pointer(v)
|
||||||
if needzero && span.needzero != 0 {
|
if span.needzero != 0 {
|
||||||
memclrNoHeapPointers(x, size)
|
memclrNoHeapPointers(x, size)
|
||||||
}
|
}
|
||||||
if goarch.PtrSize == 8 && sizeclass == 1 {
|
if goarch.PtrSize == 8 && sizeclass == 1 {
|
||||||
@ -1425,7 +1430,7 @@ func mallocgcSmallScanNoHeader(size uintptr, typ *_type, needzero bool) (unsafe.
|
|||||||
return x, size
|
return x, size
|
||||||
}
|
}
|
||||||
|
|
||||||
func mallocgcSmallScanHeader(size uintptr, typ *_type, needzero bool) (unsafe.Pointer, uintptr) {
|
func mallocgcSmallScanHeader(size uintptr, typ *_type) (unsafe.Pointer, uintptr) {
|
||||||
// Set mp.mallocing to keep from being preempted by GC.
|
// Set mp.mallocing to keep from being preempted by GC.
|
||||||
mp := acquirem()
|
mp := acquirem()
|
||||||
if doubleCheckMalloc {
|
if doubleCheckMalloc {
|
||||||
@ -1461,7 +1466,7 @@ func mallocgcSmallScanHeader(size uintptr, typ *_type, needzero bool) (unsafe.Po
|
|||||||
v, span, checkGCTrigger = c.nextFree(spc)
|
v, span, checkGCTrigger = c.nextFree(spc)
|
||||||
}
|
}
|
||||||
x := unsafe.Pointer(v)
|
x := unsafe.Pointer(v)
|
||||||
if needzero && span.needzero != 0 {
|
if span.needzero != 0 {
|
||||||
memclrNoHeapPointers(x, size)
|
memclrNoHeapPointers(x, size)
|
||||||
}
|
}
|
||||||
header := (**_type)(x)
|
header := (**_type)(x)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user