rf_debugMem.h revision 1.3 1 /* $NetBSD: rf_debugMem.h,v 1.3 1999/01/26 02:33:54 oster Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Author: Daniel Stodolsky, Mark Holland
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 (at) 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 /*
30 * rf_debugMem.h -- memory leak debugging module
31 *
32 * IMPORTANT: if you put the lock/unlock mutex stuff back in here, you
33 * need to take it out of the routines in debugMem.c
34 *
35 */
36
37 #ifndef _RF__RF_DEBUGMEM_H_
38 #define _RF__RF_DEBUGMEM_H_
39
40 #include "rf_archs.h"
41 #include "rf_alloclist.h"
42 #include "rf_options.h"
43
44 #ifndef _KERNEL
45
46 #ifndef __NetBSD__
47 void *malloc(), *calloc();
48 #endif
49 RF_DECLARE_EXTERN_MUTEX(rf_debug_mem_mutex)
50
51 /*
52 * redzone malloc, calloc, and free allocate an extra 16 bytes on each
53 * malloc/calloc call to allow tracking of overflows on free.
54 */
55
56 #if RF_MEMORY_REDZONES > 0
57 #define rf_redzone_malloc(_p_,_size_) _p_ = rf_real_redzone_malloc(_size_)
58 #define rf_redzone_calloc(_p_,_n_,_size_) _p_ = rf_real_redzone_calloc(_n_,_size_)
59 #define rf_redzone_free(_p_) rf_real_redzone_free(_p_, __LINE__, __FILE__)
60 #else /* RF_MEMORY_REDZONES > 0 */
61 #define rf_redzone_malloc(_p_,_size_) _p_ = malloc(_size_)
62 #define rf_redzone_calloc(_p_,_nel_,_size_) _p_ = calloc(_nel_,_size_)
63 #define rf_redzone_free(_ptr_) free(_ptr_)
64 #endif /* RF_MEMORY_REDZONES > 0 */
65
66 #define RF_Malloc(_p_, _size_, _cast_) { \
67 _p_ = _cast_ rf_real_Malloc(_size_, __LINE__, __FILE__); \
68 }
69
70 #define RF_MallocAndAdd(_p_, _size_, _cast_, _alist_) { \
71 _p_ = _cast_ rf_real_MallocAndAdd(_size_, _alist_, __LINE__, __FILE__); \
72 }
73
74 #define RF_Calloc(_p_, _nel_, _elsz_, _cast_) { \
75 _p_ = _cast_ rf_real_Calloc(_nel_, _elsz_, __LINE__, __FILE__); \
76 }
77
78 #define RF_CallocAndAdd(_p_, _nel_, _elsz_, _cast_, _alist_) { \
79 _p_ = _cast_ rf_real_CallocAndAdd(_nel_, _elsz_, _alist_, __LINE__, __FILE__); \
80 }
81
82 #define RF_Free(__p_, _sz_) { \
83 rf_real_Free(__p_, _sz_, __LINE__, __FILE__); \
84 }
85
86 #else /* KERNEL */
87
88 #include <sys/types.h>
89 typedef u_int32_t U32;
90 #include <sys/malloc.h>
91
92
93
94 #define RF_Malloc(_p_, _size_, _cast_) \
95 { \
96 _p_ = _cast_ malloc((u_long)_size_, M_RAIDFRAME, M_WAITOK); \
97 bzero((char *)_p_, _size_); \
98 if (rf_memDebug) rf_record_malloc(_p_, _size_, __LINE__, __FILE__); \
99 }
100
101 #define RF_MallocAndAdd(__p_, __size_, __cast_, __alist_) \
102 { \
103 RF_Malloc(__p_, __size_, __cast_); \
104 if (__alist_) rf_AddToAllocList(__alist_, __p_, __size_); \
105 }
106
107 #define RF_Calloc(_p_, _nel_, _elsz_, _cast_) \
108 { \
109 RF_Malloc( _p_, (_nel_) * (_elsz_), _cast_); \
110 bzero( (_p_), (_nel_) * (_elsz_) ); \
111 }
112
113 #define RF_CallocAndAdd(__p,__nel,__elsz,__cast,__alist) \
114 { \
115 RF_Calloc(__p, __nel, __elsz, __cast); \
116 if (__alist) rf_AddToAllocList(__alist, __p, (__nel)*(__elsz)); \
117 }
118
119 #define RF_Free(_p_, _sz_) \
120 { \
121 free((void *)(_p_), M_RAIDFRAME); \
122 if (rf_memDebug) rf_unrecord_malloc(_p_, (U32) (_sz_)); \
123 }
124
125 #endif /* _KERNEL */
126
127 #ifndef _KERNEL
128 void *rf_real_redzone_malloc(int size);
129 void *rf_real_redzone_calloc(int n, int size);
130 void rf_real_redzone_free(char *p, int line, char *filen);
131 char *rf_real_Malloc(int size, int line, char *file);
132 char *rf_real_Calloc(int nel, int elsz, int line, char *file);
133 void rf_real_Free(void *p, int sz, int line, char *file);
134 void rf_validate_mh_table(void);
135 #if RF_UTILITY == 0
136 char *rf_real_MallocAndAdd(int size, RF_AllocListElem_t *alist, int line, char *file);
137 char *rf_real_CallocAndAdd(int nel, int elsz, RF_AllocListElem_t *alist, int line, char *file);
138 #endif /* RF_UTILITY == 0 */
139 #endif /* !KERNEL */
140
141 void rf_record_malloc(void *p, int size, int line, char *filen);
142 void rf_unrecord_malloc(void *p, int sz);
143 void rf_print_unfreed(void);
144 int rf_ConfigureDebugMem(RF_ShutdownList_t **listp);
145 void rf_ReportMaxMem(void);
146
147 #endif /* !_RF__RF_DEBUGMEM_H_ */
148