testoperators.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. * example showing how to implement member new/delete operators
  22. * the implementation might not be the best, but you get a clou which
  23. * operators need to be written.
  24. *
  25. */
  26. #include <stdio.h>
  27. #include <new>
  28. #include "../dumapp.h"
  29. #include "../noduma.h"
  30. class optest
  31. {
  32. public:
  33. /* 1x : SINGLE OBJECT FORM - NO DEBUG INFORMATION */
  34. void * operator new( DUMA_SIZE_T ) throw(std::bad_alloc);
  35. void * operator new( DUMA_SIZE_T , const std::nothrow_t & ) throw();
  36. void operator delete( void * ) throw();
  37. void operator delete( void * , const std::nothrow_t & ) throw();
  38. /* 2x : ARRAY OBJECT FORM - NO DEBUG INFORMATION */
  39. void * operator new[]( DUMA_SIZE_T ) throw(std::bad_alloc);
  40. void * operator new[]( DUMA_SIZE_T , const std::nothrow_t & ) throw();
  41. void operator delete[]( void * ) throw();
  42. void operator delete[]( void *, const std::nothrow_t & ) throw();
  43. #ifndef DUMA_NO_LEAKDETECTION
  44. /* 3x : SINGLE OBJECT FORM - WITH DEBUG INFORMATION */
  45. void * operator new( DUMA_SIZE_T, const char *, int ) throw( std::bad_alloc );
  46. void * operator new( DUMA_SIZE_T, const std::nothrow_t &, const char *, int ) throw();
  47. void operator delete( void *, const char *, int ) throw();
  48. void operator delete( void *, const std::nothrow_t &, const char *, int ) throw();
  49. /* 4x : ARRAY OBJECT FORM - WITH DEBUG INFORMATION */
  50. void * operator new[]( DUMA_SIZE_T, const char *, int ) throw( std::bad_alloc );
  51. void * operator new[]( DUMA_SIZE_T, const std::nothrow_t &, const char *, int ) throw();
  52. void operator delete[]( void *, const char *, int ) throw();
  53. void operator delete[]( void *, const std::nothrow_t &, const char *, int ) throw();
  54. #endif
  55. private:
  56. int dummy;
  57. };
  58. /* 1x : SINGLE OBJECT FORM - NO DEBUG INFORMATION */
  59. void * optest::operator new( DUMA_SIZE_T s )
  60. throw(std::bad_alloc)
  61. {
  62. (void)s;
  63. return ::new optest;
  64. }
  65. void * optest::operator new( DUMA_SIZE_T s, const std::nothrow_t & n )
  66. throw()
  67. {
  68. (void)s;
  69. return ::new(n) optest;
  70. }
  71. void optest::operator delete( void * p )
  72. throw()
  73. {
  74. ::operator delete ((optest*)p);
  75. }
  76. void optest::operator delete( void * p, const std::nothrow_t & n )
  77. throw()
  78. {
  79. ::operator delete((optest*)p, n);
  80. }
  81. /* 2x : ARRAY OBJECT FORM - NO DEBUG INFORMATION */
  82. void * optest::operator new[]( DUMA_SIZE_T s )
  83. throw(std::bad_alloc)
  84. {
  85. return ::new optest[ s / sizeof(optest) ]; // "s / sizeof()" not correct but works for this test
  86. }
  87. void * optest::operator new[]( DUMA_SIZE_T s, const std::nothrow_t & n )
  88. throw()
  89. {
  90. return ::new(n) optest[ s / sizeof(optest) ]; // "s / sizeof()" not correct but works for this test
  91. }
  92. void optest::operator delete[]( void * p )
  93. throw()
  94. {
  95. ::operator delete []((optest*)p);
  96. }
  97. void optest::operator delete[]( void * p, const std::nothrow_t & n )
  98. throw()
  99. {
  100. ::operator delete[]((optest*)p, n);
  101. }
  102. #ifndef DUMA_NO_LEAKDETECTION
  103. /* 3x : SINGLE OBJECT FORM - WITH DEBUG INFORMATION */
  104. void * optest::operator new( DUMA_SIZE_T s, const char * f, int l )
  105. throw( std::bad_alloc )
  106. {
  107. (void)s;
  108. return ::new(f,l) optest;
  109. }
  110. void * optest::operator new( DUMA_SIZE_T s, const std::nothrow_t & n, const char * f, int l )
  111. throw()
  112. {
  113. (void)s;
  114. return ::new(n,f,l) optest;
  115. }
  116. void optest::operator delete( void * p, const char * f, int l )
  117. throw()
  118. {
  119. ::operator delete((optest*)p, f,l);
  120. }
  121. void optest::operator delete( void * p, const std::nothrow_t & n, const char * f, int l )
  122. throw()
  123. {
  124. ::operator delete((optest*)p, n, f,l);
  125. }
  126. /* 4x : ARRAY OBJECT FORM - WITH DEBUG INFORMATION */
  127. void * optest::operator new[]( DUMA_SIZE_T s, const char * f, int l )
  128. throw( std::bad_alloc )
  129. {
  130. return ::new(f,l) optest[s / sizeof(optest)]; // "s / sizeof()" not correct but works for this test
  131. }
  132. void * optest::operator new[]( DUMA_SIZE_T s, const std::nothrow_t & n, const char * f, int l )
  133. throw()
  134. {
  135. return ::new(n, f,l) optest[s / sizeof(optest)]; // "s / sizeof()" not correct but works for this test
  136. }
  137. void optest::operator delete[]( void * p, const char * f, int l )
  138. throw()
  139. {
  140. ::operator delete[]((optest*)p, f,l);
  141. }
  142. void optest::operator delete[]( void * p, const std::nothrow_t & n, const char * f, int l )
  143. throw()
  144. {
  145. ::operator delete[]((optest*)p, n, f,l);
  146. }
  147. #endif /* DUMA_NO_LEAKDETECTION */
  148. void pure_test()
  149. {
  150. /* how to call the operators without any DUMA macros defined */
  151. #include "../noduma.h"
  152. optest * s, * v;
  153. s = new optest;
  154. delete s;
  155. s = new(std::nothrow) optest;
  156. delete s;
  157. v = new optest[0];
  158. delete []v;
  159. v = new optest[10];
  160. delete []v;
  161. v = new(std::nothrow) optest[10];
  162. delete []v;
  163. }
  164. void rich_test()
  165. {
  166. /* how to call the operators with having DUMA macros defined */
  167. #include "../dumapp.h"
  168. optest * s, * v;
  169. s = new optest;
  170. delete s;
  171. #include "../noduma.h"
  172. #ifndef DUMA_NO_LEAKDETECTION
  173. s = new(std::nothrow,__FILE__,__LINE__) optest;
  174. #else
  175. s = new(std::nothrow) optest;
  176. #endif
  177. #include "../dumapp.h"
  178. delete s;
  179. v = new optest[0];
  180. delete []v;
  181. v = new optest[10];
  182. delete []v;
  183. #include "../noduma.h"
  184. #ifndef DUMA_NO_LEAKDETECTION
  185. v = new(std::nothrow,__FILE__,__LINE__) optest[10];
  186. #else
  187. v = new(std::nothrow) optest[10];
  188. #endif
  189. #include "../dumapp.h"
  190. delete []v;
  191. }
  192. int main( int argc, char *argv[] )
  193. {
  194. (void)argc;
  195. (void)argv;
  196. #ifdef DUMA_EXPLICIT_INIT
  197. duma_init();
  198. #endif
  199. pure_test();
  200. rich_test();
  201. return 0;
  202. }