HaikuDepot: notify work status from main window
[haiku.git] / src / libs / libc++ / new.cpp
blobf4f73d86803d38827ddb8ba97af6f3354a314fd5
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 #if defined(__APPLE__) && !defined(LIBCXXRT)
17 #include <cxxabi.h>
19 #ifndef _LIBCPPABI_VERSION
20 // On Darwin, there are two STL shared libraries and a lower level ABI
21 // shared library. The global holding the current new handler is
22 // in the ABI library and named __cxa_new_handler.
23 #define __new_handler __cxxabiapple::__cxa_new_handler
24 #endif
25 #else // __APPLE__
26 #if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
27 #include <cxxabi.h>
28 #endif // defined(LIBCXX_BUILDING_LIBCXXABI)
29 #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
30 static std::new_handler __new_handler;
31 #endif // _LIBCPPABI_VERSION
32 #endif
34 #ifndef __GLIBCXX__
36 // Implement all new and delete operators as weak definitions
37 // in this shared library, so that they can be overridden by programs
38 // that define non-weak copies of the functions.
40 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
41 void *
42 operator new(std::size_t size)
43 #if !__has_feature(cxx_noexcept)
44 throw(std::bad_alloc)
45 #endif
47 if (size == 0)
48 size = 1;
49 void* p;
50 while ((p = ::malloc(size)) == 0)
52 // If malloc fails and there is a new_handler,
53 // call it to try free up memory.
54 std::new_handler nh = std::get_new_handler();
55 if (nh)
56 nh();
57 else
58 #ifndef _LIBCPP_NO_EXCEPTIONS
59 throw std::bad_alloc();
60 #else
61 break;
62 #endif
64 return p;
67 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
68 void*
69 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
71 void* p = 0;
72 #ifndef _LIBCPP_NO_EXCEPTIONS
73 try
75 #endif // _LIBCPP_NO_EXCEPTIONS
76 p = ::operator new(size);
77 #ifndef _LIBCPP_NO_EXCEPTIONS
79 catch (...)
82 #endif // _LIBCPP_NO_EXCEPTIONS
83 return p;
86 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
87 void*
88 operator new[](size_t size)
89 #if !__has_feature(cxx_noexcept)
90 throw(std::bad_alloc)
91 #endif
93 return ::operator new(size);
96 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
97 void*
98 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
100 void* p = 0;
101 #ifndef _LIBCPP_NO_EXCEPTIONS
104 #endif // _LIBCPP_NO_EXCEPTIONS
105 p = ::operator new[](size);
106 #ifndef _LIBCPP_NO_EXCEPTIONS
108 catch (...)
111 #endif // _LIBCPP_NO_EXCEPTIONS
112 return p;
115 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
116 void
117 operator delete(void* ptr) _NOEXCEPT
119 if (ptr)
120 ::free(ptr);
123 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
124 void
125 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
127 ::operator delete(ptr);
130 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
131 void
132 operator delete(void* ptr, size_t) _NOEXCEPT
134 ::operator delete(ptr);
137 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
138 void
139 operator delete[] (void* ptr) _NOEXCEPT
141 ::operator delete(ptr);
144 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
145 void
146 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
148 ::operator delete[](ptr);
151 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
152 void
153 operator delete[] (void* ptr, size_t) _NOEXCEPT
155 ::operator delete[](ptr);
158 #endif // !__GLIBCXX__
160 namespace std
163 #ifndef __GLIBCXX__
164 const nothrow_t nothrow = {};
165 #endif
167 #ifndef _LIBCPPABI_VERSION
169 #ifndef __GLIBCXX__
171 new_handler
172 set_new_handler(new_handler handler) _NOEXCEPT
174 return __sync_lock_test_and_set(&__new_handler, handler);
177 new_handler
178 get_new_handler() _NOEXCEPT
180 return __sync_fetch_and_add(&__new_handler, nullptr);
183 #endif // !__GLIBCXX__
185 #ifndef LIBCXXRT
187 bad_alloc::bad_alloc() _NOEXCEPT
191 #ifndef __GLIBCXX__
193 bad_alloc::~bad_alloc() _NOEXCEPT
197 const char*
198 bad_alloc::what() const _NOEXCEPT
200 return "std::bad_alloc";
203 #endif // !__GLIBCXX__
205 bad_array_new_length::bad_array_new_length() _NOEXCEPT
209 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
213 const char*
214 bad_array_new_length::what() const _NOEXCEPT
216 return "bad_array_new_length";
219 #endif //LIBCXXRT
221 const char*
222 bad_array_length::what() const _NOEXCEPT
224 return "bad_array_length";
227 bad_array_length::bad_array_length() _NOEXCEPT
231 bad_array_length::~bad_array_length() _NOEXCEPT
235 #endif // _LIBCPPABI_VERSION
237 #ifndef LIBSTDCXX
239 void
240 __throw_bad_alloc()
242 #ifndef _LIBCPP_NO_EXCEPTIONS
243 throw bad_alloc();
244 #endif
247 #endif // !LIBSTDCXX
249 } // std