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

/usr/src/libcwd/libcwd/include/libcwd/debugmalloc.h

Go to the documentation of this file.
00001 // $Header: /cvsroot/libcwd/libcwd/include/libcwd/debugmalloc.h,v 1.10 2004/07/14 00:29:35 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 
00029 #if CWDEBUG_ALLOC
00030 
00031 #ifndef LIBCW_CSTDDEF
00032 #define LIBCW_CSTDDEF
00033 #include <cstddef>              // Needed for size_t.
00034 #endif
00035 #ifndef LIBCWD_CLASS_ALLOC_H
00036 #include <libcwd/class_alloc.h>
00037 #endif
00038 #ifndef LIBCW_LOCKABLE_AUTO_PTR_H
00039 #include <libcwd/lockable_auto_ptr.h>
00040 #endif
00041 #ifndef LIBCWD_PRIVATE_SET_ALLOC_CHECKING_H
00042 #include <libcwd/private_set_alloc_checking.h>
00043 #endif
00044 #ifndef LIBCWD_ENUM_MEMBLK_TYPES_H
00045 #include <libcwd/enum_memblk_types.h>
00046 #endif
00047 #if CWDEBUG_MARKER && !defined(LIBCWD_CLASS_MARKER_H)
00048 #include <libcwd/class_marker.h>
00049 #endif
00050 #ifndef LIBCWD_MACRO_ALLOCTAG_H
00051 #include <libcwd/macro_AllocTag.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 extern std::ostream& operator<<(std::ostream&, malloc_report_nt);
00087 
00088 // Accessors:
00089 
00090 extern size_t mem_size(void);
00091 extern unsigned long mem_blocks(void);
00092 extern alloc_ct const* find_alloc(void const* ptr);
00093 extern bool test_delete(void const* ptr);
00094 
00095 // Manipulators:
00096 extern void make_invisible(void const* ptr);
00097 extern void make_all_allocations_invisible_except(void const* ptr);
00098 extern void make_exit_function_list_invisible(void);
00120 inline void set_invisible_on(void) { LIBCWD_TSD_DECLARATION; _private_::set_invisible_on(LIBCWD_TSD); }
00127 inline void set_invisible_off(void) { LIBCWD_TSD_DECLARATION; _private_::set_invisible_off(LIBCWD_TSD); }
00128 #if CWDEBUG_MARKER
00129 extern void move_outside(marker_ct*, void const* ptr);
00130 #endif
00131 
00132 // Undocumented (libcwd `internal' function)
00133 extern void init_debugmalloc(void);
00134 
00135 } // namespace libcwd
00136 
00137 #else // !CWDEBUG_ALLOC
00138 
00139 namespace libcwd {
00140 
00141 inline void make_invisible(void const*) { } 
00142 inline void make_all_allocations_invisible_except(void const*) { }
00143 inline void set_invisible_on(void) { }
00144 inline void set_invisible_off(void) { }
00145 
00146 } // namespace libcwd
00147 
00148 #endif // !CWDEBUG_ALLOC
00149 
00150 #if CWDEBUG_DEBUGM
00151 #define LIBCWD_DEBUGM_CERR(x) DEBUGDEBUG_CERR(x)
00152 #else
00153 #define LIBCWD_DEBUGM_CERR(x)
00154 #endif
00155 
00156 namespace libcwd {
00157 
00158 #if CWDEBUG_ALLOC
00159 extern unsigned long list_allocations_on(debug_ct& debug_object, alloc_filter_ct const& format);
00160 extern unsigned long list_allocations_on(debug_ct& debug_object);
00161 #else // !CWDEBUG_ALLOC
00162 inline void list_allocations_on(debug_ct&) { }
00163 #endif // !CWDEBUG_ALLOC
00164 
00165 } // namespace libcwd
00166 
00167 #ifndef LIBCWD_DEBUGMALLOC_INTERNAL
00168 #if CWDEBUG_ALLOC
00169 
00170 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00171 // Ugh, use kludge.
00172 #include <cstdlib>      // Make sure the prototypes for malloc et al are declared
00173                         // before defining the macros!
00174 #define malloc __libcwd_malloc
00175 #define calloc __libcwd_calloc
00176 #define realloc __libcwd_realloc
00177 #define free __libcwd_free
00178 #endif
00179 
00180 // Use external linkage to catch ALL calls to all malloc/calloc/realloc/free functions,
00181 // also those that are done in libc, or any other shared library that might be linked.
00182 // [ Note: if LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC wasn't defined, then these are the prototypes
00183 // for __libcwd_malloc et al of course.  We still use external "C" linkage in that case
00184 // in order to avoid a collision with possibily later included prototypes for malloc. ]
00185 extern "C" void* malloc(size_t size) throw() __attribute__((__malloc__));
00186 extern "C" void* calloc(size_t nmemb, size_t size) throw() __attribute__((__malloc__));
00187 extern "C" void* realloc(void* ptr, size_t size) throw() __attribute__((__malloc__));
00188 extern "C" void  free(void* ptr) throw();
00189 
00190 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00191 // Use same kludge for other libc functions that return malloc-ed pointers.
00192 #define strdup __libcwd_strdup
00193 #ifdef HAVE_WMEMCPY
00194 #define wcsdup __libcwd_wcsdup
00195 #endif
00196 
00197 inline
00198 char*
00199 __libcwd_strdup(char const* str)
00200 {
00201   size_t size = strlen(str) + 1;
00202   char* p = (char*)malloc(size);
00203   if (p)
00204   {
00205     memcpy(p, str, size);
00206     AllocTag(p, "strdup()");
00207   }
00208   return p;
00209 }
00210 
00211 #ifdef HAVE_WMEMCPY
00212 extern "C" {
00213   size_t wcslen(wchar_t const*);
00214   wchar_t* wmemcpy(wchar_t*, wchar_t const*, size_t);
00215 }
00216 
00217 inline
00218 wchar_t*
00219 __libcwd_wcsdup(wchar_t const* str)
00220 {
00221   size_t size = wcslen(str) + 1;
00222   wchar_t* p = (wchar_t*)malloc(size * sizeof(wchar_t));
00223   if (p)
00224   {
00225     wmemcpy(p, str, size);
00226     AllocTag(p, "wcsdup()");
00227   }
00228   return p;
00229 }
00230 #endif // HAVE_WMEMCPY
00231 #endif // !LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00232 
00233 #endif // CWDEBUG_ALLOC
00234 #endif // !DEBUG_INTERNAL
00235 
00236 #endif // LIBCWD_DEBUGMALLOC_H
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.