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