/********************************************************** * remove_node * Removes a specified node from the segregated list of * free blocks **********************************************************/ void remove_node (int index, void *del_bp) { dlist *current = (dlist*) del_bp; if ((current->prev != NULL && !is_in_heap(current->prev)) || (current->next != NULL && !is_in_heap(current->next))) return; if (current->prev == NULL && current->next == NULL) if (sep_list_head[index] != current) return; PRINTDBG (("Removing node\n")); PRINTDBG (("H:%ld::", GET(HDRP((void*)current)))); PRINTDBG (("B:%p::", current)); PRINTDBG (("P:%p::", current->prev)); PRINTDBG (("N:%p\n", current->next)); if (current->prev != NULL) current->prev->next = current->next; else sep_list_head[index] = current->next; if (current->next != NULL) current->next->prev = current->prev; current->prev = NULL; current->next = NULL; }
/* remove a particular element of the heap */ void removeh( MxHeap *h, MxHeapable *t ) { MxHeapable *temp; int i, end; if( !is_in_heap( t ) ) return; i = get_heap_pos( t ); end = lengthb( &(h->data) ) - 1; MX_ASSERT( i >= 0 && i <= end ); not_in_heap( t ); if( i!= end ) { swaph( h, i, end ); chopb( &(h->data) ); temp = (MxHeapable *) getpb ( &(h->data), i ); if( get_heap_key( temp ) < get_heap_key( t ) ) downheaph( h, i ); else upheaph( h, i ); } else chopb( &(h->data) ); /* remove last element */ }
/********************************************************** * mm_check * Check the consistency of the memory heap * Return nonzero if the heap is consistant. *********************************************************/ int mm_check(void) { #ifdef DEBUG_BUILD int i; dlist *current; void *bp; //To point at the start of the heap size_t size = 0; //To measure the total size of blocks on the heap // Check if every block in free list is actually marked free. // If a allocated block is in the free list then it may result in segmentation faults // while accessing the prev and next pointers. It will also result in the allocater manipulating // memory allocated to user which will be nasty! for(i = 0; i < NUM_SEG_LIST; i++) { current = sep_list_head[i]; while (current != NULL) { // Check if block is in heap if (is_in_heap((void*)current) == 0) return 0; if (GET_ALLOC(HDRP((void*)current))) { PRINTDBG (("block (%p) in free list is allocated!\n", current)); return 0; } current = current->next; } } // Check if there exist any free blocks that may have escaped coalescing. // If found, these cases result in internal fragmentation. for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) { // Check if the current block is free and the next block is also free // Since this is done in a sequential manner, we don't need to check the previous blk if (!GET_ALLOC(HDRP(bp)) && (!GET_ALLOC(HDRP(NEXT_BLKP(bp))))) { PRINTDBG (("Consecutive blocks (%p, %p) have escaped coalescing!\n", bp, NEXT_BLKP(bp))); return 0; } } for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) { if (bp < mem_heap_lo() || bp > mem_heap_hi()) { PRINTDBG (("Block is outside the heap.\n")); return 0; } else { size += GET_SIZE(HDRP(bp)); //Add the size of the current block to the total size of the calculated blocks on the heap so far } } if (size == mem_heapsize()) PRINTDBG (("The total size of all blocks on the heap match the heap size.\n")); if (mem_heapsize() > mem_pagesize()) //Check if heap size exceeded systems page size PRINTDBG (("Heap size is more than page size. TLB misses might occur\n")); char c; scanf("%c\n", &c); #endif return 1; }