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-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H |
10 | #define LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H |
11 | |
12 | #include "src/__support/common.h" |
13 | |
14 | #include <stddef.h> // For size_t |
15 | #include <stdlib.h> // For malloc, free etc. |
16 | |
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 |
19 | // argument. |
20 | namespace std { |
21 | |
22 | enum class align_val_t : size_t {}; |
23 | |
24 | } // namespace std |
25 | |
26 | namespace LIBC_NAMESPACE { |
27 | |
28 | class AllocChecker { |
29 | bool success = false; |
30 | |
31 | LIBC_INLINE AllocChecker &operator=(bool status) { |
32 | success = status; |
33 | return *this; |
34 | } |
35 | |
36 | public: |
37 | LIBC_INLINE AllocChecker() = default; |
38 | |
39 | LIBC_INLINE operator bool() const { return success; } |
40 | |
41 | LIBC_INLINE static void *alloc(size_t s, AllocChecker &ac) { |
42 | void *mem = ::malloc(size: s); |
43 | ac = (mem != nullptr); |
44 | return mem; |
45 | } |
46 | |
47 | LIBC_INLINE static void *aligned_alloc(size_t s, std::align_val_t align, |
48 | AllocChecker &ac) { |
49 | void *mem = ::aligned_alloc(alignment: static_cast<size_t>(align), size: s); |
50 | ac = (mem != nullptr); |
51 | return mem; |
52 | } |
53 | }; |
54 | |
55 | } // namespace LIBC_NAMESPACE |
56 | |
57 | LIBC_INLINE void *operator new(size_t size, |
58 | LIBC_NAMESPACE::AllocChecker &ac) noexcept { |
59 | return LIBC_NAMESPACE::AllocChecker::alloc(s: size, ac); |
60 | } |
61 | |
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(s: size, align, ac); |
65 | } |
66 | |
67 | LIBC_INLINE void *operator new[](size_t size, |
68 | LIBC_NAMESPACE::AllocChecker &ac) noexcept { |
69 | return LIBC_NAMESPACE::AllocChecker::alloc(s: size, ac); |
70 | } |
71 | |
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(s: size, align, ac); |
75 | } |
76 | |
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. |
87 | |
88 | #define DELETE_NAME(name) \ |
89 | __asm__(LIBC_MACRO_TO_STRING(LIBC_NAMESPACE) "_" LIBC_MACRO_TO_STRING(name)) |
90 | |
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); |
103 | |
104 | #undef DELETE_NAME |
105 | |
106 | #endif // LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H |
107 | |