• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===------------------------ cxa_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 
14 #include <new>
15 #include <cstdlib>
16 
17 #if !defined(_THROW_BAD_ALLOC) || !defined(_NOEXCEPT)
18 #error _THROW_BAD_ALLOC and _NOEXCEPT libc++ macros must already be defined \
19        by libc++.
20 #endif
21 
22 /*
23 [new.delete.single]
24 
25 * Executes a loop: Within the loop, the function first attempts to allocate
26   the requested storage. Whether the attempt involves a call to the Standard C
27   library function malloc is unspecified.
28 
29 * Returns a pointer to the allocated storage if the attempt is successful.
30   Otherwise, if the current new_handler (18.6.2.5) is a null pointer value,
31   throws bad_alloc.
32 
33 * Otherwise, the function calls the current new_handler function (18.6.2.3).
34   If the called function returns, the loop repeats.
35 
36 * The loop terminates when an attempt to allocate the requested storage is
37   successful or when a called new_handler function does not return.
38 */
39 __attribute__((__weak__, __visibility__("default")))
40 void *
operator new(std::size_t size)41 operator new(std::size_t size) _THROW_BAD_ALLOC
42 {
43     if (size == 0)
44         size = 1;
45     void* p;
46     while ((p = std::malloc(size)) == 0)
47     {
48         std::new_handler nh = std::get_new_handler();
49         if (nh)
50             nh();
51         else
52 #ifndef _LIBCXXABI_NO_EXCEPTIONS
53             throw std::bad_alloc();
54 #else
55             break;
56 #endif
57     }
58     return p;
59 }
60 
61 /*
62 Note:  The relationships among these operators is both carefully considered
63 and standard in C++11.  Please do not change them without fully understanding
64 the consequences of doing so.  Reference:
65 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2158.html
66 */
67 /*
68 [new.delete.single]
69 
70 Calls operator new(size). If the call returns normally, returns the result of
71 that call. Otherwise, returns a null pointer.
72 */
73 __attribute__((__weak__, __visibility__("default")))
74 void*
operator new(size_t size,const std::nothrow_t &)75 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
76 {
77     void* p = 0;
78 #ifndef _LIBCXXABI_NO_EXCEPTIONS
79     try
80     {
81 #endif
82         p = ::operator new(size);
83 #ifndef _LIBCXXABI_NO_EXCEPTIONS
84     }
85     catch (...)
86     {
87     }
88 #endif
89     return p;
90 }
91 
92 /*
93 [new.delete.array]
94 
95 Returns operator new(size).
96 */
97 __attribute__((__weak__, __visibility__("default")))
98 void*
operator new[](size_t size)99 operator new[](size_t size) _THROW_BAD_ALLOC
100 {
101     return ::operator new(size);
102 }
103 
104 /*
105 [new.delete.array]
106 
107 Calls operator new[](size). If the call returns normally, returns the result
108 of that call. Otherwise, returns a null pointer.
109 */
110 __attribute__((__weak__, __visibility__("default")))
111 void*
operator new[](size_t size,const std::nothrow_t &)112 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
113 {
114     void* p = 0;
115 #ifndef _LIBCXXABI_NO_EXCEPTIONS
116     try
117     {
118 #endif
119         p = ::operator new[](size);
120 #ifndef _LIBCXXABI_NO_EXCEPTIONS
121     }
122     catch (...)
123     {
124     }
125 #endif
126     return p;
127 }
128 
129 /*
130 [new.delete.single]
131 
132 If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the
133 earlier call to operator new.
134 */
135 __attribute__((__weak__, __visibility__("default")))
136 void
operator delete(void * ptr)137 operator delete(void* ptr) _NOEXCEPT
138 {
139     if (ptr)
140         std::free(ptr);
141 }
142 
143 /*
144 [new.delete.single]
145 
146 calls operator delete(ptr)
147 */
148 __attribute__((__weak__, __visibility__("default")))
149 void
operator delete(void * ptr,const std::nothrow_t &)150 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
151 {
152     ::operator delete(ptr);
153 }
154 
155 /*
156 [new.delete.array]
157 
158 Calls operator delete(ptr)
159 */
160 __attribute__((__weak__, __visibility__("default")))
161 void
operator delete[](void * ptr)162 operator delete[] (void* ptr) _NOEXCEPT
163 {
164     ::operator delete(ptr);
165 }
166 
167 /*
168 [new.delete.array]
169 
170 calls operator delete[](ptr)
171 */
172 __attribute__((__weak__, __visibility__("default")))
173 void
operator delete[](void * ptr,const std::nothrow_t &)174 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
175 {
176     ::operator delete[](ptr);
177 }
178 
179 namespace std
180 {
181 
182 //  bad_alloc
183 
bad_alloc()184 bad_alloc::bad_alloc() _NOEXCEPT
185 {
186 }
187 
~bad_alloc()188 bad_alloc::~bad_alloc() _NOEXCEPT
189 {
190 }
191 
192 const char*
what() const193 bad_alloc::what() const _NOEXCEPT
194 {
195     return "std::bad_alloc";
196 }
197 
198 // bad_array_new_length
199 
bad_array_new_length()200 bad_array_new_length::bad_array_new_length() _NOEXCEPT
201 {
202 }
203 
~bad_array_new_length()204 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
205 {
206 }
207 
208 const char*
what() const209 bad_array_new_length::what() const _NOEXCEPT
210 {
211     return "bad_array_new_length";
212 }
213 
214 // bad_array_length
215 
216 #ifndef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
217 
218 class _LIBCPP_EXCEPTION_ABI bad_array_length
219     : public bad_alloc
220 {
221 public:
222     bad_array_length() _NOEXCEPT;
223     virtual ~bad_array_length() _NOEXCEPT;
224     virtual const char* what() const _NOEXCEPT;
225 };
226 
227 #endif  // _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
228 
bad_array_length()229 bad_array_length::bad_array_length() _NOEXCEPT
230 {
231 }
232 
~bad_array_length()233 bad_array_length::~bad_array_length() _NOEXCEPT
234 {
235 }
236 
237 const char*
what() const238 bad_array_length::what() const _NOEXCEPT
239 {
240     return "bad_array_length";
241 }
242 
243 }  // std
244