Main Page   Reference Manual   Namespace List   Compound List   Namespace Members   Compound Members   File Members  

debugmalloc.h
Go to the documentation of this file.
1 // $Header$
2 //
3 // Copyright (C) 2000 - 2004, by
4 //
5 // Carlo Wood, Run on IRC <carlo@alinoe.com>
6 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
7 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
8 //
9 // This file may be distributed under the terms of the Q Public License
10 // version 1.0 as appearing in the file LICENSE.QPL included in the
11 // packaging of this file.
12 //
13 
18 #ifndef LIBCWD_DEBUGMALLOC_H
19 #define LIBCWD_DEBUGMALLOC_H
20 
21 #ifndef LIBCWD_LIBRARIES_DEBUG_H
22 #error "Don't include <libcwd/debugmalloc.h> directly, include the appropriate \"debug.h\" instead."
23 #endif
24 
25 #ifndef LIBCWD_CONFIG_H
26 #include <libcwd/config.h>
27 #endif
28 #ifndef LIBCWD_MACRO_ALLOCTAG_H
29 #include <libcwd/macro_AllocTag.h>
30 #endif
31 
32 #if CWDEBUG_ALLOC
33 
34 #ifndef LIBCW_CSTDDEF
35 #define LIBCW_CSTDDEF
36 #include <cstddef> // Needed for size_t.
37 #endif
38 #ifndef LIBCWD_CLASS_ALLOC_H
39 #include <libcwd/class_alloc.h>
40 #endif
41 #ifndef LIBCW_LOCKABLE_AUTO_PTR_H
43 #endif
44 #ifndef LIBCWD_PRIVATE_SET_ALLOC_CHECKING_H
46 #endif
47 #ifndef LIBCWD_ENUM_MEMBLK_TYPES_H
49 #endif
50 #if CWDEBUG_MARKER && !defined(LIBCWD_CLASS_MARKER_H)
51 #include <libcwd/class_marker.h>
52 #endif
53 #ifndef LIBCWD_CLASS_ALLOC_FILTER_H
55 #endif
56 #ifndef LIBCW_SYS_TIME_H
57 #define LIBCW_SYS_TIME_H
58 #include <sys/time.h> // Needed for struct timeval.
59 #endif
60 
61 namespace libcwd {
62 
65 {
85 };
86 
87 // Backtrace hook:
88 int const max_frames = 16;
89 extern void (*backtrace_hook)(void** buffer, int frames LIBCWD_COMMA_TSD_PARAM);
90 
91 #ifndef LIBCWD_DOXYGEN
92 extern std::ostream& operator<<(std::ostream&, malloc_report_nt);
93 #endif
94 
95 // Accessors:
96 
97 extern size_t mem_size(void);
98 extern unsigned long mem_blocks(void);
99 extern alloc_ct const* find_alloc(void const* ptr);
100 extern bool test_delete(void const* ptr);
101 
102 // Manipulators:
103 extern void make_invisible(void const* ptr);
104 extern void make_all_allocations_invisible_except(void const* ptr);
105 extern void make_exit_function_list_invisible(void);
127 inline void set_invisible_on(void) { LIBCWD_TSD_DECLARATION; _private_::set_invisible_on(LIBCWD_TSD); }
134 inline void set_invisible_off(void) { LIBCWD_TSD_DECLARATION; _private_::set_invisible_off(LIBCWD_TSD); }
135 #if CWDEBUG_MARKER
136 extern void move_outside(marker_ct*, void const* ptr);
137 #endif
138 
139 // Undocumented (libcwd `internal' function)
140 extern void init_debugmalloc(void);
141 
142 } // namespace libcwd
143 
144 #else // !CWDEBUG_ALLOC
145 
146 namespace libcwd {
147 
148 inline void make_invisible(void const*) { }
149 inline void make_all_allocations_invisible_except(void const*) { }
150 inline void set_invisible_on(void) { }
151 inline void set_invisible_off(void) { }
152 
153 } // namespace libcwd
154 
155 #endif // !CWDEBUG_ALLOC
156 
157 #if CWDEBUG_DEBUGM
158 #define LIBCWD_DEBUGM_CERR(x) DEBUGDEBUG_CERR(x)
159 #define LIBCWD_DEBUGM_ASSERT(expr) do { if (!(expr)) { FATALDEBUGDEBUG_CERR("CWDEBUG_DEBUGM: " __FILE__ ":" << __LINE__ << ": " << __PRETTY_FUNCTION__ << ": Assertion`" << LIBCWD_STRING(expr) << "' failed."); core_dump(); } } while(0)
160 #else
161 #define LIBCWD_DEBUGM_CERR(x) do { } while(0)
162 #define LIBCWD_DEBUGM_ASSERT(x) do { } while(0)
163 #endif
164 
165 namespace libcwd {
166 
167 #if CWDEBUG_ALLOC
168 extern unsigned long list_allocations_on(debug_ct& debug_object, alloc_filter_ct const& format);
169 extern unsigned long list_allocations_on(debug_ct& debug_object);
170 #else // !CWDEBUG_ALLOC
171 inline void list_allocations_on(debug_ct&) { }
172 #endif // !CWDEBUG_ALLOC
173 
174 } // namespace libcwd
175 
176 #ifndef LIBCWD_DEBUGMALLOC_INTERNAL
177 #if CWDEBUG_ALLOC
178 
179 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
180 // Ugh, use macro kludge.
181 #include <cstdlib> // Make sure the prototypes for malloc et al are declared
182  // before defining the macros!
183 #define malloc __libcwd_malloc
184 #define calloc __libcwd_calloc
185 #define realloc __libcwd_realloc
186 #define free __libcwd_free
187 #endif
188 
189 #ifndef LIBCWD_HAVE_DLOPEN
190 // Use macro kludge for these (too):
191 #ifdef LIBCWD_HAVE_POSIX_MEMALIGN
192 // Include this header before defining the macro 'posix_memalign'.
193 #include <cstdlib>
194 #define posix_memalign __libcwd_posix_memalign
195 #endif
196 // Include this header before defining the macro 'memalign' or 'valloc'.
197 #if defined(HAVE_MALLOC_H) && (defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC))
198 #include <malloc.h>
199 #endif
200 #if defined(HAVE_UNISTD_H) && defined(HAVE_VALLOC)
201 #include <unistd.h> // This is what is needed for valloc(3) on FreeBSD. Also needed for sysconf.
202 #endif
203 #ifdef LIBCWD_HAVE_MEMALIGN
204 #define memalign __libcwd_memalign
205 #endif
206 #ifdef LIBCWD_HAVE_VALLOC
207 #define valloc __libcwd_valloc
208 #endif
209 #endif // !LIBCWD_HAVE_DLOPEN
210 
211 // Use external linkage to catch ALL calls to all malloc/calloc/realloc/free functions,
212 // also those that are done in libc, or any other shared library that might be linked.
213 // [ Note: if LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC wasn't defined, then these are the prototypes
214 // for __libcwd_malloc et al of course. We still use external "C" linkage in that case
215 // in order to avoid a collision with possibily later included prototypes for malloc. ]
216 extern "C" void* malloc(size_t size) throw() __attribute__((__malloc__));
217 extern "C" void* calloc(size_t nmemb, size_t size) throw() __attribute__((__malloc__));
218 extern "C" void* realloc(void* ptr, size_t size) throw() __attribute__((__malloc__));
219 extern "C" void free(void* ptr) throw();
220 #ifdef LIBCWD_HAVE_POSIX_MEMALIGN
221 extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size) throw();
222 #endif
223 #ifdef LIBCWD_HAVE_VALLOC
224 extern "C" void *valloc(size_t size) throw() __attribute__((__malloc__));
225 #endif
226 #ifdef LIBCWD_HAVE_MEMALIGN
227 extern "C" void *memalign(size_t boundary, size_t size) throw() __attribute__((__malloc__));
228 #endif
229 
230 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
231 // Use same kludge for other libc functions that return malloc-ed pointers.
232 #define strdup __libcwd_strdup
233 #ifdef HAVE_WMEMCPY
234 #define wcsdup __libcwd_wcsdup
235 #endif
236 
237 inline
238 char*
239 __libcwd_strdup(char const* str)
240 {
241  size_t size = strlen(str) + 1;
242  char* p = (char*)malloc(size);
243  if (p)
244  {
245  memcpy(p, str, size);
246  AllocTag(p, "strdup()");
247  }
248  return p;
249 }
250 
251 #ifdef HAVE_WMEMCPY
252 extern "C" {
253  size_t wcslen(wchar_t const*);
254  wchar_t* wmemcpy(wchar_t*, wchar_t const*, size_t);
255 }
256 
257 inline
258 wchar_t*
259 __libcwd_wcsdup(wchar_t const* str)
260 {
261  size_t size = wcslen(str) + 1;
262  wchar_t* p = (wchar_t*)malloc(size * sizeof(wchar_t));
263  if (p)
264  {
265  wmemcpy(p, str, size);
266  AllocTag(p, "wcsdup()");
267  }
268  return p;
269 }
270 #endif // HAVE_WMEMCPY
271 #endif // !LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
272 
273 #endif // CWDEBUG_ALLOC
274 #endif // !LIBCWD_DEBUGMALLOC_INTERNAL
275 
276 #endif // LIBCWD_DEBUGMALLOC_H
alloc_ct const * find_alloc(void const *ptr)
Find information about a memory allocation.Given a pointer, which points to the start of or inside an...
Definition: debugmalloc.cc:3558
unsigned long mem_blocks(void)
Returns the total number of allocated memory blocks.
Definition: debugmalloc.cc:2850
void make_invisible(void const *void_ptr)
Make allocation pointed to by ptr invisible.The allocation pointed to by ptr is made invisible; it wo...
Definition: debugmalloc.cc:3076
std::ostream & operator<<(std::ostream &os, memblk_types_nt memblk_type)
Allow writing a memblk_types_nt directly to an ostream.
Definition: debugmalloc.cc:675
void make_all_allocations_invisible_except(void const *ptr)
Make all current allocations invisible except the given pointer.All allocations, except the given poi...
Definition: debugmalloc.cc:3136
void set_invisible_on(void)
Make all future allocations invisible.All following allocations are made invisible; they won't show u...
Definition: debugmalloc.h:127
void move_outside(marker_ct *marker, void const *void_ptr)
Move memory allocation pointed to by ptr outside marker.
Definition: debugmalloc.cc:3404
Writing the current number of allocated bytes and blocks to an ostream.
Definition: debugmalloc.h:84
void make_exit_function_list_invisible(void)
Make allocations done in libc.so:__new_exitfn invisible.This makes the allocation done in __new_exitf...
Definition: debugmalloc.cc:3179
channel_ct malloc
Definition: debug.cc:473
bool test_delete(void const *void_ptr)
Test if a pointer points to the start of an allocated memory block.
Definition: debugmalloc.cc:2789
size_t mem_size(void)
Returns the total number of allocated bytes.
Definition: debugmalloc.cc:2824
unsigned long list_allocations_on(debug_ct &debug_object)
List all current allocations to a given debug object.With CWDEBUG_ALLOC set to 1, it is possible to write the overview of allocated memory to a Debug Object.
Definition: debugmalloc.cc:2937
#define AllocTag(p, x)
Annotate type of p with a static description.
Definition: macro_AllocTag.h:166
malloc_report_nt
Type of malloc_report.
Definition: debugmalloc.h:64
void set_invisible_off(void)
Cancel a call to set_invisible_on.See set_invisible_on.
Definition: debugmalloc.h:134
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.