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
| #include "mm.h"
void mem_init(){ mem_heap = (char*)malloc(MAX_HEAP);
mem_brk = (char*)mem_heap;
mem_max_addr = (char*)(mem_heap+MAX_HEAP); }
void *mem_sbrk(int incr){ char* old_brk = mem_brk; if( (incr<0) || (mem_brk+incr>mem_max_addr)){ errno = ENOMEM; fprintf(stderr,"ERROR: mem_sbrk failed. Ran out of memory...\n"); return (void*)-1; }
mem_brk+=incr; return (void*)old_brk; }
int mm_init(){ 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); if(extend_heap(CHUNKSIZE/WSIZE)==NULL) return -1; return 0; }
static void *extend_heap(size_t words){ char* bp; size_t size;
size = (words%2) ? (words+1) :words; if((long)(bp = mem_sbrk(size))==-1) return NULL;
PUT(HDRP(bp),PACK(size,0)); PUT(FTRP(bp), PACK(size,0)); PUT(HDRP(NEXT_BLKP(bp)), PACK(0,1));
}
void mm_free(void *bp){ size_t size = GET_SIZE(HDRP(bp)); PUT(HDRP(bp), PACK(size,0)); PUT(FTRP(bp), PACK(size,0)); coalesce(bp); }
static void *coalesce(void *bp){ size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))); size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp))); size_t size = GET_SIZE(HDRP(bp));
if(prev_alloc && next_alloc ){ return bp; }else if(prev_alloc && !next_alloc){ size += GET_SIZE(HDRP(NEXT_BLKP(bp))); PUT(HDRP(bp),PACK(size,0)); PUT(FTRP(bp),PACK(size,0)); }else if(!prev_alloc && next_alloc){ size += GET_SIZE(HDRP(PREV_BLKP(bp))); PUT(FTRP(bp),PACK(size,0)); PUT(HDRP(PREV_BLKP(bp)),PACK(size,0)); bp = PREV_BLKP(bp); }else{ size += GET_SIZE(HDRP(PREV_BLKP(bp)))+GET_SIZE(FTRP(NEXT_BLKP(bp))); PUT(HDRP(PREV_BLKP(bp)),PACK(size,0)); PUT(FTRP(NEXT_BLKP(bp)),PACK(size,0)); bp = PREV_BLKP(bp); }
return bp; }
static void *find_fit(size_t asize){ void *first = heap_listp; while(GET_SIZE(HDRP(first))!=0){ if (GET_SIZE(HDRP(first)) >= asize && !GET_ALLOC(HDRP(first))) { return first; } else { first = NEXT_BLKP(first); } } return NULL; }
static void place(void *bp, size_t asize){ size_t rest = GET_SIZE(HDRP(bp))-asize; if(rest<2*DSIZE){ return; }else{ PUT(HDRP(bp),PACK(asize,1)); PUT(FTRP(bp),PACK(asize,1)); bp = NEXT_BLKP(bp); PUT(HDRP(bp),PACK(rest,0)); PUT(FTRP(bp),PACK(rest,0)); } }
void *mm_malloc(size_t size){ size_t asize; size_t extendsize; char* bp;
if(size == 0){ return NULL; }
if(size <= DSIZE){ asize = 2*DSIZE; }else{ asize = DSIZE*( (size+DSIZE + DSIZE-1)/DSIZE); }
if((bp = find_fit(asize)) != NULL){ place(bp,asize); return bp; }
extendsize = MAX(asize,CHUNKSIZE); if((bp=extend_heap(extendsize/WSIZE))==NULL) return NULL; place(bp,asize); return bp; }
|