2010-05-12 03:42:16 +08:00
|
|
|
//===--------------------------- new.cpp ----------------------------------===//
|
|
|
|
//
|
2010-05-12 05:36:01 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
2010-11-17 06:09:02 +08:00
|
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
// Source Licenses. See LICENSE.TXT for details.
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-09-11 09:38:42 +08:00
|
|
|
#define _LIBCPP_BUILDING_NEW
|
|
|
|
|
2010-05-25 01:49:41 +08:00
|
|
|
#include <stdlib.h>
|
2010-05-15 04:19:37 +08:00
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
#include "new"
|
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)
|
|
|
|
// nothing todo
|
|
|
|
#elif defined(LIBCXX_BUILDING_LIBCXXABI)
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#elif defined(LIBCXXRT)
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#include "support/runtime/new_handler_fallback.ipp"
|
|
|
|
#elif defined(__GLIBCXX__)
|
|
|
|
// nothing todo
|
|
|
|
#else
|
2017-02-10 17:16:29 +08:00
|
|
|
# if defined(__APPLE__) && !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)
|
|
|
|
# include <cxxabi.h> // FIXME: remove this once buildit is gone.
|
|
|
|
# else
|
|
|
|
# include "support/runtime/new_handler_fallback.ipp"
|
|
|
|
# endif
|
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__
|
2017-02-10 16:57:35 +08:00
|
|
|
const nothrow_t nothrow = {};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LIBSTDCXX
|
|
|
|
|
|
|
|
void
|
|
|
|
__throw_bad_alloc()
|
|
|
|
{
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
throw bad_alloc();
|
|
|
|
#else
|
|
|
|
_VSTD::abort();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !LIBSTDCXX
|
|
|
|
|
|
|
|
} // std
|
|
|
|
|
2017-03-03 03:35:33 +08:00
|
|
|
#if !defined(__GLIBCXX__) && !defined(_LIBCPP_ABI_MICROSOFT) && \
|
|
|
|
!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;
|
|
|
|
while ((p = ::malloc(size)) == 0)
|
|
|
|
{
|
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*
|
2011-05-27 02:23:59 +08:00
|
|
|
operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
|
2010-05-15 04:19:37 +08:00
|
|
|
{
|
|
|
|
void* p = 0;
|
2010-08-12 01:04:31 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
2010-05-15 04:19:37 +08:00
|
|
|
try
|
|
|
|
{
|
2010-08-22 08:03:27 +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 (...)
|
|
|
|
{
|
|
|
|
}
|
2010-08-22 08:03:27 +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*
|
|
|
|
operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
|
2016-10-14 14:46:30 +08:00
|
|
|
{
|
|
|
|
void* p = 0;
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#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 (...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2017-01-20 09:13:49 +08:00
|
|
|
void
|
|
|
|
operator delete(void* ptr) _NOEXCEPT
|
2010-05-15 04:19:37 +08:00
|
|
|
{
|
2017-01-20 09:13:49 +08:00
|
|
|
if (ptr)
|
|
|
|
::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
|
|
|
|
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
|
|
|
|
operator delete(void* ptr, size_t) _NOEXCEPT
|
|
|
|
{
|
|
|
|
::operator delete(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_WEAK
|
|
|
|
void
|
|
|
|
operator delete[] (void* ptr) _NOEXCEPT
|
|
|
|
{
|
|
|
|
::operator delete(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_WEAK
|
|
|
|
void
|
|
|
|
operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
|
|
|
|
{
|
|
|
|
::operator delete[](ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_WEAK
|
|
|
|
void
|
|
|
|
operator delete[] (void* ptr, size_t) _NOEXCEPT
|
|
|
|
{
|
|
|
|
::operator delete[](ptr);
|
|
|
|
}
|
|
|
|
|
2017-01-20 09:47:26 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
|
|
|
|
|
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*));
|
|
|
|
void* p;
|
2017-06-01 06:14:05 +08:00
|
|
|
#if defined(_LIBCPP_MSVCRT_LIKE)
|
2017-01-20 09:13:49 +08:00
|
|
|
while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
|
|
|
|
#else
|
|
|
|
while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// If posix_memalign fails and there is a new_handler,
|
|
|
|
// call it to try free up memory.
|
|
|
|
std::new_handler nh = std::get_new_handler();
|
|
|
|
if (nh)
|
|
|
|
nh();
|
|
|
|
else {
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
throw std::bad_alloc();
|
|
|
|
#else
|
|
|
|
p = nullptr; // posix_memalign doesn't initialize 'p' on failure
|
|
|
|
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*
|
2017-01-20 09:13:49 +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
|
|
|
{
|
|
|
|
void* p = 0;
|
2010-08-12 01:04:31 +08:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
2010-05-15 04:19:37 +08:00
|
|
|
try
|
|
|
|
{
|
2010-08-22 08:03:27 +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 (...)
|
|
|
|
{
|
|
|
|
}
|
2010-08-22 08:03:27 +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*
|
|
|
|
operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
|
|
|
|
{
|
|
|
|
void* p = 0;
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
|
|
|
p = ::operator new[](size, alignment);
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
|
|
|
operator delete(void* ptr, std::align_val_t) _NOEXCEPT
|
|
|
|
{
|
|
|
|
if (ptr)
|
2017-06-01 06:14:05 +08:00
|
|
|
#if defined(_LIBCPP_MSVCRT_LIKE)
|
2017-01-12 14:22:36 +08:00
|
|
|
::_aligned_free(ptr);
|
|
|
|
#else
|
2016-10-14 14:46:30 +08:00
|
|
|
::free(ptr);
|
2017-01-12 14:22:36 +08:00
|
|
|
#endif
|
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
|
|
|
|
operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
|
|
|
|
{
|
|
|
|
::operator delete(ptr, alignment);
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
|
|
|
operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
|
|
|
|
{
|
|
|
|
::operator delete(ptr, alignment);
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
|
|
|
operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
|
|
|
|
{
|
|
|
|
::operator delete(ptr, alignment);
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
|
|
|
operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
|
|
|
|
{
|
|
|
|
::operator delete[](ptr, alignment);
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_WEAK
|
2016-10-14 14:46:30 +08:00
|
|
|
void
|
|
|
|
operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
|
|
|
|
{
|
|
|
|
::operator delete[](ptr, alignment);
|
|
|
|
}
|
|
|
|
|
2017-02-10 16:57:35 +08:00
|
|
|
#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
|
2017-03-03 03:35:33 +08:00
|
|
|
#endif // !__GLIBCXX__ && !_LIBCPP_ABI_MICROSOFT && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
|