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

libcwd/debugmalloc.h

Go to the documentation of this file.
00001 // $Header$
00002 //
00003 // Copyright (C) 2000 - 2004, by
00004 // 
00005 // Carlo Wood, Run on IRC <carlo@alinoe.com>
00006 // RSA-1024 0x624ACAD5 1997-01-26                    Sign & Encrypt
00007 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
00008 //
00009 // This file may be distributed under the terms of the Q Public License
00010 // version 1.0 as appearing in the file LICENSE.QPL included in the
00011 // packaging of this file.
00012 //
00013 
00018 #ifndef LIBCWD_DEBUGMALLOC_H
00019 #define LIBCWD_DEBUGMALLOC_H
00020 
00021 #ifndef LIBCWD_LIBRARIES_DEBUG_H
00022 #error "Don't include <libcwd/debugmalloc.h> directly, include the appropriate \"debug.h\" instead."
00023 #endif
00024 
00025 #ifndef LIBCWD_CONFIG_H
00026 #include <libcwd/config.h>
00027 #endif
00028 #ifndef LIBCWD_MACRO_ALLOCTAG_H
00029 #include <libcwd/macro_AllocTag.h>
00030 #endif
00031 
00032 #if CWDEBUG_ALLOC
00033 
00034 #ifndef LIBCW_CSTDDEF
00035 #define LIBCW_CSTDDEF
00036 #include <cstddef>              // Needed for size_t.
00037 #endif
00038 #ifndef LIBCWD_CLASS_ALLOC_H
00039 #include <libcwd/class_alloc.h>
00040 #endif
00041 #ifndef LIBCW_LOCKABLE_AUTO_PTR_H
00042 #include <libcwd/lockable_auto_ptr.h>
00043 #endif
00044 #ifndef LIBCWD_PRIVATE_SET_ALLOC_CHECKING_H
00045 #include <libcwd/private_set_alloc_checking.h>
00046 #endif
00047 #ifndef LIBCWD_ENUM_MEMBLK_TYPES_H
00048 #include <libcwd/enum_memblk_types.h>
00049 #endif
00050 #if CWDEBUG_MARKER && !defined(LIBCWD_CLASS_MARKER_H)
00051 #include <libcwd/class_marker.h>
00052 #endif
00053 #ifndef LIBCWD_CLASS_ALLOC_FILTER_H
00054 #include <libcwd/class_alloc_filter.h>
00055 #endif
00056 #ifndef LIBCW_SYS_TIME_H
00057 #define LIBCW_SYS_TIME_H
00058 #include <sys/time.h>           // Needed for struct timeval.
00059 #endif
00060 
00061 namespace libcwd {
00062 
00064 enum malloc_report_nt
00065 {
00084   malloc_report
00085 };
00086 
00087 #ifndef LIBCWD_DOXYGEN
00088 extern std::ostream& operator<<(std::ostream&, malloc_report_nt);
00089 #endif
00090 
00091 // Accessors:
00092 
00093 extern size_t mem_size(void);
00094 extern unsigned long mem_blocks(void);
00095 extern alloc_ct const* find_alloc(void const* ptr);
00096 extern bool test_delete(void const* ptr);
00097 
00098 // Manipulators:
00099 extern void make_invisible(void const* ptr);
00100 extern void make_all_allocations_invisible_except(void const* ptr);
00101 extern void make_exit_function_list_invisible(void);
00123 inline void set_invisible_on(void) { LIBCWD_TSD_DECLARATION; _private_::set_invisible_on(LIBCWD_TSD); }
00130 inline void set_invisible_off(void) { LIBCWD_TSD_DECLARATION; _private_::set_invisible_off(LIBCWD_TSD); }
00131 #if CWDEBUG_MARKER
00132 extern void move_outside(marker_ct*, void const* ptr);
00133 #endif
00134 
00135 // Undocumented (libcwd `internal' function)
00136 extern void init_debugmalloc(void);
00137 
00138 } // namespace libcwd
00139 
00140 #else // !CWDEBUG_ALLOC
00141 
00142 namespace libcwd {
00143 
00144 inline void make_invisible(void const*) { } 
00145 inline void make_all_allocations_invisible_except(void const*) { }
00146 inline void set_invisible_on(void) { }
00147 inline void set_invisible_off(void) { }
00148 
00149 } // namespace libcwd
00150 
00151 #endif // !CWDEBUG_ALLOC
00152 
00153 #if CWDEBUG_DEBUGM
00154 #define LIBCWD_DEBUGM_CERR(x) DEBUGDEBUG_CERR(x)
00155 #else
00156 #define LIBCWD_DEBUGM_CERR(x)
00157 #endif
00158 
00159 namespace libcwd {
00160 
00161 #if CWDEBUG_ALLOC
00162 extern unsigned long list_allocations_on(debug_ct& debug_object, alloc_filter_ct const& format);
00163 extern unsigned long list_allocations_on(debug_ct& debug_object);
00164 #else // !CWDEBUG_ALLOC
00165 inline void list_allocations_on(debug_ct&) { }
00166 #endif // !CWDEBUG_ALLOC
00167 
00168 } // namespace libcwd
00169 
00170 #ifndef LIBCWD_DEBUGMALLOC_INTERNAL
00171 #if CWDEBUG_ALLOC
00172 
00173 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00174 // Ugh, use macro kludge.
00175 #include <cstdlib>      // Make sure the prototypes for malloc et al are declared
00176                         // before defining the macros!
00177 #define malloc __libcwd_malloc
00178 #define calloc __libcwd_calloc
00179 #define realloc __libcwd_realloc
00180 #define free __libcwd_free
00181 #endif
00182 
00183 #ifndef LIBCWD_HAVE_DLOPEN
00184 // Use macro kludge for these (too):
00185 #ifdef LIBCWD_HAVE_POSIX_MEMALIGN
00186 // Include this header before defining the macro 'posix_memalign'.
00187 #include <cstdlib>
00188 #define posix_memalign __libcwd_posix_memalign
00189 #endif
00190 // Include this header before defining the macro 'memalign' or 'valloc'.
00191 #if defined(HAVE_MALLOC_H) && (defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC))
00192 #include <malloc.h>
00193 #endif
00194 #if defined(HAVE_UNISTD_H) && defined(HAVE_VALLOC)
00195 #include <unistd.h>             // This is what is needed for valloc(3) on FreeBSD. Also needed for sysconf.
00196 #endif
00197 #ifdef LIBCWD_HAVE_MEMALIGN
00198 #define memalign __libcwd_memalign
00199 #endif
00200 #ifdef LIBCWD_HAVE_VALLOC
00201 #define valloc __libcwd_valloc
00202 #endif
00203 #endif // !LIBCWD_HAVE_DLOPEN
00204 
00205 // Use external linkage to catch ALL calls to all malloc/calloc/realloc/free functions,
00206 // also those that are done in libc, or any other shared library that might be linked.
00207 // [ Note: if LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC wasn't defined, then these are the prototypes
00208 // for __libcwd_malloc et al of course.  We still use external "C" linkage in that case
00209 // in order to avoid a collision with possibily later included prototypes for malloc. ]
00210 extern "C" void* malloc(size_t size) throw() __attribute__((__malloc__));
00211 extern "C" void* calloc(size_t nmemb, size_t size) throw() __attribute__((__malloc__));
00212 extern "C" void* realloc(void* ptr, size_t size) throw() __attribute__((__malloc__));
00213 extern "C" void  free(void* ptr) throw();
00214 #ifdef LIBCWD_HAVE_POSIX_MEMALIGN
00215 extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size) throw();
00216 #endif
00217 #ifdef LIBCWD_HAVE_VALLOC
00218 extern "C" void *valloc(size_t size) throw() __attribute__((__malloc__));
00219 #endif
00220 #ifdef LIBCWD_HAVE_MEMALIGN
00221 extern "C" void *memalign(size_t boundary, size_t size) throw() __attribute__((__malloc__));
00222 #endif
00223 
00224 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00225 // Use same kludge for other libc functions that return malloc-ed pointers.
00226 #define strdup __libcwd_strdup
00227 #ifdef HAVE_WMEMCPY
00228 #define wcsdup __libcwd_wcsdup
00229 #endif
00230 
00231 inline
00232 char*
00233 __libcwd_strdup(char const* str)
00234 {
00235   size_t size = strlen(str) + 1;
00236   char* p = (char*)malloc(size);
00237   if (p)
00238   {
00239     memcpy(p, str, size);
00240     AllocTag(p, "strdup()");
00241   }
00242   return p;
00243 }
00244 
00245 #ifdef HAVE_WMEMCPY
00246 extern "C" {
00247   size_t wcslen(wchar_t const*);
00248   wchar_t* wmemcpy(wchar_t*, wchar_t const*, size_t);
00249 }
00250 
00251 inline
00252 wchar_t*
00253 __libcwd_wcsdup(wchar_t const* str)
00254 {
00255   size_t size = wcslen(str) + 1;
00256   wchar_t* p = (wchar_t*)malloc(size * sizeof(wchar_t));
00257   if (p)
00258   {
00259     wmemcpy(p, str, size);
00260     AllocTag(p, "wcsdup()");
00261   }
00262   return p;
00263 }
00264 #endif // HAVE_WMEMCPY
00265 #endif // !LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00266 
00267 #endif // CWDEBUG_ALLOC
00268 #endif // !LIBCWD_DEBUGMALLOC_INTERNAL
00269 
00270 #endif // LIBCWD_DEBUGMALLOC_H
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.