[NFC][Py Reformat] Added more commits to .git-blame-ignore-revs
[llvm-project.git] / libc / src / __support / CPP / new.h
blob4c0fb9c5100c8c64a8da88654ce6668fffa4777c
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 //===----------------------------------------------------------------------===//
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
19 // argument.
20 namespace std {
22 enum class align_val_t : size_t {};
24 } // namespace std
26 namespace __llvm_libc {
28 class AllocChecker {
29 bool success = false;
30 AllocChecker &operator=(bool status) {
31 success = status;
32 return *this;
35 public:
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);
42 return mem;
45 LIBC_INLINE static void *aligned_alloc(size_t s, std::align_val_t align,
46 AllocChecker &ac) {
47 void *mem = ::aligned_alloc(static_cast<size_t>(align), s);
48 ac = (mem != nullptr);
49 return mem;
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