zephyr/subsys/cpp/cpp_new.cpp
Piotr Pryga a0dc682bd9 cpp: new: Add no-throwing implementation of new operator
C++ subsystem defines exception throwing operator new.
Though the implementation never throws an exception.

If used by an application it should verify if requested
memory was allocated. This is against C++ standard.
If an application does not support exceptions or does not
want to call throwing new operator, it should use a
specialization of a new operator that makes sure it
never throws bad_alloc exception. The cpp subsystem
does not provide this specialization.

The commit adds missing operator new specializations.

Signed-off-by: Piotr Pryga <piotr.pryga@nordicsemi.no>
2022-10-03 10:13:25 +02:00

56 lines
842 B
C++

/*
* Copyright (c) 2018
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#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);
}
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