Skip to content

operator new: no exception without need for std::nothrow #6309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
19 changes: 19 additions & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ extern "C" {
#include <string.h>
#include <math.h>

#if defined(__cplusplus) && !defined(__cpp_exceptions)
// this is a hack working with gcc4.8
// Overload operator new so it can return nullptr instead of throwing an
// exception without (std::nothrow)`
// Requirement (or the constructor is called even on nullptr)
// - "noexcept" is needed
// - "#include <new>" must not be done *before* these definitions
#include <bits/c++config.h>
extern "C++" inline void* operator new (std::size_t size) noexcept
{
return malloc(size);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check out the old new() and new override, it sets the last-failed-alloc address which this needs to do as well:

void *operator new(size_t size)
{
void *ret = malloc(size);
if (0 != size && 0 == ret) {
umm_last_fail_alloc_addr = __builtin_return_address(0);
umm_last_fail_alloc_size = size;
}
return ret;
}
void *operator new[](size_t size)
{
void *ret = malloc(size);
if (0 != size && 0 == ret) {
umm_last_fail_alloc_addr = __builtin_return_address(0);
umm_last_fail_alloc_size = size;
}
return ret;
}

}
extern "C++" inline void* operator new [] (std::size_t size) noexcept
{
return malloc(size);
}
#include <new>
#endif // new nothrow nullptr hack

#include "stdlib_noniso.h"
#include "binary.h"
#include "esp8266_peri.h"
Expand Down