1 /* $NetBSD: rf_debugMem.c,v 1.7 2000/01/07 03:40:59 oster Exp $ */ 2 /* 3 * Copyright (c) 1995 Carnegie-Mellon University. 4 * All rights reserved. 5 * 6 * Author: Daniel Stodolsky, Mark Holland, Jim Zelenka 7 * 8 * Permission to use, copy, modify and distribute this software and 9 * its documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 */ 28 29 /* debugMem.c: memory usage debugging stuff. 30 * Malloc, Calloc, and Free are #defined everywhere 31 * to do_malloc, do_calloc, and do_free. 32 * 33 * if RF_UTILITY is nonzero, it means were compiling one of the 34 * raidframe utility programs, such as rfctrl or smd. In this 35 * case, we eliminate all references to the threads package 36 * and to the allocation list stuff. 37 */ 38 39 #include "rf_types.h" 40 41 #include "rf_threadstuff.h" 42 #include "rf_options.h" 43 #include "rf_debugMem.h" 44 #include "rf_general.h" 45 46 static long tot_mem_in_use = 0; 47 48 /* Hash table of information about memory allocations */ 49 #define RF_MH_TABLESIZE 1000 50 51 struct mh_struct { 52 void *address; 53 int size; 54 int line; 55 char *filen; 56 char allocated; 57 struct mh_struct *next; 58 }; 59 static struct mh_struct *mh_table[RF_MH_TABLESIZE]; 60 RF_DECLARE_MUTEX(rf_debug_mem_mutex) 61 static int mh_table_initialized = 0; 62 63 static void memory_hash_insert(void *addr, int size, int line, char *filen); 64 static int memory_hash_remove(void *addr, int sz); 65 66 void 67 rf_record_malloc(p, size, line, filen) 68 void *p; 69 int size, line; 70 char *filen; 71 { 72 RF_ASSERT(size != 0); 73 74 /* RF_LOCK_MUTEX(rf_debug_mem_mutex); */ 75 memory_hash_insert(p, size, line, filen); 76 tot_mem_in_use += size; 77 /* RF_UNLOCK_MUTEX(rf_debug_mem_mutex); */ 78 if ((long) p == rf_memDebugAddress) { 79 printf("Allocate: debug address allocated from line %d file %s\n", line, filen); 80 } 81 } 82 83 void 84 rf_unrecord_malloc(p, sz) 85 void *p; 86 int sz; 87 { 88 int size; 89 90 /* RF_LOCK_MUTEX(rf_debug_mem_mutex); */ 91 size = memory_hash_remove(p, sz); 92 tot_mem_in_use -= size; 93 /* RF_UNLOCK_MUTEX(rf_debug_mem_mutex); */ 94 if ((long) p == rf_memDebugAddress) { 95 printf("Free: Found debug address\n"); /* this is really only a 96 * flag line for gdb */ 97 } 98 } 99 100 void 101 rf_print_unfreed() 102 { 103 int i, foundone = 0; 104 struct mh_struct *p; 105 106 for (i = 0; i < RF_MH_TABLESIZE; i++) { 107 for (p = mh_table[i]; p; p = p->next) 108 if (p->allocated) { 109 if (!foundone) 110 printf("\n\nThere are unfreed memory locations at program shutdown:\n"); 111 foundone = 1; 112 printf("Addr 0x%lx Size %d line %d file %s\n", 113 (long) p->address, p->size, p->line, p->filen); 114 } 115 } 116 if (tot_mem_in_use) { 117 printf("%ld total bytes in use\n", tot_mem_in_use); 118 } 119 } 120 121 int 122 rf_ConfigureDebugMem(listp) 123 RF_ShutdownList_t **listp; 124 { 125 int i, rc; 126 127 rc = rf_create_managed_mutex(listp, &rf_debug_mem_mutex); 128 if (rc) { 129 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__, 130 __LINE__, rc); 131 return (rc); 132 } 133 if (rf_memDebug) { 134 for (i = 0; i < RF_MH_TABLESIZE; i++) 135 mh_table[i] = NULL; 136 mh_table_initialized = 1; 137 } 138 return (0); 139 } 140 #define HASHADDR(_a_) ( (((unsigned long) _a_)>>3) % RF_MH_TABLESIZE ) 141 142 static void 143 memory_hash_insert(addr, size, line, filen) 144 void *addr; 145 int size, line; 146 char *filen; 147 { 148 unsigned long bucket = HASHADDR(addr); 149 struct mh_struct *p; 150 151 RF_ASSERT(mh_table_initialized); 152 153 /* search for this address in the hash table */ 154 for (p = mh_table[bucket]; p && (p->address != addr); p = p->next); 155 if (!p) { 156 RF_Malloc(p, sizeof(struct mh_struct), (struct mh_struct *)); 157 RF_ASSERT(p); 158 p->next = mh_table[bucket]; 159 mh_table[bucket] = p; 160 p->address = addr; 161 p->allocated = 0; 162 } 163 if (p->allocated) { 164 printf("ERROR: reallocated address 0x%lx from line %d, file %s without intervening free\n", (long) addr, line, filen); 165 printf(" last allocated from line %d file %s\n", p->line, p->filen); 166 RF_ASSERT(0); 167 } 168 p->size = size; 169 p->line = line; 170 p->filen = filen; 171 p->allocated = 1; 172 } 173 174 static int 175 memory_hash_remove(addr, sz) 176 void *addr; 177 int sz; 178 { 179 unsigned long bucket = HASHADDR(addr); 180 struct mh_struct *p; 181 182 RF_ASSERT(mh_table_initialized); 183 for (p = mh_table[bucket]; p && (p->address != addr); p = p->next); 184 if (!p) { 185 printf("ERROR: freeing never-allocated address 0x%lx\n", (long) addr); 186 RF_PANIC(); 187 } 188 if (!p->allocated) { 189 printf("ERROR: freeing unallocated address 0x%lx. Last allocation line %d file %s\n", (long) addr, p->line, p->filen); 190 RF_PANIC(); 191 } 192 if (sz > 0 && p->size != sz) { /* you can suppress this error by 193 * using a negative value as the size 194 * to free */ 195 printf("ERROR: incorrect size at free for address 0x%lx: is %d should be %d. Alloc at line %d of file %s\n", (unsigned long) addr, sz, p->size, p->line, p->filen); 196 RF_PANIC(); 197 } 198 p->allocated = 0; 199 return (p->size); 200 } 201