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

debugmalloc.h

Go to the documentation of this file.
00001 // $Header: /cvsroot/libcwd/libcwd/include/libcwd/debugmalloc.h,v 1.14 2005/12/30 23:23:12 libcw Exp $
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 #if defined(LIBCWD_HAVE_MEMALIGN) || defined(LIBCWD_HAVE_VALLOC)
00191 // Include this header before defining the macro 'memalign' or 'valloc'.
00192 #include <malloc.h>
00193 #endif
00194 #ifdef LIBCWD_HAVE_MEMALIGN
00195 #define memalign __libcwd_memalign
00196 #endif
00197 #ifdef LIBCWD_HAVE_VALLOC
00198 #define valloc __libcwd_valloc
00199 #endif
00200 #endif // !LIBCWD_HAVE_DLOPEN
00201 
00202 // Use external linkage to catch ALL calls to all malloc/calloc/realloc/free functions,
00203 // also those that are done in libc, or any other shared library that might be linked.
00204 // [ Note: if LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC wasn't defined, then these are the prototypes
00205 // for __libcwd_malloc et al of course.  We still use external "C" linkage in that case
00206 // in order to avoid a collision with possibily later included prototypes for malloc. ]
00207 extern "C" void* malloc(size_t size) throw() __attribute__((__malloc__));
00208 extern "C" void* calloc(size_t nmemb, size_t size) throw() __attribute__((__malloc__));
00209 extern "C" void* realloc(void* ptr, size_t size) throw() __attribute__((__malloc__));
00210 extern "C" void  free(void* ptr) throw();
00211 #ifdef LIBCWD_HAVE_POSIX_MEMALIGN
00212 extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size);
00213 #endif
00214 #ifdef LIBCWD_HAVE_VALLOC
00215 extern "C" void *valloc(size_t size) throw() __attribute__((__malloc__));
00216 #endif
00217 #ifdef LIBCWD_HAVE_MEMALIGN
00218 extern "C" void *memalign(size_t boundary, size_t size) throw() __attribute__((__malloc__));
00219 #endif
00220 
00221 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00222 // Use same kludge for other libc functions that return malloc-ed pointers.
00223 #define strdup __libcwd_strdup
00224 #ifdef HAVE_WMEMCPY
00225 #define wcsdup __libcwd_wcsdup
00226 #endif
00227 
00228 inline
00229 char*
00230 __libcwd_strdup(char const* str)
00231 {
00232   size_t size = strlen(str) + 1;
00233   char* p = (char*)malloc(size);
00234   if (p)
00235   {
00236     memcpy(p, str, size);
00237     AllocTag(p, "strdup()");
00238   }
00239   return p;
00240 }
00241 
00242 #ifdef HAVE_WMEMCPY
00243 extern "C" {
00244   size_t wcslen(wchar_t const*);
00245   wchar_t* wmemcpy(wchar_t*, wchar_t const*, size_t);
00246 }
00247 
00248 inline
00249 wchar_t*
00250 __libcwd_wcsdup(wchar_t const* str)
00251 {
00252   size_t size = wcslen(str) + 1;
00253   wchar_t* p = (wchar_t*)malloc(size * sizeof(wchar_t));
00254   if (p)
00255   {
00256     wmemcpy(p, str, size);
00257     AllocTag(p, "wcsdup()");
00258   }
00259   return p;
00260 }
00261 #endif // HAVE_WMEMCPY
00262 #endif // !LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00263 
00264 #endif // CWDEBUG_ALLOC
00265 #endif // !LIBCWD_DEBUGMALLOC_INTERNAL
00266 
00267 #endif // LIBCWD_DEBUGMALLOC_H
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.