2012-01-13 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / mcache.c
blob6c60aebbe671e0f0fbec3da5c2adb11844fd7324
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);
46 else {
47 // ... except for the link pointer
48 // that we used above; zero that.
49 v->next = nil;
52 c->local_cachealloc += size;
53 c->local_objects++;
54 return v;
57 // Take n elements off l and return them to the central free list.
58 static void
59 ReleaseN(MCache *c, MCacheList *l, int32 n, int32 sizeclass)
61 MLink *first, **lp;
62 int32 i;
64 // Cut off first n elements.
65 first = l->list;
66 lp = &l->list;
67 for(i=0; i<n; i++)
68 lp = &(*lp)->next;
69 l->list = *lp;
70 *lp = nil;
71 l->nlist -= n;
72 if(l->nlist < l->nlistmin)
73 l->nlistmin = l->nlist;
74 c->size -= n*runtime_class_to_size[sizeclass];
76 // Return them to central free list.
77 runtime_MCentral_FreeList(&runtime_mheap.central[sizeclass], n, first);
80 void
81 runtime_MCache_Free(MCache *c, void *v, int32 sizeclass, uintptr size)
83 int32 i, n;
84 MCacheList *l;
85 MLink *p;
87 // Put back on list.
88 l = &c->list[sizeclass];
89 p = v;
90 p->next = l->list;
91 l->list = p;
92 l->nlist++;
93 c->size += size;
94 c->local_cachealloc -= size;
95 c->local_objects--;
97 if(l->nlist >= MaxMCacheListLen) {
98 // Release a chunk back.
99 ReleaseN(c, l, runtime_class_to_transfercount[sizeclass], sizeclass);
102 if(c->size >= MaxMCacheSize) {
103 // Scavenge.
104 for(i=0; i<NumSizeClasses; i++) {
105 l = &c->list[i];
106 n = l->nlistmin;
108 // n is the minimum number of elements we've seen on
109 // the list since the last scavenge. If n > 0, it means that
110 // we could have gotten by with n fewer elements
111 // without needing to consult the central free list.
112 // Move toward that situation by releasing n/2 of them.
113 if(n > 0) {
114 if(n > 1)
115 n /= 2;
116 ReleaseN(c, l, n, i);
118 l->nlistmin = l->nlist;
123 void
124 runtime_MCache_ReleaseAll(MCache *c)
126 int32 i;
127 MCacheList *l;
129 for(i=0; i<NumSizeClasses; i++) {
130 l = &c->list[i];
131 ReleaseN(c, l, l->nlist, i);
132 l->nlistmin = 0;