github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/runtime/mcache.c (about) 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. 4 5 // Per-P malloc cache for small objects. 6 // 7 // See malloc.h for an overview. 8 9 #include "runtime.h" 10 #include "arch_GOARCH.h" 11 #include "malloc.h" 12 13 void 14 runtime·MCache_Refill(MCache *c, int32 sizeclass) 15 { 16 MCacheList *l; 17 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"); 25 } 26 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) 30 { 31 MLink *first, **lp; 32 int32 i; 33 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; 42 43 // Return them to central free list. 44 runtime·MCentral_FreeList(&runtime·mheap.central[sizeclass], first); 45 } 46 47 void 48 runtime·MCache_Free(MCache *c, void *v, int32 sizeclass, uintptr size) 49 { 50 MCacheList *l; 51 MLink *p; 52 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; 60 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); 65 } 66 67 void 68 runtime·MCache_ReleaseAll(MCache *c) 69 { 70 int32 i; 71 MCacheList *l; 72 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; 79 } 80 } 81 }