Skip to content

Commit c02134a

Browse files
committed
runtime: add helper for getting an mcache in allocation contexts
This change adds a function getMCache which returns the current P's mcache if it's available, and otherwise tries to get mcache0 if we're bootstrapping. This function will come in handy as we need to replicate this behavior in multiple places in future changes. Change-Id: I536073d6f6dc6c6390269e613ead9f8bcb6e7f98 Reviewed-on: https://go-review.googlesource.com/c/go/+/246976 Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Michael Knyszek <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent ae585ee commit c02134a

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/runtime/malloc.go

+2-23
Original file line numberDiff line numberDiff line change
@@ -972,19 +972,7 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
972972

973973
shouldhelpgc := false
974974
dataSize := size
975-
var c *mcache
976-
if mp.p != 0 {
977-
c = mp.p.ptr().mcache
978-
} else {
979-
// We will be called without a P while bootstrapping,
980-
// in which case we use mcache0, which is set in mallocinit.
981-
// mcache0 is cleared when bootstrapping is complete,
982-
// by procresize.
983-
c = mcache0
984-
if c == nil {
985-
throw("malloc called with no P")
986-
}
987-
}
975+
c := getMCache()
988976
var span *mspan
989977
var x unsafe.Pointer
990978
noscan := typ == nil || typ.ptrdata == 0
@@ -1212,16 +1200,7 @@ func reflect_unsafe_NewArray(typ *_type, n int) unsafe.Pointer {
12121200
}
12131201

12141202
func profilealloc(mp *m, x unsafe.Pointer, size uintptr) {
1215-
var c *mcache
1216-
if mp.p != 0 {
1217-
c = mp.p.ptr().mcache
1218-
} else {
1219-
c = mcache0
1220-
if c == nil {
1221-
throw("profilealloc called with no P")
1222-
}
1223-
}
1224-
c.nextSample = nextSample()
1203+
getMCache().nextSample = nextSample()
12251204
mProf_Malloc(x, size)
12261205
}
12271206

src/runtime/mcache.go

+23
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,29 @@ func freemcache(c *mcache, recipient *mcache) {
131131
})
132132
}
133133

134+
// getMCache is a convenience function which tries to obtain an mcache.
135+
//
136+
// Must be running with a P when called (so the caller must be in a
137+
// non-preemptible state) or must be called during bootstrapping.
138+
func getMCache() *mcache {
139+
// Grab the mcache, since that's where stats live.
140+
pp := getg().m.p.ptr()
141+
var c *mcache
142+
if pp == nil {
143+
// We will be called without a P while bootstrapping,
144+
// in which case we use mcache0, which is set in mallocinit.
145+
// mcache0 is cleared when bootstrapping is complete,
146+
// by procresize.
147+
c = mcache0
148+
if c == nil {
149+
throw("getMCache called with no P or outside bootstrapping")
150+
}
151+
} else {
152+
c = pp.mcache
153+
}
154+
return c
155+
}
156+
134157
// donate flushes data and resources which have no global
135158
// pool to another mcache.
136159
func (c *mcache) donate(d *mcache) {

0 commit comments

Comments
 (0)