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
| #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <assert.h> #include <unistd.h> #include <string.h>
#define ALIGNMENT 8
#define HEADER (sizeof(size_t)) #define FOOTER (sizeof(size_t))
#define EXTRA (2*sizeof(void*)) #define FILL (ALIGN(HEADER + EXTRA))
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
#define MAX(x, y) ((x) > (y)? (x) : (y)) #define GET(p) (*(size_t *)(p)) #define GET_P(p) (*(void **)(p)) #define GET_SIZE(p) (GET(p) & ~0x7) #define GET_ALLOC(p) (GET(p) & 0x1) #define GET_PREV(p) (GET_P(p + HEADER)) #define GET_NEXT(p) (GET_P(p + HEADER + sizeof(void*)))
#define SET(p) (*(size_t*)(p) = (GET(p) | 1)) #define PUT(p, val) (*(size_t *)(p) = (val)) #define PUT_F(p, val) (*(size_t *)(p + val - FOOTER) = (val)) #define PUT_P(p, val) (*(void **)(p) = (val)) #define PUT_PREV(p, val) (PUT_P(p + HEADER, val)) #define PUT_NEXT(p, val) (PUT_P(p + HEADER + sizeof(void*), val)) #define FULL_SIZE(content) (ALIGN(FILL + content + FOOTER)) #define SPACE(sz) (sz - FILL - FOOTER)
void del_p(void *p);
static void *START, *END; static void *FP = NULL;
void set_blk(void *p, size_t sz) { PUT(p, sz); PUT_F(p, sz); }
void insert_head(void *p) { PUT_PREV(p, NULL); if (FP == NULL) { FP = p; PUT_NEXT(p, NULL); } else { PUT_NEXT(p, FP); PUT_PREV(FP, p); FP = p; } }
void printlist(void) { printf("FREE LIST\n"); void *cur = FP; int cnt = 0; while (cur) { void *nxt = GET_NEXT(cur); int sz = GET_SIZE(cur); int al = GET_ALLOC(cur); int sp = SPACE(sz); printf("pointer: %x, size: %x, space: %x, alloc: %x\n", (uintptr_t)cur, sz, sp, al); cur = nxt; cnt++; if (cnt > 10) { printf("ERROR\n"); exit(0); } } }
void printlog(void) { printf("log START: %x, END: %x\n", (uintptr_t)START, (uintptr_t)END); void *cur = START; while (cur < END) { int sz = GET_SIZE(cur); int al = GET_ALLOC(cur); int sp = SPACE(sz); printf("pointer: %x, size: %x, space: %x, alloc: %x\n", (uintptr_t)cur, sz, sp, al); cur += sz; } printlist(); printf("END\n"); }
int mm_init(void) { mem_deinit(); mem_init(); START = (void*)(ALIGN((uintptr_t)mem_heap_lo())); END = START; FP = NULL; mem_sbrk(START - mem_heap_lo()); return 0; }
void *mm_malloc(size_t size) { void *tmp = FP; while (tmp) { void *nxt = GET_NEXT(tmp); size_t sz = GET_SIZE(tmp); if (SPACE(sz) >= size) { del_p(tmp); size_t tar = FULL_SIZE(size); if (sz - tar >= FULL_SIZE(ALIGNMENT)) { set_blk(tmp, tar); set_blk(tmp + tar, sz - tar); insert_head(tmp + tar); } SET(tmp); return tmp + FILL; } tmp = nxt; } size_t sz = FULL_SIZE(ALIGN(size)); mem_sbrk(sz); set_blk(END, sz); SET(END); END += sz; return END - sz + FILL; }
void del_p(void *p) { void *prev = GET_PREV(p); void *nxt = GET_NEXT(p); if (prev == NULL && nxt == NULL) { FP = NULL; return; } if (prev == NULL) { FP = nxt; PUT_PREV(FP, NULL); return; } if (nxt == NULL) { PUT_NEXT(prev, NULL); return; } PUT_NEXT(prev, nxt); PUT_PREV(nxt, prev); return; }
void combine(void *p1, void *p2) { size_t sz = GET_SIZE(p1) + GET_SIZE(p2); del_p(p1); del_p(p2); set_blk(p1, sz); insert_head(p1); }
void mm_free(void *ptr) { void *head = ptr - FILL; void *cur = head; void *nxt = head + GET_SIZE(head); PUT(head, GET_SIZE(head)); insert_head(head); if (head > START) { void *last = head - GET_SIZE(head - FOOTER); if (!GET_ALLOC(last)) { combine(last, cur); cur = last; } } if (nxt < END) { if (!GET_ALLOC(nxt)) { combine(cur, nxt); } } }
void *mm_realloc(void *ptr, size_t size) { void *oldptr = ptr; void *newptr; size_t copySize; newptr = mm_malloc(size); if (newptr == NULL) return NULL; copySize = (size_t)SPACE(ptr); if (size < copySize) copySize = size; memcpy(newptr, oldptr, copySize); mm_free(oldptr); return newptr; }
|