/* * add_block - add a block to the list in order of address */ static void add_block(void *bp) { int list = get_list(GET_SIZE(HDRP(bp))); void *next = free_lists[list]; while (next != LIST_END) { /* Insert bp in middle of list, or beginning if prev(next) is NULL */ if (next > bp) { if (GET_PREV_FREE(next) == LIST_END) free_lists[list] = bp; /* Update list head */ else SET_NEXT_FREE(GET_PREV_FREE(next), bp); SET_PREV_FREE(bp, GET_PREV_FREE(next)); SET_PREV_FREE(next, bp); SET_NEXT_FREE(bp, next); return; /* Reached end of list, add bp to the end using saved block */ } else if (GET_NEXT_FREE(next) == LIST_END) { SET_PREV_FREE(bp, next); SET_NEXT_FREE(next, bp); SET_NEXT_FREE(bp, LIST_END); return; } next = GET_NEXT_FREE(next); } /* Edge case: list is empty, add bp */ SET_PREV_FREE(bp, LIST_END); SET_NEXT_FREE(bp, LIST_END); free_lists[list] = bp; }
/* * delete_block - delete a block from the free list */ static void delete_block(void *bp) { void *prev = GET_PREV_FREE(bp); void *next = GET_NEXT_FREE(bp); int list = get_list(GET_SIZE(HDRP(bp))); /* Case 1: bp is the only block in list */ if ((prev == LIST_END) && (next == LIST_END)) { free_lists[list] = NULL; return; /* Case 2: bp is the first block in the list */ } else if (prev == LIST_END) { SET_PREV_FREE(next, LIST_END); free_lists[list] = next; return; /* Case 3: bp is the last block in the list */ } else if (next == LIST_END) { SET_NEXT_FREE(prev, LIST_END); return; /* Case 4: bp is in the middle of the list */ } else { SET_NEXT_FREE(prev, next); SET_PREV_FREE(next, prev); return; } }
/* * add_block - add a block to the front of the free list */ static void add_block(void *bp) { /* Case 1: Add bp to front of list */ if (free_list != NULL) { SET_PREV_FREE(free_list, bp); SET_NEXT_FREE(bp, free_list); SET_PREV_FREE(bp, LIST_END); /* Case 2: List is empty, make bp the first block */ } else { SET_PREV_FREE(bp, LIST_END); SET_NEXT_FREE(bp, LIST_END); } free_list = bp; /* update list root */ }
/*Removes the free block pointer int the free_list*/ static void removeFromFree(void *bp){ if (GET_PREV_FREE(bp)) SET_NEXT_FREE(GET_PREV_FREE(bp), GET_NEXT_FREE(bp)); else free_listp = GET_NEXT_FREE(bp); SET_PREV_FREE(GET_NEXT_FREE(bp), GET_PREV_FREE(bp)); }
void addFreeBlock(void *bp) { void *current = root; void *temp = current; void *previous = NULL; while (current != NULL && bp < current) { previous = PREV_FREE_BLKP(current); temp = current; current = NEXT_FREE_BLKP(current); } SET_PREV_FREE(bp, previous); SET_NEXT_FREE(bp, temp); if (previous != NULL) { SET_NEXT_FREE(previous, bp); } else { root = bp; } if (temp != NULL) { SET_PREV_FREE(temp, bp); } }
void delFreeBlock(void *bp) { void *next = (void *) NEXT_FREE_BLKP(bp); void *previous = (void *) PREV_FREE_BLKP(bp); if (previous == NULL) { root = next; } else { SET_NEXT_FREE(previous, next); } if (next != NULL) { SET_PREV_FREE(next, previous); } }
/*Inserts the free block pointer int the free_list*/ static void addToFree(void *bp){ SET_NEXT_FREE(bp, free_listp); SET_PREV_FREE(free_listp, bp); SET_PREV_FREE(bp, NULL); free_listp = bp; }