static void check_malloc(void *p, size_t sz) { void *P, *Q; int B, I; if (!dbg_initd) check_init(); B = DBG_HASH_BUCKET(p); for (I = 0; I < DBG_ARRY_SZ; I++) { if (!(P = malloc_ptrs[B][I])) continue; Q = P + malloc_size[B][I]; if (P <= p && p < Q) { snprintf(msg, 128, "xmalloc: ERROR: p=%p falls in P=%p+%d", p, P, malloc_size[B][I]); (*failure_notify) (msg); } } for (I = 0; I < DBG_ARRY_SZ; I++) { if (malloc_ptrs[B][I]) continue; malloc_ptrs[B][I] = p; malloc_size[B][I] = (int) sz; #if XMALLOC_TRACE malloc_file[B][I] = xmalloc_file; malloc_line[B][I] = xmalloc_line; malloc_count[B][I] = xmalloc_count; #endif break; } if (I == DBG_ARRY_SZ) (*failure_notify) ("xmalloc: debug out of array space!"); }
static void check_free(void *s) { int B, I; B = DBG_HASH_BUCKET(s); for (I = 0; I < DBG_ARRY_SZ; I++) { if (malloc_ptrs[B][I] != s) continue; malloc_ptrs[B][I] = NULL; malloc_size[B][I] = 0; #if XMALLOC_TRACE malloc_file[B][I] = NULL; malloc_line[B][I] = 0; malloc_count[B][I] = 0; #endif break; } if (I == DBG_ARRY_SZ) { snprintf(msg, 128, "xfree: ERROR: s=%p not found!", s); (*failure_notify) (msg); } }
int malloc_number(void *p) { int B, I; B = DBG_HASH_BUCKET(p); for (I = 0; I < DBG_ARRY_SZ; I++) { if (malloc_ptrs[B][I] == p) return malloc_count[B][I]; } return 0; }
static char * malloc_file_name(void *p) { int B, I; B = DBG_HASH_BUCKET(p); for (I = 0; I < DBG_ARRY_SZ; I++) { if (malloc_ptrs[B][I] == p) return malloc_file[B][I]; } return 0; }
size_t xmallocblksize(void *p) { int B, I; B = DBG_HASH_BUCKET(p); for (I = 0; I < DBG_ARRY_SZ; I++) { if (malloc_ptrs[B][I] == p) return malloc_size[B][I]; } return 0; }
static void xmalloc_scan_region(void *start, int size, int depth) { int B, I; char *ptr = start; char *end = ptr + size - XMALLOC_LEAK_ALIGN; static int sum = 0; while (ptr <= end) { void *p = *(void **) ptr; if (p && p != start) { B = DBG_HASH_BUCKET(p); for (I = 0; I < DBG_ARRY_SZ; I++) { if (malloc_ptrs[B][I] == p) { if (!malloc_refs[B][I]++) { /* A new reference */ fprintf(stderr, "%*s%p %s:%d size %d allocation %d\n", depth, "", malloc_ptrs[B][I], malloc_file[B][I], malloc_line[B][I], malloc_size[B][I], malloc_count[B][I]); sum += malloc_size[B][I]; xmalloc_scan_region(malloc_ptrs[B][I], malloc_size[B][I], depth + 1); if (depth == 0) { if (sum != malloc_size[B][I]) fprintf(stderr, "=== %d bytes\n", sum); sum = 0; } #if XMALLOC_SHOW_ALL_REFERENCES } else { /* We have already scanned this pointer... */ fprintf(stderr, "%*s%p %s:%d size %d allocation %d ... (%d)\n", depth * 2, "", malloc_ptrs[B][I], malloc_file[B][I], malloc_line[B][I], malloc_size[B][I], malloc_count[B][I], malloc_refs[B][I]); #endif } } } } ptr += XMALLOC_LEAK_ALIGN; } }