void check_mem_corrupt(const char *program_name) { size_t counter = 0; memory_block *block_ptr; fprintf(stderr, "%s: checking memory blocks for corruption\n", program_name); for (block_ptr = list_head; block_ptr != NULL; block_ptr = block_ptr->next) { unsigned char *ptr = (unsigned char*)&block_ptr->begin; if (memcmp(ptr - sizeof(block_ptr), &block_ptr, sizeof(block_ptr))) { fprintf(stderr, "Fatal error: memory corruption detected in front " "of pointer %p\n", ptr); abort(); } if (memcmp(ptr + block_ptr->size, &block_ptr, sizeof(block_ptr))) { fprintf(stderr, "Fatal error: memory corruption detected at the " "end of pointer %p\n", ptr); abort(); } counter++; } fprintf(stderr, "%s: %lu memory block%s OK\n", program_name, (unsigned long) counter, counter > 1 ? "s are" : " is"); #ifdef MEMORY_DEBUG_FREE check_free_list(); #endif }
/* * Requires: * None. * * Effects: * Perform a minimal check of the heap for consistency. */ void checkheap(bool verbose) { void *bp; if (verbose) printf("Heap (%p):\n", heap_listp); if (GET_SIZE(HDRP(heap_listp)) != DSIZE || !GET_ALLOC(HDRP(heap_listp))) printf("Bad prologue header\n"); checkblock(heap_listp); for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = (void *)NEXT_BLKP(bp)) { if (verbose) printblock(bp); checkblock(bp); } if (verbose) printblock(bp); if (GET_SIZE(HDRP(bp)) != 0 || !GET_ALLOC(HDRP(bp))) printf("Bad epilogue header\n"); in_heap(bp); check_coalescing(); check_free_list(); check_free_blocks(); }
void check_mem_leak(const char *program_name) { #ifdef MEMORY_DEBUG fprintf(stderr, "%s(%d): memory usage statistics:\n" "total allocations: %lu\n" "malloc/new calls: %lu\n" "free/delete calls: %lu\n" "peak memory usage: %lu bytes\n" "average block size: %g bytes\n", program_name, (int)getpid(), (unsigned long)alloc_count, (unsigned long)malloc_count, (unsigned long) free_count, (unsigned long) peak_size, (double)total_allocated / (double)alloc_count); if (list_head != NULL) { memory_block *block_ptr = list_head; size_t counter = 0; fprintf(stderr, "unallocated blocks:\n"); do { if (block_ptr->filename != 0) { fprintf(stderr, FILENAME_FORMAT, block_ptr->filename, block_ptr->lineno); } fprintf(stderr, "\tMemory leak at %p, size %lu, ord %lu: ", (void*)&block_ptr->begin, (unsigned long)block_ptr->size, (unsigned long)block_ptr->ordinal); { const unsigned char * mm = (const unsigned char*)&block_ptr->begin; size_t x, limit = (block_ptr->size > maxprint) ? maxprint : block_ptr->size; for (x = 0; x < limit; ++x) { fputc( isprint(mm[x]) ? mm[x] : '.', stderr ); } fputc(10, stderr); } block_ptr = block_ptr->next; counter++; } while (block_ptr != NULL); fprintf(stderr, "total unallocated: %lu bytes in %lu blocks\n", (unsigned long) allocated_size, (unsigned long) counter); } #ifdef MEMORY_DEBUG_FREE check_free_list(); release_free_blocks(); #endif #else if (malloc_count != free_count) { fprintf(stderr, "%s: warning: memory leakage detected.\n" "Total malloc calls: %lu, free calls: %lu\n" "Please submit a bug report including the current input file(s).\n", program_name, (unsigned long) malloc_count, (unsigned long) free_count); } #endif }
// Returns 0 if no errors were found, otherwise returns the error int mm_checkheap(int verbose) { void *bp = (void *)(heap_listp+1); int flag=0; if (verbose) printf("Heap (%p):\n", heap_listp); flag=check_heap(1)|check_free_list(1); print_heap(bp); print_list(num); if(flag) return 1; return 0; }