annotate libcxxabi/src/stdlib_new_delete.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--------------------- stdlib_new_delete.cpp --------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //
anatofuz
parents:
diff changeset
8 // This file implements the new and delete operators.
anatofuz
parents:
diff changeset
9 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
10
anatofuz
parents:
diff changeset
11 #define _LIBCPP_BUILDING_LIBRARY
anatofuz
parents:
diff changeset
12 #include "__cxxabi_config.h"
anatofuz
parents:
diff changeset
13 #include <new>
anatofuz
parents:
diff changeset
14 #include <cstdlib>
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 #if !defined(_THROW_BAD_ALLOC) || !defined(_NOEXCEPT) || !defined(_LIBCXXABI_WEAK)
anatofuz
parents:
diff changeset
17 #error The _THROW_BAD_ALLOC, _NOEXCEPT, and _LIBCXXABI_WEAK libc++ macros must \
anatofuz
parents:
diff changeset
18 already be defined by libc++.
anatofuz
parents:
diff changeset
19 #endif
anatofuz
parents:
diff changeset
20 // Implement all new and delete operators as weak definitions
anatofuz
parents:
diff changeset
21 // in this shared library, so that they can be overridden by programs
anatofuz
parents:
diff changeset
22 // that define non-weak copies of the functions.
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
25 void *
anatofuz
parents:
diff changeset
26 operator new(std::size_t size) _THROW_BAD_ALLOC
anatofuz
parents:
diff changeset
27 {
anatofuz
parents:
diff changeset
28 if (size == 0)
anatofuz
parents:
diff changeset
29 size = 1;
anatofuz
parents:
diff changeset
30 void* p;
anatofuz
parents:
diff changeset
31 while ((p = ::malloc(size)) == 0)
anatofuz
parents:
diff changeset
32 {
anatofuz
parents:
diff changeset
33 // If malloc fails and there is a new_handler,
anatofuz
parents:
diff changeset
34 // call it to try free up memory.
anatofuz
parents:
diff changeset
35 std::new_handler nh = std::get_new_handler();
anatofuz
parents:
diff changeset
36 if (nh)
anatofuz
parents:
diff changeset
37 nh();
anatofuz
parents:
diff changeset
38 else
anatofuz
parents:
diff changeset
39 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
40 throw std::bad_alloc();
anatofuz
parents:
diff changeset
41 #else
anatofuz
parents:
diff changeset
42 break;
anatofuz
parents:
diff changeset
43 #endif
anatofuz
parents:
diff changeset
44 }
anatofuz
parents:
diff changeset
45 return p;
anatofuz
parents:
diff changeset
46 }
anatofuz
parents:
diff changeset
47
anatofuz
parents:
diff changeset
48 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
49 void*
anatofuz
parents:
diff changeset
50 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
anatofuz
parents:
diff changeset
51 {
anatofuz
parents:
diff changeset
52 void* p = 0;
anatofuz
parents:
diff changeset
53 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
54 try
anatofuz
parents:
diff changeset
55 {
anatofuz
parents:
diff changeset
56 #endif // _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
57 p = ::operator new(size);
anatofuz
parents:
diff changeset
58 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
59 }
anatofuz
parents:
diff changeset
60 catch (...)
anatofuz
parents:
diff changeset
61 {
anatofuz
parents:
diff changeset
62 }
anatofuz
parents:
diff changeset
63 #endif // _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
64 return p;
anatofuz
parents:
diff changeset
65 }
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
68 void*
anatofuz
parents:
diff changeset
69 operator new[](size_t size) _THROW_BAD_ALLOC
anatofuz
parents:
diff changeset
70 {
anatofuz
parents:
diff changeset
71 return ::operator new(size);
anatofuz
parents:
diff changeset
72 }
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
75 void*
anatofuz
parents:
diff changeset
76 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
anatofuz
parents:
diff changeset
77 {
anatofuz
parents:
diff changeset
78 void* p = 0;
anatofuz
parents:
diff changeset
79 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
80 try
anatofuz
parents:
diff changeset
81 {
anatofuz
parents:
diff changeset
82 #endif // _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
83 p = ::operator new[](size);
anatofuz
parents:
diff changeset
84 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
85 }
anatofuz
parents:
diff changeset
86 catch (...)
anatofuz
parents:
diff changeset
87 {
anatofuz
parents:
diff changeset
88 }
anatofuz
parents:
diff changeset
89 #endif // _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
90 return p;
anatofuz
parents:
diff changeset
91 }
anatofuz
parents:
diff changeset
92
anatofuz
parents:
diff changeset
93 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
94 void
anatofuz
parents:
diff changeset
95 operator delete(void* ptr) _NOEXCEPT
anatofuz
parents:
diff changeset
96 {
anatofuz
parents:
diff changeset
97 if (ptr)
anatofuz
parents:
diff changeset
98 ::free(ptr);
anatofuz
parents:
diff changeset
99 }
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
102 void
anatofuz
parents:
diff changeset
103 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
anatofuz
parents:
diff changeset
104 {
anatofuz
parents:
diff changeset
105 ::operator delete(ptr);
anatofuz
parents:
diff changeset
106 }
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
109 void
anatofuz
parents:
diff changeset
110 operator delete(void* ptr, size_t) _NOEXCEPT
anatofuz
parents:
diff changeset
111 {
anatofuz
parents:
diff changeset
112 ::operator delete(ptr);
anatofuz
parents:
diff changeset
113 }
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
116 void
anatofuz
parents:
diff changeset
117 operator delete[] (void* ptr) _NOEXCEPT
anatofuz
parents:
diff changeset
118 {
anatofuz
parents:
diff changeset
119 ::operator delete(ptr);
anatofuz
parents:
diff changeset
120 }
anatofuz
parents:
diff changeset
121
anatofuz
parents:
diff changeset
122 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
123 void
anatofuz
parents:
diff changeset
124 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
anatofuz
parents:
diff changeset
125 {
anatofuz
parents:
diff changeset
126 ::operator delete[](ptr);
anatofuz
parents:
diff changeset
127 }
anatofuz
parents:
diff changeset
128
anatofuz
parents:
diff changeset
129 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
130 void
anatofuz
parents:
diff changeset
131 operator delete[] (void* ptr, size_t) _NOEXCEPT
anatofuz
parents:
diff changeset
132 {
anatofuz
parents:
diff changeset
133 ::operator delete[](ptr);
anatofuz
parents:
diff changeset
134 }
anatofuz
parents:
diff changeset
135
anatofuz
parents:
diff changeset
136 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
139 void *
anatofuz
parents:
diff changeset
140 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
anatofuz
parents:
diff changeset
141 {
anatofuz
parents:
diff changeset
142 if (size == 0)
anatofuz
parents:
diff changeset
143 size = 1;
anatofuz
parents:
diff changeset
144 if (static_cast<size_t>(alignment) < sizeof(void*))
anatofuz
parents:
diff changeset
145 alignment = std::align_val_t(sizeof(void*));
anatofuz
parents:
diff changeset
146 void* p;
anatofuz
parents:
diff changeset
147 #if defined(_LIBCPP_WIN32API)
anatofuz
parents:
diff changeset
148 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
anatofuz
parents:
diff changeset
149 #else
anatofuz
parents:
diff changeset
150 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
anatofuz
parents:
diff changeset
151 #endif
anatofuz
parents:
diff changeset
152 {
anatofuz
parents:
diff changeset
153 // If posix_memalign fails and there is a new_handler,
anatofuz
parents:
diff changeset
154 // call it to try free up memory.
anatofuz
parents:
diff changeset
155 std::new_handler nh = std::get_new_handler();
anatofuz
parents:
diff changeset
156 if (nh)
anatofuz
parents:
diff changeset
157 nh();
anatofuz
parents:
diff changeset
158 else {
anatofuz
parents:
diff changeset
159 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
160 throw std::bad_alloc();
anatofuz
parents:
diff changeset
161 #else
anatofuz
parents:
diff changeset
162 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
anatofuz
parents:
diff changeset
163 break;
anatofuz
parents:
diff changeset
164 #endif
anatofuz
parents:
diff changeset
165 }
anatofuz
parents:
diff changeset
166 }
anatofuz
parents:
diff changeset
167 return p;
anatofuz
parents:
diff changeset
168 }
anatofuz
parents:
diff changeset
169
anatofuz
parents:
diff changeset
170 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
171 void*
anatofuz
parents:
diff changeset
172 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
anatofuz
parents:
diff changeset
173 {
anatofuz
parents:
diff changeset
174 void* p = 0;
anatofuz
parents:
diff changeset
175 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
176 try
anatofuz
parents:
diff changeset
177 {
anatofuz
parents:
diff changeset
178 #endif // _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
179 p = ::operator new(size, alignment);
anatofuz
parents:
diff changeset
180 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
181 }
anatofuz
parents:
diff changeset
182 catch (...)
anatofuz
parents:
diff changeset
183 {
anatofuz
parents:
diff changeset
184 }
anatofuz
parents:
diff changeset
185 #endif // _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
186 return p;
anatofuz
parents:
diff changeset
187 }
anatofuz
parents:
diff changeset
188
anatofuz
parents:
diff changeset
189 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
190 void*
anatofuz
parents:
diff changeset
191 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
anatofuz
parents:
diff changeset
192 {
anatofuz
parents:
diff changeset
193 return ::operator new(size, alignment);
anatofuz
parents:
diff changeset
194 }
anatofuz
parents:
diff changeset
195
anatofuz
parents:
diff changeset
196 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
197 void*
anatofuz
parents:
diff changeset
198 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
anatofuz
parents:
diff changeset
199 {
anatofuz
parents:
diff changeset
200 void* p = 0;
anatofuz
parents:
diff changeset
201 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
202 try
anatofuz
parents:
diff changeset
203 {
anatofuz
parents:
diff changeset
204 #endif // _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
205 p = ::operator new[](size, alignment);
anatofuz
parents:
diff changeset
206 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
207 }
anatofuz
parents:
diff changeset
208 catch (...)
anatofuz
parents:
diff changeset
209 {
anatofuz
parents:
diff changeset
210 }
anatofuz
parents:
diff changeset
211 #endif // _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
212 return p;
anatofuz
parents:
diff changeset
213 }
anatofuz
parents:
diff changeset
214
anatofuz
parents:
diff changeset
215 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
216 void
anatofuz
parents:
diff changeset
217 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
anatofuz
parents:
diff changeset
218 {
anatofuz
parents:
diff changeset
219 if (ptr)
anatofuz
parents:
diff changeset
220 #if defined(_LIBCPP_WIN32API)
anatofuz
parents:
diff changeset
221 ::_aligned_free(ptr);
anatofuz
parents:
diff changeset
222 #else
anatofuz
parents:
diff changeset
223 ::free(ptr);
anatofuz
parents:
diff changeset
224 #endif
anatofuz
parents:
diff changeset
225 }
anatofuz
parents:
diff changeset
226
anatofuz
parents:
diff changeset
227 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
228 void
anatofuz
parents:
diff changeset
229 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
anatofuz
parents:
diff changeset
230 {
anatofuz
parents:
diff changeset
231 ::operator delete(ptr, alignment);
anatofuz
parents:
diff changeset
232 }
anatofuz
parents:
diff changeset
233
anatofuz
parents:
diff changeset
234 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
235 void
anatofuz
parents:
diff changeset
236 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
anatofuz
parents:
diff changeset
237 {
anatofuz
parents:
diff changeset
238 ::operator delete(ptr, alignment);
anatofuz
parents:
diff changeset
239 }
anatofuz
parents:
diff changeset
240
anatofuz
parents:
diff changeset
241 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
242 void
anatofuz
parents:
diff changeset
243 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
anatofuz
parents:
diff changeset
244 {
anatofuz
parents:
diff changeset
245 ::operator delete(ptr, alignment);
anatofuz
parents:
diff changeset
246 }
anatofuz
parents:
diff changeset
247
anatofuz
parents:
diff changeset
248 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
249 void
anatofuz
parents:
diff changeset
250 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
anatofuz
parents:
diff changeset
251 {
anatofuz
parents:
diff changeset
252 ::operator delete[](ptr, alignment);
anatofuz
parents:
diff changeset
253 }
anatofuz
parents:
diff changeset
254
anatofuz
parents:
diff changeset
255 _LIBCXXABI_WEAK
anatofuz
parents:
diff changeset
256 void
anatofuz
parents:
diff changeset
257 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
anatofuz
parents:
diff changeset
258 {
anatofuz
parents:
diff changeset
259 ::operator delete[](ptr, alignment);
anatofuz
parents:
diff changeset
260 }
anatofuz
parents:
diff changeset
261
anatofuz
parents:
diff changeset
262 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION