2014-07-11 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / libgo / runtime / mcache.c
blob38f824a139b5c5297d5db967869112020d8f4c70
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Per-P malloc cache for small objects.
6 //
7 // See malloc.h for an overview.
9 #include "runtime.h"
10 #include "arch.h"
11 #include "malloc.h"
13 void
14 runtime_MCache_Refill(MCache *c, int32 sizeclass)
16 MCacheList *l;
18 // Replenish using central lists.
19 l = &c->list[sizeclass];
20 if(l->list)
21 runtime_throw("MCache_Refill: the list is not empty");
22 l->nlist = runtime_MCentral_AllocList(&runtime_mheap.central[sizeclass], &l->list);
23 if(l->list == nil)
24 runtime_throw("out of memory");
27 // Take n elements off l and return them to the central free list.
28 static void
29 ReleaseN(MCacheList *l, int32 n, int32 sizeclass)
31 MLink *first, **lp;
32 int32 i;
34 // Cut off first n elements.
35 first = l->list;
36 lp = &l->list;
37 for(i=0; i<n; i++)
38 lp = &(*lp)->next;
39 l->list = *lp;
40 *lp = nil;
41 l->nlist -= n;
43 // Return them to central free list.
44 runtime_MCentral_FreeList(&runtime_mheap.central[sizeclass], first);
47 void
48 runtime_MCache_Free(MCache *c, void *v, int32 sizeclass, uintptr size)
50 MCacheList *l;
51 MLink *p;
53 // Put back on list.
54 l = &c->list[sizeclass];
55 p = v;
56 p->next = l->list;
57 l->list = p;
58 l->nlist++;
59 c->local_cachealloc -= size;
61 // We transfer span at a time from MCentral to MCache,
62 // if we have 2 times more than that, release a half back.
63 if(l->nlist >= 2*(runtime_class_to_allocnpages[sizeclass]<<PageShift)/size)
64 ReleaseN(l, l->nlist/2, sizeclass);
67 void
68 runtime_MCache_ReleaseAll(MCache *c)
70 int32 i;
71 MCacheList *l;
73 for(i=0; i<NumSizeClasses; i++) {
74 l = &c->list[i];
75 if(l->list) {
76 runtime_MCentral_FreeList(&runtime_mheap.central[i], l->list);
77 l->list = nil;
78 l->nlist = 0;