Commit c17c42e8 authored by Matthew Dempsky's avatar Matthew Dempsky

runtime: rewrite lots of foo_Bar(f, ...) into f.bar(...)

Applies to types fixAlloc, mCache, mCentral, mHeap, mSpan, and
mSpanList.

Two special cases:

1. mHeap_Scavenge() previously didn't take an *mheap parameter, so it
was specially handled in this CL.

2. mHeap_Free() would have collided with mheap's "free" field, so it's
been renamed to (*mheap).freeSpan to parallel its underlying
(*mheap).freeSpanLocked method.

Change-Id: I325938554cca432c166fe9d9d689af2bbd68de4b
Reviewed-on: https://go-review.googlesource.com/16221Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 58db5fc9
......@@ -643,7 +643,7 @@ func mdump() {
for i := uintptr(0); i < uintptr(mheap_.nspan); i++ {
s := h_allspans[i]
if s.state == _MSpanInUse {
mSpan_EnsureSwept(s)
s.ensureSwept()
}
}
memclr(unsafe.Pointer(&typecache), unsafe.Sizeof(typecache))
......
......@@ -76,9 +76,6 @@
// or the page heap can avoid zeroing altogether.
// 2. the cost of zeroing when reusing a small object is
// charged to the mutator, not the garbage collector.
//
// This code was written with an eye toward translating to Go
// in the future. Methods have the form Type_Method(Type *t, ...).
package runtime
......@@ -359,7 +356,7 @@ func mallocinit() {
}
// Initialize the rest of the allocator.
mHeap_Init(&mheap_, spansSize)
mheap_.init(spansSize)
_g_ := getg()
_g_.m.mcache = allocmcache()
}
......@@ -387,7 +384,7 @@ func sysReserveHigh(n uintptr, reserved *bool) unsafe.Pointer {
return sysReserve(nil, n, reserved)
}
func mHeap_SysAlloc(h *mheap, n uintptr) unsafe.Pointer {
func (h *mheap) sysAlloc(n uintptr) unsafe.Pointer {
if n > h.arena_end-h.arena_used {
// We are in 32-bit mode, maybe we didn't use all possible address space yet.
// Reserve some more space.
......@@ -409,8 +406,8 @@ func mHeap_SysAlloc(h *mheap, n uintptr) unsafe.Pointer {
// Our pages are bigger than hardware pages.
h.arena_end = p + p_size
used := p + (-uintptr(p) & (_PageSize - 1))
mHeap_MapBits(h, used)
mHeap_MapSpans(h, used)
h.mapBits(used)
h.mapSpans(used)
h.arena_used = used
h.arena_reserved = reserved
} else {
......@@ -424,8 +421,8 @@ func mHeap_SysAlloc(h *mheap, n uintptr) unsafe.Pointer {
// Keep taking from our reservation.
p := h.arena_used
sysMap(unsafe.Pointer(p), n, h.arena_reserved, &memstats.heap_sys)
mHeap_MapBits(h, p+n)
mHeap_MapSpans(h, p+n)
h.mapBits(p + n)
h.mapSpans(p + n)
h.arena_used = p + n
if raceenabled {
racemapshadow(unsafe.Pointer(p), n)
......@@ -460,8 +457,8 @@ func mHeap_SysAlloc(h *mheap, n uintptr) unsafe.Pointer {
p_end := p + p_size
p += -p & (_PageSize - 1)
if uintptr(p)+n > h.arena_used {
mHeap_MapBits(h, p+n)
mHeap_MapSpans(h, p+n)
h.mapBits(p + n)
h.mapSpans(p + n)
h.arena_used = p + n
if p_end > h.arena_end {
h.arena_end = p_end
......@@ -600,7 +597,7 @@ func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer {
v := s.freelist
if v.ptr() == nil {
systemstack(func() {
mCache_Refill(c, tinySizeClass)
c.refill(tinySizeClass)
})
shouldhelpgc = true
s = c.alloc[tinySizeClass]
......@@ -632,7 +629,7 @@ func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer {
v := s.freelist
if v.ptr() == nil {
systemstack(func() {
mCache_Refill(c, int32(sizeclass))
c.refill(int32(sizeclass))
})
shouldhelpgc = true
s = c.alloc[sizeclass]
......@@ -757,7 +754,7 @@ func largeAlloc(size uintptr, flag uint32) *mspan {
// pays the debt down to npage pages.
deductSweepCredit(npages*_PageSize, npages)
s := mHeap_Alloc(&mheap_, npages, 0, true, flag&_FlagNoZero == 0)
s := mheap_.alloc(npages, 0, true, flag&_FlagNoZero == 0)
if s == nil {
throw("out of memory")
}
......
......@@ -131,7 +131,7 @@ func subtract1(p *byte) *byte {
// after observing the change to arena_used.
//
//go:nowritebarrier
func mHeap_MapBits(h *mheap, arena_used uintptr) {
func (h *mheap) mapBits(arena_used uintptr) {
// Caller has added extra mappings to the arena.
// Add extra mappings of bitmap words as needed.
// We allocate extra bitmap pieces in chunks of bitmapChunk.
......
......@@ -63,7 +63,7 @@ var emptymspan mspan
func allocmcache() *mcache {
lock(&mheap_.lock)
c := (*mcache)(fixAlloc_Alloc(&mheap_.cachealloc))
c := (*mcache)(mheap_.cachealloc.alloc())
unlock(&mheap_.lock)
memclr(unsafe.Pointer(c), unsafe.Sizeof(*c))
for i := 0; i < _NumSizeClasses; i++ {
......@@ -75,7 +75,7 @@ func allocmcache() *mcache {
func freemcache(c *mcache) {
systemstack(func() {
mCache_ReleaseAll(c)
c.releaseAll()
stackcache_clear(c)
// NOTE(rsc,rlh): If gcworkbuffree comes back, we need to coordinate
......@@ -85,14 +85,14 @@ func freemcache(c *mcache) {
lock(&mheap_.lock)
purgecachedstats(c)
fixAlloc_Free(&mheap_.cachealloc, unsafe.Pointer(c))
mheap_.cachealloc.free(unsafe.Pointer(c))
unlock(&mheap_.lock)
})
}
// Gets a span that has a free object in it and assigns it
// to be the cached span for the given sizeclass. Returns this span.
func mCache_Refill(c *mcache, sizeclass int32) *mspan {
func (c *mcache) refill(sizeclass int32) *mspan {
_g_ := getg()
_g_.m.locks++
......@@ -106,7 +106,7 @@ func mCache_Refill(c *mcache, sizeclass int32) *mspan {
}
// Get a new cached span from the central lists.
s = mCentral_CacheSpan(&mheap_.central[sizeclass].mcentral)
s = mheap_.central[sizeclass].mcentral.cacheSpan()
if s == nil {
throw("out of memory")
}
......@@ -119,11 +119,11 @@ func mCache_Refill(c *mcache, sizeclass int32) *mspan {
return s
}
func mCache_ReleaseAll(c *mcache) {
func (c *mcache) releaseAll() {
for i := 0; i < _NumSizeClasses; i++ {
s := c.alloc[i]
if s != &emptymspan {
mCentral_UncacheSpan(&mheap_.central[i].mcentral, s)
mheap_.central[i].mcentral.uncacheSpan(s)
c.alloc[i] = &emptymspan
}
}
......
......@@ -23,14 +23,14 @@ type mcentral struct {
}
// Initialize a single central free list.
func mCentral_Init(c *mcentral, sizeclass int32) {
func (c *mcentral) init(sizeclass int32) {
c.sizeclass = sizeclass
mSpanList_Init(&c.nonempty)
mSpanList_Init(&c.empty)
c.nonempty.init()
c.empty.init()
}
// Allocate a span to use in an MCache.
func mCentral_CacheSpan(c *mcentral) *mspan {
func (c *mcentral) cacheSpan() *mspan {
// Deduct credit for this span allocation and sweep if necessary.
deductSweepCredit(uintptr(class_to_size[c.sizeclass]), 0)
......@@ -40,10 +40,10 @@ retry:
var s *mspan
for s = c.nonempty.first; s != nil; s = s.next {
if s.sweepgen == sg-2 && atomic.Cas(&s.sweepgen, sg-2, sg-1) {
mSpanList_Remove(&c.nonempty, s)
mSpanList_InsertBack(&c.empty, s)
c.nonempty.remove(s)
c.empty.insertBack(s)
unlock(&c.lock)
mSpan_Sweep(s, true)
s.sweep(true)
goto havespan
}
if s.sweepgen == sg-1 {
......@@ -51,8 +51,8 @@ retry:
continue
}
// we have a nonempty span that does not require sweeping, allocate from it
mSpanList_Remove(&c.nonempty, s)
mSpanList_InsertBack(&c.empty, s)
c.nonempty.remove(s)
c.empty.insertBack(s)
unlock(&c.lock)
goto havespan
}
......@@ -61,11 +61,11 @@ retry:
if s.sweepgen == sg-2 && atomic.Cas(&s.sweepgen, sg-2, sg-1) {
// we have an empty span that requires sweeping,
// sweep it and see if we can free some space in it
mSpanList_Remove(&c.empty, s)
c.empty.remove(s)
// swept spans are at the end of the list
mSpanList_InsertBack(&c.empty, s)
c.empty.insertBack(s)
unlock(&c.lock)
mSpan_Sweep(s, true)
s.sweep(true)
if s.freelist.ptr() != nil {
goto havespan
}
......@@ -85,12 +85,12 @@ retry:
unlock(&c.lock)
// Replenish central list if empty.
s = mCentral_Grow(c)
s = c.grow()
if s == nil {
return nil
}
lock(&c.lock)
mSpanList_InsertBack(&c.empty, s)
c.empty.insertBack(s)
unlock(&c.lock)
// At this point s is a non-empty span, queued at the end of the empty list,
......@@ -113,7 +113,7 @@ havespan:
}
// Return span from an MCache.
func mCentral_UncacheSpan(c *mcentral, s *mspan) {
func (c *mcentral) uncacheSpan(s *mspan) {
lock(&c.lock)
s.incache = false
......@@ -125,8 +125,8 @@ func mCentral_UncacheSpan(c *mcentral, s *mspan) {
cap := int32((s.npages << _PageShift) / s.elemsize)
n := cap - int32(s.ref)
if n > 0 {
mSpanList_Remove(&c.empty, s)
mSpanList_Insert(&c.nonempty, s)
c.empty.remove(s)
c.nonempty.insert(s)
}
unlock(&c.lock)
}
......@@ -137,7 +137,7 @@ func mCentral_UncacheSpan(c *mcentral, s *mspan) {
// the latest generation.
// If preserve=true, don't return the span to heap nor relink in MCentral lists;
// caller takes care of it.
func mCentral_FreeSpan(c *mcentral, s *mspan, n int32, start gclinkptr, end gclinkptr, preserve bool) bool {
func (c *mcentral) freeSpan(s *mspan, n int32, start gclinkptr, end gclinkptr, preserve bool) bool {
if s.incache {
throw("freespan into cached span")
}
......@@ -151,7 +151,7 @@ func mCentral_FreeSpan(c *mcentral, s *mspan, n int32, start gclinkptr, end gcli
if preserve {
// preserve is set only when called from MCentral_CacheSpan above,
// the span must be in the empty list.
if !mSpan_InList(s) {
if !s.inList() {
throw("can't preserve unlinked span")
}
atomic.Store(&s.sweepgen, mheap_.sweepgen)
......@@ -162,8 +162,8 @@ func mCentral_FreeSpan(c *mcentral, s *mspan, n int32, start gclinkptr, end gcli
// Move to nonempty if necessary.
if wasempty {
mSpanList_Remove(&c.empty, s)
mSpanList_Insert(&c.nonempty, s)
c.empty.remove(s)
c.nonempty.insert(s)
}
// delay updating sweepgen until here. This is the signal that
......@@ -178,22 +178,22 @@ func mCentral_FreeSpan(c *mcentral, s *mspan, n int32, start gclinkptr, end gcli
}
// s is completely freed, return it to the heap.
mSpanList_Remove(&c.nonempty, s)
c.nonempty.remove(s)
s.needzero = 1
s.freelist = 0
unlock(&c.lock)
heapBitsForSpan(s.base()).initSpan(s.layout())
mHeap_Free(&mheap_, s, 0)
mheap_.freeSpan(s, 0)
return true
}
// Fetch a new span from the heap and carve into objects for the free list.
func mCentral_Grow(c *mcentral) *mspan {
func (c *mcentral) grow() *mspan {
npages := uintptr(class_to_allocnpages[c.sizeclass])
size := uintptr(class_to_size[c.sizeclass])
n := (npages << _PageShift) / size
s := mHeap_Alloc(&mheap_, npages, c.sizeclass, false, true)
s := mheap_.alloc(npages, c.sizeclass, false, true)
if s == nil {
return nil
}
......
......@@ -40,7 +40,7 @@ type mlink struct {
// Initialize f to allocate objects of the given size,
// using the allocator to obtain chunks of memory.
func fixAlloc_Init(f *fixalloc, size uintptr, first func(arg, p unsafe.Pointer), arg unsafe.Pointer, stat *uint64) {
func (f *fixalloc) init(size uintptr, first func(arg, p unsafe.Pointer), arg unsafe.Pointer, stat *uint64) {
f.size = size
f.first = first
f.arg = arg
......@@ -51,7 +51,7 @@ func fixAlloc_Init(f *fixalloc, size uintptr, first func(arg, p unsafe.Pointer),
f.stat = stat
}
func fixAlloc_Alloc(f *fixalloc) unsafe.Pointer {
func (f *fixalloc) alloc() unsafe.Pointer {
if f.size == 0 {
print("runtime: use of FixAlloc_Alloc before FixAlloc_Init\n")
throw("runtime: internal error")
......@@ -78,7 +78,7 @@ func fixAlloc_Alloc(f *fixalloc) unsafe.Pointer {
return v
}
func fixAlloc_Free(f *fixalloc, p unsafe.Pointer) {
func (f *fixalloc) free(p unsafe.Pointer) {
f.inuse -= f.size
v := (*mlink)(p)
v.next = f.list
......
......@@ -47,7 +47,7 @@ func finishsweep_m(stw bool) {
sg := mheap_.sweepgen
for _, s := range work.spans {
if s.sweepgen != sg && s.state == _MSpanInUse {
mSpan_EnsureSwept(s)
s.ensureSwept()
}
}
}
......@@ -105,7 +105,7 @@ func sweepone() uintptr {
continue
}
npages := s.npages
if !mSpan_Sweep(s, false) {
if !s.sweep(false) {
npages = 0
}
_g_.m.locks--
......@@ -129,7 +129,7 @@ func gosweepdone() bool {
// Returns only when span s has been swept.
//go:nowritebarrier
func mSpan_EnsureSwept(s *mspan) {
func (s *mspan) ensureSwept() {
// Caller must disable preemption.
// Otherwise when this function returns the span can become unswept again
// (if GC is triggered on another goroutine).
......@@ -144,7 +144,7 @@ func mSpan_EnsureSwept(s *mspan) {
}
// The caller must be sure that the span is a MSpanInUse span.
if atomic.Cas(&s.sweepgen, sg-2, sg-1) {
mSpan_Sweep(s, false)
s.sweep(false)
return
}
// unfortunate condition, and we don't have efficient means to wait
......@@ -159,7 +159,7 @@ func mSpan_EnsureSwept(s *mspan) {
// If preserve=true, don't return it to heap nor relink in MCentral lists;
// caller takes care of it.
//TODO go:nowritebarrier
func mSpan_Sweep(s *mspan, preserve bool) bool {
func (s *mspan) sweep(preserve bool) bool {
// It's critical that we enter this function with preemption disabled,
// GC must not start while we are in the middle of this function.
_g_ := getg()
......@@ -312,7 +312,7 @@ func mSpan_Sweep(s *mspan, preserve bool) bool {
}
if nfree > 0 {
c.local_nsmallfree[cl] += uintptr(nfree)
res = mCentral_FreeSpan(&mheap_.central[cl].mcentral, s, int32(nfree), head, end, preserve)
res = mheap_.central[cl].mcentral.freeSpan(s, int32(nfree), head, end, preserve)
// MCentral_FreeSpan updates sweepgen
} else if freeToHeap {
// Free large span to heap
......@@ -335,7 +335,7 @@ func mSpan_Sweep(s *mspan, preserve bool) bool {
s.limit = 0 // prevent mlookup from finding this span
sysFault(unsafe.Pointer(uintptr(s.start<<_PageShift)), size)
} else {
mHeap_Free(&mheap_, s, 1)
mheap_.freeSpan(s, 1)
}
c.local_nlargefree++
c.local_largefree += size
......
This diff is collapsed.
......@@ -325,7 +325,7 @@ func flushallmcaches() {
if c == nil {
continue
}
mCache_ReleaseAll(c)
c.releaseAll()
stackcache_clear(c)
}
}
......
......@@ -3379,7 +3379,7 @@ func sysmon() {
}
// scavenge heap once in a while
if lastscavenge+scavengelimit/2 < now {
mHeap_Scavenge(int32(nscavenge), uint64(now), uint64(scavengelimit))
mheap_.scavenge(int32(nscavenge), uint64(now), uint64(scavengelimit))
lastscavenge = now
nscavenge++
}
......
......@@ -160,9 +160,9 @@ func stackinit() {
throw("cache size must be a multiple of page size")
}
for i := range stackpool {
mSpanList_Init(&stackpool[i])
stackpool[i].init()
}
mSpanList_Init(&stackFreeQueue)
stackFreeQueue.init()
}
// Allocates a stack from the free pool. Must be called with
......@@ -172,7 +172,7 @@ func stackpoolalloc(order uint8) gclinkptr {
s := list.first
if s == nil {
// no free stacks. Allocate another span worth.
s = mHeap_AllocStack(&mheap_, _StackCacheSize>>_PageShift)
s = mheap_.allocStack(_StackCacheSize >> _PageShift)
if s == nil {
throw("out of memory")
}
......@@ -187,7 +187,7 @@ func stackpoolalloc(order uint8) gclinkptr {
x.ptr().next = s.freelist
s.freelist = x
}
mSpanList_Insert(list, s)
list.insert(s)
}
x := s.freelist
if x.ptr() == nil {
......@@ -197,20 +197,20 @@ func stackpoolalloc(order uint8) gclinkptr {
s.ref++
if s.freelist.ptr() == nil {
// all stacks in s are allocated.
mSpanList_Remove(list, s)
list.remove(s)
}
return x
}
// Adds stack x to the free pool. Must be called with stackpoolmu held.
func stackpoolfree(x gclinkptr, order uint8) {
s := mHeap_Lookup(&mheap_, unsafe.Pointer(x))
s := mheap_.lookup(unsafe.Pointer(x))
if s.state != _MSpanStack {
throw("freeing stack not in a stack span")
}
if s.freelist.ptr() == nil {
// s will now have a free stack
mSpanList_Insert(&stackpool[order], s)
stackpool[order].insert(s)
}
x.ptr().next = s.freelist
s.freelist = x
......@@ -231,9 +231,9 @@ func stackpoolfree(x gclinkptr, order uint8) {
// pointer into a free span.
//
// By not freeing, we prevent step #4 until GC is done.
mSpanList_Remove(&stackpool[order], s)
stackpool[order].remove(s)
s.freelist = 0
mHeap_FreeStack(&mheap_, s)
mheap_.freeStack(s)
}
}
......@@ -357,7 +357,7 @@ func stackalloc(n uint32) (stack, []stkbar) {
}
v = unsafe.Pointer(x)
} else {
s := mHeap_AllocStack(&mheap_, round(uintptr(n), _PageSize)>>_PageShift)
s := mheap_.allocStack(round(uintptr(n), _PageSize) >> _PageShift)
if s == nil {
throw("out of memory")
}
......@@ -424,7 +424,7 @@ func stackfree(stk stack, n uintptr) {
c.stackcache[order].size += n
}
} else {
s := mHeap_Lookup(&mheap_, v)
s := mheap_.lookup(v)
if s.state != _MSpanStack {
println(hex(s.start<<_PageShift), v)
throw("bad span state")
......@@ -432,7 +432,7 @@ func stackfree(stk stack, n uintptr) {
if gcphase == _GCoff {
// Free the stack immediately if we're
// sweeping.
mHeap_FreeStack(&mheap_, s)
mheap_.freeStack(s)
} else {
// Otherwise, add it to a list of stack spans
// to be freed at the end of GC.
......@@ -441,7 +441,7 @@ func stackfree(stk stack, n uintptr) {
// these spans as stacks, like we do for small
// stack spans. (See issue #11466.)
lock(&stackpoolmu)
mSpanList_Insert(&stackFreeQueue, s)
stackFreeQueue.insert(s)
unlock(&stackpoolmu)
}
}
......@@ -1001,19 +1001,19 @@ func freeStackSpans() {
for s := list.first; s != nil; {
next := s.next
if s.ref == 0 {
mSpanList_Remove(list, s)
list.remove(s)
s.freelist = 0
mHeap_FreeStack(&mheap_, s)
mheap_.freeStack(s)
}
s = next
}
}
// Free queued stack spans.
for !mSpanList_IsEmpty(&stackFreeQueue) {
for !stackFreeQueue.isEmpty() {
s := stackFreeQueue.first
mSpanList_Remove(&stackFreeQueue, s)
mHeap_FreeStack(&mheap_, s)
stackFreeQueue.remove(s)
mheap_.freeStack(s)
}
unlock(&stackpoolmu)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment