/*Removing a block from the free list either due to allocation or during coalascing*/ static void remove_list(void *bp){ if (GET_PREVP(bp)) SET_NEXTP(GET_PREVP(bp), GET_NEXTP(bp)); else list_head = GET_NEXTP(bp); SET_PREVP(GET_NEXTP(bp), GET_PREVP(bp)); }
/** * add_free_list_lifo - add the specific block ptr to the free list, making it * the first element in the list(LIFO). * @param bp Pointer to the block being added to the list */ static void *add_free_list_lifo(void *bp) { int list_no ; /*Coalesce the block*/ bp = coalesce_block(bp); list_no = get_list_no(GET_SIZE(HDRP(bp))); if ( GET_SEGI(seg_list,list_no) == NULL) { SET_PREVP(bp, NULL); SET_NEXTP(bp, NULL); } else if ( GET_SEGI(seg_list,list_no) != NULL) { SET_NEXTP(bp,GET_SEGI(seg_list,list_no)); SET_PREVP(bp,NULL); SET_PREVP(GET_SEGI(seg_list,list_no),bp); } /*Point next of bp to where root node was pointing*/ /*Make root to point to new node*/ SET_SEGI(seg_list, list_no, bp); return bp; }
/** * delete_free_list - Delete a block from the free seg list * @param bp block to be deleted from free list */ static inline void delete_free_list(void *bp) { int list_no = get_list_no(GET_SIZE(HDRP(bp))); temp++; void *next = GET_NEXTP(bp); void *prev = GET_PREVP(bp); /*Handle corner case of deleting head node.*/ if(bp == GET_SEGI(seg_list, list_no) ) { SET_SEGI(seg_list, list_no, next); } if(prev != NULL) { SET_NEXTP(prev, next); } if(next != NULL) { SET_PREVP(next, prev); } /*Clean Up task. Set next/prev pointers of bp to NULL*/ SET_NEXTP(bp,NULL); SET_PREVP(bp,NULL); }
/** * Initialize the memory manager. * @param - void no parameter passed in * @return - int 0 for success or -1 for failure */ int mm_init(void) { /* Create the initial empty heap. */ if ((heap_listp = mem_sbrk(8*WSIZE)) == (void *)-1) return -1; PUT(heap_listp, 0); /* Alignment padding */ PUT(heap_listp + (1 * WSIZE), PACK(DSIZE, 1)); /* Prologue header */ PUT(heap_listp + (2 * WSIZE), PACK(DSIZE, 1)); /* Prologue footer */ PUT(heap_listp + (3 * WSIZE), PACK(0, 1)); /* Epilogue header */ list_head = heap_listp + 3*WSIZE; SET_NEXTP(list_head, NULL); /*setting next pointer to null*/ /* Extend the empty heap with a free block of minimum possible block size */ if (extend_heap(CHUNKSIZE / WSIZE) == NULL){ return -1; } return 0; }
/*Inserting a free block into the free list*/ static void insert_list(void *bp){ SET_NEXTP(bp, list_head); SET_PREVP(list_head, bp); SET_PREVP(bp, NULL); list_head = bp; }