Path: blob/main/contrib/llvm-project/libc/src/__support/CPP/new.h
213799 views
//===-- Libc specific custom operator new and delete ------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H9#define LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H1011#include "hdr/func/aligned_alloc.h"12#include "hdr/func/free.h"13#include "hdr/func/malloc.h"14#include "src/__support/common.h"15#include "src/__support/macros/config.h"16#include "src/__support/macros/properties/os.h"1718#include <stddef.h> // For size_t1920// Defining members in the std namespace is not preferred. But, we do it here21// so that we can use it to define the operator new which takes std::align_val_t22// argument.23namespace std {2425enum class align_val_t : size_t {};2627} // namespace std2829namespace LIBC_NAMESPACE_DECL {3031namespace cpp {32template <class T> [[nodiscard]] constexpr T *launder(T *p) {33static_assert(__has_builtin(__builtin_launder),34"cpp::launder requires __builtin_launder");35return __builtin_launder(p);36}37} // namespace cpp3839class AllocChecker {40bool success = false;4142LIBC_INLINE AllocChecker &operator=(bool status) {43success = status;44return *this;45}4647public:48LIBC_INLINE AllocChecker() = default;4950LIBC_INLINE operator bool() const { return success; }5152LIBC_INLINE static void *alloc(size_t s, AllocChecker &ac) {53void *mem = ::malloc(s);54ac = (mem != nullptr);55return mem;56}5758LIBC_INLINE static void *aligned_alloc(size_t s, std::align_val_t align,59AllocChecker &ac) {60#ifdef LIBC_TARGET_OS_IS_WINDOWS61// std::aligned_alloc is not available on Windows because std::free on62// Windows cannot deallocate any over-aligned memory. Microsoft provides an63// alternative for std::aligned_alloc named _aligned_malloc, but it must be64// paired with _aligned_free instead of std::free.65void *mem = ::_aligned_malloc(static_cast<size_t>(align), s);66#else67void *mem = ::aligned_alloc(static_cast<size_t>(align), s);68#endif69ac = (mem != nullptr);70return mem;71}72};7374} // namespace LIBC_NAMESPACE_DECL7576LIBC_INLINE void *operator new(size_t size,77LIBC_NAMESPACE::AllocChecker &ac) noexcept {78return LIBC_NAMESPACE::AllocChecker::alloc(size, ac);79}8081LIBC_INLINE void *operator new(size_t size, std::align_val_t align,82LIBC_NAMESPACE::AllocChecker &ac) noexcept {83return LIBC_NAMESPACE::AllocChecker::aligned_alloc(size, align, ac);84}8586LIBC_INLINE void *operator new[](size_t size,87LIBC_NAMESPACE::AllocChecker &ac) noexcept {88return LIBC_NAMESPACE::AllocChecker::alloc(size, ac);89}9091LIBC_INLINE void *operator new[](size_t size, std::align_val_t align,92LIBC_NAMESPACE::AllocChecker &ac) noexcept {93return LIBC_NAMESPACE::AllocChecker::aligned_alloc(size, align, ac);94}9596LIBC_INLINE void *operator new(size_t, void *p) { return p; }9798LIBC_INLINE void *operator new[](size_t, void *p) { return p; }99100// The ideal situation would be to define the various flavors of operator delete101// inlinelike we do with operator new above. However, since we need operator102// delete prototypes to match those specified by the C++ standard, we cannot103// define them inline as the C++ standard does not allow inline definitions of104// replacement operator delete implementations. Note also that we assign a105// special linkage name to each of these replacement operator delete functions.106// This is because, if we do not give them a special libc internal linkage name,107// they will replace operator delete for the entire application. Including this108// header file in all libc source files where operator delete is called ensures109// that only libc call sites use these replacement operator delete functions.110111#define DELETE_NAME(name) \112__asm__(LIBC_MACRO_TO_STRING(LIBC_NAMESPACE) "_" LIBC_MACRO_TO_STRING(name))113114void operator delete(void *) noexcept DELETE_NAME(delete);115void operator delete(void *, std::align_val_t) noexcept116DELETE_NAME(delete_aligned);117void operator delete(void *, size_t) noexcept DELETE_NAME(delete_sized);118void operator delete(void *, size_t, std::align_val_t) noexcept119DELETE_NAME(delete_sized_aligned);120void operator delete[](void *) noexcept DELETE_NAME(delete_array);121void operator delete[](void *, std::align_val_t) noexcept122DELETE_NAME(delete_array_aligned);123void operator delete[](void *, size_t) noexcept DELETE_NAME(delete_array_sized);124void operator delete[](void *, size_t, std::align_val_t) noexcept125DELETE_NAME(delete_array_sized_aligned);126127#undef DELETE_NAME128129#endif // LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H130131132