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 <piotr.pryga@nordicsemi.no>
This commit is contained in:
Piotr Pryga 2022-09-15 18:43:50 +02:00 committed by Carles Cufí
commit 7205f3e35b
2 changed files with 30 additions and 1 deletions

View file

@ -5,6 +5,7 @@
*/
#include <stdlib.h>
#include <new>
#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<size_t>(al), size);
}
void* operator new[](std::size_t size, std::align_val_t al)
{
return aligned_alloc(static_cast<size_t>(al), size);
}
void* operator new(std::size_t size, std::align_val_t al,
const std::nothrow_t&) NOEXCEPT
{
return aligned_alloc(static_cast<size_t>(al), size);
}
void* operator new[](std::size_t size, std::align_val_t al,
const std::nothrow_t&) NOEXCEPT
{
return aligned_alloc(static_cast<size_t>(al), size);
}
#endif /* __cplusplus >= 201703L */
void operator delete(void* ptr) NOEXCEPT
{
free(ptr);

View file

@ -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_ */