| 1 |
/* |
| 2 |
Copyright (c) 2014 Arduino. All right reserved. |
| 3 |
|
| 4 |
This library is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU Lesser General Public |
| 6 |
License as published by the Free Software Foundation; either |
| 7 |
version 2.1 of the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This library is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
See the GNU Lesser General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU Lesser General Public |
| 15 |
License along with this library; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "new.h" |
| 20 |
|
| 21 |
// The C++ spec dictates that allocation failure should cause the |
| 22 |
// (non-nothrow version of the) operator new to throw an exception. |
| 23 |
// Since we expect to have exceptions disabled, it would be more |
| 24 |
// appropriate (and probably standards-compliant) to terminate instead. |
| 25 |
// Historically failure causes null to be returned, but this define |
| 26 |
// allows switching to more robust terminating behaviour (that might |
| 27 |
// become the default at some point in the future). Note that any code |
| 28 |
// that wants null to be returned can (and should) use the nothrow |
| 29 |
// versions of the new statement anyway and is unaffected by this. |
| 30 |
// #define NEW_TERMINATES_ON_FAILURE |
| 31 |
|
| 32 |
namespace std { |
| 33 |
// Defined in abi.cpp |
| 34 |
void terminate(); |
| 35 |
|
| 36 |
const nothrow_t nothrow; |
| 37 |
} |
| 38 |
|
| 39 |
static void * new_helper(std::size_t size) { |
| 40 |
// Even zero-sized allocations should return a unique pointer, but |
| 41 |
// malloc does not guarantee this |
| 42 |
if (size == 0) |
| 43 |
size = 1; |
| 44 |
return malloc(size); |
| 45 |
} |
| 46 |
|
| 47 |
void * operator new(std::size_t size) { |
| 48 |
void *res = new_helper(size); |
| 49 |
#if defined(NEW_TERMINATES_ON_FAILURE) |
| 50 |
if (!res) |
| 51 |
std::terminate(); |
| 52 |
#endif |
| 53 |
return res; |
| 54 |
} |
| 55 |
void * operator new[](std::size_t size) { |
| 56 |
return operator new(size); |
| 57 |
} |
| 58 |
|
| 59 |
void * operator new(std::size_t size, const std::nothrow_t tag) noexcept { |
| 60 |
#if defined(NEW_TERMINATES_ON_FAILURE) |
| 61 |
// Cannot call throwing operator new as standard suggests, so call |
| 62 |
// new_helper directly then |
| 63 |
return new_helper(size); |
| 64 |
#else |
| 65 |
return operator new(size); |
| 66 |
#endif |
| 67 |
} |
| 68 |
void * operator new[](std::size_t size, const std::nothrow_t& tag) noexcept { |
| 69 |
#if defined(NEW_TERMINATES_ON_FAILURE) |
| 70 |
// Cannot call throwing operator new[] as standard suggests, so call |
| 71 |
// malloc directly then |
| 72 |
return new_helper(size); |
| 73 |
#else |
| 74 |
return operator new[](size); |
| 75 |
#endif |
| 76 |
} |
| 77 |
|
| 78 |
void * operator new(std::size_t size, void *place) noexcept { |
| 79 |
// Nothing to do |
| 80 |
(void)size; // unused |
| 81 |
return place; |
| 82 |
} |
| 83 |
void * operator new[](std::size_t size, void *place) noexcept { |
| 84 |
return operator new(size, place); |
| 85 |
} |
| 86 |
|
| 87 |
void operator delete(void * ptr) noexcept { |
| 88 |
free(ptr); |
| 89 |
} |
| 90 |
void operator delete[](void * ptr) noexcept { |
| 91 |
operator delete(ptr); |
| 92 |
} |
| 93 |
|
| 94 |
#if __cplusplus >= 201402L |
| 95 |
void operator delete(void* ptr, std::size_t size) noexcept { |
| 96 |
operator delete(ptr); |
| 97 |
} |
| 98 |
void operator delete[](void * ptr, std::size_t size) noexcept { |
| 99 |
operator delete[](ptr); |
| 100 |
} |
| 101 |
#endif // __cplusplus >= 201402L |
| 102 |
|
| 103 |
void operator delete(void* ptr, const std::nothrow_t& tag) noexcept { |
| 104 |
operator delete(ptr); |
| 105 |
} |
| 106 |
void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept { |
| 107 |
operator delete[](ptr); |
| 108 |
} |
| 109 |
|
| 110 |
void operator delete(void* ptr, void* place) noexcept { |
| 111 |
(void)ptr; (void)place; // unused |
| 112 |
// Nothing to do |
| 113 |
} |
| 114 |
void operator delete[](void* ptr, void* place) noexcept { |
| 115 |
(void)ptr; (void)place; // unused |
| 116 |
// Nothing to do |
| 117 |
} |