1 //===--------------------------- new.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 
10 #define _LIBCPP_BUILDING_NEW
11 
12 #include <stdlib.h>
13 
14 #include "new"
15 
16 #ifndef __has_include
17 #define __has_include(inc) 0
18 #endif
19 
20 #if defined(__APPLE__) && !defined(LIBCXXRT)
21     #include <cxxabi.h>
22 
23     #ifndef _LIBCPPABI_VERSION
24         // On Darwin, there are two STL shared libraries and a lower level ABI
25         // shared library.  The global holding the current new handler is
26         // in the ABI library and named __cxa_new_handler.
27         #define __new_handler __cxxabiapple::__cxa_new_handler
28     #endif
29 #else  // __APPLE__
30     #if defined(LIBCXXRT) || __has_include(<cxxabi.h>)
31         #include <cxxabi.h>
32     #endif  // __has_include(<cxxabi.h>)
33     #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
34         static std::new_handler __new_handler;
35     #endif  // _LIBCPPABI_VERSION
36 #endif
37 
38 #ifndef __GLIBCXX__
39 
40 // Implement all new and delete operators as weak definitions
41 // in this shared library, so that they can be overriden by programs
42 // that define non-weak copies of the functions.
43 
44 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
45 void *
operator new(std::size_t size)46 operator new(std::size_t size)
47 #if !__has_feature(cxx_noexcept)
48     throw(std::bad_alloc)
49 #endif
50 {
51     if (size == 0)
52         size = 1;
53     void* p;
54     while ((p = ::malloc(size)) == 0)
55     {
56         // If malloc fails and there is a new_handler,
57         // call it to try free up memory.
58         std::new_handler nh = std::get_new_handler();
59         if (nh)
60             nh();
61         else
62 #ifndef _LIBCPP_NO_EXCEPTIONS
63             throw std::bad_alloc();
64 #else
65             break;
66 #endif
67     }
68     return p;
69 }
70 
71 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
72 void*
operator new(size_t size,const std::nothrow_t &)73 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
74 {
75     void* p = 0;
76 #ifndef _LIBCPP_NO_EXCEPTIONS
77     try
78     {
79 #endif  // _LIBCPP_NO_EXCEPTIONS
80         p = ::operator new(size);
81 #ifndef _LIBCPP_NO_EXCEPTIONS
82     }
83     catch (...)
84     {
85     }
86 #endif  // _LIBCPP_NO_EXCEPTIONS
87     return p;
88 }
89 
90 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
91 void*
operator new[](size_t size)92 operator new[](size_t size)
93 #if !__has_feature(cxx_noexcept)
94     throw(std::bad_alloc)
95 #endif
96 {
97     return ::operator new(size);
98 }
99 
100 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
101 void*
operator new[](size_t size,const std::nothrow_t &)102 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
103 {
104     void* p = 0;
105 #ifndef _LIBCPP_NO_EXCEPTIONS
106     try
107     {
108 #endif  // _LIBCPP_NO_EXCEPTIONS
109         p = ::operator new[](size);
110 #ifndef _LIBCPP_NO_EXCEPTIONS
111     }
112     catch (...)
113     {
114     }
115 #endif  // _LIBCPP_NO_EXCEPTIONS
116     return p;
117 }
118 
119 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
120 void
operator delete(void * ptr)121 operator delete(void* ptr) _NOEXCEPT
122 {
123     if (ptr)
124         ::free(ptr);
125 }
126 
127 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
128 void
operator delete(void * ptr,const std::nothrow_t &)129 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
130 {
131     ::operator delete(ptr);
132 }
133 
134 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
135 void
operator delete(void * ptr,size_t)136 operator delete(void* ptr, size_t) _NOEXCEPT
137 {
138     ::operator delete(ptr);
139 }
140 
141 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
142 void
operator delete(void * ptr,size_t,const std::nothrow_t & nt)143 operator delete(void* ptr, size_t, const std::nothrow_t& nt) _NOEXCEPT
144 {
145     ::operator delete(ptr, nt);
146 }
147 
148 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
149 void
operator delete[](void * ptr)150 operator delete[] (void* ptr) _NOEXCEPT
151 {
152     ::operator delete(ptr);
153 }
154 
155 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
156 void
operator delete[](void * ptr,const std::nothrow_t &)157 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
158 {
159     ::operator delete[](ptr);
160 }
161 
162 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
163 void
operator delete[](void * ptr,size_t)164 operator delete[] (void* ptr, size_t) _NOEXCEPT
165 {
166     ::operator delete[](ptr);
167 }
168 
169 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
170 void
operator delete[](void * ptr,size_t,const std::nothrow_t & nt)171 operator delete[] (void* ptr, size_t, const std::nothrow_t& nt) _NOEXCEPT
172 {
173     ::operator delete[](ptr, nt);
174 }
175 
176 #endif // !__GLIBCXX__
177 
178 namespace std
179 {
180 
181 #ifndef __GLIBCXX__
182 const nothrow_t nothrow = {};
183 #endif
184 
185 #ifndef _LIBCPPABI_VERSION
186 
187 #ifndef __GLIBCXX__
188 
189 new_handler
set_new_handler(new_handler handler)190 set_new_handler(new_handler handler) _NOEXCEPT
191 {
192     return __sync_lock_test_and_set(&__new_handler, handler);
193 }
194 
195 new_handler
get_new_handler()196 get_new_handler() _NOEXCEPT
197 {
198     return __sync_fetch_and_add(&__new_handler, nullptr);
199 }
200 
201 #endif // !__GLIBCXX__
202 
203 #ifndef LIBCXXRT
204 
bad_alloc()205 bad_alloc::bad_alloc() _NOEXCEPT
206 {
207 }
208 
209 #ifndef __GLIBCXX__
210 
~bad_alloc()211 bad_alloc::~bad_alloc() _NOEXCEPT
212 {
213 }
214 
215 const char*
what() const216 bad_alloc::what() const _NOEXCEPT
217 {
218     return "std::bad_alloc";
219 }
220 
221 #endif // !__GLIBCXX__
222 
bad_array_new_length()223 bad_array_new_length::bad_array_new_length() _NOEXCEPT
224 {
225 }
226 
~bad_array_new_length()227 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
228 {
229 }
230 
231 const char*
what() const232 bad_array_new_length::what() const _NOEXCEPT
233 {
234     return "bad_array_new_length";
235 }
236 
237 #endif //LIBCXXRT
238 
239 const char*
what() const240 bad_array_length::what() const _NOEXCEPT
241 {
242     return "bad_array_length";
243 }
244 
bad_array_length()245 bad_array_length::bad_array_length() _NOEXCEPT
246 {
247 }
248 
~bad_array_length()249 bad_array_length::~bad_array_length() _NOEXCEPT
250 {
251 }
252 
253 #endif // _LIBCPPABI_VERSION
254 
255 #ifndef LIBSTDCXX
256 
257 void
__throw_bad_alloc()258 __throw_bad_alloc()
259 {
260 #ifndef _LIBCPP_NO_EXCEPTIONS
261     throw bad_alloc();
262 #endif
263 }
264 
265 #endif // !LIBSTDCXX
266 
267 }  // std
268