/
myAllocatorNextFit.c
307 lines (264 loc) · 9.3 KB
/
myAllocatorNextFit.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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include "myAllocator.h"
/*
This is a simple endogenous first-fit allocator.
Each allocated memory region is sandwiched between a "BlockPrefix"
and a "BlockSuffix". All block info is stored in its BlockPrefix.
A block b's BlockSuffix is used by b's successor block to determine
the address of b's prefix. Prefixes' & Suffixes' sizes are rounded
up to the next multiple of 8 bytes (see prefixSize, suffixSize,
align8()). Therefore a block must be at least of size
prefixSize+suffixSize. The method makeFreeBlock() fills in a prefix
& suffix within a region, and marks it as free (->allocated=0).
Such a block can be marked as allcated by setting its "allocated"
field. The usable space between a block's prefix &
suffix (extent - (prefixSize+suffixSize) is computed by
usableSpace().
All blocks are allocated from an arena extending from arenaBegin to
arenaEnd. In particular, the first block's prefix is at address
arenaBegin, and the last block's suffix is at address
arenaEnd-suffixSize.
This allocator generally refers to a block by the address of its
prefix. The address of the prefix to block b's successor is the
address of b's suffix + suffixSize, and the address of block b's
predecessor's suffix is the address of b's prefix -
suffixSize. See computeNextPrefixAddress(),
computePrevSuffixAddr(), getNextPrefix(), getPrefPrefix().
The method findFirstFit() searches the arena for a sufficiently
large free block. Adjacent free blocks can be coalesced: See
coalescePrev(), coalesce().
Functions regionToBlock() and blockToRegion() convert between
prefixes & the first available address within the block.
FindFirstAllocRegion() uses findFirstFit to locate a suffiently
large unallocated bock. This block will be split if it contains
sufficient excess space to create another free block. FreeRegion
marks the region's allocated block as free and attempts to coalesce
it with its neighbors.
*/
/* align everything to multiples of 8 */
#define align8(x) ((x+7) & ~7)
#define prefixSize align8(sizeof(BlockPrefix_t))
#define suffixSize align8(sizeof(BlockSuffix_t))
/* how much memory to ask for */
const size_t DEFAULT_BRKSIZE = 0x100000; /* 1M */
/*Pointer to current block*/
BlockPrefix_t *mostRecentBlock;
/* create a block, mark it as free */
BlockPrefix_t *makeFreeBlock(void *addr, size_t size) {
BlockPrefix_t *p = addr;
void *limitAddr = addr + size;
BlockSuffix_t *s = limitAddr - align8(sizeof(BlockSuffix_t));
p->suffix = s;
s->prefix = p;
p->allocated = 0;
return p;
}
/* lowest & highest address in arena (global vars) */
BlockPrefix_t *arenaBegin = (void *)0;
void *arenaEnd = 0;
void initializeArena() {
if (arenaBegin != 0) /* only initialize once */
return;
arenaBegin = makeFreeBlock(sbrk(DEFAULT_BRKSIZE), DEFAULT_BRKSIZE);
arenaEnd = ((void *)arenaBegin) + DEFAULT_BRKSIZE;
}
size_t computeUsableSpace(BlockPrefix_t *p) { /* useful space within a block */
void *prefix_end = ((void*)p) + prefixSize;
return ((void *)(p->suffix)) - (prefix_end);
}
BlockPrefix_t *computeNextPrefixAddr(BlockPrefix_t *p) {
return ((void *)(p->suffix)) + suffixSize;
}
BlockSuffix_t *computePrevSuffixAddr(BlockPrefix_t *p) {
return ((void *)p) - suffixSize;
}
BlockSuffix_t *computeNextSuffixAddr(BlockPrefix_t *p) {
return ((void *)p) + suffixSize;
}
BlockPrefix_t *getNextPrefix(BlockPrefix_t *p) { /* return addr of next block (prefix), or 0 if last */
BlockPrefix_t *np = computeNextPrefixAddr(p);
if ((void*)np < (void *)arenaEnd)
return np;
else
return (BlockPrefix_t *)0;
}
BlockPrefix_t *getPrevPrefix(BlockPrefix_t *p) { /* return addr of prev block, or 0 if first */
BlockSuffix_t *ps = computePrevSuffixAddr(p);
if ((void *)ps > (void *)arenaBegin)
return ps->prefix;
else
return (BlockPrefix_t *)0;
}
BlockPrefix_t *coalescePrev(BlockPrefix_t *p) { /* coalesce p with prev, return prev if coalesced, otherwise p */
BlockPrefix_t *prev = getPrevPrefix(p);
if (p && prev && (!p->allocated) && (!prev->allocated)) {
makeFreeBlock(prev, ((void *)computeNextPrefixAddr(p)) - (void *)prev);
return prev;
}
return p;
}
void coalesce(BlockPrefix_t *p) { /* coalesce p with prev & next */
if (p != (void *)0) {
BlockPrefix_t *next;
p = coalescePrev(p);
next = getNextPrefix(p);
if (next)
coalescePrev(next);
}
}
int growingDisabled = 1; /* true: don't grow arena! (needed for cygwin) */
BlockPrefix_t *growArena(size_t s) { /* this won't work under cygwin since runtime uses brk()!! */
void *n;
BlockPrefix_t *p;
if (growingDisabled)
return (BlockPrefix_t *)0;
s += (prefixSize + suffixSize);
if (s < DEFAULT_BRKSIZE)
s = DEFAULT_BRKSIZE;
n = sbrk(s);
if ((n == 0) || (n != arenaEnd)) /* fail if brk moved or failed! */
return 0;
arenaEnd = n + s; /* new end */
p = makeFreeBlock(n, s); /* create new block */
p = coalescePrev(p); /* coalesce with old arena end */
return p;
}
int pcheck(void *p) { /* check that pointer is within arena */
return (p >= (void *)arenaBegin && p < (void *)arenaEnd);
}
void arenaCheck() { /* consistency check */
BlockPrefix_t *p = arenaBegin;
size_t amtFree = 0, amtAllocated = 0;
int numBlocks = 0;
while (p != 0) { /* walk through arena */
fprintf(stderr, " checking from %p, size=%8zd, allocated=%d...\n",
p, computeUsableSpace(p), p->allocated);
assert(pcheck(p)); /* p must remain within arena */
assert(pcheck(p->suffix)); /* suffix must be within arena */
assert(p->suffix->prefix == p); /* suffix should reference prefix */
if (p->allocated) /* update allocated & free space */
amtAllocated += computeUsableSpace(p);
else
amtFree += computeUsableSpace(p);
numBlocks += 1;
p = computeNextPrefixAddr(p);
if (p == arenaEnd) {
break;
} else {
assert(pcheck(p));
}
}
fprintf(stderr,
" mcheck: numBlocks=%d, amtAllocated=%zdk, amtFree=%zdk, arenaSize=%zdk\n",
numBlocks,
(size_t)amtAllocated / 1024LL,
(size_t)amtFree/1024LL,
((size_t)arenaEnd - (size_t)arenaBegin) / 1024);
}
BlockPrefix_t *findNextFit(size_t s) { /* find first block with usable space > s */
BlockPrefix_t *p = arenaBegin;
while (p) {
if (!p->allocated && computeUsableSpace(p) >= s){
mostRecentBlock = p;
return p;
}
p = getNextPrefix(mostRecentBlock);
mostRecentBlock = p;
}
return growArena(s);
}
/* conversion between blocks & regions (offset of prefixSize */
BlockPrefix_t *regionToPrefix(void *r) {
if (r)
return r - prefixSize;
else
return 0;
}
void *prefixToRegion(BlockPrefix_t *p) {
void * vp = p;
if (p)
return vp + prefixSize;
else
return 0;
}
/* these really are equivalent to malloc & free */
void *nextFitAllocRegion(size_t s) {
size_t asize = align8(s);
BlockPrefix_t *p;
if (arenaBegin == 0) /* arena uninitialized? */
initializeArena();
p = findNextFit(s); /* find a block */
if (p) { /* found a block */
size_t availSize = computeUsableSpace(p);
if (availSize >= (asize + prefixSize + suffixSize + 8)) { /* split block? */
void *freeSliverStart = (void *)p + prefixSize + suffixSize + asize;
void *freeSliverEnd = computeNextPrefixAddr(p);
makeFreeBlock(freeSliverStart, freeSliverEnd - freeSliverStart);
makeFreeBlock(p, freeSliverStart - (void *)p); /* piece being allocated */
}
p->allocated = 1; /* mark as allocated */
return prefixToRegion(p); /* convert to *region */
} else { /* failed */
return (void *)0;
}
}
void freeRegion(void *r) {
if (r != 0) {
BlockPrefix_t *p = regionToPrefix(r); /* convert to block */
p->allocated = 0; /* mark as free */
coalesce(p);
}
}
/*
like realloc(r, newSize), resizeRegion will return a new region of size
newSize containing the old contents of r by:
1. checking if the present region has sufficient available space to
satisfy the request (if so, do nothing)
2. allocating a new region of sufficient size & copying the data
TODO: if the successor 's' to r's block is free, and there is sufficient space
in r + s, then just adjust sizes of r & s.
*/
void *resizeRegion(void *r, size_t newSize) {
int oldSize;
// int nextPSize;
// size_t oldAndNew;
BlockPrefix_t *p = regionToPrefix(r);
BlockPrefix_t *nextp = getNextPrefix(p);
int nextpNum = getNextPrefix(p);
BlockPrefix_t *nexts;
if (r != (void *)0){ /* old region existed */
oldSize = computeUsableSpace(regionToPrefix(r));
}
else
oldSize = 0; /* non-existant regions have size 0 */
if (oldSize >= newSize) /* old region is big enough */
return r;
// else if(nextpNum != 0){
// size_t nextPSize=computeUsableSpace(nextp);
// size_t oldAndNext=nextPSize+oldSize;
// if(oldAndNext>=align8(newSize)){
// // size_t asize = align8(newSize);
// // size_t availSize = computeUsableSpace(p);
// if (oldAndNext >= (align8(newSize) + prefixSize + suffixSize + 8)) { /* split block? */
// void *freeSliverStart = (void *)p + prefixSize + suffixSize + align8(newSize);
// void *freeSliverEnd = computeNextPrefixAddr(nextp);
// makeFreeBlock(freeSliverStart, freeSliverEnd - freeSliverStart);
// makeFreeBlock(p, freeSliverStart - (void *)p); /* piece being allocated */
// }
// p->allocated = 1; /* mark as allocated */
// return prefixToRegion(p); /* convert to *region */
// }
// }
else { /* allocate new region & copy old data */
char *o = (char *)r; /* treat both regions as char* */
char *n = (char *)nextFitAllocRegion(newSize);
int i;
for (i = 0; i < oldSize; i++) /* copy byte-by-byte, should use memcpy */
n[i] = o[i];
freeRegion(o); /* free old region */
return (void *)n;
}
}