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

macro_AllocTag.h

Go to the documentation of this file.
00001 // $Header: /cvsroot/libcwd/libcwd/include/libcwd/macro_AllocTag.h,v 1.13 2005/12/19 16:02:25 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_MACRO_ALLOCTAG_H
00019 #define LIBCWD_MACRO_ALLOCTAG_H
00020 
00021 #ifndef LIBCWD_LIBRARIES_DEBUG_H
00022 #error "Don't include <libcwd/macro_AllocTag.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_SMART_PTR_H
00036 #include <libcwd/smart_ptr.h>
00037 #endif
00038 #ifndef LIBCWD_PRIVATE_SET_ALLOC_CHECKING_H
00039 #include <libcwd/private_set_alloc_checking.h>
00040 #endif
00041 #ifndef LIBCWD_TYPE_INFO_H
00042 #include <libcwd/type_info.h>
00043 #endif
00044 #ifndef LIBCWD_PRIVATE_INTERNAL_STRINGSTREAM_H
00045 #include <libcwd/private_internal_stringstream.h>
00046 #endif
00047 
00048 namespace libcwd {
00049 
00053 // Undocumented (used inside AllocTag, AllocTag_dynamic_description, AllocTag1 and AllocTag2):
00054 extern void set_alloc_label(void const* ptr, type_info_ct const& ti, char const* description LIBCWD_COMMA_TSD_PARAM);
00055     // For static descriptions
00056 extern void set_alloc_label(void const* ptr, type_info_ct const& ti, _private_::smart_ptr description LIBCWD_COMMA_TSD_PARAM);
00057     // For dynamic descriptions
00058     // allocated with new[]
00059 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00060 extern void register_external_allocation(void const* ptr, size_t size);
00061 #endif
00062  // End of group 'group_annotation'.
00064 
00065 } // namespace libcwd
00066 
00067 //===================================================================================================
00068 // Macro AllocTag
00069 //
00070 
00134 #define AllocTag1(p) \
00135     do { \
00136       LIBCWD_TSD_DECLARATION; \
00137       ::libcwd::set_alloc_label(p, ::libcwd::type_info_of(p), (char const*)NULL LIBCWD_COMMA_TSD); \
00138     } while(0)
00139 
00142 #define AllocTag2(p, desc) \
00143     do { \
00144       LIBCWD_TSD_DECLARATION; \
00145       ::libcwd::set_alloc_label(p, ::libcwd::type_info_of(p), const_cast<char const*>(desc) LIBCWD_COMMA_TSD); \
00146     } while(0)
00147 
00148 #if LIBCWD_THREAD_SAFE
00149 #define LIBCWD_ALLOCTAG_LOCK \
00150     if (!WS_desc) \
00151     { \
00152       static pthread_mutex_t WS_desc_mutex = PTHREAD_MUTEX_INITIALIZER; \
00153       pthread_mutex_lock(&WS_desc_mutex);
00154 #define LIBCWD_ALLOCTAG_UNLOCK \
00155       pthread_mutex_unlock(&WS_desc_mutex); \
00156     }
00157 #else
00158 #define LIBCWD_ALLOCTAG_LOCK
00159 #define LIBCWD_ALLOCTAG_UNLOCK
00160 #endif
00161 
00165 #define AllocTag(p, x) \
00166     do { \
00167       LIBCWD_TSD_DECLARATION; \
00168       static char* WS_desc; \
00169       LIBCWD_ALLOCTAG_LOCK; \
00170       if (!WS_desc) { \
00171         ++LIBCWD_DO_TSD_MEMBER_OFF(::libcwd::libcw_do); \
00172         if (1) \
00173         { \
00174           ::libcwd::_private_::auto_internal_stringstream buf; \
00175           buf << x << ::std::ends; \
00176           ::std::streampos pos = buf.rdbuf()->pubseekoff(0, ::std::ios_base::cur, ::std::ios_base::out); \
00177           size_t size = pos - ::std::streampos(0); \
00178           ::libcwd::_private_::set_alloc_checking_off(LIBCWD_TSD); \
00179           WS_desc = new char [size]; /* This is never deleted anymore */ \
00180           ::libcwd::_private_::set_alloc_checking_on(LIBCWD_TSD); \
00181           buf.rdbuf()->sgetn(WS_desc, size); \
00182         } \
00183         --LIBCWD_DO_TSD_MEMBER_OFF(::libcwd::libcw_do); \
00184       } \
00185       LIBCWD_ALLOCTAG_UNLOCK; \
00186       ::libcwd::set_alloc_label(p, ::libcwd::type_info_of(p), WS_desc LIBCWD_COMMA_TSD); \
00187     } while(0)
00188 
00192 #define AllocTag_dynamic_description(p, x) \
00193     do { \
00194       char* desc; \
00195       LIBCWD_TSD_DECLARATION; \
00196       ++LIBCWD_DO_TSD_MEMBER_OFF(::libcwd::libcw_do); \
00197       if (1) \
00198       { \
00199         ::libcwd::_private_::auto_internal_stringstream buf; \
00200         buf << x << ::std::ends; \
00201         ::std::streampos pos = buf.rdbuf()->pubseekoff(0, ::std::ios_base::cur, ::std::ios_base::out); \
00202         size_t size = pos - ::std::streampos(0); \
00203         ::libcwd::_private_::set_alloc_checking_off(LIBCWD_TSD); \
00204         desc = new char [size]; \
00205         ::libcwd::_private_::set_alloc_checking_on(LIBCWD_TSD); \
00206         buf.rdbuf()->sgetn(desc, size); \
00207       } \
00208       --LIBCWD_DO_TSD_MEMBER_OFF(::libcwd::libcw_do); \
00209       ::libcwd::set_alloc_label(p, ::libcwd::type_info_of(p), \
00210                                       ::libcwd::_private_::smart_ptr(desc) LIBCWD_COMMA_TSD); \
00211     } while(0)
00212 
00213 template<typename TYPE>
00214 #ifndef __FreeBSD__
00215   // There is a bug in g++ that causes the wrong line number information to be generated
00216   // when this function is inline.  I was able to use heuristics to work around that for
00217   // STABS, but not for DWARF-2 (the default of FreeBSD).
00218   // See http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=5271
00219   inline
00220 #endif
00221   TYPE*
00222   __libcwd_allocCatcher(TYPE* new_ptr)
00223   {
00224     AllocTag1(new_ptr);
00225     return new_ptr;
00226   }
00227 
00231 #define NEW(x) __libcwd_allocCatcher(new x)
00232 
00233 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00234 
00237 #define RegisterExternalAlloc(p, s) ::libcwd::register_external_allocation(p, s)
00238 #endif
00239  // End of group 'group_annotation'.
00241 
00242 #else // !CWDEBUG_ALLOC
00243 
00247 #define AllocTag(p, x)
00248 #define AllocTag_dynamic_description(p, x)
00249 #define AllocTag1(p)
00250 #define AllocTag2(p, desc)
00251 #define NEW(x) new x
00252 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00253 #define RegisterExternalAlloc(p, s)
00254 #endif
00255  // End of group 'group_annotation'.
00257 
00258 #endif // !CWDEBUG_ALLOC
00259 
00260 #endif // LIBCWD_MACRO_ALLOCTAG_H
00261 
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.