Skip to content

Commit

Permalink
replace new by new (std::nothrow), remove arduino_new
Browse files Browse the repository at this point in the history
  • Loading branch information
d-a-v committed Aug 17, 2020
1 parent 5b3d290 commit 6925982
Show file tree
Hide file tree
Showing 45 changed files with 207 additions and 167 deletions.
6 changes: 3 additions & 3 deletions cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,17 +740,17 @@ String EspClass::getSketchMD5()
}
uint32_t lengthLeft = getSketchSize();
const size_t bufSize = 512;
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
std::unique_ptr<uint8_t[]> buf(new (std::nothrow) uint8_t[bufSize]);
uint32_t offset = 0;
if(!buf.get()) {
return String();
return emptyString;
}
MD5Builder md5;
md5.begin();
while( lengthLeft > 0) {
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
if (!flashRead(offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
return String();
return emptyString;
}
md5.add(buf.get(), readBytes);
lengthLeft -= readBytes;
Expand Down
38 changes: 29 additions & 9 deletions cores/esp8266/FunctionalInterrupt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ typedef void (*voidFuncPtr)(void);
typedef void (*voidFuncPtrArg)(void*);

// Helper functions for Functional interrupt routines
extern "C" void __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtr userFunc, void*fp, int mode, bool functional);
extern "C" bool __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtr userFunc, void*fp, int mode, bool functional);


void ICACHE_RAM_ATTR interruptFunctional(void* arg)
Expand All @@ -34,32 +34,52 @@ extern "C"
}
}

void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode)
bool attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode)
{
// use the local interrupt routine which takes the ArgStructure as argument

InterruptInfo* ii = nullptr;

FunctionInfo* fi = new FunctionInfo;
FunctionInfo* fi = new (std::nothrow) FunctionInfo;
if (fi == nullptr)
return false;
fi->reqFunction = intRoutine;

ArgStructure* as = new ArgStructure;
ArgStructure* as = new (std::nothrow) ArgStructure;
if (as == nullptr)
{
delete(fi);
return false;
}
as->interruptInfo = ii;
as->functionInfo = fi;

__attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
return __attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
}

void attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> scheduledIntRoutine, int mode)
bool attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> scheduledIntRoutine, int mode)
{
InterruptInfo* ii = new InterruptInfo;
InterruptInfo* ii = new (std::nothrow) InterruptInfo;
if (ii == nullptr)
return false;

FunctionInfo* fi = new FunctionInfo;
if (fi == nullptr)
{
delete ii;
return false;
}
fi->reqScheduledFunction = scheduledIntRoutine;

ArgStructure* as = new ArgStructure;
ArgStructure* as = new (std::nothrow) ArgStructure;
if (as == nullptr)
{
delete ii;
delete fi;
return false;
}
as->interruptInfo = ii;
as->functionInfo = fi;

__attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
return __attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
}
4 changes: 2 additions & 2 deletions cores/esp8266/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ size_t Print::printf(const char *format, ...) {
size_t len = vsnprintf(temp, sizeof(temp), format, arg);
va_end(arg);
if (len > sizeof(temp) - 1) {
buffer = new char[len + 1];
buffer = new (std::nothrow) char[len + 1];
if (!buffer) {
return 0;
}
Expand All @@ -86,7 +86,7 @@ size_t Print::printf_P(PGM_P format, ...) {
size_t len = vsnprintf_P(temp, sizeof(temp), format, arg);
va_end(arg);
if (len > sizeof(temp) - 1) {
buffer = new char[len + 1];
buffer = new (std::nothrow) char[len + 1];
if (!buffer) {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
} else {
_bufferSize = 256;
}
_buffer = new uint8_t[_bufferSize];
_buffer = new (std::nothrow) uint8_t[_bufferSize];
_command = command;

#ifdef DEBUG_UPDATER
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/cbuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ size_t cbuf::resize(size_t newSize) {
return _size;
}

char *newbuf = new char[newSize];
char *newbuf = new (std::nothrow) char[newSize];
char *oldbuf = _buf;

if(!newbuf) {
Expand Down
29 changes: 0 additions & 29 deletions cores/esp8266/core_esp8266_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,6 @@
#include <stddef.h> // size_t
#include <stdint.h>

#ifdef __cplusplus

namespace arduino
{
extern "C++"
template <typename T, typename ...TConstructorArgs>
T* new0 (size_t n, TConstructorArgs... TconstructorArgs)
{
// n==0: single allocation, otherwise it is an array
size_t offset = n? sizeof(size_t): 0;
size_t arraysize = n? n: 1;
T* ptr = (T*)malloc(offset + (arraysize * sizeof(T)));
if (ptr)
{
if (n)
*(size_t*)(ptr) = n;
for (size_t i = 0; i < arraysize; i++)
new (ptr + offset + i * sizeof(T)) T(TconstructorArgs...);
return ptr + offset;
}
return nullptr;
}
}

#define arduino_new(Type, ...) arduino::new0<Type>(0, ##__VA_ARGS__)
#define arduino_newarray(Type, n, ...) arduino::new0<Type>(n, ##__VA_ARGS__)

#endif // __cplusplus

#ifndef __STRINGIFY
#define __STRINGIFY(a) #a
#endif
Expand Down
33 changes: 0 additions & 33 deletions doc/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -323,36 +323,3 @@ C++
This assures correct behavior, including handling of all subobjects, which guarantees stability.

History: `#6269 <https://github.com/esp8266/Arduino/issues/6269>`__ `#6309 <https://github.com/esp8266/Arduino/pull/6309>`__ `#6312 <https://github.com/esp8266/Arduino/pull/6312>`__

- New optional allocator ``arduino_new``

A new optional global allocator is introduced with a different semantic:

- never throws exceptions on oom

- never calls constructors on oom

- returns nullptr on oom

It is similar to arduino ``new`` semantic without side effects
(except when parent constructors, or member constructors use ``new``).

Syntax is slightly different, the following shows the different usages:

.. code:: cpp
// with new:
SomeClass* sc = new SomeClass(arg1, arg2, ...);
delete sc;
SomeClass* scs = new SomeClass[42];
delete [] scs;
// with arduino_new:
SomeClass* sc = arduino_new(SomeClass, arg1, arg2, ...);
delete sc;
SomeClass* scs = arduino_newarray(SomeClass, 42);
delete [] scs;
9 changes: 6 additions & 3 deletions libraries/ArduinoOTA/ArduinoOTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void ArduinoOTAClass::setRebootOnSuccess(bool reboot){
_rebootOnSuccess = reboot;
}

void ArduinoOTAClass::begin(bool useMDNS) {
bool ArduinoOTAClass::begin(bool useMDNS) {
if (_initialized)
return;

Expand All @@ -126,11 +126,13 @@ void ArduinoOTAClass::begin(bool useMDNS) {
_udp_ota = 0;
}

_udp_ota = new UdpContext;
_udp_ota = new (std::nothrow) UdpContext;
if (_udp_ota == nullptr)
return false;
_udp_ota->ref();

if(!_udp_ota->listen(IP_ADDR_ANY, _port))
return;
return false;
_udp_ota->onRx(std::bind(&ArduinoOTAClass::_onRx, this));

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS)
Expand All @@ -149,6 +151,7 @@ void ArduinoOTAClass::begin(bool useMDNS) {
#ifdef OTA_DEBUG
OTA_DEBUG.printf("OTA server at: %s.local:%u\n", _hostname.c_str(), _port);
#endif
return true;
}

int ArduinoOTAClass::parseInt(){
Expand Down
3 changes: 1 addition & 2 deletions libraries/DNSServer/src/DNSServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ void DNSServer::processNextRequest()
return;

std::unique_ptr<uint8_t[]> buffer(new (std::nothrow) uint8_t[currentPacketSize]);

if (buffer == NULL)
if (buffer == nullptr)
return;

_udp.read(buffer.get(), currentPacketSize);
Expand Down
7 changes: 5 additions & 2 deletions libraries/EEPROM/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ void EEPROMClass::begin(size_t size) {
//In case begin() is called a 2nd+ time, don't reallocate if size is the same
if(_data && size != _size) {
delete[] _data;
_data = new uint8_t[size];
_data = new (std::nothrow) uint8_t[size];
} else if(!_data) {
_data = new uint8_t[size];
_data = new (std::nothrow) uint8_t[size];
}
if (_data == nullptr) {
return;
}

_size = size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {

std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
std::unique_ptr<BearSSL::WiFiClientSecure>client(new (std::nothrow) BearSSL::WiFiClientSecure);
if (client == nullptr) {
return;
}

client->setFingerprint(fingerprint);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {

std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
std::unique_ptr<BearSSL::WiFiClientSecure> client(new (std::nothrow) BearSSL::WiFiClientSecure);
if (client == nullptr) {
return;
}

bool mfln = client->probeMaxFragmentLength("tls.mbed.org", 443, 1024);
Serial.printf("\nConnecting to https://tls.mbed.org\n");
Expand Down
21 changes: 11 additions & 10 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TransportTraits

virtual std::unique_ptr<WiFiClient> create()
{
return std::unique_ptr<WiFiClient>(new WiFiClient());
return std::unique_ptr<WiFiClient>(new (std::nothrow) WiFiClient());
}

virtual bool verify(WiFiClient& client, const char* host)
Expand All @@ -59,8 +59,9 @@ class BearSSLTraits : public TransportTraits

std::unique_ptr<WiFiClient> create() override
{
BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure();
client->setFingerprint(_fingerprint);
BearSSL::WiFiClientSecure *client = new (std::nothrow) BearSSL::WiFiClientSecure();
if (client != nullptr)
client->setFingerprint(_fingerprint);
return std::unique_ptr<WiFiClient>(client);
}

Expand Down Expand Up @@ -182,7 +183,7 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
if (!beginInternal(url, "https")) {
return false;
}
_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
_transportTraits = TransportTraitsPtr(new (std::nothrow) BearSSLTraits(httpsFingerprint));
if(!_transportTraits) {
DEBUG_HTTPCLIENT("[HTTP-Client][begin] could not create transport traits\n");
return false;
Expand Down Expand Up @@ -212,8 +213,8 @@ bool HTTPClient::begin(String url)
if (!beginInternal(url, "http")) {
return false;
}
_transportTraits = TransportTraitsPtr(new TransportTraits());
return true;
_transportTraits = TransportTraitsPtr(new (std::nothrow) TransportTraits());
return _transportTraits != nullptr;
}


Expand Down Expand Up @@ -290,9 +291,9 @@ bool HTTPClient::begin(String host, uint16_t port, String uri)
_host = host;
_port = port;
_uri = uri;
_transportTraits = TransportTraitsPtr(new TransportTraits());
_transportTraits = TransportTraitsPtr(new (std::nothrow) TransportTraits());
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d uri: %s\n", host.c_str(), port, uri.c_str());
return true;
return _transportTraits != nullptr;
}


Expand All @@ -309,13 +310,13 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt
_port = port;
_uri = uri;

_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
_transportTraits = TransportTraitsPtr(new (std::nothrow) BearSSLTraits(httpsFingerprint));
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d url: %s BearSSL-httpsFingerprint:", host.c_str(), port, uri.c_str());
for (size_t i=0; i < 20; i++) {
DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]);
}
DEBUG_HTTPCLIENT("\n");
return true;
return _transportTraits != nullptr;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void setup()

MDNS.begin(host);

httpServer.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
httpServer.getServer().setRSACert(new (std::nothrow) BearSSL::X509List(serverCert), new (std::nothrow) BearSSL::PrivateKey(serverKey));
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();

Expand Down
4 changes: 3 additions & 1 deletion libraries/ESP8266LLMNR/ESP8266LLMNR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ bool LLMNRResponder::_restart() {
if (igmp_joingroup(IP4_ADDR_ANY4, llmnr) != ERR_OK)
return false;

_conn = new UdpContext;
_conn = new (std::nothrow) UdpContext;
if (!_conn)
return false;
_conn->ref();

if (!_conn->listen(IP_ADDR_ANY, LLMNR_PORT))
Expand Down
9 changes: 7 additions & 2 deletions libraries/ESP8266SSDP/ESP8266SSDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ bool SSDPClass::begin() {

assert(NULL == _server);

_server = new UdpContext;
_server = new (std::nothrow) UdpContext;
if (_server == nullptr) {
return false;
}
_server->ref();

IPAddress local = WiFi.localIP();
Expand Down Expand Up @@ -508,7 +511,9 @@ void SSDPClass::_onTimerStatic(SSDPClass* self) {

void SSDPClass::_startTimer() {
_stopTimer();
_timer = new SSDPTimer();
_timer = new (std::nothrow) SSDPTimer();
if (_timer == nullptr)
return;
ETSTimer* tm = &(_timer->timer);
const int interval = 1000;
os_timer_disarm(tm);
Expand Down
Loading

0 comments on commit 6925982

Please sign in to comment.