[utils] add a flag to use std new (#11937)

The noexcept placement new is standard in c++11. This commit adds a
config OPENTHREAD_CONFIG_USE_STD_NEW to allow using <new>. The default
behavior is still using OpenThread's own placement new implementation.
This commit is contained in:
Yakun Xu
2025-09-19 05:50:15 +08:00
committed by GitHub
parent fbc123940e
commit 3b3dd203be
4 changed files with 16 additions and 2 deletions
+9
View File
@@ -617,6 +617,15 @@
#define OPENTHREAD_EXAMPLES_SIMULATION 0
#endif
/**
* @def OPENTHREAD_CONFIG_USE_STD_NEW
*
* Define 1 to enable using std <new>.
*/
#ifndef OPENTHREAD_CONFIG_USE_STD_NEW
#define OPENTHREAD_CONFIG_USE_STD_NEW 0
#endif
/**
* @}
*/
+6
View File
@@ -34,10 +34,16 @@
#ifndef OT_INCLUDE_COMMON_NEW_HPP_
#define OT_INCLUDE_COMMON_NEW_HPP_
#include <openthread/config.h>
#if defined(OPENTHREAD_CONFIG_USE_STD_NEW) && OPENTHREAD_CONFIG_USE_STD_NEW
#include <new> // IWYU pragma: keep
#else
#include <stddef.h>
#include <openthread/platform/toolchain.h>
inline void *operator new(size_t, void *p) throw() { return p; }
#endif
#endif // OT_INCLUDE_COMMON_NEW_HPP_
+1
View File
@@ -29,6 +29,7 @@
option(OT_BUILD_GTEST "enable gtest")
if(OT_FTD AND BUILD_TESTING AND (NOT OT_PLATFORM STREQUAL "nexus"))
target_compile_definitions(ot-config INTERFACE "OPENTHREAD_CONFIG_USE_STD_NEW=1")
add_subdirectory(unit)
if(OT_BUILD_GTEST)
add_subdirectory(gtest)
-2
View File
@@ -26,8 +26,6 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
// Disable OpenThread's own new implementation to avoid duplicate definition
#define OT_INCLUDE_COMMON_NEW_HPP_
#include "test_platform.h"
#include <map>