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.
7 // See malloc.h for an overview.
14 runtime_MCache_Refill(MCache
*c
, int32 sizeclass
)
18 // Replenish using central lists.
19 l
= &c
->list
[sizeclass
];
21 runtime_throw("MCache_Refill: the list is not empty");
22 l
->nlist
= runtime_MCentral_AllocList(&runtime_mheap
.central
[sizeclass
], &l
->list
);
24 runtime_throw("out of memory");
27 // Take n elements off l and return them to the central free list.
29 ReleaseN(MCacheList
*l
, int32 n
, int32 sizeclass
)
34 // Cut off first n elements.
43 // Return them to central free list.
44 runtime_MCentral_FreeList(&runtime_mheap
.central
[sizeclass
], first
);
48 runtime_MCache_Free(MCache
*c
, void *v
, int32 sizeclass
, uintptr size
)
54 l
= &c
->list
[sizeclass
];
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
);
68 runtime_MCache_ReleaseAll(MCache
*c
)
73 for(i
=0; i
<NumSizeClasses
; i
++) {
76 runtime_MCentral_FreeList(&runtime_mheap
.central
[i
], l
->list
);