2010-05-12 03:42:16 +08:00
|
|
|
// -*- C++ -*-
|
|
|
|
//===----------------------------- new ------------------------------------===//
|
|
|
|
//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_NEW
|
|
|
|
#define _LIBCPP_NEW
|
|
|
|
|
|
|
|
/*
|
|
|
|
new synopsis
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
|
|
|
class bad_alloc
|
|
|
|
: public exception
|
|
|
|
{
|
|
|
|
public:
|
2011-05-27 02:23:59 +08:00
|
|
|
bad_alloc() noexcept;
|
|
|
|
bad_alloc(const bad_alloc&) noexcept;
|
|
|
|
bad_alloc& operator=(const bad_alloc&) noexcept;
|
|
|
|
virtual const char* what() const noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
2016-10-14 14:46:30 +08:00
|
|
|
class bad_array_new_length : public bad_alloc // C++14
|
2013-09-11 09:38:42 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
bad_array_new_length() noexcept;
|
|
|
|
};
|
|
|
|
|
2016-10-14 14:46:30 +08:00
|
|
|
enum class align_val_t : size_t {}; // C++17
|
P0722R3: Implement library support for destroying delete
Summary:
This provides the `std::destroying_delete_t` declaration in C++2a and after. (Even when the compiler doesn't support the language feature).
However, the feature test macro `__cpp_lib_destroying_delete` is only defined when we have both language support and C++2a.
Reviewers: ldionne, ckennelly, serge-sans-paille, EricWF
Reviewed By: EricWF
Subscribers: dexonsmith, riccibruno, christof, jwakely, jdoerfert, mclow.lists, ldionne, libcxx-commits
Differential Revision: https://reviews.llvm.org/D55840
llvm-svn: 361572
2019-05-24 07:46:44 +08:00
|
|
|
|
|
|
|
struct destroying_delete_t { // C++20
|
|
|
|
explicit destroying_delete_t() = default;
|
|
|
|
};
|
|
|
|
inline constexpr destroying_delete_t destroying_delete{}; // C++20
|
|
|
|
|
2019-09-26 22:51:10 +08:00
|
|
|
struct nothrow_t { explicit nothrow_t() = default; };
|
2010-05-12 03:42:16 +08:00
|
|
|
extern const nothrow_t nothrow;
|
|
|
|
typedef void (*new_handler)();
|
2011-05-27 02:23:59 +08:00
|
|
|
new_handler set_new_handler(new_handler new_p) noexcept;
|
|
|
|
new_handler get_new_handler() noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-11-23 03:49:03 +08:00
|
|
|
// 21.6.4, pointer optimization barrier
|
|
|
|
template <class T> constexpr T* launder(T* p) noexcept; // C++17
|
2010-05-12 03:42:16 +08:00
|
|
|
} // std
|
|
|
|
|
2021-01-07 19:29:04 +08:00
|
|
|
void* operator new(std::size_t size); // replaceable, nodiscard in C++20
|
|
|
|
void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++20
|
|
|
|
void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
|
2016-10-14 14:46:30 +08:00
|
|
|
void* operator new(std::size_t size, std::align_val_t alignment,
|
2021-01-07 19:29:04 +08:00
|
|
|
const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
|
2011-05-27 02:23:59 +08:00
|
|
|
void operator delete(void* ptr) noexcept; // replaceable
|
2015-02-15 13:18:55 +08:00
|
|
|
void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
|
2016-10-14 14:46:30 +08:00
|
|
|
void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17
|
|
|
|
void operator delete(void* ptr, std::size_t size,
|
|
|
|
std::align_val_t alignment) noexcept; // replaceable, C++17
|
2011-05-27 02:23:59 +08:00
|
|
|
void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
|
2016-10-14 14:46:30 +08:00
|
|
|
void operator delete(void* ptr, std:align_val_t alignment,
|
|
|
|
const std::nothrow_t&) noexcept; // replaceable, C++17
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2021-01-07 19:29:04 +08:00
|
|
|
void* operator new[](std::size_t size); // replaceable, nodiscard in C++20
|
2016-10-14 14:46:30 +08:00
|
|
|
void* operator new[](std::size_t size,
|
2021-01-07 19:29:04 +08:00
|
|
|
std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++20
|
|
|
|
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
|
2016-10-14 14:46:30 +08:00
|
|
|
void* operator new[](std::size_t size, std::align_val_t alignment,
|
2021-01-07 19:29:04 +08:00
|
|
|
const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
|
2011-05-27 02:23:59 +08:00
|
|
|
void operator delete[](void* ptr) noexcept; // replaceable
|
2015-02-15 13:18:55 +08:00
|
|
|
void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
|
2016-10-14 14:46:30 +08:00
|
|
|
void operator delete[](void* ptr,
|
|
|
|
std::align_val_t alignment) noexcept; // replaceable, C++17
|
|
|
|
void operator delete[](void* ptr, std::size_t size,
|
|
|
|
std::align_val_t alignment) noexcept; // replaceable, C++17
|
2011-05-27 02:23:59 +08:00
|
|
|
void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
|
2016-10-14 14:46:30 +08:00
|
|
|
void operator delete[](void* ptr, std::align_val_t alignment,
|
|
|
|
const std::nothrow_t&) noexcept; // replaceable, C++17
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2021-01-07 19:29:04 +08:00
|
|
|
void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++20
|
|
|
|
void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++20
|
2011-05-27 02:23:59 +08:00
|
|
|
void operator delete (void* ptr, void*) noexcept;
|
|
|
|
void operator delete[](void* ptr, void*) noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-11-05 04:01:25 +08:00
|
|
|
#include <__availability>
|
2021-05-19 23:57:04 +08:00
|
|
|
#include <__config>
|
2020-11-13 04:14:33 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2010-05-12 03:42:16 +08:00
|
|
|
#include <exception>
|
2018-03-22 12:42:56 +08:00
|
|
|
#include <type_traits>
|
2018-09-13 03:41:40 +08:00
|
|
|
#include <version>
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2019-03-05 09:57:01 +08:00
|
|
|
#if defined(_LIBCPP_ABI_VCRUNTIME)
|
2017-02-10 16:57:35 +08:00
|
|
|
#include <new.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-18 04:05:10 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2010-05-12 03:42:16 +08:00
|
|
|
#pragma GCC system_header
|
2011-10-18 04:05:10 +08:00
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2018-10-26 01:21:30 +08:00
|
|
|
#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
|
|
|
|
#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
|
|
|
|
defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
|
2018-10-11 08:17:24 +08:00
|
|
|
# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
|
2018-10-26 01:21:30 +08:00
|
|
|
defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
|
2016-10-14 14:46:30 +08:00
|
|
|
# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
|
|
|
|
#endif
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
namespace std // purposefully not using versioning namespace
|
|
|
|
{
|
|
|
|
|
2019-03-05 09:57:01 +08:00
|
|
|
#if !defined(_LIBCPP_ABI_VCRUNTIME)
|
2019-09-26 22:51:10 +08:00
|
|
|
struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
|
2017-02-10 16:57:35 +08:00
|
|
|
extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
class _LIBCPP_EXCEPTION_ABI bad_alloc
|
|
|
|
: public exception
|
|
|
|
{
|
|
|
|
public:
|
2011-05-27 02:23:59 +08:00
|
|
|
bad_alloc() _NOEXCEPT;
|
|
|
|
virtual ~bad_alloc() _NOEXCEPT;
|
|
|
|
virtual const char* what() const _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class _LIBCPP_EXCEPTION_ABI bad_array_new_length
|
|
|
|
: public bad_alloc
|
|
|
|
{
|
|
|
|
public:
|
2011-05-27 02:23:59 +08:00
|
|
|
bad_array_new_length() _NOEXCEPT;
|
|
|
|
virtual ~bad_array_new_length() _NOEXCEPT;
|
|
|
|
virtual const char* what() const _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
2017-02-10 16:57:35 +08:00
|
|
|
typedef void (*new_handler)();
|
|
|
|
_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
|
|
|
|
_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
|
|
|
|
|
2019-03-05 09:57:01 +08:00
|
|
|
#endif // !_LIBCPP_ABI_VCRUNTIME
|
2017-02-10 16:57:35 +08:00
|
|
|
|
2016-08-25 23:09:01 +08:00
|
|
|
_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
|
|
|
|
|
2018-10-11 08:17:24 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
|
2019-03-05 09:57:01 +08:00
|
|
|
!defined(_LIBCPP_ABI_VCRUNTIME)
|
2016-10-14 14:46:30 +08:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
|
|
|
enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
|
|
|
|
#else
|
|
|
|
enum align_val_t { __zero = 0, __max = (size_t)-1 };
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
P0722R3: Implement library support for destroying delete
Summary:
This provides the `std::destroying_delete_t` declaration in C++2a and after. (Even when the compiler doesn't support the language feature).
However, the feature test macro `__cpp_lib_destroying_delete` is only defined when we have both language support and C++2a.
Reviewers: ldionne, ckennelly, serge-sans-paille, EricWF
Reviewed By: EricWF
Subscribers: dexonsmith, riccibruno, christof, jwakely, jdoerfert, mclow.lists, ldionne, libcxx-commits
Differential Revision: https://reviews.llvm.org/D55840
llvm-svn: 361572
2019-05-24 07:46:44 +08:00
|
|
|
#if _LIBCPP_STD_VER > 17
|
|
|
|
// Enable the declaration even if the compiler doesn't support the language
|
|
|
|
// feature.
|
|
|
|
struct destroying_delete_t {
|
|
|
|
explicit destroying_delete_t() = default;
|
|
|
|
};
|
|
|
|
_LIBCPP_INLINE_VAR constexpr destroying_delete_t destroying_delete{};
|
|
|
|
#endif // _LIBCPP_STD_VER > 17
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
} // std
|
|
|
|
|
2017-01-03 07:27:42 +08:00
|
|
|
#if defined(_LIBCPP_CXX03_LANG)
|
2016-10-14 14:46:30 +08:00
|
|
|
#define _THROW_BAD_ALLOC throw(std::bad_alloc)
|
|
|
|
#else
|
|
|
|
#define _THROW_BAD_ALLOC
|
2011-05-27 02:23:59 +08:00
|
|
|
#endif
|
2016-10-14 14:46:30 +08:00
|
|
|
|
2019-03-05 09:57:01 +08:00
|
|
|
#if !defined(_LIBCPP_ABI_VCRUNTIME)
|
2017-02-10 16:57:35 +08:00
|
|
|
|
2017-12-05 07:03:42 +08:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
|
2019-02-26 14:34:42 +08:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
|
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
|
2018-10-11 08:17:24 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
2017-05-05 01:08:54 +08:00
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
|
2015-02-20 14:13:05 +08:00
|
|
|
#endif
|
2011-05-27 02:23:59 +08:00
|
|
|
|
2017-12-05 07:03:42 +08:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
|
2019-02-26 14:34:42 +08:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
|
2016-11-17 06:18:10 +08:00
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
|
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
|
2018-10-11 08:17:24 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
2017-05-05 01:08:54 +08:00
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
|
2015-02-20 14:13:05 +08:00
|
|
|
#endif
|
2013-08-13 02:38:34 +08:00
|
|
|
|
2018-10-11 08:17:24 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
|
2017-12-05 07:03:42 +08:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
|
2019-02-26 14:34:42 +08:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
|
2017-07-01 02:50:23 +08:00
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
|
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
|
2018-10-11 08:17:24 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
2017-07-01 02:50:23 +08:00
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
|
2016-10-14 14:46:30 +08:00
|
|
|
#endif
|
|
|
|
|
2017-12-05 07:03:42 +08:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
|
2019-02-26 14:34:42 +08:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
|
2017-07-01 02:50:23 +08:00
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
|
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
|
2018-10-11 08:17:24 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
2017-07-01 02:50:23 +08:00
|
|
|
_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
|
2016-10-14 14:46:30 +08:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-12-05 07:03:42 +08:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
|
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
|
2013-10-05 06:09:00 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2019-03-05 09:57:01 +08:00
|
|
|
#endif // !_LIBCPP_ABI_VCRUNTIME
|
2017-02-10 16:57:35 +08:00
|
|
|
|
2014-06-05 03:54:15 +08:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2018-03-22 12:42:56 +08:00
|
|
|
_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
|
|
|
|
#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
|
|
|
|
return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
|
|
|
|
#else
|
|
|
|
return __align > alignment_of<max_align_t>::value;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-25 21:24:14 +08:00
|
|
|
template <class ..._Args>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
void* __libcpp_operator_new(_Args ...__args) {
|
|
|
|
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
|
|
|
|
return __builtin_operator_new(__args...);
|
2018-03-22 12:42:56 +08:00
|
|
|
#else
|
2020-09-25 21:24:14 +08:00
|
|
|
return ::operator new(__args...);
|
2014-06-05 03:54:15 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-25 21:24:14 +08:00
|
|
|
template <class ..._Args>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __libcpp_operator_delete(_Args ...__args) {
|
|
|
|
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
|
|
|
|
__builtin_operator_delete(__args...);
|
2018-10-26 01:21:30 +08:00
|
|
|
#else
|
2020-09-25 21:24:14 +08:00
|
|
|
::operator delete(__args...);
|
2018-10-26 01:21:30 +08:00
|
|
|
#endif
|
2020-09-25 21:24:14 +08:00
|
|
|
}
|
2018-10-26 01:21:30 +08:00
|
|
|
|
2020-09-25 21:24:14 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void *__libcpp_allocate(size_t __size, size_t __align) {
|
|
|
|
#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
|
|
|
|
if (__is_overaligned_for_new(__align)) {
|
|
|
|
const align_val_t __align_val = static_cast<align_val_t>(__align);
|
|
|
|
return __libcpp_operator_new(__size, __align_val);
|
2020-09-18 00:06:13 +08:00
|
|
|
}
|
2018-10-26 01:21:30 +08:00
|
|
|
#endif
|
|
|
|
|
2020-09-25 21:24:14 +08:00
|
|
|
(void)__align;
|
|
|
|
return __libcpp_operator_new(__size);
|
|
|
|
}
|
2018-10-26 01:21:30 +08:00
|
|
|
|
2020-09-25 21:24:14 +08:00
|
|
|
template <class ..._Args>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
|
2018-10-26 01:21:30 +08:00
|
|
|
#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
|
2020-09-25 21:24:14 +08:00
|
|
|
(void)__size;
|
|
|
|
return __libcpp_operator_delete(__ptr, __args...);
|
2018-10-26 01:21:30 +08:00
|
|
|
#else
|
2020-09-25 21:24:14 +08:00
|
|
|
return __libcpp_operator_delete(__ptr, __size, __args...);
|
2018-10-26 01:21:30 +08:00
|
|
|
#endif
|
2020-09-25 21:24:14 +08:00
|
|
|
}
|
2018-10-26 01:21:30 +08:00
|
|
|
|
2020-09-25 21:24:14 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
|
2020-09-18 00:06:13 +08:00
|
|
|
#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
|
2020-09-25 21:24:14 +08:00
|
|
|
(void)__align;
|
2020-09-18 00:06:13 +08:00
|
|
|
return __do_deallocate_handle_size(__ptr, __size);
|
2018-10-26 01:21:30 +08:00
|
|
|
#else
|
2020-09-18 00:06:13 +08:00
|
|
|
if (__is_overaligned_for_new(__align)) {
|
|
|
|
const align_val_t __align_val = static_cast<align_val_t>(__align);
|
|
|
|
return __do_deallocate_handle_size(__ptr, __size, __align_val);
|
|
|
|
} else {
|
|
|
|
return __do_deallocate_handle_size(__ptr, __size);
|
|
|
|
}
|
2018-10-26 01:21:30 +08:00
|
|
|
#endif
|
2020-09-25 21:24:14 +08:00
|
|
|
}
|
2018-10-26 01:21:30 +08:00
|
|
|
|
2020-09-25 21:24:14 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
|
2020-09-18 00:06:13 +08:00
|
|
|
#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
|
2020-09-25 21:24:14 +08:00
|
|
|
(void)__align;
|
|
|
|
return __libcpp_operator_delete(__ptr);
|
2014-06-05 03:54:15 +08:00
|
|
|
#else
|
2020-09-18 00:06:13 +08:00
|
|
|
if (__is_overaligned_for_new(__align)) {
|
|
|
|
const align_val_t __align_val = static_cast<align_val_t>(__align);
|
2020-09-25 21:24:14 +08:00
|
|
|
return __libcpp_operator_delete(__ptr, __align_val);
|
2020-09-18 00:06:13 +08:00
|
|
|
} else {
|
2020-09-25 21:24:14 +08:00
|
|
|
return __libcpp_operator_delete(__ptr);
|
2020-09-18 00:06:13 +08:00
|
|
|
}
|
2014-06-05 03:54:15 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-13 04:14:33 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
|
|
|
|
// Low-level helpers to call the aligned allocation and deallocation functions
|
|
|
|
// on the target platform. This is used to implement libc++'s own memory
|
|
|
|
// allocation routines -- if you need to allocate memory inside the library,
|
|
|
|
// chances are that you want to use `__libcpp_allocate` instead.
|
|
|
|
//
|
|
|
|
// Returns the allocated memory, or `nullptr` on failure.
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
|
|
|
|
#if defined(_LIBCPP_MSVCRT_LIKE)
|
|
|
|
return ::_aligned_malloc(__size, __alignment);
|
|
|
|
#else
|
|
|
|
void* __result = nullptr;
|
2021-02-27 06:29:06 +08:00
|
|
|
(void)::posix_memalign(&__result, __alignment, __size);
|
2020-11-13 04:14:33 +08:00
|
|
|
// If posix_memalign fails, __result is unmodified so we still return `nullptr`.
|
|
|
|
return __result;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __libcpp_aligned_free(void* __ptr) {
|
|
|
|
#if defined(_LIBCPP_MSVCRT_LIKE)
|
|
|
|
::_aligned_free(__ptr);
|
|
|
|
#else
|
|
|
|
::free(__ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
|
|
|
|
|
|
|
|
|
2017-11-23 03:49:03 +08:00
|
|
|
template <class _Tp>
|
2018-08-04 06:36:53 +08:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 inline
|
2017-11-23 03:49:03 +08:00
|
|
|
_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
|
|
|
|
{
|
|
|
|
static_assert (!(is_function<_Tp>::value), "can't launder functions" );
|
|
|
|
static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
|
|
|
|
#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
|
|
|
|
return __builtin_launder(__p);
|
|
|
|
#else
|
|
|
|
return __p;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if _LIBCPP_STD_VER > 14
|
|
|
|
template <class _Tp>
|
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr _Tp* launder(_Tp* __p) noexcept
|
|
|
|
{
|
2017-11-23 09:25:03 +08:00
|
|
|
return _VSTD::__launder(__p);
|
2017-11-23 03:49:03 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-05 03:54:15 +08:00
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_NEW
|