Home | History | Annotate | Line # | Download | only in raidframe
rf_debugMem.c revision 1.21
      1 /*	$NetBSD: rf_debugMem.c,v 1.21 2011/05/01 06:49:43 mrg 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 (at) 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.21 2011/05/01 06:49:43 mrg 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 #include "rf_shutdown.h"
     44 
     45 #if RF_DEBUG_MEM
     46 
     47 static long tot_mem_in_use = 0;
     48 
     49 /* Hash table of information about memory allocations */
     50 #define RF_MH_TABLESIZE 1000
     51 
     52 struct mh_struct {
     53 	void   *address;
     54 	int     size;
     55 	int     line;
     56 	const char *filen;
     57 	char    allocated;
     58 	struct mh_struct *next;
     59 };
     60 static struct mh_struct *mh_table[RF_MH_TABLESIZE];
     61 static rf_declare_mutex2(rf_debug_mem_mutex);
     62 static int mh_table_initialized = 0;
     63 
     64 static void memory_hash_insert(void *addr, int size, int line, const char *filen);
     65 static int memory_hash_remove(void *addr, int sz);
     66 
     67 void
     68 rf_record_malloc(void *p, int size, int line, const char *filen)
     69 {
     70 	RF_ASSERT(size != 0);
     71 
     72 	/* rf_lock_mutex2(rf_debug_mem_mutex); */
     73 	memory_hash_insert(p, size, line, filen);
     74 	tot_mem_in_use += size;
     75 	/* rf_unlock_mutex2(rf_debug_mem_mutex); */
     76 	if ((long) p == rf_memDebugAddress) {
     77 		printf("Allocate: debug address allocated from line %d file %s\n", line, filen);
     78 	}
     79 }
     80 
     81 void
     82 rf_unrecord_malloc(void *p, int sz)
     83 {
     84 	int     size;
     85 
     86 	/* rf_lock_mutex2(rf_debug_mem_mutex); */
     87 	size = memory_hash_remove(p, sz);
     88 	tot_mem_in_use -= size;
     89 	/* rf_unlock_mutex2(rf_debug_mem_mutex); */
     90 	if ((long) p == rf_memDebugAddress) {
     91 		printf("Free: Found debug address\n");	/* this is really only a
     92 							 * flag line for gdb */
     93 	}
     94 }
     95 
     96 void
     97 rf_print_unfreed(void)
     98 {
     99 	int     i, foundone = 0;
    100 	struct mh_struct *p;
    101 
    102 	for (i = 0; i < RF_MH_TABLESIZE; i++) {
    103 		for (p = mh_table[i]; p; p = p->next)
    104 			if (p->allocated) {
    105 				if (!foundone)
    106 					printf("\n\nThere are unfreed memory locations at program shutdown:\n");
    107 				foundone = 1;
    108 				printf("Addr 0x%lx Size %d line %d file %s\n",
    109 				    (long) p->address, p->size, p->line, p->filen);
    110 			}
    111 	}
    112 	if (tot_mem_in_use) {
    113 		printf("%ld total bytes in use\n", tot_mem_in_use);
    114 	}
    115 }
    116 #endif /* RF_DEBUG_MEM */
    117 
    118 #if RF_DEBUG_MEM
    119 static void
    120 rf_ShutdownDebugMem(void *unused)
    121 {
    122 	rf_destroy_mutex2(rf_debug_mem_mutex);
    123 }
    124 #endif
    125 
    126 int
    127 rf_ConfigureDebugMem(RF_ShutdownList_t **listp)
    128 {
    129 #if RF_DEBUG_MEM
    130 	int     i;
    131 
    132 	rf_init_mutex2(rf_debug_mem_mutex, IPL_VM);
    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 	rf_ShutdownCreate(listp, rf_ShutdownDebugMem, NULL);
    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(void *addr, int size, int line, const char *filen)
    149 {
    150 	unsigned long bucket = HASHADDR(addr);
    151 	struct mh_struct *p;
    152 
    153 	RF_ASSERT(mh_table_initialized);
    154 
    155 	/* search for this address in the hash table */
    156 	for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
    157 	if (!p) {
    158 		RF_Malloc(p, sizeof(struct mh_struct), (struct mh_struct *));
    159 		RF_ASSERT(p);
    160 		p->next = mh_table[bucket];
    161 		mh_table[bucket] = p;
    162 		p->address = addr;
    163 		p->allocated = 0;
    164 	}
    165 	if (p->allocated) {
    166 		printf("ERROR:  reallocated address 0x%lx from line %d, file %s without intervening free\n", (long) addr, line, filen);
    167 		printf("        last allocated from line %d file %s\n", p->line, p->filen);
    168 		RF_ASSERT(0);
    169 	}
    170 	p->size = size;
    171 	p->line = line;
    172 	p->filen = filen;
    173 	p->allocated = 1;
    174 }
    175 
    176 static int
    177 memory_hash_remove(void *addr, 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 #endif /* RF_DEBUG_MEM */
    202 
    203 
    204