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
27 changes: 27 additions & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ extern "C" {
#include <string.h>
#include <math.h>

#if defined(__cplusplus) \
&& !defined(__cpp_exceptions) \
&& !defined(_NEWHACK) \
&& !defined(CORE_MOCK)
// this is a hack working with gcc4.8
// Overload operator new so it can return nullptr instead of throwing an
// exception without (std::nothrow)`
// Requirements: (or the constructor is called even on nullptr)
// - "noexcept" is needed
// - "#include <new>" must not be done *before* these definitions
#define _NEWHACK
#if defined(_NEW) && !defined(_ARDUINO_HIDE_NEWHACK_WARNING)
#warning Arduino.h must be included before other c++ include files
#endif
#include <bits/c++config.h>
extern "C" void* _malloc4newabi (size_t size);
extern "C++" inline void* operator new (std::size_t size) noexcept
{
return _malloc4newabi(size);
}
extern "C++" inline void* operator new [] (std::size_t size) noexcept
{
return _malloc4newabi(size);
}
#include <new>
#endif // _NEWHACK

#include "stdlib_noniso.h"
#include "binary.h"
#include "esp8266_peri.h"
Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "Arduino.h"
#include "FS.h"
#include "FSImpl.h"

Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/FunctionalInterrupt.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <Arduino.h>
#include <FunctionalInterrupt.h>
#include <Schedule.h>
#include "Arduino.h"

// Duplicate typedefs from core_esp8266_wiring_digital_c
typedef void (*voidFuncPtr)(void);
Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/Schedule.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include <assert.h>

#include "Arduino.h"
#include "Schedule.h"
#include "PolledTimeout.h"
#include "interrupts.h"
Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/Schedule.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef ESP_SCHEDULE_H
#define ESP_SCHEDULE_H

#include <Arduino.h>
#include <functional>

#define SCHEDULED_FN_MAX_COUNT 32
Expand Down
14 changes: 14 additions & 0 deletions cores/esp8266/abi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,19 @@ extern "C" void __cxa_guard_abort(__guard* pg)
xt_wsr_ps(reinterpret_cast<guard_t*>(pg)->ps);
}

// Debugging helper, last allocation which returned NULL
extern void *umm_last_fail_alloc_addr;
extern int umm_last_fail_alloc_size;

void* _malloc4newabi (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;
}

// TODO: rebuild windows toolchain to make this unnecessary:
void* __dso_handle;
1 change: 1 addition & 0 deletions cores/esp8266/spiffs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Arduino.h"
#include "spiffs_api.h"

using namespace fs;
Expand Down
1 change: 1 addition & 0 deletions libraries/ArduinoOTA/ArduinoOTA.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef LWIP_OPEN_SRC
#define LWIP_OPEN_SRC
#endif
#include <Arduino.h>
#include <functional>
#include <WiFiUdp.h>
#include "ArduinoOTA.h"
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266LLMNR/ESP8266LLMNR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
*/

#include <debug.h>
#include <functional>
#include <ESP8266LLMNR.h>
#include <functional>
#include <WiFiUdp.h>

extern "C" {
Expand Down
3 changes: 2 additions & 1 deletion libraries/ESP8266WiFi/src/BearSSLHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>

#include <memory>
#include <vector>
#include <bearssl/bearssl.h>
#include <pgmspace.h>
#include <stdlib.h>
#include <string.h>
#include <Arduino.h>
#include <StackThunk.h>
#include "BearSSLHelpers.h"

Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef WiFi_h
#define WiFi_h

#include <Arduino.h>

#include <stdint.h>

extern "C" {
Expand Down
1 change: 1 addition & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

*/

#include <Arduino.h>
#include "ESP8266WiFi.h"
#include "ESP8266WiFiGeneric.h"
#include "ESP8266WiFiAP.h"
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

*/

#include <Arduino.h>

#include <list>
#include <string.h>
#include "ESP8266WiFi.h"
Expand Down
1 change: 1 addition & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA-WPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

*/

#include <Arduino.h>

#include "ESP8266WiFi.h"
#include "ESP8266WiFiGeneric.h"
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#define LWIP_INTERNAL

#include <Arduino.h>

extern "C"
{
#include "include/wl_definitions.h"
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

#ifndef wificlient_h
#define wificlient_h
#include <memory>
#include "Arduino.h"
#include <memory>
#include "Print.h"
#include "Client.h"
#include "IPAddress.h"
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClientSecureAxTLS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#define LWIP_INTERNAL

#include <Arduino.h>

#include "debug.h"
#include "ESP8266WiFi.h"
#include "WiFiClientSecure.h"
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#define LWIP_INTERNAL

#include <Arduino.h>

#include <list>
#include <errno.h>
#include <algorithm>
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ extern "C" {
#include "ets_sys.h"
}

#include <Arduino.h>

#include "debug.h"
#include "ESP8266WiFi.h"
#include "WiFiClient.h"
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiServerSecureBearSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ extern "C" {
#include "ets_sys.h"
}

#include <Arduino.h>

#include <StackThunk.h>
#include "debug.h"
#include "ESP8266WiFi.h"
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/

#define LWIP_INTERNAL

#include <Arduino.h>
#include <functional>

extern "C"
Expand Down
5 changes: 5 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiUdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
#ifndef WIFIUDP_H
#define WIFIUDP_H

#include <Arduino.h>

#include <functional>

#include <Udp.h>
#include <include/slist.h>


#define UDP_TX_PACKET_MAX_SIZE 8192

class UdpContext;
Expand Down
1 change: 0 additions & 1 deletion libraries/ESP8266mDNS/src/LEAmDNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
#ifndef MDNS_H
#define MDNS_H

#include <functional> // for UdpContext.h
#include "WiFiUdp.h"
#include "lwip/udp.h"
#include "debug.h"
Expand Down