28 #ifndef D_MemoryLeakDetector_h 29 #define D_MemoryLeakDetector_h 34 mem_leak_period_disabled,
35 mem_leak_period_enabled,
36 mem_leak_period_checking
39 class TestMemoryAllocator;
42 class MemoryLeakFailure
45 virtual ~MemoryLeakFailure()
49 virtual void fail(
char* fail_string)=0;
52 struct SimpleStringBuffer
56 SIMPLE_STRING_BUFFER_LEN = 4096
61 void add(
const char* format, ...) __check_format__(printf, 2, 3);
62 void addMemoryDump(const
void* memory,
size_t memorySize);
66 void setWriteLimit(
size_t write_limit);
67 void resetWriteLimit();
68 bool reachedItsCapacity();
70 char buffer_[SIMPLE_STRING_BUFFER_LEN];
71 size_t positions_filled_;
75 struct MemoryLeakDetectorNode;
77 class MemoryLeakOutputStringBuffer
80 MemoryLeakOutputStringBuffer();
84 void startMemoryLeakReporting();
85 void stopMemoryLeakReporting();
87 void reportMemoryLeak(MemoryLeakDetectorNode* leak);
89 void reportDeallocateNonAllocatedMemoryFailure(
const char* freeFile,
int freeLine, TestMemoryAllocator* freeAllocator, MemoryLeakFailure* reporter);
90 void reportMemoryCorruptionFailure(MemoryLeakDetectorNode* node,
const char* freeFile,
int freeLineNumber, TestMemoryAllocator* freeAllocator, MemoryLeakFailure* reporter);
91 void reportAllocationDeallocationMismatchFailure(MemoryLeakDetectorNode* node,
const char* freeFile,
int freeLineNumber, TestMemoryAllocator* freeAllocator, MemoryLeakFailure* reporter);
95 void addAllocationLocation(
const char* allocationFile,
int allocationLineNumber,
size_t allocationSize, TestMemoryAllocator* allocator);
96 void addDeallocationLocation(
const char* freeFile,
int freeLineNumber, TestMemoryAllocator* allocator);
98 void addMemoryLeakHeader();
99 void addMemoryLeakFooter(
int totalAmountOfLeaks);
100 void addWarningForUsingMalloc();
101 void addNoMemoryLeaksMessage();
102 void addErrorMessageForTooMuchLeaks();
107 bool giveWarningOnUsingMalloc_;
109 void reportFailure(
const char* message,
const char* allocFile,
110 int allocLine,
size_t allocSize,
111 TestMemoryAllocator* allocAllocator,
const char* freeFile,
112 int freeLine, TestMemoryAllocator* freeAllocator, MemoryLeakFailure* reporter);
114 SimpleStringBuffer outputBuffer_;
117 struct MemoryLeakDetectorNode
119 MemoryLeakDetectorNode() :
120 size_(0), number_(0), memory_(0), file_(0), line_(0), allocator_(0), period_(mem_leak_period_enabled), next_(0)
124 void init(
char* memory,
unsigned number,
size_t size, TestMemoryAllocator* allocator, MemLeakPeriod period,
const char* file,
int line);
131 TestMemoryAllocator* allocator_;
132 MemLeakPeriod period_;
135 friend struct MemoryLeakDetectorList;
136 MemoryLeakDetectorNode* next_;
139 struct MemoryLeakDetectorList
141 MemoryLeakDetectorList() :
145 void addNewNode(MemoryLeakDetectorNode* node);
146 MemoryLeakDetectorNode* retrieveNode(
char* memory);
147 MemoryLeakDetectorNode* removeNode(
char* memory);
149 MemoryLeakDetectorNode* getFirstLeak(MemLeakPeriod period);
150 MemoryLeakDetectorNode* getNextLeak(MemoryLeakDetectorNode* node,
151 MemLeakPeriod period);
152 MemoryLeakDetectorNode* getLeakFrom(MemoryLeakDetectorNode* node,
153 MemLeakPeriod period);
155 int getTotalLeaks(MemLeakPeriod period);
156 void clearAllAccounting(MemLeakPeriod period);
158 bool isInPeriod(MemoryLeakDetectorNode* node, MemLeakPeriod period);
161 MemoryLeakDetectorNode* head_;
164 struct MemoryLeakDetectorTable
166 void clearAllAccounting(MemLeakPeriod period);
168 void addNewNode(MemoryLeakDetectorNode* node);
169 MemoryLeakDetectorNode* retrieveNode(
char* memory);
170 MemoryLeakDetectorNode* removeNode(
char* memory);
172 int getTotalLeaks(MemLeakPeriod period);
174 MemoryLeakDetectorNode* getFirstLeak(MemLeakPeriod period);
175 MemoryLeakDetectorNode* getNextLeak(MemoryLeakDetectorNode* leak,
176 MemLeakPeriod period);
179 unsigned long hash(
char* memory);
183 hash_prime = MEMORY_LEAK_HASH_TABLE_SIZE
185 MemoryLeakDetectorList table_[hash_prime];
188 class MemoryLeakDetector
191 MemoryLeakDetector(MemoryLeakFailure* reporter);
192 virtual ~MemoryLeakDetector();
197 void disableAllocationTypeChecking();
198 void enableAllocationTypeChecking();
200 void startChecking();
203 const char* report(MemLeakPeriod period);
204 void markCheckingPeriodLeaksAsNonCheckingPeriod();
205 int totalMemoryLeaks(MemLeakPeriod period);
206 void clearAllAccounting(MemLeakPeriod period);
208 char* allocMemory(TestMemoryAllocator* allocator,
size_t size,
bool allocatNodesSeperately =
false);
209 char* allocMemory(TestMemoryAllocator* allocator,
size_t size,
210 const char* file,
int line,
bool allocatNodesSeperately =
false);
211 void deallocMemory(TestMemoryAllocator* allocator,
void* memory,
bool allocatNodesSeperately =
false);
212 void deallocMemory(TestMemoryAllocator* allocator,
void* memory,
const char* file,
int line,
bool allocatNodesSeperately =
false);
213 char* reallocMemory(TestMemoryAllocator* allocator,
char* memory,
size_t size,
const char* file,
int line,
bool allocatNodesSeperately =
false);
215 void invalidateMemory(
char* memory);
216 void removeMemoryLeakInformationWithoutCheckingOrDeallocatingTheMemoryButDeallocatingTheAccountInformation(TestMemoryAllocator* allocator,
void* memory,
bool allocatNodesSeperately);
219 memory_corruption_buffer_size = 3
222 unsigned getCurrentAllocationNumber();
224 SimpleMutex* getMutex(
void);
226 MemoryLeakFailure* reporter_;
227 MemLeakPeriod current_period_;
228 MemoryLeakOutputStringBuffer outputBuffer_;
229 MemoryLeakDetectorTable memoryTable_;
230 bool doAllocationTypeChecking_;
231 unsigned allocationSequenceNumber_;
234 char* allocateMemoryWithAccountingInformation(TestMemoryAllocator* allocator,
size_t size,
const char* file,
int line,
bool allocatNodesSeperately);
235 char* reallocateMemoryWithAccountingInformation(TestMemoryAllocator* allocator,
char* memory,
size_t size,
const char* file,
int line,
bool allocatNodesSeperately);
236 MemoryLeakDetectorNode* createMemoryLeakAccountingInformation(TestMemoryAllocator* allocator,
size_t size,
char* memory,
bool allocatNodesSeperately);
239 bool validMemoryCorruptionInformation(
char* memory);
240 bool matchingAllocation(TestMemoryAllocator *alloc_allocator, TestMemoryAllocator *free_allocator);
242 void storeLeakInformation(MemoryLeakDetectorNode * node,
char *new_memory,
size_t size, TestMemoryAllocator *allocator,
const char *file,
int line);
243 void ConstructMemoryLeakReport(MemLeakPeriod period);
245 size_t sizeOfMemoryWithCorruptionInfo(
size_t size);
246 MemoryLeakDetectorNode* getNodeFromMemoryPointer(
char* memory,
size_t size);
248 char* reallocateMemoryAndLeakInformation(TestMemoryAllocator* allocator,
char* memory,
size_t size,
const char* file,
int line,
bool allocatNodesSeperately);
250 void addMemoryCorruptionInformation(
char* memory);
251 void checkForCorruption(MemoryLeakDetectorNode* node,
const char* file,
int line, TestMemoryAllocator* allocator,
bool allocateNodesSeperately);
void init()
System initialization. Normally called at the beginning of all applications.
Definition: init.cpp:22