2013-02-11 Sebastian Huber <sebastian.huber@embedded-brains.de>
[official-gcc.git] / libgo / runtime / mcache.c
blob570c06a731bde541b0f001494b87bd2bf46c2adf
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-thread (in Go, per-M) 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_Alloc(MCache *c, int32 sizeclass, uintptr size, int32 zeroed)
16 MCacheList *l;
17 MLink *first, *v;
18 int32 n;
20 // Allocate from list.
21 l = &c->list[sizeclass];
22 if(l->list == nil) {
23 // Replenish using central lists.
24 n = runtime_MCentral_AllocList(&runtime_mheap.central[sizeclass],
25 runtime_class_to_transfercount[sizeclass], &first);
26 if(n == 0)
27 runtime_throw("out of memory");
28 l->list = first;
29 l->nlist = n;
30 c->size += n*size;
32 v = l->list;
33 l->list = v->next;
34 l->nlist--;
35 if(l->nlist < l->nlistmin)
36 l->nlistmin = l->nlist;
37 c->size -= size;
39 // v is zeroed except for the link pointer
40 // that we used above; zero that.
41 v->next = nil;
42 if(zeroed) {
43 // block is zeroed iff second word is zero ...
44 if(size > sizeof(uintptr) && ((uintptr*)v)[1] != 0)
45 runtime_memclr((byte*)v, size);
47 c->local_cachealloc += size;
48 c->local_objects++;
49 return v;
52 // Take n elements off l and return them to the central free list.
53 static void
54 ReleaseN(MCache *c, MCacheList *l, int32 n, int32 sizeclass)
56 MLink *first, **lp;
57 int32 i;
59 // Cut off first n elements.
60 first = l->list;
61 lp = &l->list;
62 for(i=0; i<n; i++)
63 lp = &(*lp)->next;
64 l->list = *lp;
65 *lp = nil;
66 l->nlist -= n;
67 if(l->nlist < l->nlistmin)
68 l->nlistmin = l->nlist;
69 c->size -= n*runtime_class_to_size[sizeclass];
71 // Return them to central free list.
72 runtime_MCentral_FreeList(&runtime_mheap.central[sizeclass], n, first);
75 void
76 runtime_MCache_Free(MCache *c, void *v, int32 sizeclass, uintptr size)
78 int32 i, n;
79 MCacheList *l;
80 MLink *p;
82 // Put back on list.
83 l = &c->list[sizeclass];
84 p = v;
85 p->next = l->list;
86 l->list = p;
87 l->nlist++;
88 c->size += size;
89 c->local_cachealloc -= size;
90 c->local_objects--;
92 if(l->nlist >= MaxMCacheListLen) {
93 // Release a chunk back.
94 ReleaseN(c, l, runtime_class_to_transfercount[sizeclass], sizeclass);
97 if(c->size >= MaxMCacheSize) {
98 // Scavenge.
99 for(i=0; i<NumSizeClasses; i++) {
100 l = &c->list[i];
101 n = l->nlistmin;
103 // n is the minimum number of elements we've seen on
104 // the list since the last scavenge. If n > 0, it means that
105 // we could have gotten by with n fewer elements
106 // without needing to consult the central free list.
107 // Move toward that situation by releasing n/2 of them.
108 if(n > 0) {
109 if(n > 1)
110 n /= 2;
111 ReleaseN(c, l, n, i);
113 l->nlistmin = l->nlist;
118 void
119 runtime_MCache_ReleaseAll(MCache *c)
121 int32 i;
122 MCacheList *l;
124 for(i=0; i<NumSizeClasses; i++) {
125 l = &c->list[i];
126 ReleaseN(c, l, l->nlist, i);
127 l->nlistmin = 0;