dumapp.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * DUMA - Red-Zone memory allocator.
  3. * Copyright (C) 2002-2008 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
  4. * License: GNU LGPL (GNU Lesser General Public License, see COPYING-GPL)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * FILE CONTENTS:
  21. * header file for inclusion from YOUR C++ application code
  22. * various new/delete/new[]/delete[] declarations
  23. * you must include <new> before including this file.
  24. */
  25. /* explicitly no "#ifndef _DUMAPP_H_" to allow mutliple inclusions
  26. * within single source file with inclusion of noduma.h in between
  27. */
  28. #ifndef __cplusplus
  29. /* avoid usage of C++ operator replacements in C code */
  30. #error compile with a C++ compiler, or define DUMA_NO_CPP_SUPPORT to remove this error
  31. #endif
  32. #include <new>
  33. #include "duma.h"
  34. #if __cplusplus < 201103L
  35. #define DUMA_EXCEPTION_SPECS 1
  36. #endif
  37. /* remove previous macro definitions */
  38. #include "noduma.h"
  39. #if (defined(DUMA_NO_CPP_SUPPORT) || defined(DUMA_NO_LEAKDETECTION))
  40. /* define macros as wrapper without special functionality */
  41. #define NEW_ELEM(TYPE) new TYPE
  42. #define NEW_ARRAY(TYPE, COUNT) new TYPE[COUNT]
  43. #define DEL_ELEM(PTR) delete PTR
  44. #define DEL_ARRAY(PTR) delete[] PTR
  45. #endif /* ( defined(DUMA_NO_CPP_SUPPORT) || defined(DUMA_NO_LEAKDETECTION) ) \
  46. */
  47. #ifndef DUMA_CDECL
  48. #ifdef _MSC_VER
  49. #define DUMA_CDECL __cdecl
  50. #else
  51. #define DUMA_CDECL
  52. #endif
  53. #endif
  54. #ifndef DUMA_SIZE_T
  55. #ifdef _MSC_VER
  56. #define DUMA_SIZE_T size_t
  57. #else
  58. #define DUMA_SIZE_T std::size_t
  59. #endif
  60. #endif
  61. #if __cplusplus <= 199711L
  62. #define NEW_THROW_SPEC throw(std::bad_alloc)
  63. #else
  64. #define NEW_THROW_SPEC
  65. #endif
  66. #ifndef DUMA_NO_CPP_SUPPORT
  67. #ifndef DUMA_CPP_OPERATORS_DECLARED /* && !defined(DUMA_NO_CPP_SUPPORT) */
  68. #define DUMA_CPP_OPERATORS_DECLARED
  69. /*
  70. * Classification
  71. * A allocation <-> F free
  72. * S single object form <-> A array form
  73. * W without nothrow <-> N with std::nothrow_t parameter
  74. */
  75. /* 1x : SINGLE OBJECT FORM - NO DEBUG INFORMATION */
  76. /* (11) = (a) ; ASW */
  77. /* (12) = (b) ; ASN */
  78. /* (13) = (c) ; FSW */
  79. /* (14) = (d) ; FSN */
  80. void *DUMA_CDECL operator new(DUMA_SIZE_T) NEW_THROW_SPEC;
  81. void *DUMA_CDECL operator new(DUMA_SIZE_T, const std::nothrow_t &) throw();
  82. void DUMA_CDECL operator delete(void *) throw();
  83. void DUMA_CDECL operator delete(void *, const std::nothrow_t &) throw();
  84. /* 2x : ARRAY OBJECT FORM - NO DEBUG INFORMATION */
  85. /* (21) = (a) ; AAW */
  86. /* (22) = (b) ; AAN */
  87. /* (23) = (c) ; FAW */
  88. /* (24) = (d) ; FAN */
  89. void *DUMA_CDECL operator new[](DUMA_SIZE_T) NEW_THROW_SPEC;
  90. void *DUMA_CDECL operator new[](DUMA_SIZE_T, const std::nothrow_t &) throw();
  91. void DUMA_CDECL operator delete[](void *) throw();
  92. void DUMA_CDECL operator delete[](void *, const std::nothrow_t &) throw();
  93. #ifndef DUMA_NO_LEAKDETECTION /* && !defined(DUMA_CPP_OPERATORS_DECLARED) */
  94. /* 3x : SINGLE OBJECT FORM - WITH DEBUG INFORMATION */
  95. /* (31) = (a) ; ASW */
  96. /* (32) = (b) ; ASN */
  97. /* (33) = (c) ; FSW */
  98. /* (34) = (d) ; FSN */
  99. void *DUMA_CDECL operator new(DUMA_SIZE_T, const char *, int) NEW_THROW_SPEC;
  100. void *DUMA_CDECL operator new(DUMA_SIZE_T, const std::nothrow_t &, const char *,
  101. int) throw();
  102. void DUMA_CDECL operator delete(void *, const char *, int) throw();
  103. void DUMA_CDECL operator delete(void *, const std::nothrow_t &, const char *,
  104. int) throw();
  105. /* 4x : ARRAY OBJECT FORM - WITH DEBUG INFORMATION */
  106. /* (41) = (a) ; AAW */
  107. /* (42) = (b) ; AAN */
  108. /* (43) = (c) ; FAW */
  109. /* (44) = (d) ; FAN */
  110. void *DUMA_CDECL operator new[](DUMA_SIZE_T, const char *, int) NEW_THROW_SPEC;
  111. void *DUMA_CDECL operator new[](DUMA_SIZE_T, const std::nothrow_t &,
  112. const char *, int) throw();
  113. void DUMA_CDECL operator delete[](void *, const char *, int) throw();
  114. void DUMA_CDECL operator delete[](void *, const std::nothrow_t &, const char *,
  115. int) throw();
  116. #endif /* DUMA_NO_LEAKDETECTION */
  117. #endif /* DUMA_CPP_OPERATORS_DECLARED */
  118. #ifndef DUMA_NO_LEAKDETECTION /* && !defined(DUMA_CPP_OPERATORS_DECLARED) */
  119. #ifndef DUMA_NO_THREAD_SAFETY
  120. #include "duma_sem.h"
  121. #endif
  122. /* define macros as wrapper for our special operators */
  123. #ifdef DUMA_OLD_NEW_MACRO
  124. /* throwing variants */
  125. #define NEW_ELEM(TYPE) new (__FILE__, __LINE__) TYPE
  126. #define NEW_ARRAY(TYPE, COUNT) new (__FILE__, __LINE__) TYPE[COUNT]
  127. /* non throwing variant */
  128. #define NEW_ELEM_NOTHROW(TYPE) new (std::nothrow, __FILE__, __LINE__) TYPE
  129. #define NEW_ARRAY_NOTHROW(TYPE, COUNT) \
  130. new (std::nothrow, __FILE__, __LINE__) TYPE[COUNT]
  131. #else
  132. /* #define new_NOTHROW new(std::nothrow,__FILE__,__LINE__)
  133. */
  134. #define new new (__FILE__, __LINE__)
  135. #endif /* DUMA_OLD_NEW_MACRO */
  136. #ifdef DUMA_OLD_DEL_MACRO
  137. /* always use _duma_g.TLS.DelPtr/DUMA_DeleteFile/DUMA_DeleteLine
  138. * to allow best possible filename/line reports when
  139. * non-DUMA deallocations are called from destructors
  140. */
  141. /* non-throwing */
  142. #define DEL_ELEM(PTR) \
  143. for (++_duma_g.TLS.DelPtr, _duma_g.TLS.Magic = 1; _duma_g.TLS.Magic; \
  144. _duma_g.TLS.Magic = 0, --_duma_g.TLS.DelPtr) \
  145. operator delete(PTR, __FILE__, __LINE__)
  146. #define DEL_ARRAY(PTR) \
  147. for (++_duma_g.TLS.DelPtr, _duma_g.TLS.Magic = 1; _duma_g.TLS.Magic; \
  148. _duma_g.TLS.Magic = 0, --_duma_g.TLS.DelPtr) \
  149. operator delete[](PTR, __FILE__, __LINE__)
  150. /* explicitly non-throwing */
  151. #define DEL_ELEM_NOTHROW(PTR) \
  152. for (++_duma_g.TLS.DelPtr, _duma_g.TLS.Magic = 1; _duma_g.TLS.Magic; \
  153. _duma_g.TLS.Magic = 0, --_duma_g.TLS.DelPtr) \
  154. operator delete(PTR, std::nothrow, __FILE__, __LINE__)
  155. #define DEL_ARRAY_NOTHROW(PTR) \
  156. for (++_duma_g.TLS.DelPtr, _duma_g.TLS.Magic = 1; _duma_g.TLS.Magic; \
  157. _duma_g.TLS.Magic = 0, --_duma_g.TLS.DelPtr) \
  158. operator delete[](PTR, std::nothrow, __FILE__, __LINE__)
  159. #else
  160. #ifndef DUMA_NO_THREAD_SAFETY
  161. /* define a thread safe delete */
  162. #define delete \
  163. for (DUMA_GET_SEMAPHORE(), DUMA_GET_SEMAPHORE(), ++_duma_g.TLS.DelPtr, \
  164. DUMA_ASSERT(_duma_g.TLS.DelPtr < DUMA_MAX_DEL_DEPTH), \
  165. _duma_g.TLS.Magic = 1, \
  166. _duma_g.TLS.DelFile[_duma_g.TLS.DelPtr] = __FILE__, \
  167. _duma_g.TLS.DelLine[_duma_g.TLS.DelPtr] = __LINE__; \
  168. DUMA_RELEASE_SEMAPHORE(_duma_g.TLS.Magic); \
  169. _duma_g.TLS.Magic = 0, --_duma_g.TLS.DelPtr) \
  170. delete
  171. #else
  172. /* also thread safe by using TLS variables */
  173. #define delete \
  174. for (++_duma_g.TLS.DelPtr, \
  175. DUMA_ASSERT(_duma_g.TLS.DelPtr < DUMA_MAX_DEL_DEPTH), \
  176. _duma_g.TLS.Magic = 1, \
  177. _duma_g.TLS.DelFile[_duma_g.TLS.DelPtr] = __FILE__, \
  178. _duma_g.TLS.DelLine[_duma_g.TLS.DelPtr] = __LINE__; \
  179. _duma_g.TLS.Magic; _duma_g.TLS.Magic = 0, --_duma_g.TLS.DelPtr) \
  180. delete
  181. #endif /* DUMA_NO_THREAD_SAFETY */
  182. #endif /* DUMA_OLD_DEL_MACRO */
  183. #endif /* end ifdef DUMA_NO_LEAKDETECTION */
  184. #endif /* end ifdef DUMA_NO_CPP_SUPPORT */
  185. #include "duma.h"