forked from yangl1996/malloc-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm.c
448 lines (408 loc) · 11.7 KB
/
mm.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
/*
* mm.c
*
* Use sagregated lists to store free blocks. New free blocks inserted using
* FIFO manner. Allocate blocks using best fit strategy.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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. */
/* TODO: remove them before submission */
//#define DEBUG
//#define CHECK
#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 */
/* word sizes of x64 */
#define WSIZE 8 /* x64 pointer */
#define HSIZE 4 /* x64 int */
#define DSIZE 16 /* x64 double word */
/* single word (4) or double word (8) alignment */
#define ALIGNMENT 8
/* 2^0 to 2^(SAGCOUNT-1) sized blocks will have its own list */
/* we will have SAGCOUNT + 1 classes in total */
#define SAGCOUNT 12 /* choose an even number */
/* class n: non-zero MSB at n
class 0: 1
class 1: 2-3
class 2: 4-7
...
*/
/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(p) (((size_t)(p) + (ALIGNMENT-1)) & ~0x7)
/* calculate actual pointer using heap begin and offset */
#define CPTR(o) ((unsigned int)(o) + heap_begin)
/* calculate offset from pointer and heap begin */
#define COFF(p) ((char*)(p) - heap_begin)
/* write to heap */
#define PUT(p, data) (*(unsigned int*)((CPTR(p))) = (data))
/* read from heap */
#define GET(p) (*(unsigned int*)(CPTR(p)))
/* pack header block */
#define PACK(size, allocated) ((size) | (allocated))
/* get class head offset */
#define CLASS(class) ((unsigned int)((class) * HSIZE))
/* get allocated bit */
#define ALLOCED(head) ((unsigned int)((head) & 0x01))
/* get size */
#define SIZE(head) ((unsigned int)((head) & 0xFFFFFFFE))
/* global variables */
static char *heap_begin; /* to the first byte of a heap */
static char *heap_end; /* to the last byte of a heap */
/* get class index */
static inline unsigned int find_class(unsigned int bsize)
{
dbg_printf("find_class(): finding class for size %u\n", bsize);
int class = -1;
while (bsize != 0)
{
bsize >>= 1;
class++;
}
if (class >= SAGCOUNT)
{
return SAGCOUNT;
}
dbg_printf("find_class(): class %u found\n", class);
return class;
}
static inline void remove_from_list(unsigned int offset)
{
unsigned int original_next = GET(offset + 4);
unsigned int original_prev = GET(offset + 8);
/* link next to prev */
if (original_prev > CLASS(SAGCOUNT))
{
PUT(original_prev + 4, original_next);
}
else
{
PUT(original_prev, original_next);
}
if (original_next != 0)
{
PUT(original_next + 8, original_prev);
}
dbg_printf("remove_from_list(): %x removed from list\n", offset);
return;
}
static inline void insert_into_list(unsigned int offset)
{
unsigned int size = SIZE(GET(offset));
unsigned int class = find_class(size);
unsigned int original_next = GET(CLASS(class));
PUT(offset + 4, original_next);
PUT(offset + 8, CLASS(class));
if (original_next != 0)
{
PUT(original_next + 8, offset);
}
PUT(CLASS(class), offset);
dbg_printf("insert_into_list(): %x inserted into list\n", offset);
}
/* join a block */
static inline unsigned int join(unsigned int offset)
{
dbg_printf("join(): joining %x\n", offset);
/* look backwords */
unsigned int my_size;
if (!ALLOCED(GET(offset - HSIZE)))
{
dbg_printf("join(): joining with previous block\n");
my_size = SIZE(GET(offset));
unsigned int before_size = SIZE(GET(offset - HSIZE));
unsigned int new_size = my_size + before_size;
remove_from_list(offset);
remove_from_list(offset - before_size);
/* update head and foot stamp */
PUT(offset + my_size - HSIZE, PACK(new_size, 0));
PUT(offset - before_size, PACK(new_size, 0));
offset = offset - before_size;
/* update linked list */
insert_into_list(offset);
}
my_size = SIZE(GET(offset));
if (!ALLOCED(GET(offset + my_size)))
{
dbg_printf("join(): joining with next block\n");
unsigned int next_size = SIZE(GET(offset + my_size));
unsigned int new_size = my_size + next_size;
remove_from_list(offset);
remove_from_list(offset + my_size);
PUT(offset, PACK(new_size, 0));
PUT(offset + new_size - HSIZE, PACK(new_size, 0));
insert_into_list(offset);
my_size = new_size;
}
return offset;
}
/* extend a new free block */
static inline unsigned int extend(int word)
{
unsigned int size_new = (word >= 2) ? (word * WSIZE) : (2 * WSIZE);
/* increase heap */
unsigned int new_block = COFF(mem_sbrk(size_new)) - HSIZE;
/* set header */
PUT(new_block, PACK(size_new, 0));
/* set footer */
PUT(new_block + size_new - 4, PACK(size_new, 0));
PUT(new_block + size_new, PACK(0, 1));
/* link the list */
insert_into_list(new_block);
dbg_printf("extend(): %u bytes extended from %x\n", size_new, new_block);
return join(new_block);
}
/* Heap Layout
First SAGCOUNT + 1 Ints: list heads at address 0 to SAGCOUNT
Prologue
Prologue
....
(int HEAD ... int FOOT)
Epilogue
*/
/* Free Block Layout
int Head
int prev
int next
int Foot
*/
/*
* Initialize: return -1 on error, 0 on success.
*/
int mm_init(void) {
int init_allocate = HSIZE * (SAGCOUNT + 4); /* keep it an even number for allignment */
mem_sbrk(init_allocate);
heap_begin = (char*)mem_heap_lo();
heap_end = (char*)mem_heap_hi();
for (int i = 0; i < SAGCOUNT + 1; i++)
{
PUT(CLASS(i), 0);
}
PUT((SAGCOUNT + 1) * HSIZE, PACK(WSIZE, 1));
PUT((SAGCOUNT + 2) * HSIZE, PACK(WSIZE, 1));
PUT((SAGCOUNT + 3) * HSIZE, PACK(0, 1));
dbg_printf("mm_init(): heap initialized\n");
return 0;
}
/*
* malloc
*/
void *malloc (size_t size) {
unsigned int bytes = (unsigned int)ALIGN((size + 8));
if (bytes < 16)
{
bytes = 16;
}
dbg_printf("malloc(): allocating %u bytes\n", bytes);
unsigned int class = find_class(bytes);
unsigned int find_ptr = GET(CLASS(class));
unsigned int min_space = 999999999;;
unsigned int min_ptr = 0;
/* find in the same class */
while (find_ptr != 0)
{
unsigned int current_size = SIZE(GET(find_ptr));
if ((current_size >= bytes) && ((current_size - bytes) < min_space))
{
min_space = current_size - bytes;
min_ptr = find_ptr;
if (min_space == 0)
{
break;
}
}
find_ptr = GET(find_ptr + 4);
}
/* if cannot find in current class, search higher class */
/* TODO: search for best in higher class */
class++;
if (!min_ptr)
{
while (class <= SAGCOUNT)
{
if (GET(CLASS(class)) != 0)
{
find_ptr = GET(CLASS(class));
while (find_ptr != 0)
{
unsigned int current_size = SIZE(GET(find_ptr));
if ((current_size - bytes) < min_space)
{
min_space = current_size - bytes;
min_ptr = find_ptr;
}
find_ptr = GET(find_ptr + 4);
}
break;
}
else
{
class++;
}
}
}
if (min_ptr)
{
unsigned int current_size = SIZE(GET(min_ptr));
remove_from_list(min_ptr);
if ((current_size - bytes) >= 16)
{
PUT(min_ptr, PACK(bytes, 1));
PUT(min_ptr + bytes - 4, PACK(bytes, 1));
PUT(min_ptr + bytes, PACK(current_size - bytes, 0));
PUT(min_ptr + current_size - 4, PACK(current_size - bytes, 0));
insert_into_list(min_ptr + bytes);
}
else
{
PUT(min_ptr, PACK(current_size, 1));
PUT(min_ptr + current_size - 4, PACK(current_size, 1));
}
dbg_printf("malloc(): %u bytes allocated at %x\n", bytes, min_ptr);
#ifdef CHECK
mm_checkheap(0);
#endif
return (void*)CPTR(min_ptr + 4);
}
else
{
unsigned int new_block = extend(bytes/8);
unsigned int current_size = SIZE(GET(new_block));
PUT(new_block, PACK(current_size, 1));
PUT(new_block + current_size - 4, PACK(current_size, 1));
remove_from_list(new_block);
dbg_printf("malloc(): %u bytes allocated at %x\n", bytes, new_block);
#ifdef CHECK
mm_checkheap(0);
#endif
return (void*)CPTR(new_block + 4);
}
}
/*
* free
*/
void free (void *ptr) {
if(!ptr) return;
unsigned int to_remove = COFF(ptr) - 4;
unsigned int current_size = SIZE(GET(to_remove));
dbg_printf("free(): freeing %u bytes at %x\n", current_size, to_remove);
PUT(to_remove, PACK(current_size, 0));
/* TODO: should put end tag here, but if we do, needle.rep will run out memory */
PUT(to_remove + current_size - 4, PACK(current_size, 0));
insert_into_list(to_remove);
join(to_remove);
#ifdef check
mm_checkheap(0);
#endif
return;
}
/*
* realloc - you may want to look at mm-naive.c
*/
void *realloc(void *oldptr, size_t size) {
size_t oldsize;
void *newptr;
/* If size == 0 then this is just free, and we return NULL. */
if(size == 0) {
free(oldptr);
return 0;
}
/* If oldptr is NULL, then this is just malloc. */
if(oldptr == NULL) {
return malloc(size);
}
newptr = malloc(size);
/* If realloc() fails the original block is left untouched */
if(!newptr) {
return 0;
}
/* Copy the old data. */
oldsize = (size_t)(SIZE(GET(COFF(oldptr))) - 8);
if(size < oldsize) oldsize = size;
memcpy(newptr, oldptr, oldsize);
/* Free the old block. */
free(oldptr);
return newptr;
}
/*
* calloc - you may want to look at mm-naive.c
* This function is not tested by mdriver, but it is
* needed to run the traces.
*/
void *calloc (size_t nmemb, size_t size) {
size_t bytes = nmemb * size;
void *newptr;
newptr = malloc(bytes);
memset(newptr, 0, bytes);
return newptr;
}
/*
* 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
*/
void mm_checkheap(int verbose) {
printf("verbose level %d, actually useless ;)", verbose);
for (int i = 0; i <= SAGCOUNT; i++)
{
unsigned int current_ptr = GET(CLASS(i));
while (current_ptr != 0)
{
current_ptr = GET(current_ptr + 4);
if (current_ptr == 0)
{
break;
}
unsigned current_size = SIZE(GET(current_ptr));
unsigned foot_size = SIZE(GET(current_ptr + current_size - 4));
if (current_size == foot_size)
{
/* do nothing */
}
else
{
printf("head or foot mark broken! deep dark fantasy\n");
}
if (GET(GET(current_ptr + 8) + 4) == current_ptr)
{
/* do nothing */
}
else
{
printf("linked list borken! what the hell you are doing now!\n");
}
}
}
}