28 #ifndef D_TestMemoryAllocator_h 29 #define D_TestMemoryAllocator_h 31 struct MemoryLeakNode;
32 class TestMemoryAllocator;
34 extern void setCurrentNewAllocator(TestMemoryAllocator* allocator);
35 extern TestMemoryAllocator* getCurrentNewAllocator();
36 extern void setCurrentNewAllocatorToDefault();
37 extern TestMemoryAllocator* defaultNewAllocator();
39 extern void setCurrentNewArrayAllocator(TestMemoryAllocator* allocator);
40 extern TestMemoryAllocator* getCurrentNewArrayAllocator();
41 extern void setCurrentNewArrayAllocatorToDefault();
42 extern TestMemoryAllocator* defaultNewArrayAllocator();
44 extern void setCurrentMallocAllocator(TestMemoryAllocator* allocator);
45 extern TestMemoryAllocator* getCurrentMallocAllocator();
46 extern void setCurrentMallocAllocatorToDefault();
47 extern TestMemoryAllocator* defaultMallocAllocator();
49 class TestMemoryAllocator
52 TestMemoryAllocator(
const char* name_str =
"generic",
const char* alloc_name_str =
"alloc",
const char* free_name_str =
"free");
53 virtual ~TestMemoryAllocator();
54 bool hasBeenDestroyed();
56 virtual char* alloc_memory(
size_t size,
const char* file,
int line);
57 virtual void free_memory(
char* memory,
const char* file,
int line);
59 virtual const char* name();
60 virtual const char* alloc_name();
61 virtual const char* free_name();
63 virtual bool isOfEqualType(TestMemoryAllocator* allocator);
65 virtual char* allocMemoryLeakNode(
size_t size);
66 virtual void freeMemoryLeakNode(
char* memory);
71 const char* alloc_name_;
72 const char* free_name_;
74 bool hasBeenDestroyed_;
77 class CrashOnAllocationAllocator :
public TestMemoryAllocator
79 unsigned allocationToCrashOn_;
81 CrashOnAllocationAllocator();
83 virtual void setNumberToCrashOn(
unsigned allocationToCrashOn);
85 virtual char* alloc_memory(
size_t size,
const char* file,
int line) _override;
89 class NullUnknownAllocator:
public TestMemoryAllocator
92 NullUnknownAllocator();
93 virtual char* alloc_memory(
size_t size,
const char* file,
int line) _override;
94 virtual void free_memory(
char* memory,
const char* file,
int line) _override;
96 static TestMemoryAllocator* defaultAllocator();
99 class LocationToFailAllocNode;
101 class FailableMemoryAllocator:
public TestMemoryAllocator
104 FailableMemoryAllocator(
const char* name_str =
"failable alloc",
const char* alloc_name_str =
"alloc",
const char* free_name_str =
"free");
106 virtual char* alloc_memory(
size_t size,
const char* file,
int line);
107 virtual char* allocMemoryLeakNode(
size_t size);
109 virtual void failAllocNumber(
int number);
110 virtual void failNthAllocAt(
int allocationNumber,
const char* file,
int line);
112 virtual void checkAllFailedAllocsWereDone();
113 virtual void clearFailedAllocs();
117 LocationToFailAllocNode* head_;
118 int currentAllocNumber_;