OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [runtime/] [mcache.c] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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-thread (in Go, per-M) malloc cache for small objects.
6
//
7
// See malloc.h for an overview.
8
 
9
#include "runtime.h"
10
#include "arch.h"
11
#include "malloc.h"
12
 
13
void*
14
runtime_MCache_Alloc(MCache *c, int32 sizeclass, uintptr size, int32 zeroed)
15
{
16
        MCacheList *l;
17
        MLink *first, *v;
18
        int32 n;
19
 
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;
31
        }
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;
38
 
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;
50
                }
51
        }
52
        c->local_cachealloc += size;
53
        c->local_objects++;
54
        return v;
55
}
56
 
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)
60
{
61
        MLink *first, **lp;
62
        int32 i;
63
 
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];
75
 
76
        // Return them to central free list.
77
        runtime_MCentral_FreeList(&runtime_mheap.central[sizeclass], n, first);
78
}
79
 
80
void
81
runtime_MCache_Free(MCache *c, void *v, int32 sizeclass, uintptr size)
82
{
83
        int32 i, n;
84
        MCacheList *l;
85
        MLink *p;
86
 
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--;
96
 
97
        if(l->nlist >= MaxMCacheListLen) {
98
                // Release a chunk back.
99
                ReleaseN(c, l, runtime_class_to_transfercount[sizeclass], sizeclass);
100
        }
101
 
102
        if(c->size >= MaxMCacheSize) {
103
                // Scavenge.
104
                for(i=0; i<NumSizeClasses; i++) {
105
                        l = &c->list[i];
106
                        n = l->nlistmin;
107
 
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);
117
                        }
118
                        l->nlistmin = l->nlist;
119
                }
120
        }
121
}
122
 
123
void
124
runtime_MCache_ReleaseAll(MCache *c)
125
{
126
        int32 i;
127
        MCacheList *l;
128
 
129
        for(i=0; i<NumSizeClasses; i++) {
130
                l = &c->list[i];
131
                ReleaseN(c, l, l->nlist, i);
132
                l->nlistmin = 0;
133
        }
134
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.