NetBurner 3.1
TestMemoryAllocator.h
1 /*
2  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the <organization> nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef D_TestMemoryAllocator_h
29 #define D_TestMemoryAllocator_h
30 
31 struct MemoryLeakNode;
32 class TestMemoryAllocator;
33 
34 extern void setCurrentNewAllocator(TestMemoryAllocator* allocator);
35 extern TestMemoryAllocator* getCurrentNewAllocator();
36 extern void setCurrentNewAllocatorToDefault();
37 extern TestMemoryAllocator* defaultNewAllocator();
38 
39 extern void setCurrentNewArrayAllocator(TestMemoryAllocator* allocator);
40 extern TestMemoryAllocator* getCurrentNewArrayAllocator();
41 extern void setCurrentNewArrayAllocatorToDefault();
42 extern TestMemoryAllocator* defaultNewArrayAllocator();
43 
44 extern void setCurrentMallocAllocator(TestMemoryAllocator* allocator);
45 extern TestMemoryAllocator* getCurrentMallocAllocator();
46 extern void setCurrentMallocAllocatorToDefault();
47 extern TestMemoryAllocator* defaultMallocAllocator();
48 
49 class TestMemoryAllocator
50 {
51 public:
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();
55 
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);
58 
59  virtual const char* name();
60  virtual const char* alloc_name();
61  virtual const char* free_name();
62 
63  virtual bool isOfEqualType(TestMemoryAllocator* allocator);
64 
65  virtual char* allocMemoryLeakNode(size_t size);
66  virtual void freeMemoryLeakNode(char* memory);
67 
68 protected:
69 
70  const char* name_;
71  const char* alloc_name_;
72  const char* free_name_;
73 
74  bool hasBeenDestroyed_;
75 };
76 
77 class CrashOnAllocationAllocator : public TestMemoryAllocator
78 {
79  unsigned allocationToCrashOn_;
80 public:
81  CrashOnAllocationAllocator();
82 
83  virtual void setNumberToCrashOn(unsigned allocationToCrashOn);
84 
85  virtual char* alloc_memory(size_t size, const char* file, int line) _override;
86 };
87 
88 
89 class NullUnknownAllocator: public TestMemoryAllocator
90 {
91 public:
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;
95 
96  static TestMemoryAllocator* defaultAllocator();
97 };
98 
99 class LocationToFailAllocNode;
100 
101 class FailableMemoryAllocator: public TestMemoryAllocator
102 {
103 public:
104  FailableMemoryAllocator(const char* name_str = "failable alloc", const char* alloc_name_str = "alloc", const char* free_name_str = "free");
105 
106  virtual char* alloc_memory(size_t size, const char* file, int line);
107  virtual char* allocMemoryLeakNode(size_t size);
108 
109  virtual void failAllocNumber(int number);
110  virtual void failNthAllocAt(int allocationNumber, const char* file, int line);
111 
112  virtual void checkAllFailedAllocsWereDone();
113  virtual void clearFailedAllocs();
114 
115 protected:
116 
117  LocationToFailAllocNode* head_;
118  int currentAllocNumber_;
119 };
120 
121 #endif
122