blob: 7609d7637f5a3ee860200ca98bbd59d0c11cd1d6 [file] [log] [blame]
Howard Hinnant4a6e1032011-12-20 20:38:05 +00001//===------------------------ 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
Marshall Clow6c3f6752013-09-11 01:42:02 +000012#define _LIBCPP_BUILDING_NEW
13
Howard Hinnant4a6e1032011-12-20 20:38:05 +000014#include <new>
15#include <cstdlib>
16
Eric Fiselier81b06b32017-01-03 00:16:18 +000017#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
Howard Hinnant4a6e1032011-12-20 20:38:05 +000022/*
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")))
40void *
Eric Fiselier81b06b32017-01-03 00:16:18 +000041operator new(std::size_t size) _THROW_BAD_ALLOC
Howard Hinnant4a6e1032011-12-20 20:38:05 +000042{
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
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000052#ifndef _LIBCXXABI_NO_EXCEPTIONS
Howard Hinnant4a6e1032011-12-20 20:38:05 +000053 throw std::bad_alloc();
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000054#else
55 break;
56#endif
Howard Hinnant4a6e1032011-12-20 20:38:05 +000057 }
58 return p;
59}
60
61/*
62Note: The relationships among these operators is both carefully considered
63and standard in C++11. Please do not change them without fully understanding
64the consequences of doing so. Reference:
65http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2158.html
66*/
67/*
68[new.delete.single]
69
70Calls operator new(size). If the call returns normally, returns the result of
71that call. Otherwise, returns a null pointer.
72*/
73__attribute__((__weak__, __visibility__("default")))
74void*
Eric Fiselier81b06b32017-01-03 00:16:18 +000075operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
Howard Hinnant4a6e1032011-12-20 20:38:05 +000076{
77 void* p = 0;
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000078#ifndef _LIBCXXABI_NO_EXCEPTIONS
Howard Hinnant4a6e1032011-12-20 20:38:05 +000079 try
80 {
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000081#endif
Howard Hinnant4a6e1032011-12-20 20:38:05 +000082 p = ::operator new(size);
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000083#ifndef _LIBCXXABI_NO_EXCEPTIONS
Howard Hinnant4a6e1032011-12-20 20:38:05 +000084 }
85 catch (...)
86 {
87 }
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000088#endif
Howard Hinnant4a6e1032011-12-20 20:38:05 +000089 return p;
90}
91
92/*
93[new.delete.array]
94
95Returns operator new(size).
96*/
97__attribute__((__weak__, __visibility__("default")))
98void*
Eric Fiselier81b06b32017-01-03 00:16:18 +000099operator new[](size_t size) _THROW_BAD_ALLOC
Howard Hinnant4a6e1032011-12-20 20:38:05 +0000100{
101 return ::operator new(size);
102}
103
104/*
105[new.delete.array]
106
107Calls operator new[](size). If the call returns normally, returns the result
108of that call. Otherwise, returns a null pointer.
109*/
110__attribute__((__weak__, __visibility__("default")))
111void*
Eric Fiselier81b06b32017-01-03 00:16:18 +0000112operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
Howard Hinnant4a6e1032011-12-20 20:38:05 +0000113{
114 void* p = 0;
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +0000115#ifndef _LIBCXXABI_NO_EXCEPTIONS
Howard Hinnant4a6e1032011-12-20 20:38:05 +0000116 try
117 {
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +0000118#endif
Howard Hinnant4a6e1032011-12-20 20:38:05 +0000119 p = ::operator new[](size);
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +0000120#ifndef _LIBCXXABI_NO_EXCEPTIONS
Howard Hinnant4a6e1032011-12-20 20:38:05 +0000121 }
122 catch (...)
123 {
124 }
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +0000125#endif
Howard Hinnant4a6e1032011-12-20 20:38:05 +0000126 return p;
127}
128
129/*
130[new.delete.single]
131
132If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the
133earlier call to operator new.
134*/
135__attribute__((__weak__, __visibility__("default")))
136void
Eric Fiselier81b06b32017-01-03 00:16:18 +0000137operator delete(void* ptr) _NOEXCEPT
Howard Hinnant4a6e1032011-12-20 20:38:05 +0000138{
139 if (ptr)
140 std::free(ptr);
141}
142
143/*
144[new.delete.single]
145
146calls operator delete(ptr)
147*/
148__attribute__((__weak__, __visibility__("default")))
149void
Eric Fiselier81b06b32017-01-03 00:16:18 +0000150operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
Howard Hinnant4a6e1032011-12-20 20:38:05 +0000151{
152 ::operator delete(ptr);
153}
154
155/*
156[new.delete.array]
157
158Calls operator delete(ptr)
159*/
160__attribute__((__weak__, __visibility__("default")))
161void
Eric Fiselier81b06b32017-01-03 00:16:18 +0000162operator delete[] (void* ptr) _NOEXCEPT
Howard Hinnant4a6e1032011-12-20 20:38:05 +0000163{
164 ::operator delete(ptr);
165}
166
167/*
168[new.delete.array]
169
170calls operator delete[](ptr)
171*/
172__attribute__((__weak__, __visibility__("default")))
173void
Eric Fiselier81b06b32017-01-03 00:16:18 +0000174operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
Howard Hinnant4a6e1032011-12-20 20:38:05 +0000175{
176 ::operator delete[](ptr);
177}
Howard Hinnant35b2c2a2012-01-24 21:35:18 +0000178
179namespace std
180{
181
182// bad_alloc
183
184bad_alloc::bad_alloc() _NOEXCEPT
185{
186}
187
188bad_alloc::~bad_alloc() _NOEXCEPT
189{
190}
191
192const char*
193bad_alloc::what() const _NOEXCEPT
194{
195 return "std::bad_alloc";
196}
197
198// bad_array_new_length
199
200bad_array_new_length::bad_array_new_length() _NOEXCEPT
201{
202}
203
204bad_array_new_length::~bad_array_new_length() _NOEXCEPT
205{
206}
207
208const char*
209bad_array_new_length::what() const _NOEXCEPT
210{
211 return "bad_array_new_length";
212}
213
Marshall Clow6c3f6752013-09-11 01:42:02 +0000214// bad_array_length
215
Howard Hinnant38a0eeb2013-11-07 17:16:37 +0000216#ifndef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
217
218class _LIBCPP_EXCEPTION_ABI bad_array_length
219 : public bad_alloc
220{
221public:
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
Marshall Clow6c3f6752013-09-11 01:42:02 +0000229bad_array_length::bad_array_length() _NOEXCEPT
230{
231}
232
233bad_array_length::~bad_array_length() _NOEXCEPT
234{
235}
236
237const char*
238bad_array_length::what() const _NOEXCEPT
239{
240 return "bad_array_length";
241}
242
Howard Hinnant35b2c2a2012-01-24 21:35:18 +0000243} // std