Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of cpputest by
src/CppUTest/TestMemoryAllocator.cpp@1:4769360130ed, 2014-06-17 (annotated)
- Committer:
- Rohit Grover 
- Date:
- Tue Jun 17 15:52:54 2014 +0100
- Revision:
- 1:4769360130ed
- Parent:
- 0:0b799af9d58e
updating to the latest version of cppUtest; tested against nordic's m-kit
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| Rohit Grover | 1:4769360130ed | 1 | /* | 
| Rohit Grover | 1:4769360130ed | 2 | * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde | 
| Rohit Grover | 1:4769360130ed | 3 | * All rights reserved. | 
| Rohit Grover | 1:4769360130ed | 4 | * | 
| Rohit Grover | 1:4769360130ed | 5 | * Redistribution and use in source and binary forms, with or without | 
| Rohit Grover | 1:4769360130ed | 6 | * modification, are permitted provided that the following conditions are met: | 
| Rohit Grover | 1:4769360130ed | 7 | * * Redistributions of source code must retain the above copyright | 
| Rohit Grover | 1:4769360130ed | 8 | * notice, this list of conditions and the following disclaimer. | 
| Rohit Grover | 1:4769360130ed | 9 | * * Redistributions in binary form must reproduce the above copyright | 
| Rohit Grover | 1:4769360130ed | 10 | * notice, this list of conditions and the following disclaimer in the | 
| Rohit Grover | 1:4769360130ed | 11 | * documentation and/or other materials provided with the distribution. | 
| Rohit Grover | 1:4769360130ed | 12 | * * Neither the name of the <organization> nor the | 
| Rohit Grover | 1:4769360130ed | 13 | * names of its contributors may be used to endorse or promote products | 
| Rohit Grover | 1:4769360130ed | 14 | * derived from this software without specific prior written permission. | 
| Rohit Grover | 1:4769360130ed | 15 | * | 
| Rohit Grover | 1:4769360130ed | 16 | * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY | 
| Rohit Grover | 1:4769360130ed | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 
| Rohit Grover | 1:4769360130ed | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
| Rohit Grover | 1:4769360130ed | 19 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY | 
| Rohit Grover | 1:4769360130ed | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 
| Rohit Grover | 1:4769360130ed | 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 
| Rohit Grover | 1:4769360130ed | 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 
| Rohit Grover | 1:4769360130ed | 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| Rohit Grover | 1:4769360130ed | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
| Rohit Grover | 1:4769360130ed | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| Rohit Grover | 1:4769360130ed | 26 | */ | 
| Rohit Grover | 1:4769360130ed | 27 | |
| Rohit Grover | 1:4769360130ed | 28 | #include "CppUTest/TestHarness.h" | 
| Rohit Grover | 1:4769360130ed | 29 | #include "CppUTest/TestMemoryAllocator.h" | 
| Rohit Grover | 1:4769360130ed | 30 | #include "CppUTest/PlatformSpecificFunctions.h" | 
| Rohit Grover | 1:4769360130ed | 31 | #include "CppUTest/MemoryLeakDetector.h" | 
| Rohit Grover | 1:4769360130ed | 32 | |
| Rohit Grover | 1:4769360130ed | 33 | static char* checkedMalloc(size_t size) | 
| Rohit Grover | 1:4769360130ed | 34 | { | 
| Rohit Grover | 1:4769360130ed | 35 | char* mem = (char*) PlatformSpecificMalloc(size); | 
| Rohit Grover | 1:4769360130ed | 36 | if (mem == 0) | 
| Rohit Grover | 1:4769360130ed | 37 | FAIL("malloc returned null pointer"); | 
| Rohit Grover | 1:4769360130ed | 38 | return mem; | 
| Rohit Grover | 1:4769360130ed | 39 | } | 
| Rohit Grover | 1:4769360130ed | 40 | |
| Rohit Grover | 1:4769360130ed | 41 | static TestMemoryAllocator* currentNewAllocator = 0; | 
| Rohit Grover | 1:4769360130ed | 42 | static TestMemoryAllocator* currentNewArrayAllocator = 0; | 
| Rohit Grover | 1:4769360130ed | 43 | static TestMemoryAllocator* currentMallocAllocator = 0; | 
| Rohit Grover | 1:4769360130ed | 44 | |
| Rohit Grover | 1:4769360130ed | 45 | void setCurrentNewAllocator(TestMemoryAllocator* allocator) | 
| Rohit Grover | 1:4769360130ed | 46 | { | 
| Rohit Grover | 1:4769360130ed | 47 | currentNewAllocator = allocator; | 
| Rohit Grover | 1:4769360130ed | 48 | } | 
| Rohit Grover | 1:4769360130ed | 49 | |
| Rohit Grover | 1:4769360130ed | 50 | TestMemoryAllocator* getCurrentNewAllocator() | 
| Rohit Grover | 1:4769360130ed | 51 | { | 
| Rohit Grover | 1:4769360130ed | 52 | if (currentNewAllocator == 0) setCurrentNewAllocatorToDefault(); | 
| Rohit Grover | 1:4769360130ed | 53 | return currentNewAllocator; | 
| Rohit Grover | 1:4769360130ed | 54 | } | 
| Rohit Grover | 1:4769360130ed | 55 | |
| Rohit Grover | 1:4769360130ed | 56 | void setCurrentNewAllocatorToDefault() | 
| Rohit Grover | 1:4769360130ed | 57 | { | 
| Rohit Grover | 1:4769360130ed | 58 | currentNewAllocator = defaultNewAllocator(); | 
| Rohit Grover | 1:4769360130ed | 59 | } | 
| Rohit Grover | 1:4769360130ed | 60 | |
| Rohit Grover | 1:4769360130ed | 61 | TestMemoryAllocator* defaultNewAllocator() | 
| Rohit Grover | 1:4769360130ed | 62 | { | 
| Rohit Grover | 1:4769360130ed | 63 | static TestMemoryAllocator allocator("Standard New Allocator", "new", "delete"); | 
| Rohit Grover | 1:4769360130ed | 64 | return &allocator; | 
| Rohit Grover | 1:4769360130ed | 65 | } | 
| Rohit Grover | 1:4769360130ed | 66 | |
| Rohit Grover | 1:4769360130ed | 67 | void setCurrentNewArrayAllocator(TestMemoryAllocator* allocator) | 
| Rohit Grover | 1:4769360130ed | 68 | { | 
| Rohit Grover | 1:4769360130ed | 69 | currentNewArrayAllocator = allocator; | 
| Rohit Grover | 1:4769360130ed | 70 | } | 
| Rohit Grover | 1:4769360130ed | 71 | |
| Rohit Grover | 1:4769360130ed | 72 | TestMemoryAllocator* getCurrentNewArrayAllocator() | 
| Rohit Grover | 1:4769360130ed | 73 | { | 
| Rohit Grover | 1:4769360130ed | 74 | if (currentNewArrayAllocator == 0) setCurrentNewArrayAllocatorToDefault(); | 
| Rohit Grover | 1:4769360130ed | 75 | return currentNewArrayAllocator; | 
| Rohit Grover | 1:4769360130ed | 76 | } | 
| Rohit Grover | 1:4769360130ed | 77 | |
| Rohit Grover | 1:4769360130ed | 78 | void setCurrentNewArrayAllocatorToDefault() | 
| Rohit Grover | 1:4769360130ed | 79 | { | 
| Rohit Grover | 1:4769360130ed | 80 | currentNewArrayAllocator = defaultNewArrayAllocator(); | 
| Rohit Grover | 1:4769360130ed | 81 | } | 
| Rohit Grover | 1:4769360130ed | 82 | |
| Rohit Grover | 1:4769360130ed | 83 | TestMemoryAllocator* defaultNewArrayAllocator() | 
| Rohit Grover | 1:4769360130ed | 84 | { | 
| Rohit Grover | 1:4769360130ed | 85 | static TestMemoryAllocator allocator("Standard New [] Allocator", "new []", "delete []"); | 
| Rohit Grover | 1:4769360130ed | 86 | return &allocator; | 
| Rohit Grover | 1:4769360130ed | 87 | } | 
| Rohit Grover | 1:4769360130ed | 88 | |
| Rohit Grover | 1:4769360130ed | 89 | void setCurrentMallocAllocator(TestMemoryAllocator* allocator) | 
| Rohit Grover | 1:4769360130ed | 90 | { | 
| Rohit Grover | 1:4769360130ed | 91 | currentMallocAllocator = allocator; | 
| Rohit Grover | 1:4769360130ed | 92 | } | 
| Rohit Grover | 1:4769360130ed | 93 | |
| Rohit Grover | 1:4769360130ed | 94 | TestMemoryAllocator* getCurrentMallocAllocator() | 
| Rohit Grover | 1:4769360130ed | 95 | { | 
| Rohit Grover | 1:4769360130ed | 96 | if (currentMallocAllocator == 0) setCurrentMallocAllocatorToDefault(); | 
| Rohit Grover | 1:4769360130ed | 97 | return currentMallocAllocator; | 
| Rohit Grover | 1:4769360130ed | 98 | } | 
| Rohit Grover | 1:4769360130ed | 99 | |
| Rohit Grover | 1:4769360130ed | 100 | void setCurrentMallocAllocatorToDefault() | 
| Rohit Grover | 1:4769360130ed | 101 | { | 
| Rohit Grover | 1:4769360130ed | 102 | currentMallocAllocator = defaultMallocAllocator(); | 
| Rohit Grover | 1:4769360130ed | 103 | } | 
| Rohit Grover | 1:4769360130ed | 104 | |
| Rohit Grover | 1:4769360130ed | 105 | TestMemoryAllocator* defaultMallocAllocator() | 
| Rohit Grover | 1:4769360130ed | 106 | { | 
| Rohit Grover | 1:4769360130ed | 107 | static TestMemoryAllocator allocator("Standard Malloc Allocator", "malloc", "free"); | 
| Rohit Grover | 1:4769360130ed | 108 | return &allocator; | 
| Rohit Grover | 1:4769360130ed | 109 | } | 
| Rohit Grover | 1:4769360130ed | 110 | |
| Rohit Grover | 1:4769360130ed | 111 | ///////////////////////////////////////////// | 
| Rohit Grover | 1:4769360130ed | 112 | |
| Rohit Grover | 1:4769360130ed | 113 | TestMemoryAllocator::TestMemoryAllocator(const char* name_str, const char* alloc_name_str, const char* free_name_str) | 
| Rohit Grover | 1:4769360130ed | 114 | : name_(name_str), alloc_name_(alloc_name_str), free_name_(free_name_str), hasBeenDestroyed_(false) | 
| Rohit Grover | 1:4769360130ed | 115 | { | 
| Rohit Grover | 1:4769360130ed | 116 | } | 
| Rohit Grover | 1:4769360130ed | 117 | |
| Rohit Grover | 1:4769360130ed | 118 | TestMemoryAllocator::~TestMemoryAllocator() | 
| Rohit Grover | 1:4769360130ed | 119 | { | 
| Rohit Grover | 1:4769360130ed | 120 | hasBeenDestroyed_ = true; | 
| Rohit Grover | 1:4769360130ed | 121 | } | 
| Rohit Grover | 1:4769360130ed | 122 | |
| Rohit Grover | 1:4769360130ed | 123 | bool TestMemoryAllocator::hasBeenDestroyed() | 
| Rohit Grover | 1:4769360130ed | 124 | { | 
| Rohit Grover | 1:4769360130ed | 125 | return hasBeenDestroyed_; | 
| Rohit Grover | 1:4769360130ed | 126 | } | 
| Rohit Grover | 1:4769360130ed | 127 | |
| Rohit Grover | 1:4769360130ed | 128 | bool TestMemoryAllocator::isOfEqualType(TestMemoryAllocator* allocator) | 
| Rohit Grover | 1:4769360130ed | 129 | { | 
| Rohit Grover | 1:4769360130ed | 130 | return PlatformSpecificStrCmp(this->name(), allocator->name()) == 0; | 
| Rohit Grover | 1:4769360130ed | 131 | } | 
| Rohit Grover | 1:4769360130ed | 132 | |
| Rohit Grover | 1:4769360130ed | 133 | char* TestMemoryAllocator::allocMemoryLeakNode(size_t size) | 
| Rohit Grover | 1:4769360130ed | 134 | { | 
| Rohit Grover | 1:4769360130ed | 135 | return alloc_memory(size, "MemoryLeakNode", 1); | 
| Rohit Grover | 1:4769360130ed | 136 | } | 
| Rohit Grover | 1:4769360130ed | 137 | |
| Rohit Grover | 1:4769360130ed | 138 | void TestMemoryAllocator::freeMemoryLeakNode(char* memory) | 
| Rohit Grover | 1:4769360130ed | 139 | { | 
| Rohit Grover | 1:4769360130ed | 140 | free_memory(memory, "MemoryLeakNode", 1); | 
| Rohit Grover | 1:4769360130ed | 141 | } | 
| Rohit Grover | 1:4769360130ed | 142 | |
| Rohit Grover | 1:4769360130ed | 143 | char* TestMemoryAllocator::alloc_memory(size_t size, const char*, int) | 
| Rohit Grover | 1:4769360130ed | 144 | { | 
| Rohit Grover | 1:4769360130ed | 145 | return checkedMalloc(size); | 
| Rohit Grover | 1:4769360130ed | 146 | } | 
| Rohit Grover | 1:4769360130ed | 147 | |
| Rohit Grover | 1:4769360130ed | 148 | void TestMemoryAllocator::free_memory(char* memory, const char*, int) | 
| Rohit Grover | 1:4769360130ed | 149 | { | 
| Rohit Grover | 1:4769360130ed | 150 | PlatformSpecificFree(memory); | 
| Rohit Grover | 1:4769360130ed | 151 | } | 
| Rohit Grover | 1:4769360130ed | 152 | const char* TestMemoryAllocator::name() | 
| Rohit Grover | 1:4769360130ed | 153 | { | 
| Rohit Grover | 1:4769360130ed | 154 | return name_; | 
| Rohit Grover | 1:4769360130ed | 155 | } | 
| Rohit Grover | 1:4769360130ed | 156 | |
| Rohit Grover | 1:4769360130ed | 157 | const char* TestMemoryAllocator::alloc_name() | 
| Rohit Grover | 1:4769360130ed | 158 | { | 
| Rohit Grover | 1:4769360130ed | 159 | return alloc_name_; | 
| Rohit Grover | 1:4769360130ed | 160 | } | 
| Rohit Grover | 1:4769360130ed | 161 | |
| Rohit Grover | 1:4769360130ed | 162 | const char* TestMemoryAllocator::free_name() | 
| Rohit Grover | 1:4769360130ed | 163 | { | 
| Rohit Grover | 1:4769360130ed | 164 | return free_name_; | 
| Rohit Grover | 1:4769360130ed | 165 | } | 
| Rohit Grover | 1:4769360130ed | 166 | |
| Rohit Grover | 1:4769360130ed | 167 | CrashOnAllocationAllocator::CrashOnAllocationAllocator() : allocationToCrashOn_(0) | 
| Rohit Grover | 1:4769360130ed | 168 | { | 
| Rohit Grover | 1:4769360130ed | 169 | } | 
| Rohit Grover | 1:4769360130ed | 170 | |
| Rohit Grover | 1:4769360130ed | 171 | void CrashOnAllocationAllocator::setNumberToCrashOn(unsigned allocationToCrashOn) | 
| Rohit Grover | 1:4769360130ed | 172 | { | 
| Rohit Grover | 1:4769360130ed | 173 | allocationToCrashOn_ = allocationToCrashOn; | 
| Rohit Grover | 1:4769360130ed | 174 | } | 
| Rohit Grover | 1:4769360130ed | 175 | |
| Rohit Grover | 1:4769360130ed | 176 | char* CrashOnAllocationAllocator::alloc_memory(size_t size, const char* file, int line) | 
| Rohit Grover | 1:4769360130ed | 177 | { | 
| Rohit Grover | 1:4769360130ed | 178 | if (MemoryLeakWarningPlugin::getGlobalDetector()->getCurrentAllocationNumber() == allocationToCrashOn_) | 
| Rohit Grover | 1:4769360130ed | 179 | UT_CRASH(); | 
| Rohit Grover | 1:4769360130ed | 180 | |
| Rohit Grover | 1:4769360130ed | 181 | return TestMemoryAllocator::alloc_memory(size, file, line); | 
| Rohit Grover | 1:4769360130ed | 182 | } | 
| Rohit Grover | 1:4769360130ed | 183 | |
| Rohit Grover | 1:4769360130ed | 184 | |
| Rohit Grover | 1:4769360130ed | 185 | char* NullUnknownAllocator::alloc_memory(size_t /*size*/, const char*, int) | 
| Rohit Grover | 1:4769360130ed | 186 | { | 
| Rohit Grover | 1:4769360130ed | 187 | return 0; | 
| Rohit Grover | 1:4769360130ed | 188 | } | 
| Rohit Grover | 1:4769360130ed | 189 | |
| Rohit Grover | 1:4769360130ed | 190 | void NullUnknownAllocator::free_memory(char* /*memory*/, const char*, int) | 
| Rohit Grover | 1:4769360130ed | 191 | { | 
| Rohit Grover | 1:4769360130ed | 192 | } | 
| Rohit Grover | 1:4769360130ed | 193 | |
| Rohit Grover | 1:4769360130ed | 194 | NullUnknownAllocator::NullUnknownAllocator() | 
| Rohit Grover | 1:4769360130ed | 195 | : TestMemoryAllocator("Null Allocator", "unknown", "unknown") | 
| Rohit Grover | 1:4769360130ed | 196 | { | 
| Rohit Grover | 1:4769360130ed | 197 | } | 
| Rohit Grover | 1:4769360130ed | 198 | |
| Rohit Grover | 1:4769360130ed | 199 | |
| Rohit Grover | 1:4769360130ed | 200 | TestMemoryAllocator* NullUnknownAllocator::defaultAllocator() | 
| Rohit Grover | 1:4769360130ed | 201 | { | 
| Rohit Grover | 1:4769360130ed | 202 | static NullUnknownAllocator allocator; | 
| Rohit Grover | 1:4769360130ed | 203 | return &allocator; | 
| Rohit Grover | 1:4769360130ed | 204 | } | 
