Home | History | Annotate | Line # | Download | only in raidframe
rf_debugMem.c revision 1.6
      1  1.6  oster /*	$NetBSD: rf_debugMem.c,v 1.6 1999/09/04 21:11:21 oster 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.1  oster 
     39  1.1  oster #include "rf_types.h"
     40  1.1  oster 
     41  1.1  oster #include "rf_threadstuff.h"
     42  1.1  oster #include "rf_threadid.h"
     43  1.1  oster #include "rf_options.h"
     44  1.1  oster #include "rf_debugMem.h"
     45  1.1  oster #include "rf_general.h"
     46  1.1  oster 
     47  1.6  oster static long tot_mem_in_use = 0;
     48  1.1  oster 
     49  1.1  oster /* Hash table of information about memory allocations */
     50  1.1  oster #define RF_MH_TABLESIZE 1000
     51  1.1  oster 
     52  1.1  oster struct mh_struct {
     53  1.3  oster 	void   *address;
     54  1.3  oster 	int     size;
     55  1.3  oster 	int     line;
     56  1.3  oster 	char   *filen;
     57  1.3  oster 	char    allocated;
     58  1.3  oster 	struct mh_struct *next;
     59  1.1  oster };
     60  1.1  oster static struct mh_struct *mh_table[RF_MH_TABLESIZE];
     61  1.1  oster RF_DECLARE_MUTEX(rf_debug_mem_mutex)
     62  1.3  oster 	static int mh_table_initialized = 0;
     63  1.1  oster 
     64  1.3  oster 	static void memory_hash_insert(void *addr, int size, int line, char *filen);
     65  1.3  oster 	static int memory_hash_remove(void *addr, int sz);
     66  1.1  oster 
     67  1.3  oster void
     68  1.3  oster rf_record_malloc(p, size, line, filen)
     69  1.3  oster 	void   *p;
     70  1.3  oster 	int     size, line;
     71  1.3  oster 	char   *filen;
     72  1.3  oster {
     73  1.3  oster 	RF_ASSERT(size != 0);
     74  1.3  oster 
     75  1.3  oster 	/* RF_LOCK_MUTEX(rf_debug_mem_mutex); */
     76  1.3  oster 	memory_hash_insert(p, size, line, filen);
     77  1.3  oster 	tot_mem_in_use += size;
     78  1.3  oster 	/* RF_UNLOCK_MUTEX(rf_debug_mem_mutex); */
     79  1.3  oster 	if ((long) p == rf_memDebugAddress) {
     80  1.3  oster 		printf("Allocate: debug address allocated from line %d file %s\n", line, filen);
     81  1.3  oster 	}
     82  1.1  oster }
     83  1.1  oster 
     84  1.3  oster void
     85  1.3  oster rf_unrecord_malloc(p, sz)
     86  1.3  oster 	void   *p;
     87  1.3  oster 	int     sz;
     88  1.3  oster {
     89  1.3  oster 	int     size;
     90  1.3  oster 
     91  1.3  oster 	/* RF_LOCK_MUTEX(rf_debug_mem_mutex); */
     92  1.3  oster 	size = memory_hash_remove(p, sz);
     93  1.3  oster 	tot_mem_in_use -= size;
     94  1.3  oster 	/* RF_UNLOCK_MUTEX(rf_debug_mem_mutex); */
     95  1.3  oster 	if ((long) p == rf_memDebugAddress) {
     96  1.3  oster 		printf("Free: Found debug address\n");	/* this is really only a
     97  1.3  oster 							 * flag line for gdb */
     98  1.3  oster 	}
     99  1.3  oster }
    100  1.3  oster 
    101  1.3  oster void
    102  1.3  oster rf_print_unfreed()
    103  1.3  oster {
    104  1.3  oster 	int     i, foundone = 0;
    105  1.3  oster 	struct mh_struct *p;
    106  1.3  oster 
    107  1.3  oster 	for (i = 0; i < RF_MH_TABLESIZE; i++) {
    108  1.3  oster 		for (p = mh_table[i]; p; p = p->next)
    109  1.3  oster 			if (p->allocated) {
    110  1.3  oster 				if (!foundone)
    111  1.3  oster 					printf("\n\nThere are unfreed memory locations at program shutdown:\n");
    112  1.3  oster 				foundone = 1;
    113  1.3  oster 				printf("Addr 0x%lx Size %d line %d file %s\n",
    114  1.3  oster 				    (long) p->address, p->size, p->line, p->filen);
    115  1.3  oster 			}
    116  1.3  oster 	}
    117  1.3  oster 	if (tot_mem_in_use) {
    118  1.3  oster 		printf("%ld total bytes in use\n", tot_mem_in_use);
    119  1.3  oster 	}
    120  1.3  oster }
    121  1.3  oster 
    122  1.3  oster int
    123  1.3  oster rf_ConfigureDebugMem(listp)
    124  1.3  oster 	RF_ShutdownList_t **listp;
    125  1.3  oster {
    126  1.3  oster 	int     i, rc;
    127  1.3  oster 
    128  1.3  oster 	rc = rf_create_managed_mutex(listp, &rf_debug_mem_mutex);
    129  1.3  oster 	if (rc) {
    130  1.3  oster 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    131  1.3  oster 		    __LINE__, rc);
    132  1.3  oster 		return (rc);
    133  1.3  oster 	}
    134  1.3  oster 	if (rf_memDebug) {
    135  1.3  oster 		for (i = 0; i < RF_MH_TABLESIZE; i++)
    136  1.3  oster 			mh_table[i] = NULL;
    137  1.3  oster 		mh_table_initialized = 1;
    138  1.3  oster 	}
    139  1.3  oster 	return (0);
    140  1.3  oster }
    141  1.1  oster #define HASHADDR(_a_)      ( (((unsigned long) _a_)>>3) % RF_MH_TABLESIZE )
    142  1.1  oster 
    143  1.3  oster static void
    144  1.3  oster memory_hash_insert(addr, size, line, filen)
    145  1.3  oster 	void   *addr;
    146  1.3  oster 	int     size, line;
    147  1.3  oster 	char   *filen;
    148  1.3  oster {
    149  1.3  oster 	unsigned long bucket = HASHADDR(addr);
    150  1.3  oster 	struct mh_struct *p;
    151  1.3  oster 
    152  1.3  oster 	RF_ASSERT(mh_table_initialized);
    153  1.3  oster 
    154  1.3  oster 	/* search for this address in the hash table */
    155  1.3  oster 	for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
    156  1.3  oster 	if (!p) {
    157  1.3  oster 		RF_Malloc(p, sizeof(struct mh_struct), (struct mh_struct *));
    158  1.3  oster 		RF_ASSERT(p);
    159  1.3  oster 		p->next = mh_table[bucket];
    160  1.3  oster 		mh_table[bucket] = p;
    161  1.3  oster 		p->address = addr;
    162  1.3  oster 		p->allocated = 0;
    163  1.3  oster 	}
    164  1.3  oster 	if (p->allocated) {
    165  1.3  oster 		printf("ERROR:  reallocated address 0x%lx from line %d, file %s without intervening free\n", (long) addr, line, filen);
    166  1.3  oster 		printf("        last allocated from line %d file %s\n", p->line, p->filen);
    167  1.3  oster 		RF_ASSERT(0);
    168  1.3  oster 	}
    169  1.3  oster 	p->size = size;
    170  1.3  oster 	p->line = line;
    171  1.3  oster 	p->filen = filen;
    172  1.3  oster 	p->allocated = 1;
    173  1.3  oster }
    174  1.3  oster 
    175  1.3  oster static int
    176  1.3  oster memory_hash_remove(addr, sz)
    177  1.3  oster 	void   *addr;
    178  1.3  oster 	int     sz;
    179  1.3  oster {
    180  1.3  oster 	unsigned long bucket = HASHADDR(addr);
    181  1.3  oster 	struct mh_struct *p;
    182  1.3  oster 
    183  1.3  oster 	RF_ASSERT(mh_table_initialized);
    184  1.3  oster 	for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
    185  1.3  oster 	if (!p) {
    186  1.3  oster 		printf("ERROR:  freeing never-allocated address 0x%lx\n", (long) addr);
    187  1.3  oster 		RF_PANIC();
    188  1.3  oster 	}
    189  1.3  oster 	if (!p->allocated) {
    190  1.3  oster 		printf("ERROR:  freeing unallocated address 0x%lx.  Last allocation line %d file %s\n", (long) addr, p->line, p->filen);
    191  1.3  oster 		RF_PANIC();
    192  1.3  oster 	}
    193  1.3  oster 	if (sz > 0 && p->size != sz) {	/* you can suppress this error by
    194  1.3  oster 					 * using a negative value as the size
    195  1.3  oster 					 * to free */
    196  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);
    197  1.3  oster 		RF_PANIC();
    198  1.3  oster 	}
    199  1.3  oster 	p->allocated = 0;
    200  1.3  oster 	return (p->size);
    201  1.1  oster }
    202