Skip to content

Commit 6925982

Browse files
committed
replace new by new (std::nothrow), remove arduino_new
1 parent 5b3d290 commit 6925982

File tree

45 files changed

+207
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+207
-167
lines changed

cores/esp8266/Esp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,17 +740,17 @@ String EspClass::getSketchMD5()
740740
}
741741
uint32_t lengthLeft = getSketchSize();
742742
const size_t bufSize = 512;
743-
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
743+
std::unique_ptr<uint8_t[]> buf(new (std::nothrow) uint8_t[bufSize]);
744744
uint32_t offset = 0;
745745
if(!buf.get()) {
746-
return String();
746+
return emptyString;
747747
}
748748
MD5Builder md5;
749749
md5.begin();
750750
while( lengthLeft > 0) {
751751
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
752752
if (!flashRead(offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
753-
return String();
753+
return emptyString;
754754
}
755755
md5.add(buf.get(), readBytes);
756756
lengthLeft -= readBytes;

cores/esp8266/FunctionalInterrupt.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ typedef void (*voidFuncPtr)(void);
77
typedef void (*voidFuncPtrArg)(void*);
88

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

1212

1313
void ICACHE_RAM_ATTR interruptFunctional(void* arg)
@@ -34,32 +34,52 @@ extern "C"
3434
}
3535
}
3636

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

4141
InterruptInfo* ii = nullptr;
4242

43-
FunctionInfo* fi = new FunctionInfo;
43+
FunctionInfo* fi = new (std::nothrow) FunctionInfo;
44+
if (fi == nullptr)
45+
return false;
4446
fi->reqFunction = intRoutine;
4547

46-
ArgStructure* as = new ArgStructure;
48+
ArgStructure* as = new (std::nothrow) ArgStructure;
49+
if (as == nullptr)
50+
{
51+
delete(fi);
52+
return false;
53+
}
4754
as->interruptInfo = ii;
4855
as->functionInfo = fi;
4956

50-
__attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
57+
return __attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
5158
}
5259

53-
void attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> scheduledIntRoutine, int mode)
60+
bool attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> scheduledIntRoutine, int mode)
5461
{
55-
InterruptInfo* ii = new InterruptInfo;
62+
InterruptInfo* ii = new (std::nothrow) InterruptInfo;
63+
if (ii == nullptr)
64+
return false;
5665

5766
FunctionInfo* fi = new FunctionInfo;
67+
if (fi == nullptr)
68+
{
69+
delete ii;
70+
return false;
71+
}
5872
fi->reqScheduledFunction = scheduledIntRoutine;
5973

60-
ArgStructure* as = new ArgStructure;
74+
ArgStructure* as = new (std::nothrow) ArgStructure;
75+
if (as == nullptr)
76+
{
77+
delete ii;
78+
delete fi;
79+
return false;
80+
}
6181
as->interruptInfo = ii;
6282
as->functionInfo = fi;
6383

64-
__attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
84+
return __attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
6585
}

