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