/* Insert the block before the root */ static inline void insert_first(void *bp) { size_t asize = GET_SIZE(GET_HEADER(bp)); /* root block adress for class size of asize */ char *bclassp = get_class_ptr_by_bsize(asize); dbg_printf("bclassp = 0x%lx\n", (size_t)bclassp); char *rootbp = GET_CLASS_ROOT_BLK(bclassp); if (rootbp == NULL) { SET_PRED(bp, NULL); SET_SUCC(bp, NULL); } else { SET_SUCC(bp, rootbp); SET_PRED(bp, NULL); SET_PRED(rootbp, bp); } rootbp = bp; SET_CLASS_ROOT_BLK(bclassp, rootbp); }
static inline void split_free_block(void *bp, void *nextbp) { if (!GET_SUCC_BLK(bp)) { SET_SUCC(nextbp, NULL); } else { SET_SUCC(nextbp, GET_SUCC_BLK(bp)); SET_PRED(GET_SUCC_BLK(bp), nextbp); } SET_SUCC(bp, nextbp); SET_PRED(nextbp, bp); }
static void remove_block(void *bp, size_t size) { //printf ("remove block\n"); REQUIRES ( bp != NULL ); REQUIRES ( (size_t)(bp) % 8 == 0); int offset = get_offset(size); void *pred = get_pred(bp); void *succ = get_succ(bp); if ( pred == NULL && succ != NULL) //block to be removed is the first block in the list { SET_SUCC(bp) = 0; SET_PRED(succ) = 0; SET_ROOT(GET_BUCKET(root, offset)) = (long)succ; } else if (pred != NULL && succ == NULL) //block to be removed is the last block in the list { SET_SUCC(pred) = 0; SET_PRED(bp) = 0; } else if (pred != NULL && succ != NULL) //block to be removed is located somewhere within the list. { SET_SUCC(pred) = (int)( (long)succ - BASE); SET_PRED(succ) = (int)((long)pred - BASE); SET_PRED(bp) = 0; SET_SUCC(bp) = 0; } else if ( pred == NULL && succ == NULL) { //printf("resetting root\n"); SET_ROOT(GET_BUCKET(root, offset)) = 0; } print_list(); return; }
/* Equals to remove a node from linked list */ static inline void remove_free_block(void *bp, int class_idx) { char *bclassp = GET_CLASS(class_idx); char *rootbp = GET_CLASS_ROOT_BLK(bclassp); if (!bp) { dbg_printf("Trying to remove NULL\n"); exit(1); } if (!GET_PRED_BLK(bp) && !GET_SUCC_BLK(bp)) { rootbp = NULL; SET_CLASS_ROOT_BLK(bclassp, rootbp); } /* has pred, no succ */ if (GET_PRED_BLK(bp) && !GET_SUCC_BLK(bp)) { SET_SUCC(GET_PRED_BLK(bp), NULL); } /* has succ, no pred */ if (!GET_PRED_BLK(bp) && GET_SUCC_BLK(bp)) { rootbp = GET_SUCC_BLK(bp); SET_PRED(rootbp, NULL); SET_CLASS_ROOT_BLK(bclassp, rootbp); } if (GET_PRED_BLK(bp) && GET_SUCC_BLK(bp)) { SET_SUCC(GET_PRED_BLK(bp), GET_SUCC_BLK(bp)); SET_PRED(GET_SUCC_BLK(bp), GET_PRED_BLK(bp)); } SET_PRED(bp, NULL); SET_SUCC(bp, NULL); }
static void add_block(void *bp, size_t size) { // printf ("in add block\n"); REQUIRES ( bp != NULL ) ; REQUIRES ((size_t)(bp) % 8 == 0); int offset = get_offset(size); if ( (*(long*)GET_BUCKET(root, offset) != 0)) { //printf ("replacing root\n"); SET_PRED((void*)(*(long*)GET_BUCKET(root, offset))) = (int)( (long)bp -BASE ); } int val = (int)(*(long*)GET_BUCKET(root,offset) - BASE); //printf ("val = 0x%x \n",val); SET_SUCC(bp) = val; SET_PRED(bp) = 0; SET_ROOT(GET_BUCKET(root, offset)) = (long)bp; //print_list(); //printf("returned from adding block\n"); return; }
static void *extend_heap(int words) { /* last block of heap, size = 0, alloc = 1 */ char *epilogue = (mem_heap_hi() + 1); char *bp; /* block pointer */ int bsize; /* block size to extend */ size_t flag = 0; /* Allocate even number of words to maintain alignment */ bsize = (words % 2) ? ((words + 1) * WSIZE) : (words * WSIZE); dbg_printf("EXTEND_HEAD: words = %d bszie = %d\n", words, bsize); dbg_printf("!!!!!!!!!!!!!!!!!!!!!!!!Before Extend!!!!\n"); mm_checkheap(CHECK_HEAP); /* Record if last block is allocated or not */ flag = GET_PREV_ALLOC(GET_HEADER(epilogue)) ? 0x2 : 0x0; if ((long)(bp = mem_sbrk(bsize)) == -1) return NULL; /* Init free block header/footer and the epilogue header */ PUT(GET_HEADER(bp), PACK(bsize, flag)); PUT(GET_FOOTER(bp), PACK(bsize, flag)); /* Set epilogue to be size = 0, alloc = 1 */ PUT(GET_HEADER(NEXT_BLKP(bp)), PACK(0, 1)); SET_PRED(bp, NULL); SET_SUCC(bp, NULL); mm_checkheap(CHECK_HEAP); return coalesce(bp); }