1 //===--------------------- stdlib_new_delete.cpp --------------------------===//
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 // This file implements the new and delete operators.
9 //===----------------------------------------------------------------------===//
10 
11 #include "__cxxabi_config.h"
12 #include <new>
13 #include <cstdlib>
14 
15 #if !defined(_THROW_BAD_ALLOC) || !defined(_NOEXCEPT) || !defined(_LIBCXXABI_WEAK)
16 #error The _THROW_BAD_ALLOC, _NOEXCEPT, and _LIBCXXABI_WEAK libc++ macros must \
17        already be defined by libc++.
18 #endif
19 // Implement all new and delete operators as weak definitions
20 // in this shared library, so that they can be overridden by programs
21 // that define non-weak copies of the functions.
22 
23 _LIBCXXABI_WEAK
24 void *
25 operator new(std::size_t size) _THROW_BAD_ALLOC
26 {
27     if (size == 0)
28         size = 1;
29     void* p;
30     while ((p = ::malloc(size)) == 0)
31     {
32         // If malloc fails and there is a new_handler,
33         // call it to try free up memory.
34         std::new_handler nh = std::get_new_handler();
35         if (nh)
36             nh();
37         else
38 #ifndef _LIBCXXABI_NO_EXCEPTIONS
39             throw std::bad_alloc();
40 #else
41             break;
42 #endif
43     }
44     return p;
45 }
46 
47 _LIBCXXABI_WEAK
48 void*
49 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
50 {
51     void* p = 0;
52 #ifndef _LIBCXXABI_NO_EXCEPTIONS
53     try
54     {
55 #endif  // _LIBCXXABI_NO_EXCEPTIONS
56         p = ::operator new(size);
57 #ifndef _LIBCXXABI_NO_EXCEPTIONS
58     }
59     catch (...)
60     {
61     }
62 #endif  // _LIBCXXABI_NO_EXCEPTIONS
63     return p;
64 }
65 
66 _LIBCXXABI_WEAK
67 void*
68 operator new[](size_t size) _THROW_BAD_ALLOC
69 {
70     return ::operator new(size);
71 }
72 
73 _LIBCXXABI_WEAK
74 void*
75 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
76 {
77     void* p = 0;
78 #ifndef _LIBCXXABI_NO_EXCEPTIONS
79     try
80     {
81 #endif  // _LIBCXXABI_NO_EXCEPTIONS
82         p = ::operator new[](size);
83 #ifndef _LIBCXXABI_NO_EXCEPTIONS
84     }
85     catch (...)
86     {
87     }
88 #endif  // _LIBCXXABI_NO_EXCEPTIONS
89     return p;
90 }
91 
92 _LIBCXXABI_WEAK
93 void
94 operator delete(void* ptr) _NOEXCEPT
95 {
96     if (ptr)
97         ::free(ptr);
98 }
99 
100 _LIBCXXABI_WEAK
101 void
102 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
103 {
104     ::operator delete(ptr);
105 }
106 
107 _LIBCXXABI_WEAK
108 void
109 operator delete(void* ptr, size_t) _NOEXCEPT
110 {
111     ::operator delete(ptr);
112 }
113 
114 _LIBCXXABI_WEAK
115 void
116 operator delete[] (void* ptr) _NOEXCEPT
117 {
118     ::operator delete(ptr);
119 }
120 
121 _LIBCXXABI_WEAK
122 void
123 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
124 {
125     ::operator delete[](ptr);
126 }
127 
128 _LIBCXXABI_WEAK
129 void
130 operator delete[] (void* ptr, size_t) _NOEXCEPT
131 {
132     ::operator delete[](ptr);
133 }
134 
135 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
136 
137 _LIBCXXABI_WEAK
138 void *
139 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
140 {
141     if (size == 0)
142         size = 1;
143     if (static_cast<size_t>(alignment) < sizeof(void*))
144       alignment = std::align_val_t(sizeof(void*));
145     void* p;
146 #if defined(_LIBCPP_WIN32API)
147     while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
148 #else
149     while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
150 #endif
151     {
152         // If posix_memalign fails and there is a new_handler,
153         // call it to try free up memory.
154         std::new_handler nh = std::get_new_handler();
155         if (nh)
156             nh();
157         else {
158 #ifndef _LIBCXXABI_NO_EXCEPTIONS
159             throw std::bad_alloc();
160 #else
161             p = nullptr; // posix_memalign doesn't initialize 'p' on failure
162             break;
163 #endif
164         }
165     }
166     return p;
167 }
168 
169 _LIBCXXABI_WEAK
170 void*
171 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
172 {
173     void* p = 0;
174 #ifndef _LIBCXXABI_NO_EXCEPTIONS
175     try
176     {
177 #endif  // _LIBCXXABI_NO_EXCEPTIONS
178         p = ::operator new(size, alignment);
179 #ifndef _LIBCXXABI_NO_EXCEPTIONS
180     }
181     catch (...)
182     {
183     }
184 #endif  // _LIBCXXABI_NO_EXCEPTIONS
185     return p;
186 }
187 
188 _LIBCXXABI_WEAK
189 void*
190 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
191 {
192     return ::operator new(size, alignment);
193 }
194 
195 _LIBCXXABI_WEAK
196 void*
197 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
198 {
199     void* p = 0;
200 #ifndef _LIBCXXABI_NO_EXCEPTIONS
201     try
202     {
203 #endif  // _LIBCXXABI_NO_EXCEPTIONS
204         p = ::operator new[](size, alignment);
205 #ifndef _LIBCXXABI_NO_EXCEPTIONS
206     }
207     catch (...)
208     {
209     }
210 #endif  // _LIBCXXABI_NO_EXCEPTIONS
211     return p;
212 }
213 
214 _LIBCXXABI_WEAK
215 void
216 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
217 {
218     if (ptr)
219 #if defined(_LIBCPP_WIN32API)
220         ::_aligned_free(ptr);
221 #else
222         ::free(ptr);
223 #endif
224 }
225 
226 _LIBCXXABI_WEAK
227 void
228 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
229 {
230     ::operator delete(ptr, alignment);
231 }
232 
233 _LIBCXXABI_WEAK
234 void
235 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
236 {
237     ::operator delete(ptr, alignment);
238 }
239 
240 _LIBCXXABI_WEAK
241 void
242 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
243 {
244     ::operator delete(ptr, alignment);
245 }
246 
247 _LIBCXXABI_WEAK
248 void
249 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
250 {
251     ::operator delete[](ptr, alignment);
252 }
253 
254 _LIBCXXABI_WEAK
255 void
256 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
257 {
258     ::operator delete[](ptr, alignment);
259 }
260 
261 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
262