|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Arduino SA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "new.h" |
| 8 | + |
| 9 | +// The C++ spec dictates that allocation failure should cause the |
| 10 | +// (non-nothrow version of the) operator new to throw an exception. |
| 11 | +// Since we expect to have exceptions disabled, it would be more |
| 12 | +// appropriate (and probably standards-compliant) to terminate instead. |
| 13 | +// Historically failure causes null to be returned, but this define |
| 14 | +// allows switching to more robust terminating behaviour (that might |
| 15 | +// become the default at some point in the future). Note that any code |
| 16 | +// that wants null to be returned can (and should) use the nothrow |
| 17 | +// versions of the new statement anyway and is unaffected by this. |
| 18 | +// #define NEW_TERMINATES_ON_FAILURE |
| 19 | + |
| 20 | +namespace std { |
| 21 | + // Defined in abi.cpp |
| 22 | + void terminate(); |
| 23 | + |
| 24 | + const nothrow_t nothrow; |
| 25 | +} |
| 26 | + |
| 27 | +static void * new_helper(std::size_t size) { |
| 28 | + // Even zero-sized allocations should return a unique pointer, but |
| 29 | + // malloc does not guarantee this |
| 30 | + if (size == 0) |
| 31 | + size = 1; |
| 32 | + return malloc(size); |
| 33 | +} |
| 34 | + |
| 35 | +void * operator new(std::size_t size) { |
| 36 | + void *res = new_helper(size); |
| 37 | +#if defined(NEW_TERMINATES_ON_FAILURE) |
| 38 | + if (!res) |
| 39 | + std::terminate(); |
| 40 | +#endif |
| 41 | + return res; |
| 42 | +} |
| 43 | +void * operator new[](std::size_t size) { |
| 44 | + return operator new(size); |
| 45 | +} |
| 46 | + |
| 47 | +void * operator new(std::size_t size, const std::nothrow_t tag) noexcept { |
| 48 | +#if defined(NEW_TERMINATES_ON_FAILURE) |
| 49 | + // Cannot call throwing operator new as standard suggests, so call |
| 50 | + // new_helper directly then |
| 51 | + return new_helper(size); |
| 52 | +#else |
| 53 | + return operator new(size); |
| 54 | +#endif |
| 55 | +} |
| 56 | +void * operator new[](std::size_t size, const std::nothrow_t& tag) noexcept { |
| 57 | +#if defined(NEW_TERMINATES_ON_FAILURE) |
| 58 | + // Cannot call throwing operator new[] as standard suggests, so call |
| 59 | + // malloc directly then |
| 60 | + return new_helper(size); |
| 61 | +#else |
| 62 | + return operator new[](size); |
| 63 | +#endif |
| 64 | +} |
| 65 | + |
| 66 | +void * operator new(std::size_t size, void *place) noexcept { |
| 67 | + // Nothing to do |
| 68 | + (void)size; // unused |
| 69 | + return place; |
| 70 | +} |
| 71 | +void * operator new[](std::size_t size, void *place) noexcept { |
| 72 | + return operator new(size, place); |
| 73 | +} |
| 74 | + |
| 75 | +void operator delete(void * ptr) noexcept { |
| 76 | + free(ptr); |
| 77 | +} |
| 78 | +void operator delete[](void * ptr) noexcept { |
| 79 | + operator delete(ptr); |
| 80 | +} |
| 81 | + |
| 82 | +#if __cplusplus >= 201402L |
| 83 | +void operator delete(void* ptr, std::size_t size) noexcept { |
| 84 | + operator delete(ptr); |
| 85 | +} |
| 86 | +void operator delete[](void * ptr, std::size_t size) noexcept { |
| 87 | + operator delete[](ptr); |
| 88 | +} |
| 89 | +#endif // __cplusplus >= 201402L |
| 90 | + |
| 91 | +void operator delete(void* ptr, const std::nothrow_t& tag) noexcept { |
| 92 | + operator delete(ptr); |
| 93 | +} |
| 94 | +void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept { |
| 95 | + operator delete[](ptr); |
| 96 | +} |
| 97 | + |
| 98 | +void operator delete(void* ptr, void* place) noexcept { |
| 99 | + (void)ptr; (void)place; // unused |
| 100 | + // Nothing to do |
| 101 | +} |
| 102 | +void operator delete[](void* ptr, void* place) noexcept { |
| 103 | + (void)ptr; (void)place; // unused |
| 104 | + // Nothing to do |
| 105 | +} |
0 commit comments