void removeFromFreeList( heapAllocator* heap, block* b ) { //validateFreeList( heap ); block* const pFree = prevFree( b ); block* const nFree = nextFree( b ); //printf( "prev: " xPTRf ", next: " xPTRf "\n", (uintptr_t)pFree, (uintptr_t)nFree ); //vAssert( nFree || pFree ); if ( pFree ) { vAssert( pFree->free ); //vAssert( nFree || pFree->prevFree ); setNextFree( pFree, nFree ); } else { vAssert( heap->free == b ); if (!nFree ) { block* bl = heap->first; while (bl) { vAssert( bl == b || !bl->free ); bl = bl->next; } } heap->free = nFree; } if ( nFree ) { vAssert( nFree->free ); setPrevFree( nFree, pFree ); } setNextFree( b, NULL ); setPrevFree( b, NULL ); b->free = false; //validateFreeList( heap ); b->free = true; }
void addToFreeList( heapAllocator* heap, block* b ) { b->free = true; block* oldFree = heap->free; vAssert( oldFree != b ); setNextFree( b, oldFree ); vAssert( oldFree != b ); vAssert( oldFree == NULL || prevFree( oldFree ) == NULL ); // It should have been first in the list setPrevFree( oldFree, b ); heap->free = b; setPrevFree( b, NULL ); }
/* checkheap: Returns 0 if no errors were found, * otherwise returns the error */ int mm_checkheap(int verbose) { char *firstList = heap_listp + DSIZE; char *lastList = firstList + (LISTNUM-1) * DSIZE; char *curBlock = heap_listp + DSIZE; size_t size = getSize(getHeader(curBlock)); int freeCountHeap = 0; int freeCountList = 0; char *nextFreePtr; char *list; char *ptr; int s,a; size_t prevAlloc; verbose = verbose; /* check alignment padding*/ if (get(mem_heap_lo()) != 0) { printf("Alignment padding error!\n"); } /* check prologue */ if (!getAlloc(getHeader(heap_listp+DSIZE))) { printf("Prologue error!\n"); } /* Check the heap */ while (size != 0){ // Check whether the block is in heap if (!inHeap(curBlock)){ dbg_printf("Block %p is not in the heap\n", curBlock); } // Check each blk's address alignment if (!aligned(curBlock)) { dbg_printf("Block %p is not aligned.\n", curBlock); exit(0); } if (!getAlloc(getHeader(curBlock))){ freeCountHeap ++; // Check each blk's header and footer s = (getSize(getHeader(curBlock)) == getSize(getFooter(curBlock))); a = (getAlloc(getHeader(curBlock)) == getAlloc(getFooter(curBlock))); if (!s || !a) { dbg_printf("Header&Footer does not match in %p\n", curBlock); } // Check coalescing prevAlloc = getPrevAlloc(getHeader(curBlock)); if (!prevAlloc){ dbg_printf("Coalescing error! \n"); } } curBlock = nextBlock(curBlock); size = getSize(getHeader(curBlock)); } /* Check the free list */ for (list = firstList; list != (char *)lastList + DSIZE; list = (char *)list + DSIZE){ for (ptr = nextFree(list); ptr != list; ptr = nextFree(ptr)){ // Check the free list node is in heap if (!inHeap(ptr)){ dbg_printf("list node %p is not in the heap\n", ptr); } freeCountList ++; nextFreePtr = nextFree(ptr); // Check all next/prev pointers are consistent if (prevFree(nextFreePtr) != (ptr)){ dbg_printf("next/prev pointers is not consistent!\n"); } } } // Check free blocks by iterating thru every blk and traversing // free list by pointers and see if they match. if (freeCountHeap != freeCountList){ dbg_printf("Free block count does not match!\n"); } return 0; }
static inline void deleteListNode(void *bp){ put(pred(nextFree(bp)), get(pred(bp))); put(succ(prevFree(bp)), get(succ(bp))); }