/*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)); }
/** * check_count_free_list -Check for count of free blocks, iterating over blocks * and by going through next pointers */ static void check_count_free_list() { void *bp; unsigned int counti = 0; unsigned int countp = 0; /*Iterate over list*/ for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) { if(!GET_ALLOC(HDRP(bp))) { counti++; } } /* Moving free list by pointers*/ for (int i = 0; i < NO_OF_LISTS; i++) { for (bp = GET_SEGI(seg_list,i); (bp!=NULL) && (GET_SIZE(HDRP(bp)) > 0);bp = GET_NEXTP(bp)) { countp++; } } /*If count is not matching, print error, with debug Info*/ if(countp!=counti) { printf("ERROR: No. of free block mismatch\n"); dbg_printf("free\n"); print_free(); dbg_printf("all\n"); print_all(); } }
/** * print_free - Prints all segregated free lists */ void print_free() { void *bp; for(int i=0; i<NO_OF_LISTS;i++) { dbg_printf("List No: %d",i); bp = GET_SEGI(seg_list,i); while(bp != NULL && GET_SIZE(HDRP(bp)) > 0) { printblock(bp); bp = GET_NEXTP(bp); } } }
/*This is to check if there are any free blocks that missed the coalescing and end up in the free list as two different blocks */ static void check_coalescing(){ void *bp; void *temp; for (bp = list_head; bp != NULL; bp = GET_NEXTP(bp) ){ for (temp = list_head; temp != NULL; temp = GET_NEXTP(temp)) { /* code */ if(NEXT_BLKP(bp) == temp) { printf("ERROR: A block missed during Coalescing."); printblock(bp); printblock(temp); } } } }
/* This is to check if there are any allocated blocks in the free blocks! */ static void check_free_list(){ void *bp; for (bp = list_head; bp != NULL; bp = GET_NEXTP(bp) ){ if (GET_ALLOC(bp)) { printf("ERROR: allocated block in free list!\n"); printblock(bp); } } }
static void * find_fit(size_t asize) { void *bp; for (bp = list_head; bp != NULL; bp = GET_NEXTP(bp) ){ if (asize <= (size_t)GET_SIZE(HDRP(bp)) ) { return bp; } } return NULL; }
/** * check_cycle - Check for cycle in the free lists. */ static void check_cycle() { void *hare; void *tortoise; /*Implementing Tortoise and hare algo, Iterating over each seg list*/ for (int i = 0; i < NO_OF_LISTS; ++i) { hare= GET_SEGI(seg_list,i); tortoise = GET_SEGI(seg_list,i); while( tortoise != NULL && hare != NULL ) { if( hare != NULL ) { hare = GET_NEXTP(hare); } if( hare != NULL ) { tortoise = GET_NEXTP(hare); } if( hare == tortoise ){ /*Its a cycle .. error . */ printf("ERROR: There is a cycle in the free_list\n"); } } } }
/** * check_seg_pointers - Check the seg lists for correctness of all the previous * and next pointers */ static void check_seg_pointers() { void *bp; void *next; size_t asize; /* Check for all the pointers in the blocks to be correct */ for (int i = 0; i < NO_OF_LISTS; i++) { for (bp = GET_SEGI(seg_list,i); (bp!=NULL) && (GET_SIZE(HDRP(bp)) > 0);bp = GET_NEXTP(bp)) { next = GET_NEXTP(bp); asize = GET_SIZE(HDRP(bp)); /*Check for correct bucket*/ if( (int)get_list_no(asize) != i ) { printf("ERROR: Belong to wrong seg list %p\n",bp ); } if( next != NULL && GET_PREVP(next) != bp) { printf("ERROR: Link at block %p is broken\n",bp ); } } } }
/** * find_fit_segregated - Find the free block for the current requested size * in all the segregated lists * @param asize size of the free block to be searched */ static inline void *find_fit_segregated (size_t asize) { void *bp = 0; unsigned int list_no = get_list_no(asize); /* list_no is the minimum list it should start to search for*/ for (int i = list_no; i < NO_OF_LISTS; i++) { for (bp = GET_SEGI(seg_list,i); (bp!=NULL) && GET_SIZE(HDRP(bp)) > 0; bp = GET_NEXTP(bp)) { if ( bp!=NULL && (asize <= GET_SIZE(HDRP(bp)))) { return bp; } } } return NULL; /* No fit */ }
static void check_free_blocks(){ void *bp; unsigned int heapCount = 0; unsigned int listCount = 0; /*Iterate over list*/ for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) { if(!GET_ALLOC(HDRP(bp))) { heapCount++; } } /* Moving free list by pointers*/ for (bp = list_head; bp != NULL; bp = GET_NEXTP(bp) ){ listCount++; } if(heapCount!=listCount) { printf("ERROR: There is a mismatch between the free blocks in the heap_list and the free_list.\n"); } }
/** * print_free_block - Print the contents of the given free block, including * the pointers * @param bp Block to be printed */ static void print_free_block(void *bp) { size_t hsize = -1, halloc, fsize, falloc; if(bp != NULL) { hsize = GET_SIZE(HDRP(bp)); halloc = GET_ALLOC(HDRP(bp)); fsize = GET_SIZE(FTRP(bp)); falloc = GET_ALLOC(FTRP(bp)); dbg_printf("%p: header: [%lu:%c] footer: [%lu:%c]\n", bp,hsize, \ (halloc ? 'a' : 'f'), fsize, (falloc ? 'a' : 'f')); dbg_printf( "%p: next: [%p] prev: [%p]\n ", bp, GET_NEXTP(bp), \ GET_PREVP(bp) ); } else { dbg_printf("bp is null\n"); } if (hsize == 0) { dbg_printf("%p: EOL\n", bp); return; } }
/** * 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); }