1 /* $NetBSD: rf_debugMem.c,v 1.12 2002/11/23 02:44:15 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 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: rf_debugMem.c,v 1.12 2002/11/23 02:44:15 oster Exp $"); 36 37 #include <dev/raidframe/raidframevar.h> 38 39 #include "rf_threadstuff.h" 40 #include "rf_options.h" 41 #include "rf_debugMem.h" 42 #include "rf_general.h" 43 44 #if RF_DEBUG_MEM 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 #endif /* RF_DEBUG_MEM */ 121 122 int 123 rf_ConfigureDebugMem(listp) 124 RF_ShutdownList_t **listp; 125 { 126 #if RF_DEBUG_MEM 127 int i, rc; 128 129 rc = rf_create_managed_mutex(listp, &rf_debug_mem_mutex); 130 if (rc) { 131 rf_print_unable_to_init_mutex( __FILE__, __LINE__, rc); 132 return (rc); 133 } 134 if (rf_memDebug) { 135 for (i = 0; i < RF_MH_TABLESIZE; i++) 136 mh_table[i] = NULL; 137 mh_table_initialized = 1; 138 } 139 #endif 140 return (0); 141 } 142 143 #if RF_DEBUG_MEM 144 145 #define HASHADDR(_a_) ( (((unsigned long) _a_)>>3) % RF_MH_TABLESIZE ) 146 147 static void 148 memory_hash_insert(addr, size, line, filen) 149 void *addr; 150 int size, line; 151 char *filen; 152 { 153 unsigned long bucket = HASHADDR(addr); 154 struct mh_struct *p; 155 156 RF_ASSERT(mh_table_initialized); 157 158 /* search for this address in the hash table */ 159 for (p = mh_table[bucket]; p && (p->address != addr); p = p->next); 160 if (!p) { 161 RF_Malloc(p, sizeof(struct mh_struct), (struct mh_struct *)); 162 RF_ASSERT(p); 163 p->next = mh_table[bucket]; 164 mh_table[bucket] = p; 165 p->address = addr; 166 p->allocated = 0; 167 } 168 if (p->allocated) { 169 printf("ERROR: reallocated address 0x%lx from line %d, file %s without intervening free\n", (long) addr, line, filen); 170 printf(" last allocated from line %d file %s\n", p->line, p->filen); 171 RF_ASSERT(0); 172 } 173 p->size = size; 174 p->line = line; 175 p->filen = filen; 176 p->allocated = 1; 177 } 178 179 static int 180 memory_hash_remove(addr, sz) 181 void *addr; 182 int sz; 183 { 184 unsigned long bucket = HASHADDR(addr); 185 struct mh_struct *p; 186 187 RF_ASSERT(mh_table_initialized); 188 for (p = mh_table[bucket]; p && (p->address != addr); p = p->next); 189 if (!p) { 190 printf("ERROR: freeing never-allocated address 0x%lx\n", (long) addr); 191 RF_PANIC(); 192 } 193 if (!p->allocated) { 194 printf("ERROR: freeing unallocated address 0x%lx. Last allocation line %d file %s\n", (long) addr, p->line, p->filen); 195 RF_PANIC(); 196 } 197 if (sz > 0 && p->size != sz) { /* you can suppress this error by 198 * using a negative value as the size 199 * to free */ 200 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); 201 RF_PANIC(); 202 } 203 p->allocated = 0; 204 return (p->size); 205 } 206 #endif /* RF_DEBUG_MEM */ 207 208 209