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