NetBurner 3.1
MemoryLeakDetectorMallocMacros.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 malloc. This will pass additional information to the
5  * malloc 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  */
11 
12 #include "CppUTestConfig.h"
13 
14 #if CPPUTEST_USE_MEM_LEAK_DETECTION
15 
16 /* This prevents the declaration from done twice and makes sure the file only #defines malloc, so it can be included anywhere */
17 #ifndef CPPUTEST_USE_MALLOC_MACROS
18 
19 #ifdef __cplusplus
20 extern "C"
21 {
22 #endif
23 
24 extern void* cpputest_malloc_location(size_t size, const char* file, int line);
25 extern void* cpputest_calloc_location(size_t count, size_t size, const char* file, int line);
26 extern void* cpputest_realloc_location(void *, size_t, const char* file, int line);
27 extern void cpputest_free_location(void* buffer, const char* file, int line);
28 
29 #ifdef __cplusplus
30 }
31 #endif
32 
33 extern void crash_on_allocation_number(unsigned number);
34 
35 #endif
36 
37 /* NOTE on strdup!
38  *
39  * strdup was implemented earlier, however it is *not* an Standard C function but a POSIX function.
40  * Because of that, it can lead to portability issues by providing more than is available on the local platform.
41  * For that reason, strdup is *not* implemented as a macro. If you still want to use it, an easy implementation would be:
42  *
43  * size_t length = 1 + strlen(str);
44  * char* result = (char*) cpputest_malloc_location(length, file, line);
45  * memcpy(result, str, length);
46  * return result;
47  *
48  */
49 
50 #define malloc(a) cpputest_malloc_location(a, __FILE__, __LINE__)
51 #define calloc(a, b) cpputest_calloc_location(a, b, __FILE__, __LINE__)
52 #define realloc(a, b) cpputest_realloc_location(a, b, __FILE__, __LINE__)
53 #define free(a) cpputest_free_location(a, __FILE__, __LINE__)
54 
55 #define CPPUTEST_USE_MALLOC_MACROS 1
56 #endif