2021-11-18 05:25:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
2019-01-19 18:56:40 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-02-12 02:00:39 +08:00
|
|
|
#include <new>
|
2010-05-25 01:49:41 +08:00
|
|
|
#include <stdlib.h>
|
2010-05-15 04:19:37 +08:00
|
|
|
|
2017-09-20 07:18:03 +08:00
|
|
|
#include "include/atomic_support.h"
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-02-10 16:57:35 +08:00
|
|
|
#if defined(_LIBCPP_ABI_MICROSOFT)
|
2019-03-22 02:19:21 +08:00
|
|
|
# if !defined(_LIBCPP_ABI_VCRUNTIME)
|
|
|
|
# include "support/runtime/new_handler_fallback.ipp"
|
|
|
|
# endif
|
2017-02-10 16:57:35 +08:00
|
|
|
#elif defined(LIBCXX_BUILDING_LIBCXXABI)
|
2019-03-22 02:19:21 +08:00
|
|
|
# include <cxxabi.h>
|
2017-02-10 16:57:35 +08:00
|
|
|
#elif defined(LIBCXXRT)
|
2019-03-22 02:19:21 +08:00
|
|
|
# include <cxxabi.h>
|
|
|
|
# include "support/runtime/new_handler_fallback.ipp"
|
2017-02-10 16:57:35 +08:00
|
|
|
#elif defined(__GLIBCXX__)
|
2019-03-22 02:19:21 +08:00
|
|
|
// nothing to do
|
2019-04-18 05:57:49 +08:00
|
|
|
#else
|
2019-04-17 03:26:56 +08:00
|
|
|
# include "support/runtime/new_handler_fallback.ipp"
|
2010-05-15 04:19:37 +08:00
|
|
|
#endif
|
|
|
|
|
2017-02-10 16:57:35 +08:00
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
2013-10-07 06:13:16 +08:00
|
|
|
#ifndef __GLIBCXX__
|
2019-01-31 03:09:41 +08:00
|
|
|
const nothrow_t nothrow{};
|
2017-02-10 16:57:35 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LIBSTDCXX
|
|
|
|
|
|
|
|
void
|
|
|
|
__throw_bad_alloc()
|
|
|
|
{
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
throw bad_alloc();
|
|
|
|
#else
|
|
|
|
_VSTD::abort();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !LIBSTDCXX
|
|
|
|
|
|
|
|
} // std
|
|
|
|
|
2017-10-10 03:25:17 +08:00
|
|
|
#if !defined(__GLIBCXX__) && \
|
2019-03-05 09:57:01 +08:00
|
|
|
!defined(_LIBCPP_ABI_VCRUNTIME) && \
|
2017-03-03 03:35:33 +08:00
|
|
|
!defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
|
2013-10-07 06:13:16 +08:00
|
|
|
|
2010-05-15 04:19:37 +08:00
|
|
|
// Implement all new and delete operators as weak definitions
|
2015-08-20 13:23:16 +08:00
|
|
|
// in this shared library, so that they can be overridden by programs
|
2010-05-15 04:19:37 +08:00
|
|
|
// that define non-weak copies of the functions.
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2010-05-15 04:19:37 +08:00
|
|
|
void *
|
2016-10-14 14:46:30 +08:00
|
|
|
operator new(std::size_t size) _THROW_BAD_ALLOC
|
2010-05-15 04:19:37 +08:00
|
|
|
{
|
|
|
|
if (size == 0)
|
|
|
|
size = 1;
|
|
|
|
void* p;
|
2020-11-25 01:53:53 +08:00
|
|
|
while ((p = ::malloc(size)) == nullptr)
|
2010-05-15 04:19:37 +08:00
|
|
|
{
|
2010-08-22 21:53:14 +08:00
|
|
|
// If malloc fails and there is a new_handler,
|
|
|
|
// call it to try free up memory.
|
2010-12-05 03:56:43 +08:00
|
|
|
std::new_handler nh = std::get_new_handler();
|
2010-12-05 03:54:11 +08:00
|
|
|
if (nh)
|
|
|
|
nh();
|
2010-05-15 04:19:37 +08:00
|
|
|
else
|
2010-08-12 01:04:31 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
2010-05-15 04:19:37 +08:00
|
|
|
throw std::bad_alloc();
|
2010-08-12 01:04:31 +08:00
|
|
|
#else
|
|
|
|
break;
|
|
|
|
#endif
|
2010-05-15 04:19:37 +08:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2010-05-15 04:19:37 +08:00
|
|
|
void*
|
2021-03-02 01:09:45 +08:00
|
|
|
operator new(size_t size, const std::nothrow_t&) noexcept
|
2010-05-15 04:19:37 +08:00
|
|
|
{
|
2020-11-25 01:53:53 +08:00
|
|
|
void* p = nullptr;
|
2010-08-12 01:04:31 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
2010-05-15 04:19:37 +08:00
|
|
|
try
|
|
|
|
{
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
2010-05-15 04:19:37 +08:00
|
|
|
p = ::operator new(size);
|
2010-08-12 01:04:31 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
2010-05-15 04:19:37 +08:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
2010-05-15 04:19:37 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2010-05-15 04:19:37 +08:00
|
|
|
void*
|
2017-01-20 09:13:49 +08:00
|
|
|
operator new[](size_t size) _THROW_BAD_ALLOC
|
|
|
|
{
|
|
|
|
return ::operator new(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_WEAK
|
|
|
|
void*
|
2021-03-02 01:09:45 +08:00
|
|
|
operator new[](size_t size, const std::nothrow_t&) noexcept
|
2016-10-14 14:46:30 +08:00
|
|
|
{
|
2020-11-25 01:53:53 +08:00
|
|
|
void* p = nullptr;
|
2016-10-14 14:46:30 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
try
|
|
|
|
{
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
2017-01-20 09:13:49 +08:00
|
|
|
p = ::operator new[](size);
|
2016-10-14 14:46:30 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
2016-10-14 14:46:30 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2017-01-20 09:13:49 +08:00
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete(void* ptr) noexcept
|
2010-05-15 04:19:37 +08:00
|
|
|
{
|
2020-12-18 02:26:47 +08:00
|
|
|
::free(ptr);
|
2010-05-15 04:19:37 +08:00
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2017-01-20 09:13:49 +08:00
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete(void* ptr, const std::nothrow_t&) noexcept
|
2016-10-14 14:46:30 +08:00
|
|
|
{
|
2017-01-20 09:13:49 +08:00
|
|
|
::operator delete(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_WEAK
|
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete(void* ptr, size_t) noexcept
|
2017-01-20 09:13:49 +08:00
|
|
|
{
|
|
|
|
::operator delete(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_WEAK
|
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete[] (void* ptr) noexcept
|
2017-01-20 09:13:49 +08:00
|
|
|
{
|
|
|
|
::operator delete(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_WEAK
|
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete[] (void* ptr, const std::nothrow_t&) noexcept
|
2017-01-20 09:13:49 +08:00
|
|
|
{
|
|
|
|
::operator delete[](ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_WEAK
|
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete[] (void* ptr, size_t) noexcept
|
2017-01-20 09:13:49 +08:00
|
|
|
{
|
|
|
|
::operator delete[](ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-11 08:17:24 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
|
2017-01-20 09:47:26 +08:00
|
|
|
|
2017-01-20 09:13:49 +08:00
|
|
|
_LIBCPP_WEAK
|
|
|
|
void *
|
|
|
|
operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
|
|
|
|
{
|
|
|
|
if (size == 0)
|
|
|
|
size = 1;
|
|
|
|
if (static_cast<size_t>(alignment) < sizeof(void*))
|
|
|
|
alignment = std::align_val_t(sizeof(void*));
|
2020-11-13 04:14:33 +08:00
|
|
|
|
|
|
|
// Try allocating memory. If allocation fails and there is a new_handler,
|
|
|
|
// call it to try free up memory, and try again until it succeeds, or until
|
|
|
|
// the new_handler decides to terminate.
|
|
|
|
//
|
|
|
|
// If allocation fails and there is no new_handler, we throw bad_alloc
|
|
|
|
// (or return nullptr if exceptions are disabled).
|
2017-01-20 09:13:49 +08:00
|
|
|
void* p;
|
2020-11-13 04:14:33 +08:00
|
|
|
while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr)
|
2017-01-20 09:13:49 +08:00
|
|
|
{
|
|
|
|
std::new_handler nh = std::get_new_handler();
|
|
|
|
if (nh)
|
|
|
|
nh();
|
|
|
|
else {
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
throw std::bad_alloc();
|
|
|
|
#else
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
2016-10-14 14:46:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2010-05-15 04:19:37 +08:00
|
|
|
void*
|
2021-03-02 01:09:45 +08:00
|
|
|
operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
|
2010-05-15 04:19:37 +08:00
|
|
|
{
|
2020-11-25 01:53:53 +08:00
|
|
|
void* p = nullptr;
|
2010-08-12 01:04:31 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
2010-05-15 04:19:37 +08:00
|
|
|
try
|
|
|
|
{
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
2017-01-20 09:13:49 +08:00
|
|
|
p = ::operator new(size, alignment);
|
2010-08-12 01:04:31 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
2010-05-15 04:19:37 +08:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
2010-05-15 04:19:37 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-01-20 09:13:49 +08:00
|
|
|
_LIBCPP_WEAK
|
|
|
|
void*
|
|
|
|
operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
|
|
|
|
{
|
|
|
|
return ::operator new(size, alignment);
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void*
|
2021-03-02 01:09:45 +08:00
|
|
|
operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
|
2016-10-14 14:46:30 +08:00
|
|
|
{
|
2020-11-25 01:53:53 +08:00
|
|
|
void* p = nullptr;
|
2016-10-14 14:46:30 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
try
|
|
|
|
{
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
2016-10-14 14:46:30 +08:00
|
|
|
p = ::operator new[](size, alignment);
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
2016-10-14 14:46:30 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete(void* ptr, std::align_val_t) noexcept
|
2016-10-14 14:46:30 +08:00
|
|
|
{
|
2020-12-18 02:26:47 +08:00
|
|
|
std::__libcpp_aligned_free(ptr);
|
2016-10-14 14:46:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
|
2016-10-14 14:46:30 +08:00
|
|
|
{
|
|
|
|
::operator delete(ptr, alignment);
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete(void* ptr, size_t, std::align_val_t alignment) noexcept
|
2016-10-14 14:46:30 +08:00
|
|
|
{
|
|
|
|
::operator delete(ptr, alignment);
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete[] (void* ptr, std::align_val_t alignment) noexcept
|
2016-10-14 14:46:30 +08:00
|
|
|
{
|
|
|
|
::operator delete(ptr, alignment);
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
|
2016-10-14 14:46:30 +08:00
|
|
|
{
|
|
|
|
::operator delete[](ptr, alignment);
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
2021-03-02 01:09:45 +08:00
|
|
|
operator delete[] (void* ptr, size_t, std::align_val_t alignment) noexcept
|
2016-10-14 14:46:30 +08:00
|
|
|
{
|
|
|
|
::operator delete[](ptr, alignment);
|
|
|
|
}
|
|
|
|
|
2018-10-11 08:17:24 +08:00
|
|
|
#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
|
2019-03-05 09:57:01 +08:00
|
|
|
#endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
|