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