00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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>
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
00054 extern void set_alloc_label(void const* ptr, type_info_ct const& ti, char const* description LIBCWD_COMMA_TSD_PARAM);
00055
00056 extern void set_alloc_label(void const* ptr, type_info_ct const& ti, _private_::smart_ptr description LIBCWD_COMMA_TSD_PARAM);
00057
00058
00059 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
00060 extern void register_external_allocation(void const* ptr, size_t size);
00061 #endif
00062
00064
00065 }
00066
00067
00068
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]; \
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
00216
00217
00218
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
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
00257
00258 #endif // !CWDEBUG_ALLOC
00259
00260 #endif // LIBCWD_MACRO_ALLOCTAG_H
00261