Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / raidframe / rf_debugMem.c
blob665e5a3c150b4edbc33fd56916b57acb5930baad
1 /* $NetBSD: rf_debugMem.c,v 1.19 2008/02/12 03:12:41 oster Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
6 * Author: Daniel Stodolsky, Mark Holland, Jim Zelenka
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.
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.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 /* debugMem.c: memory usage debugging stuff.
30 * Malloc, Calloc, and Free are #defined everywhere
31 * to do_malloc, do_calloc, and do_free.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: rf_debugMem.c,v 1.19 2008/02/12 03:12:41 oster Exp $");
37 #include <dev/raidframe/raidframevar.h>
39 #include "rf_threadstuff.h"
40 #include "rf_options.h"
41 #include "rf_debugMem.h"
42 #include "rf_general.h"
44 #if RF_DEBUG_MEM
46 static long tot_mem_in_use = 0;
48 /* Hash table of information about memory allocations */
49 #define RF_MH_TABLESIZE 1000
51 struct mh_struct {
52 void *address;
53 int size;
54 int line;
55 char *filen;
56 char allocated;
57 struct mh_struct *next;
59 static struct mh_struct *mh_table[RF_MH_TABLESIZE];
60 RF_DECLARE_MUTEX(rf_debug_mem_mutex)
61 static int mh_table_initialized = 0;
63 static void memory_hash_insert(void *addr, int size, int line, char *filen);
64 static int memory_hash_remove(void *addr, int sz);
66 void
67 rf_record_malloc(void *p, int size, int line, char *filen)
69 RF_ASSERT(size != 0);
71 /* RF_LOCK_MUTEX(rf_debug_mem_mutex); */
72 memory_hash_insert(p, size, line, filen);
73 tot_mem_in_use += size;
74 /* RF_UNLOCK_MUTEX(rf_debug_mem_mutex); */
75 if ((long) p == rf_memDebugAddress) {
76 printf("Allocate: debug address allocated from line %d file %s\n", line, filen);
80 void
81 rf_unrecord_malloc(void *p, int sz)
83 int size;
85 /* RF_LOCK_MUTEX(rf_debug_mem_mutex); */
86 size = memory_hash_remove(p, sz);
87 tot_mem_in_use -= size;
88 /* RF_UNLOCK_MUTEX(rf_debug_mem_mutex); */
89 if ((long) p == rf_memDebugAddress) {
90 printf("Free: Found debug address\n"); /* this is really only a
91 * flag line for gdb */
95 void
96 rf_print_unfreed(void)
98 int i, foundone = 0;
99 struct mh_struct *p;
101 for (i = 0; i < RF_MH_TABLESIZE; i++) {
102 for (p = mh_table[i]; p; p = p->next)
103 if (p->allocated) {
104 if (!foundone)
105 printf("\n\nThere are unfreed memory locations at program shutdown:\n");
106 foundone = 1;
107 printf("Addr 0x%lx Size %d line %d file %s\n",
108 (long) p->address, p->size, p->line, p->filen);
111 if (tot_mem_in_use) {
112 printf("%ld total bytes in use\n", tot_mem_in_use);
115 #endif /* RF_DEBUG_MEM */
118 rf_ConfigureDebugMem(RF_ShutdownList_t **listp)
120 #if RF_DEBUG_MEM
121 int i;
123 rf_mutex_init(&rf_debug_mem_mutex);
124 if (rf_memDebug) {
125 for (i = 0; i < RF_MH_TABLESIZE; i++)
126 mh_table[i] = NULL;
127 mh_table_initialized = 1;
129 #endif
130 return (0);
133 #if RF_DEBUG_MEM
135 #define HASHADDR(_a_) ( (((unsigned long) _a_)>>3) % RF_MH_TABLESIZE )
137 static void
138 memory_hash_insert(void *addr, int size, int line, char *filen)
140 unsigned long bucket = HASHADDR(addr);
141 struct mh_struct *p;
143 RF_ASSERT(mh_table_initialized);
145 /* search for this address in the hash table */
146 for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
147 if (!p) {
148 RF_Malloc(p, sizeof(struct mh_struct), (struct mh_struct *));
149 RF_ASSERT(p);
150 p->next = mh_table[bucket];
151 mh_table[bucket] = p;
152 p->address = addr;
153 p->allocated = 0;
155 if (p->allocated) {
156 printf("ERROR: reallocated address 0x%lx from line %d, file %s without intervening free\n", (long) addr, line, filen);
157 printf(" last allocated from line %d file %s\n", p->line, p->filen);
158 RF_ASSERT(0);
160 p->size = size;
161 p->line = line;
162 p->filen = filen;
163 p->allocated = 1;
166 static int
167 memory_hash_remove(void *addr, int sz)
169 unsigned long bucket = HASHADDR(addr);
170 struct mh_struct *p;
172 RF_ASSERT(mh_table_initialized);
173 for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
174 if (!p) {
175 printf("ERROR: freeing never-allocated address 0x%lx\n", (long) addr);
176 RF_PANIC();
178 if (!p->allocated) {
179 printf("ERROR: freeing unallocated address 0x%lx. Last allocation line %d file %s\n", (long) addr, p->line, p->filen);
180 RF_PANIC();
182 if (sz > 0 && p->size != sz) { /* you can suppress this error by
183 * using a negative value as the size
184 * to free */
185 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 RF_PANIC();
188 p->allocated = 0;
189 return (p->size);
191 #endif /* RF_DEBUG_MEM */