mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-16 00:43:00 +03:00
exceptions: optionally enforce c++ standards (#6333)
* exceptions: 3 choices: legacy, std::new never returns 0, or exceptions enabled * arduino_new (doc, example, array)
This commit is contained in:
@ -32,7 +32,7 @@ extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
|
||||
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
|
||||
|
||||
|
||||
#ifndef __cpp_exceptions
|
||||
#if !defined(__cpp_exceptions) && !defined(NEW_OOM_ABORT)
|
||||
void *operator new(size_t size)
|
||||
{
|
||||
void *ret = malloc(size);
|
||||
@ -52,7 +52,7 @@ void *operator new[](size_t size)
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#endif // arduino's std::new legacy
|
||||
|
||||
void __cxa_pure_virtual(void)
|
||||
{
|
||||
|
@ -32,6 +32,37 @@
|
||||
|
||||
#define WIFI_HAS_EVENT_CALLBACK
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include <stdlib.h> // malloc()
|
||||
#include <stddef.h> // size_t
|
||||
|
||||
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
|
||||
@ -61,4 +92,4 @@ inline uint32_t esp_get_cycle_count() {
|
||||
}
|
||||
#endif // not CORE_MOCK
|
||||
|
||||
#endif
|
||||
#endif // CORE_ESP8266_FEATURES_H
|
||||
|
Reference in New Issue
Block a user