Retro68/gcc/libgo/runtime/mcache.c

82 lines
1.8 KiB
C
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2014-09-21 17:33:12 +00:00
// Per-P malloc cache for small objects.
2012-03-27 23:13:14 +00:00
//
// See malloc.h for an overview.
#include "runtime.h"
#include "arch.h"
#include "malloc.h"
2014-09-21 17:33:12 +00:00
void
runtime_MCache_Refill(MCache *c, int32 sizeclass)
2012-03-27 23:13:14 +00:00
{
MCacheList *l;
2014-09-21 17:33:12 +00:00
// Replenish using central lists.
2012-03-27 23:13:14 +00:00
l = &c->list[sizeclass];
2014-09-21 17:33:12 +00:00
if(l->list)
runtime_throw("MCache_Refill: the list is not empty");
l->nlist = runtime_MCentral_AllocList(&runtime_mheap.central[sizeclass], &l->list);
if(l->list == nil)
runtime_throw("out of memory");
2012-03-27 23:13:14 +00:00
}
// Take n elements off l and return them to the central free list.
static void
2014-09-21 17:33:12 +00:00
ReleaseN(MCacheList *l, int32 n, int32 sizeclass)
2012-03-27 23:13:14 +00:00
{
MLink *first, **lp;
int32 i;
// Cut off first n elements.
first = l->list;
lp = &l->list;
for(i=0; i<n; i++)
lp = &(*lp)->next;
l->list = *lp;
*lp = nil;
l->nlist -= n;
// Return them to central free list.
2014-09-21 17:33:12 +00:00
runtime_MCentral_FreeList(&runtime_mheap.central[sizeclass], first);
2012-03-27 23:13:14 +00:00
}
void
runtime_MCache_Free(MCache *c, void *v, int32 sizeclass, uintptr size)
{
MCacheList *l;
MLink *p;
// Put back on list.
l = &c->list[sizeclass];
p = v;
p->next = l->list;
l->list = p;
l->nlist++;
c->local_cachealloc -= size;
2014-09-21 17:33:12 +00:00
// We transfer span at a time from MCentral to MCache,
// if we have 2 times more than that, release a half back.
if(l->nlist >= 2*(runtime_class_to_allocnpages[sizeclass]<<PageShift)/size)
ReleaseN(l, l->nlist/2, sizeclass);
2012-03-27 23:13:14 +00:00
}
void
runtime_MCache_ReleaseAll(MCache *c)
{
int32 i;
MCacheList *l;
for(i=0; i<NumSizeClasses; i++) {
l = &c->list[i];
2014-09-21 17:33:12 +00:00
if(l->list) {
runtime_MCentral_FreeList(&runtime_mheap.central[i], l->list);
l->list = nil;
l->nlist = 0;
}
2012-03-27 23:13:14 +00:00
}
}