Home | History | Annotate | Line # | Download | only in raidframe
rf_debugMem.c revision 1.18.24.1
      1  1.18.24.1      matt /*	rf_debugMem.c,v 1.18 2006/11/16 01:33:23 christos 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.9     lukem 
     34        1.9     lukem #include <sys/cdefs.h>
     35  1.18.24.1      matt __KERNEL_RCSID(0, "rf_debugMem.c,v 1.18 2006/11/16 01:33:23 christos Exp");
     36        1.1     oster 
     37        1.8     oster #include <dev/raidframe/raidframevar.h>
     38        1.1     oster 
     39        1.1     oster #include "rf_threadstuff.h"
     40        1.1     oster #include "rf_options.h"
     41        1.1     oster #include "rf_debugMem.h"
     42        1.1     oster #include "rf_general.h"
     43        1.1     oster 
     44       1.11     oster #if RF_DEBUG_MEM
     45       1.11     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.11     oster static int mh_table_initialized = 0;
     62        1.1     oster 
     63       1.11     oster static void memory_hash_insert(void *addr, int size, int line, char *filen);
     64       1.11     oster static int memory_hash_remove(void *addr, int sz);
     65        1.1     oster 
     66       1.15     perry void
     67       1.14     oster rf_record_malloc(void *p, int size, int line, char *filen)
     68        1.3     oster {
     69        1.3     oster 	RF_ASSERT(size != 0);
     70        1.3     oster 
     71        1.3     oster 	/* RF_LOCK_MUTEX(rf_debug_mem_mutex); */
     72        1.3     oster 	memory_hash_insert(p, size, line, filen);
     73        1.3     oster 	tot_mem_in_use += size;
     74        1.3     oster 	/* RF_UNLOCK_MUTEX(rf_debug_mem_mutex); */
     75        1.3     oster 	if ((long) p == rf_memDebugAddress) {
     76        1.3     oster 		printf("Allocate: debug address allocated from line %d file %s\n", line, filen);
     77        1.3     oster 	}
     78        1.1     oster }
     79        1.1     oster 
     80       1.15     perry void
     81       1.14     oster rf_unrecord_malloc(void *p, int sz)
     82        1.3     oster {
     83        1.3     oster 	int     size;
     84        1.3     oster 
     85        1.3     oster 	/* RF_LOCK_MUTEX(rf_debug_mem_mutex); */
     86        1.3     oster 	size = memory_hash_remove(p, sz);
     87        1.3     oster 	tot_mem_in_use -= size;
     88        1.3     oster 	/* RF_UNLOCK_MUTEX(rf_debug_mem_mutex); */
     89        1.3     oster 	if ((long) p == rf_memDebugAddress) {
     90        1.3     oster 		printf("Free: Found debug address\n");	/* this is really only a
     91        1.3     oster 							 * flag line for gdb */
     92        1.3     oster 	}
     93        1.3     oster }
     94        1.3     oster 
     95       1.15     perry void
     96        1.3     oster rf_print_unfreed()
     97        1.3     oster {
     98        1.3     oster 	int     i, foundone = 0;
     99        1.3     oster 	struct mh_struct *p;
    100        1.3     oster 
    101        1.3     oster 	for (i = 0; i < RF_MH_TABLESIZE; i++) {
    102        1.3     oster 		for (p = mh_table[i]; p; p = p->next)
    103        1.3     oster 			if (p->allocated) {
    104        1.3     oster 				if (!foundone)
    105        1.3     oster 					printf("\n\nThere are unfreed memory locations at program shutdown:\n");
    106        1.3     oster 				foundone = 1;
    107        1.3     oster 				printf("Addr 0x%lx Size %d line %d file %s\n",
    108        1.3     oster 				    (long) p->address, p->size, p->line, p->filen);
    109        1.3     oster 			}
    110        1.3     oster 	}
    111        1.3     oster 	if (tot_mem_in_use) {
    112        1.3     oster 		printf("%ld total bytes in use\n", tot_mem_in_use);
    113        1.3     oster 	}
    114        1.3     oster }
    115       1.11     oster #endif /* RF_DEBUG_MEM */
    116        1.3     oster 
    117       1.15     perry int
    118       1.18  christos rf_ConfigureDebugMem(RF_ShutdownList_t **listp)
    119        1.3     oster {
    120       1.11     oster #if RF_DEBUG_MEM
    121  1.18.24.1      matt 	int     i;
    122        1.3     oster 
    123       1.13     oster 	rf_mutex_init(&rf_debug_mem_mutex);
    124        1.3     oster 	if (rf_memDebug) {
    125        1.3     oster 		for (i = 0; i < RF_MH_TABLESIZE; i++)
    126        1.3     oster 			mh_table[i] = NULL;
    127        1.3     oster 		mh_table_initialized = 1;
    128        1.3     oster 	}
    129       1.11     oster #endif
    130        1.3     oster 	return (0);
    131        1.3     oster }
    132       1.11     oster 
    133       1.11     oster #if RF_DEBUG_MEM
    134       1.11     oster 
    135        1.1     oster #define HASHADDR(_a_)      ( (((unsigned long) _a_)>>3) % RF_MH_TABLESIZE )
    136        1.1     oster 
    137       1.15     perry static void
    138       1.14     oster memory_hash_insert(void *addr, int size, int line, char *filen)
    139        1.3     oster {
    140        1.3     oster 	unsigned long bucket = HASHADDR(addr);
    141        1.3     oster 	struct mh_struct *p;
    142        1.3     oster 
    143        1.3     oster 	RF_ASSERT(mh_table_initialized);
    144        1.3     oster 
    145        1.3     oster 	/* search for this address in the hash table */
    146        1.3     oster 	for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
    147        1.3     oster 	if (!p) {
    148        1.3     oster 		RF_Malloc(p, sizeof(struct mh_struct), (struct mh_struct *));
    149        1.3     oster 		RF_ASSERT(p);
    150        1.3     oster 		p->next = mh_table[bucket];
    151        1.3     oster 		mh_table[bucket] = p;
    152        1.3     oster 		p->address = addr;
    153        1.3     oster 		p->allocated = 0;
    154        1.3     oster 	}
    155        1.3     oster 	if (p->allocated) {
    156        1.3     oster 		printf("ERROR:  reallocated address 0x%lx from line %d, file %s without intervening free\n", (long) addr, line, filen);
    157        1.3     oster 		printf("        last allocated from line %d file %s\n", p->line, p->filen);
    158        1.3     oster 		RF_ASSERT(0);
    159        1.3     oster 	}
    160        1.3     oster 	p->size = size;
    161        1.3     oster 	p->line = line;
    162        1.3     oster 	p->filen = filen;
    163        1.3     oster 	p->allocated = 1;
    164        1.3     oster }
    165        1.3     oster 
    166       1.15     perry static int
    167       1.14     oster memory_hash_remove(void *addr, int sz)
    168        1.3     oster {
    169        1.3     oster 	unsigned long bucket = HASHADDR(addr);
    170        1.3     oster 	struct mh_struct *p;
    171        1.3     oster 
    172        1.3     oster 	RF_ASSERT(mh_table_initialized);
    173        1.3     oster 	for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
    174        1.3     oster 	if (!p) {
    175        1.3     oster 		printf("ERROR:  freeing never-allocated address 0x%lx\n", (long) addr);
    176        1.3     oster 		RF_PANIC();
    177        1.3     oster 	}
    178        1.3     oster 	if (!p->allocated) {
    179        1.3     oster 		printf("ERROR:  freeing unallocated address 0x%lx.  Last allocation line %d file %s\n", (long) addr, p->line, p->filen);
    180        1.3     oster 		RF_PANIC();
    181        1.3     oster 	}
    182        1.3     oster 	if (sz > 0 && p->size != sz) {	/* you can suppress this error by
    183        1.3     oster 					 * using a negative value as the size
    184        1.3     oster 					 * to free */
    185        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);
    186        1.3     oster 		RF_PANIC();
    187        1.3     oster 	}
    188        1.3     oster 	p->allocated = 0;
    189        1.3     oster 	return (p->size);
    190        1.1     oster }
    191       1.11     oster #endif /* RF_DEBUG_MEM */
    192       1.11     oster 
    193       1.11     oster 
    194