Skip to content
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