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