etc/services - sync with NetBSD-8
[minix.git] / external / bsd / libc++ / dist / libcxx / src / new.cpp
blob1144651b86e805436cd01b673232135f6995cabc
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 //===----------------------------------------------------------------------===//
10 #define _LIBCPP_BUILDING_NEW
12 #include <stdlib.h>
14 #include "new"
16 #ifndef __has_include
17 #define __has_include(inc) 0
18 #endif
20 #if defined(__APPLE__) && !defined(LIBCXXRT)
21 #include <cxxabi.h>
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
38 #ifndef __GLIBCXX__
40 // Implement all new and delete operators as weak definitions
41 // in this shared library, so that they can be overridden by programs
42 // that define non-weak copies of the functions.
44 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
45 void *
46 operator new(std::size_t size)
47 #if !__has_feature(cxx_noexcept)
48 throw(std::bad_alloc)
49 #endif
51 if (size == 0)
52 size = 1;
53 void* p;
54 while ((p = ::malloc(size)) == 0)
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
68 return p;
71 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
72 void*
73 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
75 void* p = 0;
76 #ifndef _LIBCPP_NO_EXCEPTIONS
77 try
79 #endif // _LIBCPP_NO_EXCEPTIONS
80 p = ::operator new(size);
81 #ifndef _LIBCPP_NO_EXCEPTIONS
83 catch (...)
86 #endif // _LIBCPP_NO_EXCEPTIONS
87 return p;
90 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
91 void*
92 operator new[](size_t size)
93 #if !__has_feature(cxx_noexcept)
94 throw(std::bad_alloc)
95 #endif
97 return ::operator new(size);
100 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
101 void*
102 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
104 void* p = 0;
105 #ifndef _LIBCPP_NO_EXCEPTIONS
108 #endif // _LIBCPP_NO_EXCEPTIONS
109 p = ::operator new[](size);
110 #ifndef _LIBCPP_NO_EXCEPTIONS
112 catch (...)
115 #endif // _LIBCPP_NO_EXCEPTIONS
116 return p;
119 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
120 void
121 operator delete(void* ptr) _NOEXCEPT
123 if (ptr)
124 ::free(ptr);
127 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
128 void
129 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
131 ::operator delete(ptr);
134 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
135 void
136 operator delete(void* ptr, size_t) _NOEXCEPT
138 ::operator delete(ptr);
141 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
142 void
143 operator delete[] (void* ptr) _NOEXCEPT
145 ::operator delete(ptr);
148 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
149 void
150 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
152 ::operator delete[](ptr);
155 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
156 void
157 operator delete[] (void* ptr, size_t) _NOEXCEPT
159 ::operator delete[](ptr);
162 #endif // !__GLIBCXX__
164 namespace std
167 #ifndef __GLIBCXX__
168 const nothrow_t nothrow = {};
169 #endif
171 #ifndef _LIBCPPABI_VERSION
173 #ifndef __GLIBCXX__
175 new_handler
176 set_new_handler(new_handler handler) _NOEXCEPT
178 return __sync_lock_test_and_set(&__new_handler, handler);
181 new_handler
182 get_new_handler() _NOEXCEPT
184 return __sync_fetch_and_add(&__new_handler, nullptr);
187 #endif // !__GLIBCXX__
189 #ifndef LIBCXXRT
191 bad_alloc::bad_alloc() _NOEXCEPT
195 #ifndef __GLIBCXX__
197 bad_alloc::~bad_alloc() _NOEXCEPT
201 const char*
202 bad_alloc::what() const _NOEXCEPT
204 return "std::bad_alloc";
207 #endif // !__GLIBCXX__
209 bad_array_new_length::bad_array_new_length() _NOEXCEPT
213 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
217 const char*
218 bad_array_new_length::what() const _NOEXCEPT
220 return "bad_array_new_length";
223 #endif //LIBCXXRT
225 const char*
226 bad_array_length::what() const _NOEXCEPT
228 return "bad_array_length";
231 bad_array_length::bad_array_length() _NOEXCEPT
235 bad_array_length::~bad_array_length() _NOEXCEPT
239 #endif // _LIBCPPABI_VERSION
241 #ifndef LIBSTDCXX
243 void
244 __throw_bad_alloc()
246 #ifndef _LIBCPP_NO_EXCEPTIONS
247 throw bad_alloc();
248 #endif
251 #endif // !LIBSTDCXX
253 } // std