Commit f3dd6df6 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

runtime: simplify code

Full spans can't be passed to UncacheSpan since we get rid of free.

LGTM=rsc
R=golang-codereviews
CC=golang-codereviews, khr, rsc
https://golang.org/cl/119490044
parent de1ab89f
...@@ -508,7 +508,6 @@ void runtime·MHeap_Free(MHeap *h, MSpan *s, int32 acct); ...@@ -508,7 +508,6 @@ void runtime·MHeap_Free(MHeap *h, MSpan *s, int32 acct);
void runtime·MHeap_FreeStack(MHeap *h, MSpan *s); void runtime·MHeap_FreeStack(MHeap *h, MSpan *s);
MSpan* runtime·MHeap_Lookup(MHeap *h, void *v); MSpan* runtime·MHeap_Lookup(MHeap *h, void *v);
MSpan* runtime·MHeap_LookupMaybe(MHeap *h, void *v); MSpan* runtime·MHeap_LookupMaybe(MHeap *h, void *v);
void runtime·MGetSizeClassInfo(int32 sizeclass, uintptr *size, int32 *npages, int32 *nobj);
void* runtime·MHeap_SysAlloc(MHeap *h, uintptr n); void* runtime·MHeap_SysAlloc(MHeap *h, uintptr n);
void runtime·MHeap_MapBits(MHeap *h); void runtime·MHeap_MapBits(MHeap *h);
void runtime·MHeap_MapSpans(MHeap *h); void runtime·MHeap_MapSpans(MHeap *h);
......
...@@ -82,7 +82,7 @@ runtime·MCache_Refill(MCache *c, int32 sizeclass) ...@@ -82,7 +82,7 @@ runtime·MCache_Refill(MCache *c, int32 sizeclass)
if(s->freelist != nil) if(s->freelist != nil)
runtime·throw("refill on a nonempty span"); runtime·throw("refill on a nonempty span");
if(s != &emptymspan) if(s != &emptymspan)
runtime·MCentral_UncacheSpan(&runtime·mheap.central[sizeclass], s); s->incache = false;
// Get a new cached span from the central lists. // Get a new cached span from the central lists.
s = runtime·MCentral_CacheSpan(&runtime·mheap.central[sizeclass]); s = runtime·MCentral_CacheSpan(&runtime·mheap.central[sizeclass]);
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "malloc.h" #include "malloc.h"
static bool MCentral_Grow(MCentral *c); static bool MCentral_Grow(MCentral *c);
static void MCentral_ReturnToHeap(MCentral *c, MSpan *s);
// Initialize a single central free list. // Initialize a single central free list.
void void
...@@ -110,11 +109,8 @@ runtime·MCentral_UncacheSpan(MCentral *c, MSpan *s) ...@@ -110,11 +109,8 @@ runtime·MCentral_UncacheSpan(MCentral *c, MSpan *s)
s->incache = false; s->incache = false;
if(s->ref == 0) { if(s->ref == 0)
// Free back to heap. Unlikely, but possible. runtime·throw("uncaching full span");
MCentral_ReturnToHeap(c, s); // unlocks c
return;
}
cap = (s->npages << PageShift) / s->elemsize; cap = (s->npages << PageShift) / s->elemsize;
n = cap - s->ref; n = cap - s->ref;
...@@ -159,36 +155,29 @@ runtime·MCentral_FreeSpan(MCentral *c, MSpan *s, int32 n, MLink *start, MLink * ...@@ -159,36 +155,29 @@ runtime·MCentral_FreeSpan(MCentral *c, MSpan *s, int32 n, MLink *start, MLink *
} }
// s is completely freed, return it to the heap. // s is completely freed, return it to the heap.
MCentral_ReturnToHeap(c, s); // unlocks c runtime·MSpanList_Remove(s);
s->needzero = 1;
s->freelist = nil;
runtime·unlock(c);
runtime·unmarkspan((byte*)(s->start<<PageShift), s->npages<<PageShift);
runtime·MHeap_Free(&runtime·mheap, s, 0);
return true; return true;
} }
void
runtime·MGetSizeClassInfo(int32 sizeclass, uintptr *sizep, int32 *npagesp, int32 *nobj)
{
int32 size;
int32 npages;
npages = runtime·class_to_allocnpages[sizeclass];
size = runtime·class_to_size[sizeclass];
*npagesp = npages;
*sizep = size;
*nobj = (npages << PageShift) / size;
}
// Fetch a new span from the heap and // Fetch a new span from the heap and
// carve into objects for the free list. // carve into objects for the free list.
static bool static bool
MCentral_Grow(MCentral *c) MCentral_Grow(MCentral *c)
{ {
int32 i, n, npages; uintptr size, npages, cap, i, n;
uintptr size;
MLink **tailp, *v; MLink **tailp, *v;
byte *p; byte *p;
MSpan *s; MSpan *s;
runtime·unlock(c); runtime·unlock(c);
runtime·MGetSizeClassInfo(c->sizeclass, &size, &npages, &n); npages = runtime·class_to_allocnpages[c->sizeclass];
size = runtime·class_to_size[c->sizeclass];
n = (npages << PageShift) / size;
s = runtime·MHeap_Alloc(&runtime·mheap, npages, c->sizeclass, 0, 1); s = runtime·MHeap_Alloc(&runtime·mheap, npages, c->sizeclass, 0, 1);
if(s == nil) { if(s == nil) {
// TODO(rsc): Log out of memory // TODO(rsc): Log out of memory
...@@ -213,17 +202,3 @@ MCentral_Grow(MCentral *c) ...@@ -213,17 +202,3 @@ MCentral_Grow(MCentral *c)
runtime·MSpanList_Insert(&c->nonempty, s); runtime·MSpanList_Insert(&c->nonempty, s);
return true; return true;
} }
// Return s to the heap. s must be unused (s->ref == 0). Unlocks c.
static void
MCentral_ReturnToHeap(MCentral *c, MSpan *s)
{
runtime·MSpanList_Remove(s);
s->needzero = 1;
s->freelist = nil;
if(s->ref != 0)
runtime·throw("ref wrong");
runtime·unlock(c);
runtime·unmarkspan((byte*)(s->start<<PageShift), s->npages<<PageShift);
runtime·MHeap_Free(&runtime·mheap, s, 0);
}
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