1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Per-P malloc cache for small objects.
//
// See malloc.h for an overview.
#include "runtime.h"
#include "arch_GOARCH.h"
#include "malloc.h"
void
runtime·MCache_Refill(MCache *c, int32 sizeclass)
{
MCacheList *l;
// Replenish using central lists.
l = &c->list[sizeclass];
if(l->list)
runtime·throw("MCache_Refill: the list is not empty");
l->nlist = runtime·MCentral_AllocList(&runtime·mheap.central[sizeclass], &l->list);
if(l->list == nil)
runtime·throw("out of memory");
}
// Take n elements off l and return them to the central free list.
static void
ReleaseN(MCacheList *l, int32 n, int32 sizeclass)
{
MLink *first, **lp;
int32 i;
// Cut off first n elements.
first = l->list;
lp = &l->list;
for(i=0; i<n; i++)
lp = &(*lp)->next;
l->list = *lp;
*lp = nil;
l->nlist -= n;
// Return them to central free list.
runtime·MCentral_FreeList(&runtime·mheap.central[sizeclass], first);
}
void
runtime·MCache_Free(MCache *c, void *v, int32 sizeclass, uintptr size)
{
MCacheList *l;
MLink *p;
// Put back on list.
l = &c->list[sizeclass];
p = v;
p->next = l->list;
l->list = p;
l->nlist++;
c->local_cachealloc -= size;
// We transfer span at a time from MCentral to MCache,
// if we have 2 times more than that, release a half back.
if(l->nlist >= 2*(runtime·class_to_allocnpages[sizeclass]<<PageShift)/size)
ReleaseN(l, l->nlist/2, sizeclass);
}
void
runtime·MCache_ReleaseAll(MCache *c)
{
int32 i;
MCacheList *l;
for(i=0; i<NumSizeClasses; i++) {
l = &c->list[i];
if(l->list) {
runtime·MCentral_FreeList(&runtime·mheap.central[i], l->list);
l->list = nil;
l->nlist = 0;
}
}
}
|