Path: blob/main/system/lib/libcxx/src/new.cpp
6175 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "include/overridable_function.h"9#include <__assert>10#include <__memory/aligned_alloc.h>11#include <cstddef>12#include <cstdlib>13#include <new>1415#if !defined(__GLIBCXX__) && !defined(_LIBCPP_ABI_VCRUNTIME)1617// The code below is copied as-is into libc++abi's libcxxabi/src/stdlib_new_delete.cpp18// file. The version in this file is the canonical one.1920inline void __throw_bad_alloc_shim() { std::__throw_bad_alloc(); }2122# define _LIBCPP_ASSERT_SHIM(expr, str) _LIBCPP_ASSERT(expr, str)2324// ------------------ BEGIN COPY ------------------25// Implement all new and delete operators as weak definitions26// in this shared library, so that they can be overridden by programs27// that define non-weak copies of the functions.2829static void* operator_new_impl(std::size_t size) {30if (size == 0)31size = 1;32void* p;33while ((p = std::malloc(size)) == nullptr) {34// If malloc fails and there is a new_handler,35// call it to try free up memory.36std::new_handler nh = std::get_new_handler();37if (nh)38nh();39else40break;41}42return p;43}4445_LIBCPP_OVERRIDABLE_FUNCTION(void*, operator new, (std::size_t size)) _THROW_BAD_ALLOC {46void* p = operator_new_impl(size);47if (p == nullptr)48__throw_bad_alloc_shim();49return p;50}5152_LIBCPP_WEAK void* operator new(size_t size, const std::nothrow_t&) noexcept {53# if !_LIBCPP_HAS_EXCEPTIONS54# if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION55_LIBCPP_ASSERT_SHIM(56(!std::__is_function_overridden < void*(std::size_t), &operator new>()),57"libc++ was configured with exceptions disabled and `operator new(size_t)` has been overridden, "58"but `operator new(size_t, nothrow_t)` has not been overridden. This is problematic because "59"`operator new(size_t, nothrow_t)` must call `operator new(size_t)`, which will terminate in case "60"it fails to allocate, making it impossible for `operator new(size_t, nothrow_t)` to fulfill its "61"contract (since it should return nullptr upon failure). Please make sure you override "62"`operator new(size_t, nothrow_t)` as well.");63# endif6465return operator_new_impl(size);66# else67void* p = nullptr;68try {69p = ::operator new(size);70} catch (...) {71}72return p;73# endif74}7576_LIBCPP_OVERRIDABLE_FUNCTION(void*, operator new[], (size_t size)) _THROW_BAD_ALLOC { return ::operator new(size); }7778_LIBCPP_WEAK void* operator new[](size_t size, const std::nothrow_t&) noexcept {79# if !_LIBCPP_HAS_EXCEPTIONS80# if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION81_LIBCPP_ASSERT_SHIM(82(!std::__is_function_overridden < void*(std::size_t), &operator new[]>()),83"libc++ was configured with exceptions disabled and `operator new[](size_t)` has been overridden, "84"but `operator new[](size_t, nothrow_t)` has not been overridden. This is problematic because "85"`operator new[](size_t, nothrow_t)` must call `operator new[](size_t)`, which will terminate in case "86"it fails to allocate, making it impossible for `operator new[](size_t, nothrow_t)` to fulfill its "87"contract (since it should return nullptr upon failure). Please make sure you override "88"`operator new[](size_t, nothrow_t)` as well.");89# endif9091return operator_new_impl(size);92# else93void* p = nullptr;94try {95p = ::operator new[](size);96} catch (...) {97}98return p;99# endif100}101102_LIBCPP_WEAK void operator delete(void* ptr) noexcept { std::free(ptr); }103104_LIBCPP_WEAK void operator delete(void* ptr, const std::nothrow_t&) noexcept { ::operator delete(ptr); }105106_LIBCPP_WEAK void operator delete(void* ptr, size_t) noexcept { ::operator delete(ptr); }107108_LIBCPP_WEAK void operator delete[](void* ptr) noexcept { ::operator delete(ptr); }109110_LIBCPP_WEAK void operator delete[](void* ptr, const std::nothrow_t&) noexcept { ::operator delete[](ptr); }111112_LIBCPP_WEAK void operator delete[](void* ptr, size_t) noexcept { ::operator delete[](ptr); }113114# if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION115116static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignment) {117if (size == 0)118size = 1;119if (static_cast<size_t>(alignment) < sizeof(void*))120alignment = std::align_val_t(sizeof(void*));121122// Try allocating memory. If allocation fails and there is a new_handler,123// call it to try free up memory, and try again until it succeeds, or until124// the new_handler decides to terminate.125void* p;126while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr) {127std::new_handler nh = std::get_new_handler();128if (nh)129nh();130else131break;132}133return p;134}135136_LIBCPP_OVERRIDABLE_FUNCTION(void*, operator new, (std::size_t size, std::align_val_t alignment)) _THROW_BAD_ALLOC {137void* p = operator_new_aligned_impl(size, alignment);138if (p == nullptr)139__throw_bad_alloc_shim();140return p;141}142143_LIBCPP_WEAK void* operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept {144# if !_LIBCPP_HAS_EXCEPTIONS145# if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION146_LIBCPP_ASSERT_SHIM(147(!std::__is_function_overridden < void*(std::size_t, std::align_val_t), &operator new>()),148"libc++ was configured with exceptions disabled and `operator new(size_t, align_val_t)` has been overridden, "149"but `operator new(size_t, align_val_t, nothrow_t)` has not been overridden. This is problematic because "150"`operator new(size_t, align_val_t, nothrow_t)` must call `operator new(size_t, align_val_t)`, which will "151"terminate in case it fails to allocate, making it impossible for `operator new(size_t, align_val_t, nothrow_t)` "152"to fulfill its contract (since it should return nullptr upon failure). Please make sure you override "153"`operator new(size_t, align_val_t, nothrow_t)` as well.");154# endif155156return operator_new_aligned_impl(size, alignment);157# else158void* p = nullptr;159try {160p = ::operator new(size, alignment);161} catch (...) {162}163return p;164# endif165}166167_LIBCPP_OVERRIDABLE_FUNCTION(void*, operator new[], (size_t size, std::align_val_t alignment)) _THROW_BAD_ALLOC {168return ::operator new(size, alignment);169}170171_LIBCPP_WEAK void* operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept {172# if !_LIBCPP_HAS_EXCEPTIONS173# if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION174_LIBCPP_ASSERT_SHIM(175(!std::__is_function_overridden < void*(std::size_t, std::align_val_t), &operator new[]>()),176"libc++ was configured with exceptions disabled and `operator new[](size_t, align_val_t)` has been overridden, "177"but `operator new[](size_t, align_val_t, nothrow_t)` has not been overridden. This is problematic because "178"`operator new[](size_t, align_val_t, nothrow_t)` must call `operator new[](size_t, align_val_t)`, which will "179"terminate in case it fails to allocate, making it impossible for `operator new[](size_t, align_val_t, "180"nothrow_t)` to fulfill its contract (since it should return nullptr upon failure). Please make sure you "181"override `operator new[](size_t, align_val_t, nothrow_t)` as well.");182# endif183184return operator_new_aligned_impl(size, alignment);185# else186void* p = nullptr;187try {188p = ::operator new[](size, alignment);189} catch (...) {190}191return p;192# endif193}194195_LIBCPP_WEAK void operator delete(void* ptr, std::align_val_t) noexcept { std::__libcpp_aligned_free(ptr); }196197_LIBCPP_WEAK void operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept {198::operator delete(ptr, alignment);199}200201_LIBCPP_WEAK void operator delete(void* ptr, size_t, std::align_val_t alignment) noexcept {202::operator delete(ptr, alignment);203}204205_LIBCPP_WEAK void operator delete[](void* ptr, std::align_val_t alignment) noexcept {206::operator delete(ptr, alignment);207}208209_LIBCPP_WEAK void operator delete[](void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept {210::operator delete[](ptr, alignment);211}212213_LIBCPP_WEAK void operator delete[](void* ptr, size_t, std::align_val_t alignment) noexcept {214::operator delete[](ptr, alignment);215}216217# endif // _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION218// ------------------ END COPY ------------------219220#endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME221222223