cores/esp8266/Print.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ size_t Print::printf(const char *format, ...) {
6363
size_t len = vsnprintf(temp, sizeof(temp), format, arg);
6464
va_end(arg);
6565
if (len > sizeof(temp) - 1) {
66-
buffer = new char[len + 1];
66+
buffer = new (std::nothrow) char[len + 1];
6767
if (!buffer) {
6868
return 0;
6969
}
@@ -86,7 +86,7 @@ size_t Print::printf_P(PGM_P format, ...) {
8686
size_t len = vsnprintf_P(temp, sizeof(temp), format, arg);
8787
va_end(arg);
8888
if (len > sizeof(temp) - 1) {
89-
buffer = new char[len + 1];
89+
buffer = new (std::nothrow) char[len + 1];
9090
if (!buffer) {
9191
return 0;
9292
}

cores/esp8266/Updater.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
180180
} else {
181181
_bufferSize = 256;
182182
}
183-
_buffer = new uint8_t[_bufferSize];
183+
_buffer = new (std::nothrow) uint8_t[_bufferSize];
184184
_command = command;
185185

186186
#ifdef DEBUG_UPDATER

cores/esp8266/cbuf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ size_t cbuf::resize(size_t newSize) {
4343
return _size;
4444
}
4545

46-
char *newbuf = new char[newSize];
46+
char *newbuf = new (std::nothrow) char[newSize];
4747
char *oldbuf = _buf;
4848

4949
if(!newbuf) {

cores/esp8266/core_esp8266_features.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,6 @@
3636
#include <stddef.h> // size_t
3737
#include <stdint.h>
3838

39-
#ifdef __cplusplus
40-
41-
namespace arduino
42-
{
43-
extern "C++"
44-
template <typename T, typename ...TConstructorArgs>
45-
T* new0 (size_t n, TConstructorArgs... TconstructorArgs)
46-
{
47-
// n==0: single allocation, otherwise it is an array
48-
size_t offset = n? sizeof(size_t): 0;
49-
size_t arraysize = n? n: 1;
50-
T* ptr = (T*)malloc(offset + (arraysize * sizeof(T)));
51-
if (ptr)
52-
{
53-
if (n)
54-
*(size_t*)(ptr) = n;
55-
for (size_t i = 0; i < arraysize; i++)
56-
new (ptr + offset + i * sizeof(T)) T(TconstructorArgs...);
57-
return ptr + offset;
58-
}
59-
return nullptr;
60-
}
61-
}
62-
63-
#define arduino_new(Type, ...) arduino::new0<Type>(0, ##__VA_ARGS__)
64-
#define arduino_newarray(Type, n, ...) arduino::new0<Type>(n, ##__VA_ARGS__)
65-
66-
#endif // __cplusplus
67-
6839
#ifndef __STRINGIFY
6940
#define __STRINGIFY(a) #a
7041
#endif

doc/reference.rst

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -323,36 +323,3 @@ C++
323323
This assures correct behavior, including handling of all subobjects, which guarantees stability.
324324

325325
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>`__
326-
327-
- New optional allocator ``arduino_new``
328-
329-
A new optional global allocator is introduced with a different semantic:
330-
331-
- never throws exceptions on oom
332-
333-
- never calls constructors on oom
334-
335-
- returns nullptr on oom
336-
337-
It is similar to arduino ``new`` semantic without side effects
338-
(except when parent constructors, or member constructors use ``new``).
339-
340-
Syntax is slightly different, the following shows the different usages:
341-
342-
.. code:: cpp
343-
344-
// with new:
345-
346-
SomeClass* sc = new SomeClass(arg1, arg2, ...);
347-
delete sc;
348-
349-
SomeClass* scs = new SomeClass[42];
350-
delete [] scs;
351-
352-
// with arduino_new:
353-
354-
SomeClass* sc = arduino_new(SomeClass, arg1, arg2, ...);
355-
delete sc;
356-
357-
SomeClass* scs = arduino_newarray(SomeClass, 42);
358-
delete [] scs;

libraries/ArduinoOTA/ArduinoOTA.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void ArduinoOTAClass::setRebootOnSuccess(bool reboot){
106106
_rebootOnSuccess = reboot;
107107
}
108108

109-
void ArduinoOTAClass::begin(bool useMDNS) {
109+
bool ArduinoOTAClass::begin(bool useMDNS) {
110110
if (_initialized)
111111
return;
112112

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

129-
_udp_ota = new UdpContext;
129+
_udp_ota = new (std::nothrow) UdpContext;
130+
if (_udp_ota == nullptr)
131+
return false;
130132
_udp_ota->ref();
131133

132134
if(!_udp_ota->listen(IP_ADDR_ANY, _port))
133-
return;
135+
return false;
134136
_udp_ota->onRx(std::bind(&ArduinoOTAClass::_onRx, this));
135137

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

154157
int ArduinoOTAClass::parseInt(){

libraries/DNSServer/src/DNSServer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ void DNSServer::processNextRequest()
178178
return;
179179

180180
std::unique_ptr<uint8_t[]> buffer(new (std::nothrow) uint8_t[currentPacketSize]);
181-
182-
if (buffer == NULL)
181+
if (buffer == nullptr)
183182
return;
184183

185184
_udp.read(buffer.get(), currentPacketSize);

libraries/EEPROM/EEPROM.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ void EEPROMClass::begin(size_t size) {
6464
//In case begin() is called a 2nd+ time, don't reallocate if size is the same
6565
if(_data && size != _size) {
6666
delete[] _data;
67-
_data = new uint8_t[size];
67+
_data = new (std::nothrow) uint8_t[size];
6868
} else if(!_data) {
69-
_data = new uint8_t[size];
69+
_data = new (std::nothrow) uint8_t[size];
70+
}
71+
if (_data == nullptr) {
72+
return;
7073
}
7174

7275
_size = size;

0 commit comments

Comments
 (0)