-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocator_magazine.c
148 lines (128 loc) · 4.04 KB
/
allocator_magazine.c
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (c) 2002 IBM Corporation.
* Copyright (c) 2005 Tom Hart;
*/
#include "test.h"
#include "node.h"
#include "spinlock.h"
#include "util.h"
#include <stdio.h> /* for stderr() */
#include <string.h> /* for memset() */
#define PT_FREELIST_TARGET 100
/* Push this magazine onto the global "stack" of magazines. */
static void kfree_gbl(node_t *p)
{
node_t *old;
do {
old = tg->global_freelist;
p->next = old;
write_barrier();
} while (!CAS(&tg->global_freelist, old, p));
}
/* Pop a magaine off the global "stack". */
static node_t * kmalloc_gbl()
{
node_t *p;
node_t *next;
do {
p = tg->global_freelist;
if (p == NULL) {
atomic_xadd4(&tg->out_of_memory, 1);
return NULL; /* Out of memory. */
}
next = p->next;
} while (!CAS(&tg->global_freelist, p, next));
return p;
}
void free_node(node_t *p)
{
node_t *q;
p->next = (node_t*)0x00300300;
if (++(this_thread()->freelist_count) > PT_FREELIST_TARGET) {
if (this_thread()->freelist2 != NULL) {
q = this_thread()->freelist2;
this_thread()->freelist2 = NULL;
kfree_gbl(q);
}
this_thread()->freelist2 = this_thread()->freelist;
this_thread()->freelist = NULL;
this_thread()->freelist_count = 1;
}
p->mr_next = this_thread()->freelist;
this_thread()->freelist = p;
}
node_t *new_node()
{
node_t *p;
/* Make sure freelist has nodes. */
if (this_thread()->freelist == NULL) {
if (this_thread()->freelist2 != NULL) {
this_thread()->freelist = this_thread()->freelist2;
this_thread()->freelist2 = NULL;
} else {
p = kmalloc_gbl();
if (p == NULL) {
return (NULL);
}
this_thread()->freelist = p;
}
this_thread()->freelist_count = PT_FREELIST_TARGET;
}
/* We're really out of memory. */
if (this_thread()->freelist == NULL) {
return NULL;
}
/* Fast path should go straight to this. Pop from freelist. */
p = this_thread()->freelist;
this_thread()->freelist = this_thread()->freelist->mr_next;
this_thread()->freelist_count--;
/* Clear memory before returning it. */
memset(p, 0, sizeof(node_t));
return (p);
}
void init_allocator()
{
int i;
int j;
int nelementsfree;
node_t *shdmem;
tg->out_of_memory = 0;
nelementsfree = tg->nelements
+ (2 * MAX_THREADS * PT_FREELIST_TARGET)
+ (MAX_THREADS * 64000);
/* Initialize the global freelist. */
tg->global_freelist = NULL;
/* Initialize the per-threads data. "+1" because of the parent.*/
for (i = 0; i < MAX_THREADS + 1; i++) {
get_thread(i)->freelist = NULL;
get_thread(i)->freelist2 = NULL;
get_thread(i)->freelist_count = 0.;
}
/* Allocate a huge chunk of memory. */
shdmem = (node_t*)mapmem(nelementsfree * sizeof(node_t));
if (shdmem == NULL) {
fprintf(stderr, "init_allocator(): Out of memory\n");
exit(-1);
}
/* Initialize the nodes and place them in the freelists. */
for (i = 0; i < nelementsfree; i++) {
shdmem[i].key = -1;
shdmem[i].next = NULL;
shdmem[i].mr_next = NULL;
free_node(&shdmem[i]);
}
}