-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalloc.c
369 lines (327 loc) · 12.8 KB
/
alloc.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* area-based allocation built on malloc/free
*/
#include "sh.h"
#define ICELLS 200 /* number of Cells in small Block */
typedef union Cell Cell;
typedef struct Block Block;
/*
* The Cells in a Block are organized as a set of objects.
* Each object (pointed to by dp) begins with the block it is in
* (dp-2)->block, then has a size in (dp-1)->size, which is
* followed with "size" data Cells. Free objects are
* linked together via dp->next.
*/
#define NOBJECT_FIELDS 2 /* the block and size `fields' */
union Cell {
size_t size;
Cell *next;
Block *block;
struct {int _;} junk; /* alignment */
double djunk; /* alignment */
};
struct Block {
Block *next; /* list of Blocks in Area */
Block *prev; /* previous block in list */
Cell *freelist; /* object free list */
Cell *last; /* &b.cell[size] */
Cell cell [1]; /* [size] Cells for allocation */
};
static Block aempty = {&aempty, &aempty, aempty.cell, aempty.cell};
static void ablockfree(Block *, Area *);
static void *asplit(Area *, Block *, Cell *, Cell *, int);
/* create empty Area */
Area *
ainit(Area *ap)
{
ap->freelist = &aempty;
return ap;
}
/* free all object in Area */
void
afreeall(Area *ap)
{
Block *bp;
Block *tmp;
bp = ap->freelist;
if (bp != NULL && bp != &aempty) {
do {
tmp = bp;
bp = bp->next;
free((void*)tmp);
} while (bp != ap->freelist);
ap->freelist = &aempty;
}
}
/* allocate object from Area */
void *
alloc(size_t size, Area *ap)
{
int cells, acells;
Block *bp = 0;
Cell *fp = 0, *fpp = 0;
if (size <= 0)
internal_errorf(1, "allocate bad size");
cells = (unsigned)(size + sizeof(Cell) - 1) / sizeof(Cell);
/* allocate at least this many cells */
acells = cells + NOBJECT_FIELDS;
/*
* Only attempt to track small objects - let malloc deal
* with larger objects. (this way we don't have to deal with
* coalescing memory, or with releasing it to the system)
*/
if (cells <= ICELLS) {
/* find free Cell large enough */
for (bp = ap->freelist; ; bp = bp->next) {
for (fpp = NULL, fp = bp->freelist;
fp != bp->last; fpp = fp, fp = fp->next)
{
if ((fp-1)->size >= cells)
goto Found;
}
/* wrapped around Block list, create new Block */
if (bp->next == ap->freelist) {
bp = 0;
break;
}
}
/* Not much free space left? Allocate a big object this time */
acells += ICELLS;
}
if (bp == 0) {
bp = (Block*) malloc(offsetof(Block, cell) +
acells * sizeof(Cell));
if (bp == NULL)
internal_errorf(1, "cannot allocate");
if (ap->freelist == &aempty) {
ap->freelist = bp->next = bp->prev = bp;
} else {
bp->next = ap->freelist->next;
ap->freelist->next->prev = bp;
ap->freelist->next = bp;
bp->prev = ap->freelist;
}
bp->last = bp->cell + acells;
/* initial free list */
fp = bp->freelist = bp->cell + NOBJECT_FIELDS;
(fp-1)->size = acells - NOBJECT_FIELDS;
(fp-2)->block = bp;
fp->next = bp->last;
fpp = NULL;
}
Found:
return asplit(ap, bp, fp, fpp, cells);
}
/* Do the work of splitting an object into allocated and (possibly) unallocated
* objects. Returns the `allocated' object.
*/
static void *
asplit(Area *ap, Block *bp, Cell *fp, Cell *fpp, int cells)
{
Cell *dp = fp; /* allocated object */
int split = (fp-1)->size - cells;
if (split < 0)
internal_errorf(1, "allocated object too small");
if (split <= NOBJECT_FIELDS) { /* allocate all */
fp = fp->next;
} else { /* allocate head, free tail */
Cell *next = fp->next; /* needed, as cells may be 0 */
ap->freelist = bp; /* next time, start looking for space here */
(fp-1)->size = cells;
fp += cells + NOBJECT_FIELDS;
(fp-1)->size = split - NOBJECT_FIELDS;
(fp-2)->block = bp;
fp->next = next;
}
if (fpp == NULL)
bp->freelist = fp;
else
fpp->next = fp;
return (void*) dp;
}
/* change size of object -- like realloc */
void *
aresize(void *ptr, size_t size, Area *ap)
{
int cells;
Cell *dp = (Cell*) ptr;
int oldcells = dp ? (dp-1)->size : 0;
if (size <= 0)
internal_errorf(1, "allocate bad size");
/* New size (in cells) */
cells = (unsigned)(size - 1) / sizeof(Cell) + 1;
/* Is this a large object? If so, let malloc deal with it
* directly (unless we are crossing the ICELLS border, in
* which case the alloc/free below handles it - this should
* cut down on fragmentation, and will also keep the code
* working (as it assumes size < ICELLS means it is not
* a `large object').
*/
if (oldcells > ICELLS && cells > ICELLS
&& ((dp-2)->block->last == dp+oldcells) && (((Cell*)((dp-2)->block+1))+NOBJECT_FIELDS == dp) /* don't destroy blocks which have grown! */
) {
Block *bp = (dp-2)->block;
Block *nbp;
/* Saved in case realloc fails.. */
Block *next = bp->next, *prev = bp->prev;
if (bp->freelist != bp->last)
internal_errorf(1, "allocation resizing free pointer");
nbp = realloc((void *) bp,
offsetof(Block, cell) +
(cells + NOBJECT_FIELDS) * sizeof(Cell));
if (!nbp) {
/* Have to clean up... */
/* NOTE: If this code changes, similar changes may be
* needed in ablockfree().
*/
if (next == bp) /* only block */
ap->freelist = &aempty;
else {
next->prev = prev;
prev->next = next;
if (ap->freelist == bp)
ap->freelist = next;
}
internal_errorf(1, "cannot re-allocate");
}
/* If location changed, keep pointers straight... */
if (nbp != bp) {
if (next == bp) /* only one block */
nbp->next = nbp->prev = nbp;
else {
next->prev = nbp;
prev->next = nbp;
}
if (ap->freelist == bp)
ap->freelist = nbp;
dp = nbp->cell + NOBJECT_FIELDS;
(dp-2)->block = nbp;
}
(dp-1)->size = cells;
nbp->last = nbp->cell + cells + NOBJECT_FIELDS;
nbp->freelist = nbp->last;
return (void*) dp;
}
/* Check if we can just grow this cell
* (need to check that cells < ICELLS so we don't make an
* object a `large' - that would mess everything up).
*/
if (dp && cells > oldcells) {
Cell *fp, *fpp;
Block *bp = (dp-2)->block;
int need = cells - oldcells - NOBJECT_FIELDS;
/* XXX if we had a flag in an object indicating
* if the object was free/allocated, we could
* avoid this loop (perhaps)
*/
for (fpp = NULL, fp = bp->freelist;
fp != bp->last
&& dp + oldcells + NOBJECT_FIELDS <= fp
; fpp = fp, fp = fp->next)
{
if (dp + oldcells + NOBJECT_FIELDS == fp
&& (fp-1)->size >= need)
{
Cell *np = asplit(ap, bp, fp, fpp, need);
/* May get more than we need here */
(dp-1)->size += (np-1)->size + NOBJECT_FIELDS;
return ptr;
}
}
}
/* Check if we can just shrink this cell
* (if oldcells > ICELLS, this is a large object and we leave
* it to malloc...)
* Note: this also handles cells == oldcells (a no-op).
*/
if (dp && cells <= oldcells) {
int split;
split = oldcells - cells;
if (split <= NOBJECT_FIELDS) /* cannot split */
;
else { /* shrink head, free tail */
Block *bp = (dp-2)->block;
(dp-1)->size = cells;
dp += cells + NOBJECT_FIELDS;
(dp-1)->size = split - NOBJECT_FIELDS;
(dp-2)->block = bp;
afree((void*)dp, ap);
}
/* ACHECK() done in afree() */
return ptr;
}
/* Have to do it the hard way... */
ptr = alloc(size, ap);
if (dp != NULL) {
size_t s = (dp-1)->size * sizeof(Cell);
if (s > size)
s = size;
memcpy(ptr, dp, s);
afree((void *) dp, ap);
}
return ptr;
}
void
afree(void *ptr, Area *ap)
{
Block *bp;
Cell *fp, *fpp;
Cell *dp = (Cell*)ptr;
if (ptr == 0)
internal_errorf(1, "freeing null pointer");
bp = (dp-2)->block;
/* If this is a large object, just free it up... */
/* Release object... */
if ((dp-1)->size > ICELLS
&& (bp->last == dp + (dp-1)->size) && (((Cell*)(bp+1))+NOBJECT_FIELDS == dp) /* don't free non-free blocks which have grown! */
) {
ablockfree(bp, ap);
return;
}
if (dp < &bp->cell[NOBJECT_FIELDS] || dp >= bp->last)
internal_errorf(1, "freeing memory outside of block (corrupted?)");
/* find position in free list */
/* XXX if we had prev/next pointers for objects, this loop could go */
for (fpp = NULL, fp = bp->freelist; fp < dp; fpp = fp, fp = fp->next)
;
if (fp == dp)
internal_errorf(1, "freeing free object");
/* join object with next */
if (dp + (dp-1)->size == fp-NOBJECT_FIELDS) { /* adjacent */
(dp-1)->size += (fp-1)->size + NOBJECT_FIELDS;
dp->next = fp->next;
} else /* non-adjacent */
dp->next = fp;
/* join previous with object */
if (fpp == NULL)
bp->freelist = dp;
else if (fpp + (fpp-1)->size == dp-NOBJECT_FIELDS) { /* adjacent */
(fpp-1)->size += (dp-1)->size + NOBJECT_FIELDS;
fpp->next = dp->next;
} else /* non-adjacent */
fpp->next = dp;
/* If whole block is free (and we have some other blocks
* around), release this block back to the system...
*/
if (bp->next != bp && bp->freelist == bp->cell + NOBJECT_FIELDS
&& bp->freelist + (bp->freelist-1)->size == bp->last
/* XXX and the other block has some free memory? */
)
ablockfree(bp, ap);
}
static void
ablockfree(Block *bp, Area *ap)
{
/* NOTE: If this code changes, similar changes may be
* needed in alloc() (where realloc fails).
*/
if (bp->next == bp) /* only block */
ap->freelist = &aempty;
else {
bp->next->prev = bp->prev;
bp->prev->next = bp->next;
if (ap->freelist == bp)
ap->freelist = bp->next;
}
free((void*) bp);
}