zephyr/subsys/cpp/cpp_new.cpp
Piotr Pryga 7205f3e35b 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>
2022-10-03 10:13:25 +02:00

80 lines
1.4 KiB
C++

/*
* Copyright (c) 2018
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <new>
#if __cplusplus < 201103L
#define NOEXCEPT
#else /* >= C++11 */
#define NOEXCEPT noexcept
#endif /* __cplusplus */
void* operator new(size_t size)
{
return malloc(size);
}
void* operator new[](size_t size)
{
return malloc(size);
}
void* operator new(std::size_t size, const std::nothrow_t& tag) NOEXCEPT
{
return malloc(size);
}
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);
}
void operator delete[](void* ptr) NOEXCEPT
{
free(ptr);
}
#if (__cplusplus > 201103L)
void operator delete(void* ptr, size_t) NOEXCEPT
{
free(ptr);
}
void operator delete[](void* ptr, size_t) NOEXCEPT
{
free(ptr);
}
#endif // __cplusplus > 201103L