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