[Clang][CodeGen]`vtable`, `typeinfo` et al. are globals
[llvm-project.git] / libcxxabi / src / stdlib_new_delete.cpp
blob080f932ccc60ecfd80e248c24339ae08d17e8af4
1 //===----------------------------------------------------------------------===//
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 #include "__cxxabi_config.h"
10 #include <__memory/aligned_alloc.h>
11 #include <cstdlib>
12 #include <new>
14 // Perform a few sanity checks on libc++ and libc++abi macros to ensure that
15 // the code below can be an exact copy of the code in libcxx/src/new.cpp.
16 #if !defined(_THROW_BAD_ALLOC)
17 # error The _THROW_BAD_ALLOC macro should be already defined by libc++
18 #endif
20 #ifndef _LIBCPP_WEAK
21 # error The _LIBCPP_WEAK macro should be already defined by libc++
22 #endif
24 #if defined(_LIBCXXABI_NO_EXCEPTIONS) != defined(_LIBCPP_HAS_NO_EXCEPTIONS)
25 # error libc++ and libc++abi seem to disagree on whether exceptions are enabled
26 #endif
28 // ------------------ BEGIN COPY ------------------
29 // Implement all new and delete operators as weak definitions
30 // in this shared library, so that they can be overridden by programs
31 // that define non-weak copies of the functions.
33 _LIBCPP_WEAK
34 void *
35 operator new(std::size_t size) _THROW_BAD_ALLOC
37 if (size == 0)
38 size = 1;
39 void* p;
40 while ((p = std::malloc(size)) == nullptr)
42 // If malloc fails and there is a new_handler,
43 // call it to try free up memory.
44 std::new_handler nh = std::get_new_handler();
45 if (nh)
46 nh();
47 else
48 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
49 throw std::bad_alloc();
50 #else
51 break;
52 #endif
54 return p;
57 _LIBCPP_WEAK
58 void*
59 operator new(size_t size, const std::nothrow_t&) noexcept
61 void* p = nullptr;
62 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
63 try
65 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
66 p = ::operator new(size);
67 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
69 catch (...)
72 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
73 return p;
76 _LIBCPP_WEAK
77 void*
78 operator new[](size_t size) _THROW_BAD_ALLOC
80 return ::operator new(size);
83 _LIBCPP_WEAK
84 void*
85 operator new[](size_t size, const std::nothrow_t&) noexcept
87 void* p = nullptr;
88 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
89 try
91 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
92 p = ::operator new[](size);
93 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
95 catch (...)
98 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
99 return p;
102 _LIBCPP_WEAK
103 void
104 operator delete(void* ptr) noexcept
106 std::free(ptr);
109 _LIBCPP_WEAK
110 void
111 operator delete(void* ptr, const std::nothrow_t&) noexcept
113 ::operator delete(ptr);
116 _LIBCPP_WEAK
117 void
118 operator delete(void* ptr, size_t) noexcept
120 ::operator delete(ptr);
123 _LIBCPP_WEAK
124 void
125 operator delete[] (void* ptr) noexcept
127 ::operator delete(ptr);
130 _LIBCPP_WEAK
131 void
132 operator delete[] (void* ptr, const std::nothrow_t&) noexcept
134 ::operator delete[](ptr);
137 _LIBCPP_WEAK
138 void
139 operator delete[] (void* ptr, size_t) noexcept
141 ::operator delete[](ptr);
144 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
146 _LIBCPP_WEAK
147 void *
148 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
150 if (size == 0)
151 size = 1;
152 if (static_cast<size_t>(alignment) < sizeof(void*))
153 alignment = std::align_val_t(sizeof(void*));
155 // Try allocating memory. If allocation fails and there is a new_handler,
156 // call it to try free up memory, and try again until it succeeds, or until
157 // the new_handler decides to terminate.
159 // If allocation fails and there is no new_handler, we throw bad_alloc
160 // (or return nullptr if exceptions are disabled).
161 void* p;
162 while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr)
164 std::new_handler nh = std::get_new_handler();
165 if (nh)
166 nh();
167 else {
168 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
169 throw std::bad_alloc();
170 #else
171 break;
172 #endif
175 return p;
178 _LIBCPP_WEAK
179 void*
180 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
182 void* p = nullptr;
183 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
186 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
187 p = ::operator new(size, alignment);
188 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
190 catch (...)
193 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
194 return p;
197 _LIBCPP_WEAK
198 void*
199 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
201 return ::operator new(size, alignment);
204 _LIBCPP_WEAK
205 void*
206 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
208 void* p = nullptr;
209 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
212 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
213 p = ::operator new[](size, alignment);
214 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
216 catch (...)
219 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
220 return p;
223 _LIBCPP_WEAK
224 void
225 operator delete(void* ptr, std::align_val_t) noexcept
227 std::__libcpp_aligned_free(ptr);
230 _LIBCPP_WEAK
231 void
232 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
234 ::operator delete(ptr, alignment);
237 _LIBCPP_WEAK
238 void
239 operator delete(void* ptr, size_t, std::align_val_t alignment) noexcept
241 ::operator delete(ptr, alignment);
244 _LIBCPP_WEAK
245 void
246 operator delete[] (void* ptr, std::align_val_t alignment) noexcept
248 ::operator delete(ptr, alignment);
251 _LIBCPP_WEAK
252 void
253 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
255 ::operator delete[](ptr, alignment);
258 _LIBCPP_WEAK
259 void
260 operator delete[] (void* ptr, size_t, std::align_val_t alignment) noexcept
262 ::operator delete[](ptr, alignment);
265 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
266 // ------------------ END COPY ------------------