From 554fd8c5195424bdbcabf5de30fdc183aba391bd Mon Sep 17 00:00:00 2001 From: upstream source tree Date: Sun, 15 Mar 2015 20:14:05 -0400 Subject: obtained gcc-4.6.4.tar.bz2 from upstream website; verified gcc-4.6.4.tar.bz2.sig; imported gcc-4.6.4 source tree from verified upstream tarball. downloading a git-generated archive based on the 'upstream' tag should provide you with a source tree that is binary identical to the one extracted from the above tarball. if you have obtained the source via the command 'git clone', however, do note that line-endings of files in your working directory might differ from line-endings of the respective files in the upstream repository. --- libgo/runtime/mcache.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 libgo/runtime/mcache.c (limited to 'libgo/runtime/mcache.c') diff --git a/libgo/runtime/mcache.c b/libgo/runtime/mcache.c new file mode 100644 index 000000000..ce6575758 --- /dev/null +++ b/libgo/runtime/mcache.c @@ -0,0 +1,131 @@ +// 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. + +// Per-thread (in Go, per-M) malloc cache for small objects. +// +// See malloc.h for an overview. + +#include "runtime.h" +#include "malloc.h" + +void* +runtime_MCache_Alloc(MCache *c, int32 sizeclass, uintptr size, int32 zeroed) +{ + MCacheList *l; + MLink *first, *v; + int32 n; + + // Allocate from list. + l = &c->list[sizeclass]; + if(l->list == nil) { + // Replenish using central lists. + n = runtime_MCentral_AllocList(&runtime_mheap.central[sizeclass], + runtime_class_to_transfercount[sizeclass], &first); + l->list = first; + l->nlist = n; + c->size += n*size; + } + v = l->list; + l->list = v->next; + l->nlist--; + if(l->nlist < l->nlistmin) + l->nlistmin = l->nlist; + c->size -= size; + + // v is zeroed except for the link pointer + // that we used above; zero that. + v->next = nil; + if(zeroed) { + // block is zeroed iff second word is zero ... + if(size > sizeof(uintptr) && ((uintptr*)v)[1] != 0) + runtime_memclr((byte*)v, size); + else { + // ... except for the link pointer + // that we used above; zero that. + v->next = nil; + } + } + c->local_alloc += size; + c->local_objects++; + return v; +} + +// Take n elements off l and return them to the central free list. +static void +ReleaseN(MCache *c, MCacheList *l, int32 n, int32 sizeclass) +{ + MLink *first, **lp; + int32 i; + + // Cut off first n elements. + first = l->list; + lp = &l->list; + for(i=0; inext; + l->list = *lp; + *lp = nil; + l->nlist -= n; + if(l->nlist < l->nlistmin) + l->nlistmin = l->nlist; + c->size -= n*runtime_class_to_size[sizeclass]; + + // Return them to central free list. + runtime_MCentral_FreeList(&runtime_mheap.central[sizeclass], n, first); +} + +void +runtime_MCache_Free(MCache *c, void *v, int32 sizeclass, uintptr size) +{ + int32 i, n; + MCacheList *l; + MLink *p; + + // Put back on list. + l = &c->list[sizeclass]; + p = v; + p->next = l->list; + l->list = p; + l->nlist++; + c->size += size; + c->local_alloc -= size; + c->local_objects--; + + if(l->nlist >= MaxMCacheListLen) { + // Release a chunk back. + ReleaseN(c, l, runtime_class_to_transfercount[sizeclass], sizeclass); + } + + if(c->size >= MaxMCacheSize) { + // Scavenge. + for(i=0; ilist[i]; + n = l->nlistmin; + + // n is the minimum number of elements we've seen on + // the list since the last scavenge. If n > 0, it means that + // we could have gotten by with n fewer elements + // without needing to consult the central free list. + // Move toward that situation by releasing n/2 of them. + if(n > 0) { + if(n > 1) + n /= 2; + ReleaseN(c, l, n, i); + } + l->nlistmin = l->nlist; + } + } +} + +void +runtime_MCache_ReleaseAll(MCache *c) +{ + int32 i; + MCacheList *l; + + for(i=0; ilist[i]; + ReleaseN(c, l, l->nlist, i); + l->nlistmin = 0; + } +} -- cgit v1.2.3