136 lines · c
1//===-- Libc specific custom operator new and delete ------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H10#define LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H11 12#include "hdr/func/aligned_alloc.h"13#include "hdr/func/free.h"14#include "hdr/func/malloc.h"15#include "src/__support/common.h"16#include "src/__support/macros/config.h"17#include "src/__support/macros/properties/compiler.h"18#include "src/__support/macros/properties/os.h"19 20#include <stddef.h> // For size_t21 22// Defining members in the std namespace is not preferred. But, we do it here23// so that we can use it to define the operator new which takes std::align_val_t24// argument.25namespace std {26 27enum class align_val_t : size_t {};28 29} // namespace std30 31namespace LIBC_NAMESPACE_DECL {32 33namespace cpp {34template <class T> [[nodiscard]] constexpr T *launder(T *p) {35 static_assert(__has_builtin(__builtin_launder),36 "cpp::launder requires __builtin_launder");37 return __builtin_launder(p);38}39} // namespace cpp40 41class AllocChecker {42 bool success = false;43 44 LIBC_INLINE AllocChecker &operator=(bool status) {45 success = status;46 return *this;47 }48 49public:50 LIBC_INLINE AllocChecker() = default;51 52 LIBC_INLINE operator bool() const { return success; }53 54 LIBC_INLINE static void *alloc(size_t s, AllocChecker &ac) {55 void *mem = ::malloc(s);56 ac = (mem != nullptr);57 return mem;58 }59 60 LIBC_INLINE static void *aligned_alloc(size_t s, std::align_val_t align,61 AllocChecker &ac) {62#ifdef LIBC_TARGET_OS_IS_WINDOWS63 // std::aligned_alloc is not available on Windows because std::free on64 // Windows cannot deallocate any over-aligned memory. Microsoft provides an65 // alternative for std::aligned_alloc named _aligned_malloc, but it must be66 // paired with _aligned_free instead of std::free.67 void *mem = ::_aligned_malloc(static_cast<size_t>(align), s);68#else69 void *mem = ::aligned_alloc(static_cast<size_t>(align), s);70#endif71 ac = (mem != nullptr);72 return mem;73 }74};75 76} // namespace LIBC_NAMESPACE_DECL77 78LIBC_INLINE void *operator new(size_t size,79 LIBC_NAMESPACE::AllocChecker &ac) noexcept {80 return LIBC_NAMESPACE::AllocChecker::alloc(size, ac);81}82 83LIBC_INLINE void *operator new(size_t size, std::align_val_t align,84 LIBC_NAMESPACE::AllocChecker &ac) noexcept {85 return LIBC_NAMESPACE::AllocChecker::aligned_alloc(size, align, ac);86}87 88LIBC_INLINE void *operator new[](size_t size,89 LIBC_NAMESPACE::AllocChecker &ac) noexcept {90 return LIBC_NAMESPACE::AllocChecker::alloc(size, ac);91}92 93LIBC_INLINE void *operator new[](size_t size, std::align_val_t align,94 LIBC_NAMESPACE::AllocChecker &ac) noexcept {95 return LIBC_NAMESPACE::AllocChecker::aligned_alloc(size, align, ac);96}97 98LIBC_INLINE void *operator new(size_t, void *p) { return p; }99 100LIBC_INLINE void *operator new[](size_t, void *p) { return p; }101 102// The ideal situation would be to define the various flavors of operator delete103// inlinelike we do with operator new above. However, since we need operator104// delete prototypes to match those specified by the C++ standard, we cannot105// define them inline as the C++ standard does not allow inline definitions of106// replacement operator delete implementations. Note also that we assign a107// special linkage name to each of these replacement operator delete functions.108// This is because, if we do not give them a special libc internal linkage name,109// they will replace operator delete for the entire application. Including this110// header file in all libc source files where operator delete is called ensures111// that only libc call sites use these replacement operator delete functions.112 113#ifndef LIBC_COMPILER_IS_MSVC114#define DELETE_NAME(name) \115 __asm__(LIBC_MACRO_TO_STRING(LIBC_NAMESPACE) "_" LIBC_MACRO_TO_STRING(name))116#else117#define DELETE_NAME(name)118#endif // LIBC_COMPILER_IS_MSVC119 120void operator delete(void *) noexcept DELETE_NAME(delete);121void operator delete(void *, std::align_val_t) noexcept122 DELETE_NAME(delete_aligned);123void operator delete(void *, size_t) noexcept DELETE_NAME(delete_sized);124void operator delete(void *, size_t, std::align_val_t) noexcept125 DELETE_NAME(delete_sized_aligned);126void operator delete[](void *) noexcept DELETE_NAME(delete_array);127void operator delete[](void *, std::align_val_t) noexcept128 DELETE_NAME(delete_array_aligned);129void operator delete[](void *, size_t) noexcept DELETE_NAME(delete_array_sized);130void operator delete[](void *, size_t, std::align_val_t) noexcept131 DELETE_NAME(delete_array_sized_aligned);132 133#undef DELETE_NAME134 135#endif // LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H136