]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: factor out GC assist credit accounting
authorMichael Anthony Knyszek <mknyszek@google.com>
Sat, 13 Aug 2022 16:37:09 +0000 (16:37 +0000)
committerMichael Knyszek <mknyszek@google.com>
Wed, 12 Oct 2022 20:23:16 +0000 (20:23 +0000)
No-op change in preparation for arenas.

For #51317.

Change-Id: I0777f21763fcd34957b7e709580cf2b7a962ba67
Reviewed-on: https://go-review.googlesource.com/c/go/+/423365
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/runtime/malloc.go

index f2b93c04f4a42afab5e00c271f9e4ec4b1ddafba..c18ed07d49961856bf32ee63daa4a18402ffc778 100644 (file)
@@ -895,24 +895,7 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
 
        // assistG is the G to charge for this allocation, or nil if
        // GC is not currently active.
-       var assistG *g
-       if gcBlackenEnabled != 0 {
-               // Charge the current user G for this allocation.
-               assistG = getg()
-               if assistG.m.curg != nil {
-                       assistG = assistG.m.curg
-               }
-               // Charge the allocation against the G. We'll account
-               // for internal fragmentation at the end of mallocgc.
-               assistG.gcAssistBytes -= int64(size)
-
-               if assistG.gcAssistBytes < 0 {
-                       // This G is in debt. Assist the GC to correct
-                       // this before allocating. This must happen
-                       // before disabling preemption.
-                       gcAssistAlloc(assistG)
-               }
-       }
+       assistG := deductAssistCredit(size)
 
        // Set mp.mallocing to keep from being preempted by GC.
        mp := acquirem()
@@ -1165,6 +1148,34 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
        return x
 }
 
+// deductAssistCredit reduces the current G's assist credit
+// by size bytes, and assists the GC if necessary.
+//
+// Caller must be preemptible.
+//
+// Returns the G for which the assist credit was accounted.
+func deductAssistCredit(size uintptr) *g {
+       var assistG *g
+       if gcBlackenEnabled != 0 {
+               // Charge the current user G for this allocation.
+               assistG = getg()
+               if assistG.m.curg != nil {
+                       assistG = assistG.m.curg
+               }
+               // Charge the allocation against the G. We'll account
+               // for internal fragmentation at the end of mallocgc.
+               assistG.gcAssistBytes -= int64(size)
+
+               if assistG.gcAssistBytes < 0 {
+                       // This G is in debt. Assist the GC to correct
+                       // this before allocating. This must happen
+                       // before disabling preemption.
+                       gcAssistAlloc(assistG)
+               }
+       }
+       return assistG
+}
+
 // memclrNoHeapPointersChunked repeatedly calls memclrNoHeapPointers
 // on chunks of the buffer to be zeroed, with opportunities for preemption
 // along the way.  memclrNoHeapPointers contains no safepoints and also