|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Christopher Durand |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#include <new> |
| 13 | +#include <bits/functexcept.h> |
| 14 | +#include <cstdlib> |
| 15 | +#include <cstddef> |
| 16 | + |
| 17 | +template<bool abortOnFail> |
| 18 | +static inline void* |
| 19 | +allocate(size_t size) _GLIBCXX_USE_NOEXCEPT |
| 20 | +{ |
| 21 | + void* ptr = malloc(size); |
| 22 | + |
| 23 | + if constexpr(abortOnFail) { |
| 24 | + if(!ptr) { |
| 25 | + std::__throw_bad_alloc(); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return ptr; |
| 30 | +} |
| 31 | + |
| 32 | +void * |
| 33 | +operator new(size_t size) _GLIBCXX_USE_NOEXCEPT |
| 34 | +{ |
| 35 | + return allocate<true>(size); |
| 36 | +} |
| 37 | + |
| 38 | +void * |
| 39 | +operator new[](size_t size) _GLIBCXX_USE_NOEXCEPT |
| 40 | +{ |
| 41 | + return allocate<true>(size); |
| 42 | +} |
| 43 | + |
| 44 | +void* |
| 45 | +operator new(std::size_t size, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT |
| 46 | +{ |
| 47 | + return allocate<false>(size); |
| 48 | +} |
| 49 | + |
| 50 | +void* |
| 51 | +operator new[](std::size_t size, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT |
| 52 | +{ |
| 53 | + return allocate<false>(size); |
| 54 | +} |
| 55 | + |
| 56 | +void |
| 57 | +operator delete(void* ptr) _GLIBCXX_USE_NOEXCEPT |
| 58 | +{ |
| 59 | + free(ptr); |
| 60 | +} |
| 61 | + |
| 62 | +void |
| 63 | +operator delete(void* ptr, size_t) _GLIBCXX_USE_NOEXCEPT |
| 64 | +{ |
| 65 | + free(ptr); |
| 66 | +} |
| 67 | + |
| 68 | +void |
| 69 | +operator delete[](void* ptr) _GLIBCXX_USE_NOEXCEPT |
| 70 | +{ |
| 71 | + free(ptr); |
| 72 | +} |
| 73 | + |
| 74 | +void |
| 75 | +operator delete[](void* ptr, size_t) _GLIBCXX_USE_NOEXCEPT |
| 76 | +{ |
| 77 | + free(ptr); |
| 78 | +} |
| 79 | + |
| 80 | +void |
| 81 | +operator delete(void* ptr, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT |
| 82 | +{ |
| 83 | + free(ptr); |
| 84 | +} |
| 85 | + |
| 86 | +void |
| 87 | +operator delete[](void* ptr, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT |
| 88 | +{ |
| 89 | + free(ptr); |
| 90 | +} |
0 commit comments