-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmm96.c
508 lines (422 loc) · 11.8 KB
/
mm96.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* mm-naive.c - The fastest, least memory-efficient malloc package.
*
* In this naive approach, a block is allocated by simply incrementing
* the brk pointer. A block is pure payload. There are no headers or
* footers. Blocks are never coalesced or reused. Realloc is
* implemented directly using mm_malloc and mm_free.
*
* NOTE TO STUDENTS: Replace this header comment with your own header
* comment that gives a high level description of your solution.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include "mm.h"
#include "memlib.h"
/* single word (4) or double word (8) alignment */
#define ALIGNMENT 8
/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
#define SIZE_T_SIZE (ALIGN(sizeof(size_t))) /* SIZE_T_SIZE = 8 */
/* ###################################################################### */
/* Basic constants and macros */
#define WSIZE 4
#define DSIZE 8 /* Double word size(bytes) */
#define CHUNKSIZE (480) /* Extend heap by this amount(bytes), 4096*/
#define MAX(x,y) ((x)>(y) ? (x) : (y))
/* 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 */
#define GET_SIZE(p) (GET(p) & ~0x7)
#define GET_ALLOC(p) (GET(p) & 0x1)
/* 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)
/* Given block ptr bp, compute address of next and previous block */
#define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE((char *)(bp) - WSIZE))
#define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE((char *)(bp) - DSIZE))
/* ###################################################################### */
#define OVERHEAD 16
#define PREVPTR(bp) (bp)
#define NEXTPTR(bp) ((bp) + WSIZE)
#define GET_OFFSET(bp) (bp - heap_listp)
#define GET_ADDR(offset) (heap_listp + offset)
#define GET_PREVPTR(p) GET(PREVPTR(p))
#define GET_NEXTPTR(p) GET(NEXTPTR(p))
/* ###################################################################### */
/* the pointer to the beginning of heap */
static void *heap_listp;
/* the header and footer of free block list */
static void *free_listh = NULL;
static void *free_listf = NULL;
/* the pointer to the latest malloc block*/
static void *cache_ptr = NULL;
/* total size of free block */
static size_t free_size = 0;
/* function declaration */
static void *extend_heap(size_t words);
static void *coalesce(void *bp);
static void *find_fit(size_t asize);
static void *place(void *bp, size_t asize);
static void *next_freeblk(void *bp);
static void *prev_freeblk(void *bp);
static void *insert_freeblk(void *bp);
static void *remove_freeblk(void *bp);
static void *replace_freeblk(void *bp, void *new_bp);
/*
* mm_init - initialize the malloc package.
*/
int mm_init(void)
{
/* Create the initial empty heap */
if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1)
return -1;
PUT(heap_listp, 0);
PUT(heap_listp + (1*WSIZE), PACK(DSIZE, 1));
PUT(heap_listp + (2*WSIZE), PACK(DSIZE, 1));
PUT(heap_listp + (3*WSIZE), PACK(0, 1));
heap_listp += (2*WSIZE);
free_listh = NULL;
free_listf = NULL;
/* Extend the empty heap with a free block of CHUNKSIZE bytes */
if (extend_heap(CHUNKSIZE/WSIZE) == NULL)
return -1;
free_size += mem_pagesize() - (4*WSIZE);
return 0;
}
/*
* extend_heap - extend the heap to get more free block
*/
void *extend_heap(size_t words)
{
char *bp = NULL;
size_t size = 0;
/* 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;
free_size += size;
void *prev = PREV_BLKP(bp);
size_t prev_alloc = GET_ALLOC(HDRP(prev));
if (!prev_alloc){
size += GET_SIZE(HDRP(prev));
PUT(HDRP(prev), PACK(size, 0));
PUT(FTRP(prev), PACK(size, 0));
PUT(HDRP(NEXT_BLKP(prev)), PACK(0, 1)); /* New epilogue header */
return prev;
}
else{
PUT(HDRP(bp), PACK(size, 0)); /* Free block header */
PUT(FTRP(bp), PACK(size, 0)); /* Free block footer */
PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */
insert_freeblk(bp);
return bp;
}
}
/*
* mm_malloc - Allocate a block by incrementing the brk pointer.
* Always allocate a block whose size is a multiple of the alignment.
*/
void *mm_malloc(size_t size)
{
size_t asize; /* Adjusted block size */
size_t extendsize; /* Amount to extend heap if no fit */
char *bp;
/* Ignore spurious requests */
if (size == 0) return NULL;
if (size == 448) size = 512;
if (size == 112) size = 128; /* aim at trace7 and trace8 */
/* Adjust block size to include overhead and alignment reqs. */
if (size <= DSIZE)
asize = DSIZE + DSIZE;
else
asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE);
/* Search the free list for a fit */
if ((asize <= free_size) && (bp = find_fit(asize)) != NULL){
bp = place(bp, asize);
return bp;
}
/* No fit found. Get more memory and place the block */
extendsize = MAX(asize, CHUNKSIZE);
if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
return NULL;
bp = place(bp, asize);
return bp;
}
/*
* mm_free - Freeing a block does nothing.
*/
void mm_free(void *bp)
{
size_t size = GET_SIZE(HDRP(bp));
PUT(HDRP(bp), PACK(size, 0));
PUT(FTRP(bp), PACK(size, 0));
free_size += size;
coalesce(bp);
}
/*
* coalesce - coalesce two adjecent free block
*/
void *coalesce(void *bp)
{
void *prev = PREV_BLKP(bp);
void *next = NEXT_BLKP(bp);
size_t prev_alloc = GET_ALLOC(HDRP(prev));
size_t next_alloc = GET_ALLOC(HDRP(next));
size_t size = GET_SIZE(HDRP(bp));
// Case 1
if (prev_alloc && next_alloc){
insert_freeblk(bp);
}
// Case 2
else if (prev_alloc && !next_alloc){
size += GET_SIZE(HDRP(next));
PUT(HDRP(bp), PACK(size, 0));
PUT(FTRP(bp), PACK(size, 0));
replace_freeblk(next, bp);
}
// Case 3
else if (!prev_alloc && next_alloc){
size += GET_SIZE(HDRP(prev));
PUT(FTRP(bp), PACK(size, 0));
PUT(HDRP(prev), PACK(size, 0));
bp = PREV_BLKP(bp);
}
// Case 4
else {
size += GET_SIZE(HDRP(prev)) + GET_SIZE(FTRP(next));
PUT(HDRP(prev), PACK(size, 0));
PUT(FTRP(next), PACK(size, 0));
bp = PREV_BLKP(bp);
remove_freeblk(next);
}
return bp;
}
/*
* find_fit - find the fit free block for malloc
*/
void *find_fit(size_t asize)
{
void *bp = free_listh;
size_t size;
while (bp){
size = GET_SIZE(HDRP(bp));
if ((size <= 0) || (bp == NULL))
return NULL;
if ((asize <= size) && (bp != cache_ptr)) {
if (asize < 512) cache_ptr = bp;
return bp;
}
bp = next_freeblk(bp);
}
return NULL;
}
/*
* place - put the block to suitable place
*/
void *place(void *bp, size_t asize)
{
size_t csize = GET_SIZE(HDRP(bp));
if ((csize - asize) >= (2*DSIZE))
{
PUT(HDRP(bp), PACK(csize - asize, 0));
PUT(FTRP(bp), PACK(csize - asize, 0));
void *next = NEXT_BLKP(bp);
PUT(HDRP(next), PACK(asize, 1));
PUT(FTRP(next), PACK(asize, 1));
free_size -= asize;
return next;
}
else{
PUT(HDRP(bp), PACK(csize, 1));
PUT(FTRP(bp), PACK(csize, 1));
remove_freeblk(bp);
free_size -= csize;
return bp;
}
}
/*
* next_freeblk - get next free block
*/
static void *next_freeblk(void *bp)
{
size_t offset = GET_NEXTPTR(bp);
if (offset)
return GET_ADDR(offset);
else
return NULL;
}
/*
* prev_freeblk - get previous free block
*/
static void *prev_freeblk(void *bp)
{
size_t offset = GET_PREVPTR(bp);
if (offset)
return GET_ADDR(offset);
else
return NULL;
}
/*
* insert_freeblk - insert free block to free block list
*/
static void *insert_freeblk(void *bp)
{
/* insert the block to the header of the list*/
if (free_listh != NULL)
{
PUT(PREVPTR(free_listh), GET_OFFSET(bp));
PUT(NEXTPTR(bp), GET_OFFSET(free_listh));
PUT(PREVPTR(bp), 0);
free_listh = bp;
}
else{
free_listh = bp;
free_listf = bp;
PUT(PREVPTR(bp), 0);
PUT(NEXTPTR(bp), 0);
}
return bp;
}
/*
* remove_freeblk - remove the block from free block list
*/
static void *remove_freeblk(void *bp)
{
void *prev_freeblkp = prev_freeblk(bp);
void *next_freeblkp = next_freeblk(bp);
/* judge if bp is header */
if (prev_freeblkp){
PUT(NEXTPTR(prev_freeblkp), GET_NEXTPTR(bp));
}
else{
free_listh = next_freeblkp;
}
/* judge if bp is footer */
if (next_freeblkp){
PUT(next_freeblkp, GET_PREVPTR(bp));
}
else{
free_listf = prev_freeblkp;
}
return bp;
}
/*
* replace_freeblk - replace one block in free block list
*/
static void *replace_freeblk(void *bp, void *new_bp)
{
memcpy(new_bp, bp, 2*WSIZE);
size_t new_offset = GET_OFFSET(new_bp);
/* judge if bp is header */
if (free_listh != bp){
void *prev_freeblkp = prev_freeblk(bp);
PUT(NEXTPTR(prev_freeblkp), new_offset);
}
else{
free_listh = new_bp;
}
/* judge if bp is footer */
if (free_listf != bp){
void *next_freeblkp = next_freeblk(bp);
PUT(PREVPTR(next_freeblkp), new_offset);
}
else{
free_listf = new_bp;
}
return new_bp;
}
/*
* mm_realloc - Implemented simply in terms of mm_nalloc and mm_free
*/
void *mm_realloc(void *bp, size_t size)
{
if (size == 0){
mm_free(bp);
return bp;
}
size_t asize = 0;
if (size <= DSIZE)
asize = 2*DSIZE;
else
asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE);
if (bp == NULL){
return mm_malloc(asize);
}
else {
size_t csize = GET_SIZE(HDRP(bp));
// csize equals new size or just a little larger than new size
if ((csize >= asize) && (csize < asize + 2*DSIZE))
return bp;
// csize is much larger than new size
else if (csize >= asize + 2*DSIZE)
{
void *next = NEXT_BLKP(bp);
size_t remainder = csize - asize;
PUT(HDRP(bp), PACK(asize, 1));
PUT(FTRP(bp), PACK(asize, 1));
void *left = NEXT_BLKP(bp);
free_size += remainder;
if (!GET_ALLOC(HDRP(next)))
{
remainder += GET_SIZE(HDRP(next));
PUT(HDRP(left), PACK(remainder, 0));
PUT(FTRP(left), PACK(remainder, 0));
replace_freeblk(next, left);
}
else {
PUT(HDRP(left), PACK(remainder, 0));
PUT(HDRP(left), PACK(remainder, 0));
insert_freeblk(left);
}
return bp;
}
// csize is litter than new size
else {
void *next = NEXT_BLKP(bp);
if (!GET_ALLOC(HDRP(next)))
{
size_t next_size = GET_SIZE(HDRP(next));
if (csize + next_size >= asize)
{
if (csize + next_size < asize + 4*WSIZE)
{
PUT(HDRP(bp), PACK((csize + next_size), 1));
PUT(FTRP(bp), PACK((csize + next_size), 1));
free_size -= next_size;
remove_freeblk(next);
}
else {
size_t remainder = csize + next_size - asize;
void *left = bp + asize;
replace_freeblk(next, left);
PUT(HDRP(bp), PACK(asize, 1));
PUT(FTRP(bp), PACK(asize, 1));
PUT(HDRP(left), PACK(remainder, 0));
PUT(FTRP(left), PACK(remainder, 0));
free_size = free_size - next_size + remainder;
}
return bp;
}
}
void *new_bp = mm_malloc(asize);
if (new_bp != bp)
memcpy(new_bp, bp, csize - DSIZE);
mm_free(bp);
return new_bp;
}
}
}
/*
* mm_check - check the correctness of code
*/
void mm_check(void)
{
printf("this is mm_check.\n");
}