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

macro_AllocTag.h
Go to the documentation of this file.
1 // $Header$
2 //
3 // Copyright (C) 2000 - 2004, by
4 //
5 // Carlo Wood, Run on IRC <carlo@alinoe.com>
6 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
7 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
8 //
9 // This file may be distributed under the terms of the Q Public License
10 // version 1.0 as appearing in the file LICENSE.QPL included in the
11 // packaging of this file.
12 //
13 
18 #ifndef LIBCWD_MACRO_ALLOCTAG_H
19 #define LIBCWD_MACRO_ALLOCTAG_H
20 
21 #ifndef LIBCWD_LIBRARIES_DEBUG_H
22 #error "Don't include <libcwd/macro_AllocTag.h> directly, include the appropriate \"debug.h\" instead."
23 #endif
24 
25 #ifndef LIBCWD_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #if CWDEBUG_ALLOC
30 
31 #ifndef LIBCW_CSTDDEF
32 #define LIBCW_CSTDDEF
33 #include <cstddef> // Needed for size_t.
34 #endif
35 #ifndef LIBCWD_SMART_PTR_H
36 #include "smart_ptr.h"
37 #endif
38 #ifndef LIBCWD_PRIVATE_SET_ALLOC_CHECKING_H
40 #endif
41 #ifndef LIBCWD_TYPE_INFO_H
42 #include "type_info.h"
43 #endif
44 #ifndef LIBCWD_PRIVATE_INTERNAL_STRINGSTREAM_H
46 #endif
47 
48 namespace libcwd {
49 
53 // Undocumented (used inside AllocTag, AllocTag_dynamic_description, AllocTag1 and AllocTag2):
54 extern void set_alloc_label(void const* ptr, type_info_ct const& ti, char const* description LIBCWD_COMMA_TSD_PARAM);
55  // For static descriptions
56 extern void set_alloc_label(void const* ptr, type_info_ct const& ti, _private_::smart_ptr description LIBCWD_COMMA_TSD_PARAM);
57  // For dynamic descriptions
58  // allocated with new[]
59 extern char* allocate_AllocTag_WS_desc(size_t size);
60 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
61 extern void register_external_allocation(void const* ptr, size_t size);
62 #endif
63  // End of group 'group_annotation'.
65 
66 } // namespace libcwd
67 
68 //===================================================================================================
69 // Macro AllocTag
70 //
71 
135 #define AllocTag1(p) \
136  do { \
137  LIBCWD_TSD_DECLARATION; \
138  ::libcwd::set_alloc_label(p, ::libcwd::type_info_of(p), (char const*)NULL LIBCWD_COMMA_TSD); \
139  } while(0)
143 #define AllocTag2(p, desc) \
144  do { \
145  LIBCWD_TSD_DECLARATION; \
146  ::libcwd::set_alloc_label(p, ::libcwd::type_info_of(p), const_cast<char const*>(desc) LIBCWD_COMMA_TSD); \
147  } while(0)
148 
149 #if LIBCWD_THREAD_SAFE
150 #define LIBCWD_ALLOCTAG_LOCK \
151  if (!WS_desc) \
152  { \
153  static pthread_mutex_t WS_desc_mutex = PTHREAD_MUTEX_INITIALIZER; \
154  pthread_mutex_lock(&WS_desc_mutex);
155 #define LIBCWD_ALLOCTAG_UNLOCK \
156  pthread_mutex_unlock(&WS_desc_mutex); \
157  }
158 #else
159 #define LIBCWD_ALLOCTAG_LOCK
160 #define LIBCWD_ALLOCTAG_UNLOCK
161 #endif
162 
166 #define AllocTag(p, x) \
167  do { \
168  LIBCWD_TSD_DECLARATION; \
169  static char* WS_desc; \
170  LIBCWD_ALLOCTAG_LOCK; \
171  if (!WS_desc) { \
172  ++LIBCWD_DO_TSD_MEMBER_OFF(::libcwd::libcw_do); \
173  if (1) \
174  { \
175  ::libcwd::_private_::auto_internal_stringstream buf; \
176  buf << x << ::std::ends; \
177  ::std::streampos pos = buf.rdbuf()->pubseekoff(0, ::std::ios_base::cur, ::std::ios_base::out); \
178  size_t size = pos - ::std::streampos(0); \
179  ::libcwd::_private_::set_alloc_checking_off(LIBCWD_TSD); \
180  WS_desc = ::libcwd::allocate_AllocTag_WS_desc(size); /* This is never deleted anymore */ \
181  ::libcwd::_private_::set_alloc_checking_on(LIBCWD_TSD); \
182  buf.rdbuf()->sgetn(WS_desc, size); \
183  } \
184  --LIBCWD_DO_TSD_MEMBER_OFF(::libcwd::libcw_do); \
185  } \
186  LIBCWD_ALLOCTAG_UNLOCK; \
187  ::libcwd::set_alloc_label(p, ::libcwd::type_info_of(p), WS_desc LIBCWD_COMMA_TSD); \
188  } while(0)
189 
193 #define AllocTag_dynamic_description(p, x) \
194  do { \
195  char* desc; \
196  LIBCWD_TSD_DECLARATION; \
197  ++LIBCWD_DO_TSD_MEMBER_OFF(::libcwd::libcw_do); \
198  if (1) \
199  { \
200  ::libcwd::_private_::auto_internal_stringstream buf; \
201  buf << x << ::std::ends; \
202  ::std::streampos pos = buf.rdbuf()->pubseekoff(0, ::std::ios_base::cur, ::std::ios_base::out); \
203  size_t size = pos - ::std::streampos(0); \
204  ::libcwd::_private_::set_alloc_checking_off(LIBCWD_TSD); \
205  desc = new char [size]; \
206  ::libcwd::_private_::set_alloc_checking_on(LIBCWD_TSD); \
207  buf.rdbuf()->sgetn(desc, size); \
208  } \
209  --LIBCWD_DO_TSD_MEMBER_OFF(::libcwd::libcw_do); \
210  ::libcwd::set_alloc_label(p, ::libcwd::type_info_of(p), \
211  ::libcwd::_private_::smart_ptr(desc) LIBCWD_COMMA_TSD); \
212  } while(0)
213 
214 template<typename TYPE>
215 #ifndef __FreeBSD__
216  // There is a bug in g++ that causes the wrong line number information to be generated
217  // when this function is inline. I was able to use heuristics to work around that for
218  // STABS, but not for DWARF-2 (the default of FreeBSD).
219  // See http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=5271
220  inline
221 #endif
222  TYPE*
223  __libcwd_allocCatcher(TYPE* new_ptr)
224  {
225  AllocTag1(new_ptr);
226  return new_ptr;
227  }
228 
232 #define NEW(x) __libcwd_allocCatcher(new x)
233 
234 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
238 #define RegisterExternalAlloc(p, s) ::libcwd::register_external_allocation(p, s)
239 #endif
240  // End of group 'group_annotation'.
242 
243 #else // !CWDEBUG_ALLOC
244 
248 #define AllocTag(p, x)
249 #define AllocTag_dynamic_description(p, x)
250 #define AllocTag1(p)
251 #define AllocTag2(p, desc)
252 #define NEW(x) new x
253 #ifndef LIBCWD_USE_EXTERNAL_C_LINKAGE_FOR_MALLOC
254 #define RegisterExternalAlloc(p, s)
255 #endif
256  // End of group 'group_annotation'.
258 
259 #endif // !CWDEBUG_ALLOC
260 
261 #endif // LIBCWD_MACRO_ALLOCTAG_H
262 
namespace for libcwd.
Definition: debug.cc:87
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.