1 /* $NetBSD: rf_debugMem.c,v 1.19 2008/02/12 03:12:41 oster Exp $ */
3 * Copyright (c) 1995 Carnegie-Mellon University.
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"
46 static long tot_mem_in_use
= 0;
48 /* Hash table of information about memory allocations */
49 #define RF_MH_TABLESIZE 1000
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
);
67 rf_record_malloc(void *p
, int size
, int line
, char *filen
)
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
);
81 rf_unrecord_malloc(void *p
, int sz
)
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 */
96 rf_print_unfreed(void)
101 for (i
= 0; i
< RF_MH_TABLESIZE
; i
++) {
102 for (p
= mh_table
[i
]; p
; p
= p
->next
)
105 printf("\n\nThere are unfreed memory locations at program shutdown:\n");
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
)
123 rf_mutex_init(&rf_debug_mem_mutex
);
125 for (i
= 0; i
< RF_MH_TABLESIZE
; i
++)
127 mh_table_initialized
= 1;
135 #define HASHADDR(_a_) ( (((unsigned long) _a_)>>3) % RF_MH_TABLESIZE )
138 memory_hash_insert(void *addr
, int size
, int line
, char *filen
)
140 unsigned long bucket
= HASHADDR(addr
);
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
);
148 RF_Malloc(p
, sizeof(struct mh_struct
), (struct mh_struct
*));
150 p
->next
= mh_table
[bucket
];
151 mh_table
[bucket
] = p
;
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
);
167 memory_hash_remove(void *addr
, int sz
)
169 unsigned long bucket
= HASHADDR(addr
);
172 RF_ASSERT(mh_table_initialized
);
173 for (p
= mh_table
[bucket
]; p
&& (p
->address
!= addr
); p
= p
->next
);
175 printf("ERROR: freeing never-allocated address 0x%lx\n", (long) addr
);
179 printf("ERROR: freeing unallocated address 0x%lx. Last allocation line %d file %s\n", (long) addr
, p
->line
, p
->filen
);
182 if (sz
> 0 && p
->size
!= sz
) { /* you can suppress this error by
183 * using a negative value as the size
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
);
191 #endif /* RF_DEBUG_MEM */