• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
alloc(size_t s,AllocChecker & ac)41   LIBC_INLINE static void *alloc(size_t s, AllocChecker &ac) {
42     void *mem = ::malloc(s);
43     ac = (mem != nullptr);
44     return mem;
45   }
46 
aligned_alloc(size_t s,std::align_val_t align,AllocChecker & ac)47   LIBC_INLINE static void *aligned_alloc(size_t s, std::align_val_t align,
48                                          AllocChecker &ac) {
49     void *mem = ::aligned_alloc(static_cast<size_t>(align), s);
50     ac = (mem != nullptr);
51     return mem;
52   }
53 };
54 
55 } // namespace LIBC_NAMESPACE
56 
new(size_t size,LIBC_NAMESPACE::AllocChecker & ac)57 LIBC_INLINE void *operator new(size_t size,
58                                LIBC_NAMESPACE::AllocChecker &ac) noexcept {
59   return LIBC_NAMESPACE::AllocChecker::alloc(size, ac);
60 }
61 
new(size_t size,std::align_val_t align,LIBC_NAMESPACE::AllocChecker & 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);
65 }
66 
67 LIBC_INLINE void *operator new[](size_t size,
68                                  LIBC_NAMESPACE::AllocChecker &ac) noexcept {
69   return LIBC_NAMESPACE::AllocChecker::alloc(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(size, align, ac);
75 }
76 
new(size_t,void * p)77 LIBC_INLINE void *operator new(size_t, void *p) { return p; }
78 
79 LIBC_INLINE void *operator new[](size_t, void *p) { return p; }
80 
81 // The ideal situation would be to define the various flavors of operator delete
82 // inlinelike we do with operator new above. However, since we need operator
83 // delete prototypes to match those specified by the C++ standard, we cannot
84 // define them inline as the C++ standard does not allow inline definitions of
85 // replacement operator delete implementations. Note also that we assign a
86 // special linkage name to each of these replacement operator delete functions.
87 // This is because, if we do not give them a special libc internal linkage name,
88 // they will replace operator delete for the entire application. Including this
89 // header file in all libc source files where operator delete is called ensures
90 // that only libc call sites use these replacement operator delete functions.
91 
92 #define DELETE_NAME(name)                                                      \
93   __asm__(LIBC_MACRO_TO_STRING(LIBC_NAMESPACE) "_" LIBC_MACRO_TO_STRING(name))
94 
95 void operator delete(void *) noexcept DELETE_NAME(delete);
96 void operator delete(void *, std::align_val_t) noexcept
97     DELETE_NAME(delete_aligned);
98 void operator delete(void *, size_t) noexcept DELETE_NAME(delete_sized);
99 void operator delete(void *, size_t, std::align_val_t) noexcept
100     DELETE_NAME(delete_sized_aligned);
101 void operator delete[](void *) noexcept DELETE_NAME(delete_array);
102 void operator delete[](void *, std::align_val_t) noexcept
103     DELETE_NAME(delete_array_aligned);
104 void operator delete[](void *, size_t) noexcept DELETE_NAME(delete_array_sized);
105 void operator delete[](void *, size_t, std::align_val_t) noexcept
106     DELETE_NAME(delete_array_sized_aligned);
107 
108 #undef DELETE_NAME
109 
110 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H
111