1 //===-- Libc specific custom operator new and delete ------------*- C++ -*-===//
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-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_CPP_NEW_H
10 #define LLVM_LIBC_SRC_SUPPORT_CPP_NEW_H
12 #include "src/__support/common.h"
14 #include <stddef.h> // For size_t
15 #include <stdlib.h> // For malloc, free etc.
17 // Defining members in the std namespace is not preferred. But, we do it here
18 // so that we can use it to define the operator new which takes std::align_val_t
22 enum class align_val_t
: size_t {};
26 namespace __llvm_libc
{
30 AllocChecker
&operator=(bool status
) {
36 AllocChecker() = default;
37 operator bool() const { return success
; }
39 LIBC_INLINE
static void *alloc(size_t s
, AllocChecker
&ac
) {
40 void *mem
= ::malloc(s
);
41 ac
= (mem
!= nullptr);
45 LIBC_INLINE
static void *aligned_alloc(size_t s
, std::align_val_t align
,
47 void *mem
= ::aligned_alloc(static_cast<size_t>(align
), s
);
48 ac
= (mem
!= nullptr);
53 } // namespace __llvm_libc
55 LIBC_INLINE
void *operator new(size_t size
,
56 __llvm_libc::AllocChecker
&ac
) noexcept
{
57 return __llvm_libc::AllocChecker::alloc(size
, ac
);
60 LIBC_INLINE
void *operator new(size_t size
, std::align_val_t align
,
61 __llvm_libc::AllocChecker
&ac
) noexcept
{
62 return __llvm_libc::AllocChecker::aligned_alloc(size
, align
, ac
);
65 LIBC_INLINE
void *operator new[](size_t size
,
66 __llvm_libc::AllocChecker
&ac
) noexcept
{
67 return __llvm_libc::AllocChecker::alloc(size
, ac
);
70 LIBC_INLINE
void *operator new[](size_t size
, std::align_val_t align
,
71 __llvm_libc::AllocChecker
&ac
) noexcept
{
72 return __llvm_libc::AllocChecker::aligned_alloc(size
, align
, ac
);
75 // The ideal situation would be to define the various flavors of operator delete
76 // inlinelike we do with operator new above. However, since we need operator
77 // delete prototypes to match those specified by the C++ standard, we cannot
78 // define them inline as the C++ standard does not allow inline definitions of
79 // replacement operator delete implementations. Note also that we assign a
80 // special linkage name to each of these replacement operator delete functions.
81 // This is because, if we do not give them a special libc internal linkage name,
82 // they will replace operator delete for the entire application. Including this
83 // header file in all libc source files where operator delete is called ensures
84 // that only libc call sites use these replacement operator delete functions.
85 void operator delete(void *) noexcept
__asm__("__llvm_libc_delete");
86 void operator delete(void *, std::align_val_t
) noexcept
87 __asm__("__llvm_libc_delete_aligned");
88 void operator delete(void *, size_t) noexcept
89 __asm__("__llvm_libc_delete_sized");
90 void operator delete(void *, size_t, std::align_val_t
) noexcept
91 __asm__("__llvm_libc_delete_sized_aligned");
92 void operator delete[](void *) noexcept
__asm__("__llvm_libc_delete_array");
93 void operator delete[](void *, std::align_val_t
) noexcept
94 __asm__("__llvm_libc_delete_array_aligned");
95 void operator delete[](void *, size_t) noexcept
96 __asm__("__llvm_libc_delete_array_sized");
97 void operator delete[](void *, size_t, std::align_val_t
) noexcept
98 __asm__("__llvm_libc_delete_array_sized_aligned");
100 #endif // LLVM_LIBC_SRC_SUPPORT_CPP_NEW_H