util: A few more memory debugging checks.

This commit is contained in:
José Fonseca 2008-09-18 23:00:18 +09:00
parent 451888ee8f
commit eb5c288df4

View file

@ -122,8 +122,12 @@ debug_malloc(const char *file, unsigned line, const char *function,
struct debug_memory_footer *ftr; struct debug_memory_footer *ftr;
hdr = real_malloc(sizeof(*hdr) + size + sizeof(*ftr)); hdr = real_malloc(sizeof(*hdr) + size + sizeof(*ftr));
if(!hdr) if(!hdr) {
debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
file, line, function,
(long unsigned)size);
return NULL; return NULL;
}
hdr->no = last_no++; hdr->no = last_no++;
hdr->file = file; hdr->file = file;
@ -219,8 +223,12 @@ debug_realloc(const char *file, unsigned line, const char *function,
/* alloc new */ /* alloc new */
new_hdr = real_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr)); new_hdr = real_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr));
if(!new_hdr) if(!new_hdr) {
debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
file, line, function,
(long unsigned)new_size);
return NULL; return NULL;
}
new_hdr->no = old_hdr->no; new_hdr->no = old_hdr->no;
new_hdr->file = old_hdr->file; new_hdr->file = old_hdr->file;
new_hdr->line = old_hdr->line; new_hdr->line = old_hdr->line;
@ -261,8 +269,19 @@ debug_memory_end(unsigned long start_no)
for (; entry != &list; entry = entry->prev) { for (; entry != &list; entry = entry->prev) {
struct debug_memory_header *hdr; struct debug_memory_header *hdr;
void *ptr; void *ptr;
struct debug_memory_footer *ftr;
hdr = LIST_ENTRY(struct debug_memory_header, entry, head); hdr = LIST_ENTRY(struct debug_memory_header, entry, head);
ptr = data_from_header(hdr); ptr = data_from_header(hdr);
ftr = footer_from_header(hdr);
if(hdr->magic != DEBUG_MEMORY_MAGIC) {
debug_printf("%s:%u:%s: bad or corrupted memory %p\n",
hdr->file, hdr->line, hdr->function,
ptr);
debug_assert(0);
}
if((start_no <= hdr->no && hdr->no < last_no) || if((start_no <= hdr->no && hdr->no < last_no) ||
(last_no < start_no && (hdr->no < last_no || start_no <= hdr->no))) { (last_no < start_no && (hdr->no < last_no || start_no <= hdr->no))) {
debug_printf("%s:%u:%s: %u bytes at %p not freed\n", debug_printf("%s:%u:%s: %u bytes at %p not freed\n",
@ -270,7 +289,15 @@ debug_memory_end(unsigned long start_no)
hdr->size, ptr); hdr->size, ptr);
total_size += hdr->size; total_size += hdr->size;
} }
if(ftr->magic != DEBUG_MEMORY_MAGIC) {
debug_printf("%s:%u:%s: buffer overflow %p\n",
hdr->file, hdr->line, hdr->function,
ptr);
debug_assert(0);
}
} }
if(total_size) { if(total_size) {
debug_printf("Total of %u KB of system memory apparently leaked\n", debug_printf("Total of %u KB of system memory apparently leaked\n",
(total_size + 1023)/1024); (total_size + 1023)/1024);