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 LIBC_NAMESPACE
{
31 LIBC_INLINE AllocChecker
&operator=(bool status
) {
37 LIBC_INLINE
AllocChecker() = default;
39 LIBC_INLINE
operator bool() const { return success
; }
41 LIBC_INLINE
static void *alloc(size_t s
, AllocChecker
&ac
) {
42 void *mem
= ::malloc(s
);
43 ac
= (mem
!= nullptr);
47 LIBC_INLINE
static void *aligned_alloc(size_t s
, std::align_val_t align
,
49 void *mem
= ::aligned_alloc(static_cast<size_t>(align
), s
);
50 ac
= (mem
!= nullptr);
55 } // namespace LIBC_NAMESPACE
57 LIBC_INLINE
void *operator new(size_t size
,
58 LIBC_NAMESPACE::AllocChecker
&ac
) noexcept
{
59 return LIBC_NAMESPACE::AllocChecker::alloc(size
, ac
);
62 LIBC_INLINE
void *operator new(size_t size
, std::align_val_t align
,
63 LIBC_NAMESPACE::AllocChecker
&ac
) noexcept
{
64 return LIBC_NAMESPACE::AllocChecker::aligned_alloc(size
, align
, ac
);
67 LIBC_INLINE
void *operator new[](size_t size
,
68 LIBC_NAMESPACE::AllocChecker
&ac
) noexcept
{
69 return LIBC_NAMESPACE::AllocChecker::alloc(size
, ac
);
72 LIBC_INLINE
void *operator new[](size_t size
, std::align_val_t align
,
73 LIBC_NAMESPACE::AllocChecker
&ac
) noexcept
{
74 return LIBC_NAMESPACE::AllocChecker::aligned_alloc(size
, align
, ac
);
77 // The ideal situation would be to define the various flavors of operator delete
78 // inlinelike we do with operator new above. However, since we need operator
79 // delete prototypes to match those specified by the C++ standard, we cannot
80 // define them inline as the C++ standard does not allow inline definitions of
81 // replacement operator delete implementations. Note also that we assign a
82 // special linkage name to each of these replacement operator delete functions.
83 // This is because, if we do not give them a special libc internal linkage name,
84 // they will replace operator delete for the entire application. Including this
85 // header file in all libc source files where operator delete is called ensures
86 // that only libc call sites use these replacement operator delete functions.
88 #define DELETE_NAME(name) \
89 __asm__(LIBC_MACRO_TO_STRING(LIBC_NAMESPACE) "_" LIBC_MACRO_TO_STRING(name))
91 void operator delete(void *) noexcept
DELETE_NAME(delete);
92 void operator delete(void *, std::align_val_t
) noexcept
93 DELETE_NAME(delete_aligned
);
94 void operator delete(void *, size_t) noexcept
DELETE_NAME(delete_sized
);
95 void operator delete(void *, size_t, std::align_val_t
) noexcept
96 DELETE_NAME(delete_sized_aligned
);
97 void operator delete[](void *) noexcept
DELETE_NAME(delete_array
);
98 void operator delete[](void *, std::align_val_t
) noexcept
99 DELETE_NAME(delete_array_aligned
);
100 void operator delete[](void *, size_t) noexcept
DELETE_NAME(delete_array_sized
);
101 void operator delete[](void *, size_t, std::align_val_t
) noexcept
102 DELETE_NAME(delete_array_sized_aligned
);
106 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H