zephyr/lib/cpp/minimal/cpp_new.cpp
Stephanos Ioannidis 96234b15df lib: cpp: Relocate C++ minimal library components to lib/cpp/minimal
This commit relocates the "C++ minimal library" components, that
implement a very limited subset of the standard C++ library, to a
dedicated directory, `lib/cpp/minimal`, in order to provide a clear
separation among the different C++ library components.

After this refactoring, the top `lib/cpp` directory should only contain
the sub-directories for each C++ library (i.e. `abi`, `minimal`). In
the future, a C++ library-specific shim layer implementation may be
added as sub-directories under `lib/cpp` as well (e.g. `libstdc++`
sub-directory containing the shim layer implementation for the GCC
libstdc++ library) -- this is similar to how the libc directories are
structured under `lib/libc`.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2023-01-13 17:42:55 -05:00

86 lines
1.6 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 */
#if __cplusplus < 202002L
#define NODISCARD
#else
#define NODISCARD [[nodiscard]]
#endif /* __cplusplus */
NODISCARD void* operator new(size_t size)
{
return malloc(size);
}
NODISCARD void* operator new[](size_t size)
{
return malloc(size);
}
NODISCARD void* operator new(std::size_t size, const std::nothrow_t& tag) NOEXCEPT
{
return malloc(size);
}
NODISCARD void* operator new[](std::size_t size, const std::nothrow_t& tag) NOEXCEPT
{
return malloc(size);
}
#if __cplusplus >= 201703L
NODISCARD void* operator new(size_t size, std::align_val_t al)
{
return aligned_alloc(static_cast<size_t>(al), size);
}
NODISCARD void* operator new[](std::size_t size, std::align_val_t al)
{
return aligned_alloc(static_cast<size_t>(al), size);
}
NODISCARD 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);
}
NODISCARD 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