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