From 3b3dd203be690995d1a89b110322e4eafafc3a3b Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Fri, 19 Sep 2025 05:50:15 +0800 Subject: [PATCH] [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 . The default behavior is still using OpenThread's own placement new implementation. --- src/core/config/misc.h | 9 +++++++++ src/include/common/new.hpp | 6 ++++++ tests/CMakeLists.txt | 1 + tests/unit/test_platform.cpp | 2 -- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/core/config/misc.h b/src/core/config/misc.h index fe5aaa62f..ea50a0801 100644 --- a/src/core/config/misc.h +++ b/src/core/config/misc.h @@ -617,6 +617,15 @@ #define OPENTHREAD_EXAMPLES_SIMULATION 0 #endif +/** + * @def OPENTHREAD_CONFIG_USE_STD_NEW + * + * Define 1 to enable using std . + */ +#ifndef OPENTHREAD_CONFIG_USE_STD_NEW +#define OPENTHREAD_CONFIG_USE_STD_NEW 0 +#endif + /** * @} */ diff --git a/src/include/common/new.hpp b/src/include/common/new.hpp index bb49e3c64..1a7fbedff 100644 --- a/src/include/common/new.hpp +++ b/src/include/common/new.hpp @@ -34,10 +34,16 @@ #ifndef OT_INCLUDE_COMMON_NEW_HPP_ #define OT_INCLUDE_COMMON_NEW_HPP_ +#include + +#if defined(OPENTHREAD_CONFIG_USE_STD_NEW) && OPENTHREAD_CONFIG_USE_STD_NEW +#include // IWYU pragma: keep +#else #include #include inline void *operator new(size_t, void *p) throw() { return p; } +#endif #endif // OT_INCLUDE_COMMON_NEW_HPP_ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4fde25208..485d7fb44 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/unit/test_platform.cpp b/tests/unit/test_platform.cpp index 6e313dae6..fa901a98c 100644 --- a/tests/unit/test_platform.cpp +++ b/tests/unit/test_platform.cpp @@ -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