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: /cvsroot/libcwd/libcwd/include/libcwd/debugmalloc.h,v 1.4 2004/05/27 03:03:52 libcw Exp $
00002 //
00003 // Copyright (C) 2000 - 2003, 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_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_OOAM_FILTER_H
00054 #include <libcwd/class_ooam_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 #if CWDEBUG_MARKER
00099 extern void move_outside(marker_ct*, void const* ptr);
00100 #endif
00101 
00102 // Undocumented (libcwd `internal' function)
00103 extern void init_debugmalloc(void);
00104 
00105 } // namespace libcwd
00106 
00107 #else // !CWDEBUG_ALLOC
00108 
00109 namespace libcwd {
00110 
00111 __inline__ void make_invisible(void const*) { } 
00112 __inline__ void make_all_allocations_invisible_except(void const*) { }
00113 
00114 } // namespace libcwd
00115 
00116 #endif // !CWDEBUG_ALLOC
00117 
00118 #if CWDEBUG_DEBUGM
00119 #define LIBCWD_DEBUGM_CERR(x) DEBUGDEBUG_CERR(x)
00120 #else
00121 #define LIBCWD_DEBUGM_CERR(x)
00122 #endif
00123 
00124 namespace libcwd {
00125 
00126 #if CWDEBUG_ALLOC
00127 extern void list_allocations_on(debug_ct& debug_object, ooam_filter_ct const& format);
00128 extern void list_allocations_on(debug_ct& debug_object);
00129 #else // !CWDEBUG_ALLOC
00130 __inline__ void list_allocations_on(debug_ct&) { }
00131 #endif // !CWDEBUG_ALLOC
00132 
00133 } // namespace libcwd
00134 
00135 #ifndef LIBCWD_DEBUGMALLOC_INTERNAL
00136 #if CWDEBUG_ALLOC
00137 
00138 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00139 // Ugh, use kludge.
00140 #include <cstdlib>      // Make sure the prototypes for malloc et al are declared
00141                         // before defining the macros!
00142 #define malloc __libcwd_malloc
00143 #define calloc __libcwd_calloc
00144 #define realloc __libcwd_realloc
00145 #define free __libcwd_free
00146 #endif
00147 
00148 // Use external linkage to catch ALL calls to all malloc/calloc/realloc/free functions,
00149 // also those that are done in libc, or any other shared library that might be linked.
00150 // [ Note: if LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC wasn't defined, then these are the prototypes
00151 // for __libcwd_malloc et al of course.  We still use external "C" linkage in that case
00152 // in order to avoid a collision with possibily later included prototypes for malloc. ]
00153 extern "C" void* malloc(size_t size) throw() __attribute__((__malloc__));
00154 extern "C" void* calloc(size_t nmemb, size_t size) throw() __attribute__((__malloc__));
00155 extern "C" void* realloc(void* ptr, size_t size) throw() __attribute__((__malloc__));
00156 extern "C" void  free(void* ptr) throw();
00157 
00158 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00159 // Use same kludge for other libc functions that return malloc-ed pointers.
00160 #define strdup __libcwd_strdup
00161 #ifdef HAVE_WMEMCPY
00162 #define wcsdup __libcwd_wcsdup
00163 #endif
00164 
00165 __inline__
00166 char*
00167 __libcwd_strdup(char const* str)
00168 {
00169   size_t size = strlen(str) + 1;
00170   char* p = (char*)malloc(size);
00171   if (p)
00172   {
00173     memcpy(p, str, size);
00174     AllocTag(p, "strdup()");
00175   }
00176   return p;
00177 }
00178 
00179 #ifdef HAVE_WMEMCPY
00180 extern "C" {
00181   size_t wcslen(wchar_t const*);
00182   wchar_t* wmemcpy(wchar_t*, wchar_t const*, size_t);
00183 }
00184 
00185 __inline__
00186 wchar_t*
00187 __libcwd_wcsdup(wchar_t const* str)
00188 {
00189   size_t size = wcslen(str) + 1;
00190   wchar_t* p = (wchar_t*)malloc(size * sizeof(wchar_t));
00191   if (p)
00192   {
00193     wmemcpy(p, str, size);
00194     AllocTag(p, "wcsdup()");
00195   }
00196   return p;
00197 }
00198 #endif // HAVE_WMEMCPY
00199 #endif // !LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00200 
00201 #endif // CWDEBUG_ALLOC
00202 #endif // !DEBUG_INTERNAL
00203 
00204 #endif // LIBCWD_DEBUGMALLOC_H
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.