-
Notifications
You must be signed in to change notification settings - Fork 1
/
mm-implicit.c
497 lines (418 loc) · 14.3 KB
/
mm-implicit.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* mm.c
*
* Jack Kasbeer (jkasbeer)
* July 14, 2015
*
* Simple, 32-bit and 64-bit clean allocator based on IMPLICIT free
* lists, NEXT FIT placement, and boundary tag coalescing, as described
* in the CS:APP2e text. Also note that colescing occurs once we've run
* out of space.Blocks must be aligned to doubleword (8 byte) boundaries.
* Minimum block size is 16 bytes.
*
* Passes all traces.
* $Perfindex = 38(util) + 11(thru)
* = 49
*/
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "mm.h"
#include "memlib.h"
/* If you want debugging output, use the following macro. When you hand
* in, remove the #define DEBUG line. */
#define DEBUG
#ifdef DEBUG
# define dbg_printf(...) printf(__VA_ARGS__)
#else
# define dbg_printf(...)
#endif
/* do not change the following! */
#ifdef DRIVER
/* create aliases for driver tests */
#define malloc mm_malloc
#define free mm_free
#define realloc mm_realloc
#define calloc mm_calloc
#endif /* def DRIVER */
/*
* If NEXT_FIT defined use next fit search, else use first fit search
*/
#define NEXT_FIT
/* Basic constants and macros */
#define WSIZE 4 /* Word and header/footer size (bytes) */
#define DSIZE 8 /* Doubleword size (bytes) */
#define ALIGNMENT 8
#define CHUNKSIZE (1<<12) /* Extend heap by this amount (bytes) */
/* Determine larger variable, given two */
#define MAX(x, y) ((x) > (y)? (x) : (y))
/* Rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(p) (((size_t)(p) + (ALIGNMENT-1)) & ~0x7)
/* Pack a size and allocated bit into a word */
#define PACK(size, alloc) ((size) | (alloc))
/* Read and write a word at address p */
#define GET(p) (*(unsigned int *)(p))
#define PUT(p, val) (*(unsigned int *)(p) = (val))
/* Read the size and allocated fields from address p */
#define GET_SIZE(p) (GET(p) & ~0x7)
#define GET_ALLOC(p) (GET(p) & 0x1)
/* NOTE!
* Header: [ MSB's(29) | LSB's(3) ]
* Footer: [ MSB's(29) | LSB's(3) ] (identical)
* least significant byte of header: allocate?.. most significant bytes: size
*/
/* ## IMPLICIT FREE LIST MACROS ## */
/* Given block ptr bp, compute address of its header and footer */
#define HDRP(bp) ((char *)(bp) - WSIZE)
#define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE)
/* NOTE!
* Block: [ HDR | PAYLOAD | FTR ]
* HDRP: header is word away from block content
* FTRP: add size (includes hdr & ftr), then subtract hdr+ftr size
*/
/* Given block ptr bp, compute address of next and previous blocks */
#define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE)))
#define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE)))
/* Global variables */
static char *heap_listp = 0; /* Pointer to first block */
static char *og_heap_listp = 0;
#ifdef NEXT_FIT
static char *traveler; /* Next fit traveler pointer */
#endif
/* Function prototypes for internal helper routines */
static void *extend_heap(size_t words); /* Additional heap ext. function */
static void place(void *ptr, size_t asize); /* Text book exercise: place block */
static void *find_fit(size_t asize); /* Test book exercise: search for place to PUT */
static void *coalesce(void *ptr); /* Merge free blocks */
/* Function prototypes for debugging routines */
static void myheapcheck();
static void mylistcheck();
static void printb(void *blok);
static void checkb(void *blok);
static int in_heap(const void *p);
static int aligned(const void *p);
/* NOTE: change header and footer size to much smaller */
/* NOTE: mem_sbrk(0) will return the current location of the program brk */
/*
* mm_init - initialize an empty heap: return -1 on error, 0 on success.
*/
int mm_init(void)
{
/* Check for error then initialize empty heap */
if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1)
return -1;
/* Set up the heap with padding, prologue, and epilogue */
PUT(heap_listp, 0); /* Alignment padding */
PUT(heap_listp + (1*WSIZE), PACK(DSIZE, 1)); /* Prologue header */
PUT(heap_listp + (2*WSIZE), PACK(DSIZE, 1)); /* Prologue footer */
PUT(heap_listp + (3*WSIZE), PACK(0, 1)); /* Epilogue header */
/* Minor optimization: move heap_listp to first block after HDR */
heap_listp += (4*WSIZE);
og_heap_listp = heap_listp - (2*WSIZE); /* keep track of prologue */
/****** NEXT FIT IMPL. ******/
#ifdef NEXT_FIT
/* Next fit ==> initialize traveler */
traveler = heap_listp;
#endif
/****************************/
/* Extend the empty heap with a free block of CHUNKSIZE bytes */
if (extend_heap(CHUNKSIZE/WSIZE) == NULL)
return -1; //failure
assert(in_heap(heap_listp));
assert(aligned(heap_listp));
return 0;
}
/*
* malloc - memory allocation.. allocate requested size (bytes)
*/
void *malloc(size_t size)
{
size_t asize; /* Adjusted block size */
size_t extendsize; /* Amount to extend heap if no fit */
char *bp;
/* Specific case checks -- */
if (heap_listp == 0) mm_init();
if (size == 0) return NULL;
/* Typical malloc cases -- */
if (size <= DSIZE) /* less than a double, easy */
asize = 2*DSIZE; /* 8 bytes: hdr & ftr, alloc space: <= 8 bytes */
else /* calc how many dwords needed */
asize = DSIZE * ((size + DSIZE + (DSIZE - 1)) / DSIZE);
/* Search for a fit.. return immediately if found */
if ((bp = find_fit(asize)) != NULL) {
place(bp, asize);
return bp;
} /* No fit found ==> need more heap! */
extendsize = MAX(asize,CHUNKSIZE);
if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
return NULL;
/* Allocate space in memory before returning */
place(bp, asize);
return bp;
}
/*
* free - free block of memory starting at bp
*/
void free(void *bp)
{
/* Specific case checks -- */
if (bp == 0) return;
if (heap_listp == 0) mm_init();
size_t size = GET_SIZE(HDRP(bp));
PUT(HDRP(bp), PACK(size, 0));
PUT(FTRP(bp), PACK(size, 0)); // FTRP!
coalesce(bp);
}
/*
* coalesce - Boundary tag coalescing; return ptr to coalesced block
*/
static void *coalesce(void *bp)
{
size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))); // FTRP!
size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
size_t size = GET_SIZE(HDRP(bp));
if (prev_alloc && next_alloc) /* Case 1 */
return bp;
else if (prev_alloc && !next_alloc) { /* Case 2 */
size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
PUT(HDRP(bp), PACK(size, 0));
PUT(FTRP(bp), PACK(size,0)); // FTRP!
}
else if (!prev_alloc && next_alloc) { /* Case 3 */
size += GET_SIZE(HDRP(PREV_BLKP(bp)));
PUT(FTRP(bp), PACK(size, 0)); // FTRP!
PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
bp = PREV_BLKP(bp);
}
else { /* Case 4 */
size += GET_SIZE(HDRP(PREV_BLKP(bp))) +
GET_SIZE(FTRP(NEXT_BLKP(bp))); // FTRP!
PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0)); // FTRP!
bp = PREV_BLKP(bp);
}
/****** NEXT FIT IMPL. ******/
#ifdef NEXT_FIT
/* Make sure the traveler isn't pointing into the free block
* that we just coalesced */
if ((traveler > (char *)bp) && (traveler < NEXT_BLKP(bp)))
traveler = bp;
#endif
/****************************/
return bp;
}
/*
* realloc - alias for mm_realloc
*/
void *realloc(void *oldptr, size_t size)
{
size_t oldsize;
void *newptr;
/* Special case checks -- */
if (size == 0) {
free(oldptr);
return 0;
}
if (oldptr == NULL)
return malloc(size);
/* Attempt to malloc -- */
newptr = malloc(size);
if (!newptr) return 0;
/* Copy the old data if malloc successful -- */
oldsize = GET_SIZE(HDRP(oldptr));
if (size < oldsize) oldsize = size;
memcpy(newptr, oldptr, oldsize);
/* Free the old block. */
free(oldptr);
return newptr;
}
/****************************
* HELPER FUNCTIONS FROM TEXT
****************************/
/*
* extend_heap - Extend heap with free block and return its block pointer
*/
static void *extend_heap(size_t words)
{
char *bp;
size_t size;
/* Allocate an even number of words to maintain alignment */
size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
if ((long)(bp = mem_sbrk(size)) == -1)
return NULL; /* if heap extension fails, return NULL ptr */
/* Initialize free block header/footer and the epilogue header */
PUT(HDRP(bp), PACK(size, 0)); /* Free block header */
PUT(FTRP(bp), PACK(size, 0)); /* Free block footer */ // FTRP!
PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */
/* Coalesce if the previous block was free */
return coalesce(bp);
}
/*
* place - Place block of asize bytes at start of free block bp
* and split if remainder would be at least minimum block size
*/
static void place(void *bp, size_t asize)
{
size_t csize = GET_SIZE(HDRP(bp));
u_int pack_it, pack_em;
if ((csize - asize) >= (2*DSIZE)) { pack_it = PACK(asize, 1);
pack_em = PACK(csize-asize, 0);
PUT(HDRP(bp), pack_it);
PUT(FTRP(bp), pack_it); // FTRP!
bp = NEXT_BLKP(bp);
PUT(HDRP(bp), pack_em);
PUT(FTRP(bp), pack_em); // FTRP!
}
else { pack_it = PACK(csize, 1);
PUT(HDRP(bp), pack_it);
PUT(FTRP(bp), pack_it); // FTRP!
}
}
/*
* find_fit - Find a fit for a block with asize bytes
*/
static void *find_fit(size_t asize)
{
/* Next fit search */
#ifdef NEXT_FIT
char *oldtraveler = traveler;
/* Search from the traveler to the end of list */
for ( ; GET_SIZE(HDRP(traveler)) > 0; traveler = NEXT_BLKP(traveler)) {
if (!GET_ALLOC(HDRP(traveler)) && (asize <= GET_SIZE(HDRP(traveler))))
return traveler;
}
/* Search from start of list to old traveler */
for (traveler = heap_listp; traveler < oldtraveler; traveler = NEXT_BLKP(traveler)) {
if (!GET_ALLOC(HDRP(traveler)) && (asize <= GET_SIZE(HDRP(traveler))))
return traveler;
}
return NULL; /* no fit found */
/* First fit search */
#else
void *bp;
for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
if (!GET_ALLOC(HDRP(bp)) && (asize <= GET_SIZE(HDRP(bp))))
return bp;
}
return NULL; /* No fit */
#endif
}
/*********************
* DEBUGGING FUNCTIONS
*********************/
/*
* Return whether the pointer is in the heap.
* May be useful for debugging.
*/
static int in_heap(const void *p) {
return p <= mem_heap_hi() && p >= mem_heap_lo();
}
/*
* Return whether the pointer is aligned.
* May be useful for debugging.
*/
static int aligned(const void *p) {
return (size_t)ALIGN(p) == (size_t)p;
}
/************************************************************************
******* MM_CHECKHEAP - check all aspects of heap & [NOT FINISHED]
************************************************************************/
void mm_checkheap(int lineno) {
/* This routine is always verbose */
//printf("LINE: %d\n", lineno);
printf("################### HEAP ################### \n");
myheapcheck();
mylistcheck(); // currently does nothing
printf("############################################ \n\n");
lineno = lineno;
}
/*****************************
*mm_checkheap helper routines
*****************************/
/* IMPLICIT, EXPLICIT, AND SEGREGATED FREE LIST
* myheapcheck - Check all essential aspects of heap
* 1. check epilogue & prologue blocks
* 2. check each block's address alignment
* 3. check heap boundaries
* 4. check each block's header & footer (agreement)
* 5. check coalescing: no two consecutive free blocks in the heap (FINISH!)
*/
static void myheapcheck()
{
char *bp = og_heap_listp;
/* Check prologue block */
if ((GET_SIZE(HDRP(og_heap_listp)) != DSIZE) || !GET_ALLOC(HDRP(og_heap_listp))) {
printf("Bad prologue header\n");
}
checkb(og_heap_listp);
/* Check all blocks in heap */
for (bp = og_heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
printb(bp);
if (bp == og_heap_listp) { /* check heap start boundary */
if ((bp-DSIZE) != mem_heap_lo())
printf("Bad heap start\n");
} /* check for consecutive free blocks */
if ((GET_ALLOC(HDRP(bp)) == 0) && (GET_ALLOC(HDRP(NEXT_BLKP(bp)))) == 0)
printf("Bad coalescing\n");
checkb(bp);
}
/* Check epilogue block */
printb(bp);
if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp))))
printf("Bad epilogue header\n");
if ((bp-1) != mem_heap_hi()) /* check heap end boundary */
printf("Bad heap end\n");
}
/* EXPLICIT & SEGREGATED FREE LIST
* mylistcheck - Checking essential aspects of free list
* 1. all next/previous pointers are consistent
* 2. all free list pointers between mem_heap_lo() & mem_heap_high()
* 3. free block count thru entire heap == free block count thru
* free list
* 4. SEGREGATED ONLY: all blocks in each list bucket fall w/in
* bucket size range
*/
static void mylistcheck()
{
/* UNIMPLIMENTED */
}
/************************************
* Private heap check helper routines
************************************/
/*
* printb - Print header & footer of bp
* Useful helper function for debugging.
*/
static void printb(void *bp)
{
size_t hsize, halloc; /* Header bit encodings */
size_t fsize, falloc; /* Footer bit encodings */
hsize = GET_SIZE(HDRP(bp));
halloc = GET_ALLOC(HDRP(bp));
fsize = GET_SIZE(FTRP(bp)); // FTRP!
falloc = GET_ALLOC(FTRP(bp)); // FTRP!
if (hsize == 0) { /* if the block size is 0, end of heap */
printf("%c: header: [%u:%c] ",(halloc ? 'A' : 'F'),
(u_int)hsize, (halloc ? 'a' : 'f'));
printf("(EOL @ %p)\n", bp);
return;
}
/* Useful debugging info.. */
printf("%c: header: [%u:%c] footer: [%u:%c]\n",
(halloc ? 'A' : 'F'),
(u_int)hsize, (halloc ? 'a' : 'f'),
(u_int)fsize, (falloc ? 'a' : 'f'));
}
/*
* checkb - alignment & header=footer check on bp
* Helper function for checkheap.
*/
static void checkb(void *bp)
{
if (!aligned(bp)) /* Check block alignment */
printf("Error: %p is not doubleword aligned\n", bp);
if (GET(HDRP(bp)) != GET(FTRP(bp))) /* Check header & footer agreement */ // FTRP!
printf("Error: header does not match footer\n");
}