From 7205f3e35b0ea3f0d1c60fe92697050921a10e98 Mon Sep 17 00:00:00 2001 From: Piotr Pryga Date: Thu, 15 Sep 2022 18:43:50 +0200 Subject: [PATCH] cpp: Add spec of new operator with alignment request C++17 delivered another specialization of a new operator, that allows to allocate a memory with requested alignment. The commit adds these specialziations to C++ subsystem. Signed-off-by: Piotr Pryga --- subsys/cpp/cpp_new.cpp | 25 +++++++++++++++++++++++++ subsys/cpp/include/new | 6 +++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/subsys/cpp/cpp_new.cpp b/subsys/cpp/cpp_new.cpp index 38c66a71370..4a2c0044602 100644 --- a/subsys/cpp/cpp_new.cpp +++ b/subsys/cpp/cpp_new.cpp @@ -5,6 +5,7 @@ */ #include +#include #if __cplusplus < 201103L #define NOEXCEPT @@ -32,6 +33,30 @@ void* operator new[](std::size_t size, const std::nothrow_t& tag) NOEXCEPT return malloc(size); } +#if __cplusplus >= 201703L +void* operator new(size_t size, std::align_val_t al) +{ + return aligned_alloc(static_cast(al), size); +} + +void* operator new[](std::size_t size, std::align_val_t al) +{ + return aligned_alloc(static_cast(al), size); +} + +void* operator new(std::size_t size, std::align_val_t al, + const std::nothrow_t&) NOEXCEPT +{ + return aligned_alloc(static_cast(al), size); +} + +void* operator new[](std::size_t size, std::align_val_t al, + const std::nothrow_t&) NOEXCEPT +{ + return aligned_alloc(static_cast(al), size); +} +#endif /* __cplusplus >= 201703L */ + void operator delete(void* ptr) NOEXCEPT { free(ptr); diff --git a/subsys/cpp/include/new b/subsys/cpp/include/new index 535727c9dcf..cbefcd50c41 100644 --- a/subsys/cpp/include/new +++ b/subsys/cpp/include/new @@ -24,6 +24,10 @@ namespace std { }; #endif /* __cplusplus */ extern const std::nothrow_t nothrow; -} +#if __cplusplus >= 201703L + enum class align_val_t : std::size_t {}; +#endif /* CONFIG_STD_CPP17 */ + +} #endif /* ZEPHYR_SUBSYS_CPP_INCLUDE_NEW_ */