xref: /freebsd-12.1/contrib/libc++/src/new.cpp (revision 3df5ecac)
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 #include <stdlib.h>
11 
12 #include "new"
13 
14 #if __APPLE__
15     #include <cxxabi.h>
16     // On Darwin, there are two STL shared libraries and a lower level ABI
17     // shared libray.  The global holding the current new handler is
18     // in the ABI library and named __cxa_new_handler.
19     #define __new_handler __cxxabiapple::__cxa_new_handler
20 #else  // __APPLE__
21     static std::new_handler __new_handler;
22 #endif
23 
24 // Implement all new and delete operators as weak definitions
25 // in this shared library, so that they can be overriden by programs
26 // that define non-weak copies of the functions.
27 
28 __attribute__((__weak__, __visibility__("default")))
29 void *
30 operator new(std::size_t size)
31 #if !__has_feature(cxx_noexcept)
32     throw(std::bad_alloc)
33 #endif
34 {
35     if (size == 0)
36         size = 1;
37     void* p;
38     while ((p = ::malloc(size)) == 0)
39     {
40         // If malloc fails and there is a new_handler,
41         // call it to try free up memory.
42         std::new_handler nh = std::get_new_handler();
43         if (nh)
44             nh();
45         else
46 #ifndef _LIBCPP_NO_EXCEPTIONS
47             throw std::bad_alloc();
48 #else
49             break;
50 #endif
51     }
52     return p;
53 }
54 
55 __attribute__((__weak__, __visibility__("default")))
56 void*
57 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
58 {
59     void* p = 0;
60 #ifndef _LIBCPP_NO_EXCEPTIONS
61     try
62     {
63 #endif  // _LIBCPP_NO_EXCEPTIONS
64         p = ::operator new(size);
65 #ifndef _LIBCPP_NO_EXCEPTIONS
66     }
67     catch (...)
68     {
69     }
70 #endif  // _LIBCPP_NO_EXCEPTIONS
71     return p;
72 }
73 
74 __attribute__((__weak__, __visibility__("default")))
75 void*
76 operator new[](size_t size)
77 #if !__has_feature(cxx_noexcept)
78     throw(std::bad_alloc)
79 #endif
80 {
81     return ::operator new(size);
82 }
83 
84 __attribute__((__weak__, __visibility__("default")))
85 void*
86 operator new[](size_t size, const std::nothrow_t& nothrow) _NOEXCEPT
87 {
88     void* p = 0;
89 #ifndef _LIBCPP_NO_EXCEPTIONS
90     try
91     {
92 #endif  // _LIBCPP_NO_EXCEPTIONS
93         p = ::operator new[](size);
94 #ifndef _LIBCPP_NO_EXCEPTIONS
95     }
96     catch (...)
97     {
98     }
99 #endif  // _LIBCPP_NO_EXCEPTIONS
100     return p;
101 }
102 
103 __attribute__((__weak__, __visibility__("default")))
104 void
105 operator delete(void* ptr) _NOEXCEPT
106 {
107     if (ptr)
108         ::free(ptr);
109 }
110 
111 __attribute__((__weak__, __visibility__("default")))
112 void
113 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
114 {
115     ::operator delete(ptr);
116 }
117 
118 __attribute__((__weak__, __visibility__("default")))
119 void
120 operator delete[] (void* ptr) _NOEXCEPT
121 {
122     ::operator delete (ptr);
123 }
124 
125 __attribute__((__weak__, __visibility__("default")))
126 void
127 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
128 {
129     ::operator delete[](ptr);
130 }
131 
132 namespace std
133 {
134 
135 const nothrow_t nothrow = {};
136 
137 new_handler
138 set_new_handler(new_handler handler) _NOEXCEPT
139 {
140     return __sync_lock_test_and_set(&__new_handler, handler);
141 }
142 
143 new_handler
144 get_new_handler() _NOEXCEPT
145 {
146     return __sync_fetch_and_add(&__new_handler, (new_handler)0);
147 }
148 
149 bad_alloc::bad_alloc() _NOEXCEPT
150 {
151 }
152 
153 bad_alloc::~bad_alloc() _NOEXCEPT
154 {
155 }
156 
157 const char*
158 bad_alloc::what() const _NOEXCEPT
159 {
160     return "std::bad_alloc";
161 }
162 
163 bad_array_new_length::bad_array_new_length() _NOEXCEPT
164 {
165 }
166 
167 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
168 {
169 }
170 
171 const char*
172 bad_array_new_length::what() const _NOEXCEPT
173 {
174     return "bad_array_new_length";
175 }
176 
177 void
178 __throw_bad_alloc()
179 {
180 #ifndef _LIBCPP_NO_EXCEPTIONS
181     throw bad_alloc();
182 #endif
183 }
184 
185 }  // std
186