28 #ifndef D_MemoryLeakDetector_h
29 #define D_MemoryLeakDetector_h
31 #define MEM_LEAK_NONE "No memory leaks were detected."
32 #define MEM_LEAK_HEADER "Memory leak(s) found.\n"
33 #define MEM_LEAK_LEAK "Alloc num (%u) Leak size: %d Allocated at: %s and line: %d. Type: \"%s\"\n\t Memory: <%p> Content: \"%.15s\"\n"
34 #define MEM_LEAK_TOO_MUCH "\netc etc etc etc. !!!! Too much memory leaks to report. Bailing out\n"
35 #define MEM_LEAK_FOOTER "Total number of leaks: "
36 #define MEM_LEAK_ADDITION_MALLOC_WARNING "NOTE:\n" \
37 "\tMemory leak reports about malloc and free can be caused by allocating using the cpputest version of malloc,\n" \
38 "\tbut deallocate using the standard free.\n" \
39 "\tIf this is the case, check whether your malloc/free replacements are working (#define malloc cpputest_malloc etc).\n"
41 #define MEM_LEAK_NORMAL_FOOTER_SIZE (sizeof(MEM_LEAK_FOOTER) + 10 + sizeof(MEM_LEAK_TOO_MUCH))
42 #define MEM_LEAK_NORMAL_MALLOC_FOOTER_SIZE (MEM_LEAK_NORMAL_FOOTER_SIZE + sizeof(MEM_LEAK_ADDITION_MALLOC_WARNING))
45 #define MEM_LEAK_ALLOC_DEALLOC_MISMATCH "Allocation/deallocation type mismatch\n"
46 #define MEM_LEAK_MEMORY_CORRUPTION "Memory corruption (written out of bounds?)\n"
47 #define MEM_LEAK_ALLOC_LOCATION " allocated at file: %s line: %d size: %d type: %s\n"
48 #define MEM_LEAK_DEALLOC_LOCATION " deallocated at file: %s line: %d type: %s\n"
49 #define MEM_LEAK_DEALLOC_NON_ALLOCATED "Deallocating non-allocated memory\n"
54 mem_leak_period_disabled,
55 mem_leak_period_enabled,
56 mem_leak_period_checking
68 virtual void fail(
char* fail_string)=0;
75 SIMPLE_STRING_BUFFER_LEN = 4096
80 void add(
const char* format, ...);
83 void setWriteLimit(
size_t write_limit);
84 void resetWriteLimit();
85 bool reachedItsCapacity();
87 char buffer_[SIMPLE_STRING_BUFFER_LEN];
88 size_t positions_filled_;
99 void init(
char* memory,
unsigned number,
size_t size,
TestMemoryAllocator* allocator, MemLeakPeriod period,
const char* file,
int line);
107 MemLeakPeriod period_;
126 MemLeakPeriod period);
128 MemLeakPeriod period);
130 int getTotalLeaks(MemLeakPeriod period);
131 bool hasLeaks(MemLeakPeriod period);
132 void clearAllAccounting(MemLeakPeriod period);
142 void clearAllAccounting(MemLeakPeriod period);
148 bool hasLeaks(MemLeakPeriod period);
149 int getTotalLeaks(MemLeakPeriod period);
153 MemLeakPeriod period);
156 unsigned long hash(
char* memory);
160 hash_prime = MEMORY_LEAK_HASH_TABLE_SIZE
176 void disableAllocationTypeChecking();
177 void enableAllocationTypeChecking();
179 void startChecking();
182 const char* report(MemLeakPeriod period);
183 void markCheckingPeriodLeaksAsNonCheckingPeriod();
184 int totalMemoryLeaks(MemLeakPeriod period);
185 void clearAllAccounting(MemLeakPeriod period);
187 char* allocMemory(
TestMemoryAllocator* allocator,
size_t size,
bool allocatNodesSeperately =
false);
189 const char* file,
int line,
bool allocatNodesSeperately =
false);
190 void deallocMemory(
TestMemoryAllocator* allocator,
void* memory,
bool allocatNodesSeperately =
false);
191 void deallocMemory(
TestMemoryAllocator* allocator,
void* memory,
const char* file,
int line,
bool allocatNodesSeperately =
false);
192 char* reallocMemory(
TestMemoryAllocator* allocator,
char* memory,
size_t size,
const char* file,
int line,
bool allocatNodesSeperately =
false);
194 void invalidateMemory(
char* memory);
195 void removeMemoryLeakInformationWithoutCheckingOrDeallocating(
void* memory);
198 memory_corruption_buffer_size = 3
201 unsigned getCurrentAllocationNumber();
204 MemLeakPeriod current_period_;
207 bool doAllocationTypeChecking_;
208 unsigned allocationSequenceNumber_;
210 bool validMemoryCorruptionInformation(
char* memory);
214 void ConstructMemoryLeakReport(MemLeakPeriod period);
215 void reportFailure(
const char* message,
const char* allocFile,
216 int allocLine,
size_t allocSize,
220 size_t sizeOfMemoryWithCorruptionInfo(
size_t size);
223 char* reallocateMemoryAndLeakInformation(
TestMemoryAllocator* allocator,
char* memory,
size_t size,
const char* file,
int line);
225 void addMemoryCorruptionInformation(
char* memory);
Definition: MemoryLeakDetector.h:140
Definition: MemoryLeakDetector.h:71
Definition: MemoryLeakDetector.h:165
Definition: MemoryLeakDetector.h:92
Definition: MemoryLeakDetector.h:114
Definition: MemoryLeakDetector.h:61
Definition: TestMemoryAllocator.h:49