CppUTest
MemoryLeakDetectorNewMacros.h
1 
2 /*
3  * This file can be used to get extra debugging information about memory leaks in your production code.
4  * It defines a preprocessor macro for operator new. This will pass additional information to the
5  * operator new and this will give the line/file information of the memory leaks in your code.
6  *
7  * You can use this by including this file to all your production code. When using gcc, you can use
8  * the -include file to do this for you.
9  *
10  * Warning: Using the new macro can cause a conflict with newly declared operator news. This can be
11  * resolved by:
12  * 1. #undef operator new before including this file
13  * 2. Including the files that override operator new before this file.
14  * This can be done by creating your own NewMacros.h file that includes your operator new overrides
15  * and THEN this file.
16  *
17  * STL (or StdC++ lib) also does overrides for operator new. Therefore, you'd need to include the STL
18  * files *before* this file too.
19  *
20  */
21 
22 /* Warning for maintainers:
23  * This macro code is duplicate from TestHarness.h. The reason for this is to make the two files
24  * completely independent from each other. NewMacros file can be included in production code whereas
25  * TestHarness.h is only included in test code.
26  */
27 
28 #include "CppUTestConfig.h"
29 
30 #if CPPUTEST_USE_MEM_LEAK_DETECTION
31 
32 /* This #ifndef prevents <new> from being included twice and enables the file to be included anywhere */
33 #ifndef CPPUTEST_USE_NEW_MACROS
34 
35  #if CPPUTEST_USE_STD_CPP_LIB
36  #include <new>
37  #include <memory>
38  #include <string>
39 
40  void* operator new(size_t size, const char* file, int line) throw (std::bad_alloc);
41  void* operator new[](size_t size, const char* file, int line) throw (std::bad_alloc);
42  void* operator new(size_t size) throw(std::bad_alloc);
43  void* operator new[](size_t size) throw(std::bad_alloc);
44 
45  #else
46 
47  void* operator new(size_t size, const char* file, int line);
48  void* operator new[](size_t size, const char* file, int line);
49  void* operator new(size_t size);
50  void* operator new[](size_t size);
51  #endif
52 #endif
53 
54 #define new new(__FILE__, __LINE__)
55 
56 #define CPPUTEST_USE_NEW_MACROS 1
57 
58 #endif