|
16 | 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
17 | 17 | */
|
18 | 18 |
|
19 |
| -#include <stdlib.h> |
| 19 | +#include "new.h" |
20 | 20 |
|
21 |
| -void *operator new(size_t size) { |
22 |
| - return malloc(size); |
| 21 | +// The C++ spec dicates 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; |
23 | 37 | }
|
24 | 38 |
|
25 |
| -void *operator new[](size_t size) { |
| 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; |
26 | 44 | return malloc(size);
|
27 | 45 | }
|
28 | 46 |
|
29 |
| -void * operator new(size_t size, void * ptr) noexcept { |
30 |
| - (void)size; |
31 |
| - return ptr; |
| 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); |
32 | 57 | }
|
33 | 58 |
|
34 |
| -void operator delete(void * ptr) { |
35 |
| - free(ptr); |
| 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); |
36 | 85 | }
|
37 | 86 |
|
38 |
| -void operator delete[](void * ptr) { |
| 87 | +void operator delete(void * ptr) noexcept { |
39 | 88 | free(ptr);
|
40 | 89 | }
|
| 90 | +void operator delete[](void * ptr) noexcept { |
| 91 | + operator delete(ptr); |
| 92 | +} |
41 | 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 | +} |
0 commit comments