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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
#include <u.h>
#include <base.h>
#include "pool.h"
// -----------------------------------------------------------------------
// globals/macros
#define NOMAGIC 0xDEADFA11u
#define DEADMAGIC 0xDEADDEADu
#define TAILMAGIC0 0xBEu
#define TAILMAGIC1 0xEFu
#define FREEMAGIC 0xBA5EBA11u
#define USEDMAGIC 0x0A110C09u
#define UNUSEDMAGIC (0xCAB00D1Eu+1)
#define COREMAGIC (0xC0A1E5CEu+1)
#define CORETAILMAGIC (0xEC5E1A0Cu+1)
#define ALIGNMAGIC 0xA1F1D1C1u
#define POISON ((void*)0xCAFEBABEu)
// -----------------------------------------------------------------------
// hunk maintenance
#define HUNKTAIL(h) ((Tail*)((uchar*)(h)+(h)->size-sizeof(Tail)))
static uintptr
gethunksize(mem·Pool *pool, uintptr size)
{
size += sizeof(Head) + sizeof(Tail);
if(size < pool->minhunk)
size = pool->minhunk;
size = (size+pool->quantum) & ~(pool->quantum-1); // aligns to quantum
return size;
}
static Head *
sethunksize(Head *h, uintptr size)
{
Tail *t;
assert(h->magic != FREEMAGIC);
h->size = size;
t = HUNKTAIL(h);
t->size = size;
t->magic0 = TAILMAGIC0;
t->magic1 = TAILMAGIC1;
return h;
}
static void
checkhunk(mem·Pool *p, Head *h)
{
switch(h->magic){
default:
abort();
case FREEMAGIC:
case UNUSEDMAGIC:
case DEADMAGIC:
case COREMAGIC:
case CORETAILMAGIC:
case USEDMAGIC:
;
}
}
// -----------------------------------------------------------------------
// core maintenance
#define COREHEAD(c) ((Head*)((uchar*)(c)+(c)->len -sizeof(Head)))
static uintptr
getcoresize(mem·Pool *pool, uintptr size)
{
size += sizeof(Core) + sizeof(Tail);
if(size < pool->mincore)
size = pool->mincore;
size = (size+pool->quantum) & ~(pool->quantum-1);
return size;
}
static Core *
setcoresize(Core *c, uintptr size)
{
Head *h;
c->len = size;
h = COREHEAD(c);
h->size = 0;
h->magic = CORETAILMAGIC;
return c;
}
static void
newcore(mem·Pool *pool, uintptr size)
{
Core *c;
// XXX: check for size overflow
if(!(c = pool->mem.alloc(pool->heap, 1, size)))
return;
pool->cursize += size;
/* core header */
c->magic = COREMAGIC;
sethunksize((Head*)c, sizeof(Core));
setcoresize(c, size);
checkhunk(pool, (Head*)c);
}
// -----------------------------------------------------------------------
// free list maintenance
static Free *
findgreater(Free *root, uintptr size)
{
Free *last;
last = nil;
for(;;){
if(!root)
return last;
if(size == root->size)
return root;
if(size < root->size){
last = root;
root = root->left;
}else
root = root->right;
}
}
// -----------------------------------------------------------------------
// exported functions
/* constructors / destructors */
mem·Pool*
mem·makepool(mem·Allocator mem, void *heap, char *name, int flags, intptr maxsize, intptr mincore)
{
mem·Pool *pool = mem.alloc(heap, 1, sizeof(*pool));
mem·set(pool, sizeof(*pool), 0);
pool->mem = mem;
pool->heap = heap;
pool->name = name;
pool->flags = flags;
pool->mincore = mincore;
pool->maxsize = maxsize;
return pool;
}
void
mem·freepool(mem·Pool *pool)
{
void *heap = pool->heap;
mem·Allocator mem = pool->mem;
mem.free(heap, pool);
}
/* base functions */
void *
mem·poolalloc(mem·Pool *pool, long n, uintptr eltsize)
{
Free *node;
uintptr size, bksz;
if((size = n*eltsize) >= 0x80000000UL){
errorf("allocation overflow");
return nil;
}
bksz = gethunksize(pool, size);
node = findgreater(pool->free, bksz);
if(!node){
newcore(pool, getcoresize(pool, bksz));
if(!(node = findgreater(pool->free, bksz)))
return nil;
}
return nil;
}
|