Eric Fiselier | 38236b5 | 2016-01-19 21:52:04 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 10 | #ifndef COUNT_NEW_HPP |
| 11 | #define COUNT_NEW_HPP |
| 12 | |
| 13 | # include <cstdlib> |
| 14 | # include <cassert> |
| 15 | # include <new> |
| 16 | |
Eric Fiselier | f2f2a63 | 2016-06-14 21:31:42 +0000 | [diff] [blame] | 17 | #include "test_macros.h" |
Eric Fiselier | 5a4ee41 | 2015-01-27 23:03:38 +0000 | [diff] [blame] | 18 | |
Eric Fiselier | 3740071 | 2016-06-26 19:42:59 +0000 | [diff] [blame] | 19 | #if defined(TEST_HAS_SANITIZERS) |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 20 | #define DISABLE_NEW_COUNT |
| 21 | #endif |
| 22 | |
Eric Fiselier | 8ff7432 | 2016-06-22 03:46:32 +0000 | [diff] [blame] | 23 | namespace detail |
| 24 | { |
Eric Fiselier | 3740071 | 2016-06-26 19:42:59 +0000 | [diff] [blame] | 25 | TEST_NORETURN |
Eric Fiselier | 8ff7432 | 2016-06-22 03:46:32 +0000 | [diff] [blame] | 26 | inline void throw_bad_alloc_helper() { |
| 27 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 28 | throw std::bad_alloc(); |
| 29 | #else |
| 30 | std::abort(); |
| 31 | #endif |
| 32 | } |
| 33 | } |
| 34 | |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 35 | class MemCounter |
| 36 | { |
| 37 | public: |
| 38 | // Make MemCounter super hard to accidentally construct or copy. |
| 39 | class MemCounterCtorArg_ {}; |
Eric Fiselier | 85b788c | 2015-02-10 15:17:46 +0000 | [diff] [blame] | 40 | explicit MemCounter(MemCounterCtorArg_) { reset(); } |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 41 | |
| 42 | private: |
| 43 | MemCounter(MemCounter const &); |
| 44 | MemCounter & operator=(MemCounter const &); |
| 45 | |
| 46 | public: |
| 47 | // All checks return true when disable_checking is enabled. |
| 48 | static const bool disable_checking; |
| 49 | |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 50 | // Disallow any allocations from occurring. Useful for testing that |
| 51 | // code doesn't perform any allocations. |
| 52 | bool disable_allocations; |
| 53 | |
Eric Fiselier | 8ff7432 | 2016-06-22 03:46:32 +0000 | [diff] [blame] | 54 | // number of allocations to throw after. Default (unsigned)-1. If |
| 55 | // throw_after has the default value it will never be decremented. |
| 56 | static const unsigned never_throw_value = static_cast<unsigned>(-1); |
| 57 | unsigned throw_after; |
| 58 | |
Eric Fiselier | 85b788c | 2015-02-10 15:17:46 +0000 | [diff] [blame] | 59 | int outstanding_new; |
| 60 | int new_called; |
| 61 | int delete_called; |
Stephan T. Lavavej | f41847c | 2016-12-06 01:14:43 +0000 | [diff] [blame] | 62 | std::size_t last_new_size; |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 63 | |
Eric Fiselier | 85b788c | 2015-02-10 15:17:46 +0000 | [diff] [blame] | 64 | int outstanding_array_new; |
| 65 | int new_array_called; |
| 66 | int delete_array_called; |
Stephan T. Lavavej | f41847c | 2016-12-06 01:14:43 +0000 | [diff] [blame] | 67 | std::size_t last_new_array_size; |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 68 | |
| 69 | public: |
| 70 | void newCalled(std::size_t s) |
| 71 | { |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 72 | assert(disable_allocations == false); |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 73 | assert(s); |
Eric Fiselier | 8ff7432 | 2016-06-22 03:46:32 +0000 | [diff] [blame] | 74 | if (throw_after == 0) { |
| 75 | throw_after = never_throw_value; |
| 76 | detail::throw_bad_alloc_helper(); |
| 77 | } else if (throw_after != never_throw_value) { |
| 78 | --throw_after; |
| 79 | } |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 80 | ++new_called; |
| 81 | ++outstanding_new; |
| 82 | last_new_size = s; |
| 83 | } |
| 84 | |
| 85 | void deleteCalled(void * p) |
| 86 | { |
| 87 | assert(p); |
| 88 | --outstanding_new; |
| 89 | ++delete_called; |
| 90 | } |
| 91 | |
| 92 | void newArrayCalled(std::size_t s) |
| 93 | { |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 94 | assert(disable_allocations == false); |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 95 | assert(s); |
Eric Fiselier | 8ff7432 | 2016-06-22 03:46:32 +0000 | [diff] [blame] | 96 | if (throw_after == 0) { |
| 97 | throw_after = never_throw_value; |
| 98 | detail::throw_bad_alloc_helper(); |
| 99 | } else { |
| 100 | // don't decrement throw_after here. newCalled will end up doing that. |
| 101 | } |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 102 | ++outstanding_array_new; |
| 103 | ++new_array_called; |
| 104 | last_new_array_size = s; |
| 105 | } |
| 106 | |
| 107 | void deleteArrayCalled(void * p) |
| 108 | { |
| 109 | assert(p); |
| 110 | --outstanding_array_new; |
| 111 | ++delete_array_called; |
| 112 | } |
| 113 | |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 114 | void disableAllocations() |
| 115 | { |
| 116 | disable_allocations = true; |
| 117 | } |
| 118 | |
| 119 | void enableAllocations() |
| 120 | { |
| 121 | disable_allocations = false; |
| 122 | } |
| 123 | |
Eric Fiselier | 8ff7432 | 2016-06-22 03:46:32 +0000 | [diff] [blame] | 124 | |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 125 | void reset() |
| 126 | { |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 127 | disable_allocations = false; |
Eric Fiselier | 8ff7432 | 2016-06-22 03:46:32 +0000 | [diff] [blame] | 128 | throw_after = never_throw_value; |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 129 | |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 130 | outstanding_new = 0; |
| 131 | new_called = 0; |
| 132 | delete_called = 0; |
| 133 | last_new_size = 0; |
| 134 | |
| 135 | outstanding_array_new = 0; |
| 136 | new_array_called = 0; |
| 137 | delete_array_called = 0; |
| 138 | last_new_array_size = 0; |
| 139 | } |
| 140 | |
| 141 | public: |
| 142 | bool checkOutstandingNewEq(int n) const |
| 143 | { |
| 144 | return disable_checking || n == outstanding_new; |
| 145 | } |
| 146 | |
| 147 | bool checkOutstandingNewNotEq(int n) const |
| 148 | { |
| 149 | return disable_checking || n != outstanding_new; |
| 150 | } |
| 151 | |
| 152 | bool checkNewCalledEq(int n) const |
| 153 | { |
| 154 | return disable_checking || n == new_called; |
| 155 | } |
| 156 | |
| 157 | bool checkNewCalledNotEq(int n) const |
| 158 | { |
| 159 | return disable_checking || n != new_called; |
| 160 | } |
| 161 | |
Eric Fiselier | c797958 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 162 | bool checkNewCalledGreaterThan(int n) const |
| 163 | { |
| 164 | return disable_checking || new_called > n; |
| 165 | } |
| 166 | |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 167 | bool checkDeleteCalledEq(int n) const |
| 168 | { |
| 169 | return disable_checking || n == delete_called; |
| 170 | } |
| 171 | |
| 172 | bool checkDeleteCalledNotEq(int n) const |
| 173 | { |
| 174 | return disable_checking || n != delete_called; |
| 175 | } |
| 176 | |
Stephan T. Lavavej | f41847c | 2016-12-06 01:14:43 +0000 | [diff] [blame] | 177 | bool checkLastNewSizeEq(std::size_t n) const |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 178 | { |
| 179 | return disable_checking || n == last_new_size; |
| 180 | } |
| 181 | |
Stephan T. Lavavej | f41847c | 2016-12-06 01:14:43 +0000 | [diff] [blame] | 182 | bool checkLastNewSizeNotEq(std::size_t n) const |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 183 | { |
| 184 | return disable_checking || n != last_new_size; |
| 185 | } |
| 186 | |
| 187 | bool checkOutstandingArrayNewEq(int n) const |
| 188 | { |
| 189 | return disable_checking || n == outstanding_array_new; |
| 190 | } |
| 191 | |
| 192 | bool checkOutstandingArrayNewNotEq(int n) const |
| 193 | { |
| 194 | return disable_checking || n != outstanding_array_new; |
| 195 | } |
| 196 | |
| 197 | bool checkNewArrayCalledEq(int n) const |
| 198 | { |
| 199 | return disable_checking || n == new_array_called; |
| 200 | } |
| 201 | |
| 202 | bool checkNewArrayCalledNotEq(int n) const |
| 203 | { |
| 204 | return disable_checking || n != new_array_called; |
| 205 | } |
| 206 | |
| 207 | bool checkDeleteArrayCalledEq(int n) const |
| 208 | { |
| 209 | return disable_checking || n == delete_array_called; |
| 210 | } |
| 211 | |
| 212 | bool checkDeleteArrayCalledNotEq(int n) const |
| 213 | { |
| 214 | return disable_checking || n != delete_array_called; |
| 215 | } |
| 216 | |
Stephan T. Lavavej | f41847c | 2016-12-06 01:14:43 +0000 | [diff] [blame] | 217 | bool checkLastNewArraySizeEq(std::size_t n) const |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 218 | { |
| 219 | return disable_checking || n == last_new_array_size; |
| 220 | } |
| 221 | |
Stephan T. Lavavej | f41847c | 2016-12-06 01:14:43 +0000 | [diff] [blame] | 222 | bool checkLastNewArraySizeNotEq(std::size_t n) const |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 223 | { |
| 224 | return disable_checking || n != last_new_array_size; |
| 225 | } |
| 226 | }; |
| 227 | |
| 228 | #ifdef DISABLE_NEW_COUNT |
| 229 | const bool MemCounter::disable_checking = true; |
| 230 | #else |
| 231 | const bool MemCounter::disable_checking = false; |
| 232 | #endif |
| 233 | |
Eric Fiselier | 0509238 | 2017-06-21 21:42:50 +0000 | [diff] [blame] | 234 | inline MemCounter* getGlobalMemCounter() { |
| 235 | static MemCounter counter((MemCounter::MemCounterCtorArg_())); |
| 236 | return &counter; |
| 237 | } |
| 238 | |
| 239 | MemCounter &globalMemCounter = *getGlobalMemCounter(); |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 240 | |
| 241 | #ifndef DISABLE_NEW_COUNT |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 242 | void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 243 | { |
Eric Fiselier | 0509238 | 2017-06-21 21:42:50 +0000 | [diff] [blame] | 244 | getGlobalMemCounter()->newCalled(s); |
Eric Fiselier | 8ff7432 | 2016-06-22 03:46:32 +0000 | [diff] [blame] | 245 | void* ret = std::malloc(s); |
| 246 | if (ret == nullptr) |
| 247 | detail::throw_bad_alloc_helper(); |
| 248 | return ret; |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 251 | void operator delete(void* p) TEST_NOEXCEPT |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 252 | { |
Eric Fiselier | 0509238 | 2017-06-21 21:42:50 +0000 | [diff] [blame] | 253 | getGlobalMemCounter()->deleteCalled(p); |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 254 | std::free(p); |
| 255 | } |
| 256 | |
| 257 | |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 258 | void* operator new[](std::size_t s) TEST_THROW_SPEC(std::bad_alloc) |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 259 | { |
Eric Fiselier | 0509238 | 2017-06-21 21:42:50 +0000 | [diff] [blame] | 260 | getGlobalMemCounter()->newArrayCalled(s); |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 261 | return operator new(s); |
| 262 | } |
| 263 | |
| 264 | |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 265 | void operator delete[](void* p) TEST_NOEXCEPT |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 266 | { |
Eric Fiselier | 0509238 | 2017-06-21 21:42:50 +0000 | [diff] [blame] | 267 | getGlobalMemCounter()->deleteArrayCalled(p); |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 268 | operator delete(p); |
| 269 | } |
| 270 | |
| 271 | #endif // DISABLE_NEW_COUNT |
| 272 | |
Eric Fiselier | 3461dbc | 2015-07-31 02:24:58 +0000 | [diff] [blame] | 273 | |
| 274 | struct DisableAllocationGuard { |
| 275 | explicit DisableAllocationGuard(bool disable = true) : m_disabled(disable) |
| 276 | { |
| 277 | // Don't re-disable if already disabled. |
| 278 | if (globalMemCounter.disable_allocations == true) m_disabled = false; |
| 279 | if (m_disabled) globalMemCounter.disableAllocations(); |
| 280 | } |
| 281 | |
| 282 | void release() { |
| 283 | if (m_disabled) globalMemCounter.enableAllocations(); |
| 284 | m_disabled = false; |
| 285 | } |
| 286 | |
| 287 | ~DisableAllocationGuard() { |
| 288 | release(); |
| 289 | } |
| 290 | |
| 291 | private: |
| 292 | bool m_disabled; |
| 293 | |
| 294 | DisableAllocationGuard(DisableAllocationGuard const&); |
| 295 | DisableAllocationGuard& operator=(DisableAllocationGuard const&); |
| 296 | }; |
| 297 | |
Eric Fiselier | c797958 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 298 | |
| 299 | struct RequireAllocationGuard { |
| 300 | explicit RequireAllocationGuard(std::size_t RequireAtLeast = 1) |
| 301 | : m_req_alloc(RequireAtLeast), |
| 302 | m_new_count_on_init(globalMemCounter.new_called), |
| 303 | m_outstanding_new_on_init(globalMemCounter.outstanding_new), |
| 304 | m_exactly(false) |
| 305 | { |
| 306 | } |
| 307 | |
| 308 | void requireAtLeast(std::size_t N) { m_req_alloc = N; m_exactly = false; } |
| 309 | void requireExactly(std::size_t N) { m_req_alloc = N; m_exactly = true; } |
| 310 | |
| 311 | ~RequireAllocationGuard() { |
Stephan T. Lavavej | f41847c | 2016-12-06 01:14:43 +0000 | [diff] [blame] | 312 | assert(globalMemCounter.checkOutstandingNewEq(static_cast<int>(m_outstanding_new_on_init))); |
Eric Fiselier | c797958 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 313 | std::size_t Expect = m_new_count_on_init + m_req_alloc; |
Stephan T. Lavavej | f41847c | 2016-12-06 01:14:43 +0000 | [diff] [blame] | 314 | assert(globalMemCounter.checkNewCalledEq(static_cast<int>(Expect)) || |
| 315 | (!m_exactly && globalMemCounter.checkNewCalledGreaterThan(static_cast<int>(Expect)))); |
Eric Fiselier | c797958 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | private: |
| 319 | std::size_t m_req_alloc; |
| 320 | const std::size_t m_new_count_on_init; |
| 321 | const std::size_t m_outstanding_new_on_init; |
| 322 | bool m_exactly; |
| 323 | RequireAllocationGuard(RequireAllocationGuard const&); |
| 324 | RequireAllocationGuard& operator=(RequireAllocationGuard const&); |
| 325 | }; |
| 326 | |
Eric Fiselier | 2cbc654 | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 327 | #endif /* COUNT_NEW_HPP */ |