| 1 |
// -*- C++ -*- |
| 2 |
//===----------------------------------------------------------------------===// |
| 3 |
// |
| 4 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 |
// See https://llvm.org/LICENSE.txt for license information. |
| 6 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 |
// |
| 8 |
//===----------------------------------------------------------------------===// |
| 9 |
|
| 10 |
#ifndef _LIBCPP___CXX03___MEMORY_ALLOCATOR_H |
| 11 |
#define _LIBCPP___CXX03___MEMORY_ALLOCATOR_H |
| 12 |
|
| 13 |
#include <__cxx03/__config> |
| 14 |
#include <__cxx03/__memory/addressof.h> |
| 15 |
#include <__cxx03/__memory/allocate_at_least.h> |
| 16 |
#include <__cxx03/__memory/allocator_traits.h> |
| 17 |
#include <__cxx03/__type_traits/is_const.h> |
| 18 |
#include <__cxx03/__type_traits/is_constant_evaluated.h> |
| 19 |
#include <__cxx03/__type_traits/is_same.h> |
| 20 |
#include <__cxx03/__type_traits/is_void.h> |
| 21 |
#include <__cxx03/__type_traits/is_volatile.h> |
| 22 |
#include <__cxx03/__utility/forward.h> |
| 23 |
#include <__cxx03/cstddef> |
| 24 |
#include <__cxx03/new> |
| 25 |
|
| 26 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 27 |
# pragma GCC system_header |
| 28 |
#endif |
| 29 |
|
| 30 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 31 |
|
| 32 |
template <class _Tp> |
| 33 |
class allocator; |
| 34 |
|
| 35 |
template <> |
| 36 |
class _LIBCPP_TEMPLATE_VIS allocator<void> { |
| 37 |
public: |
| 38 |
typedef void* pointer; |
| 39 |
typedef const void* const_pointer; |
| 40 |
typedef void value_type; |
| 41 |
|
| 42 |
template <class _Up> |
| 43 |
struct rebind { |
| 44 |
typedef allocator<_Up> other; |
| 45 |
}; |
| 46 |
}; |
| 47 |
|
| 48 |
// TODO(LLVM 20): Remove the escape hatch |
| 49 |
#ifdef _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST |
| 50 |
template <> |
| 51 |
class _LIBCPP_TEMPLATE_VIS allocator<const void> { |
| 52 |
public: |
| 53 |
typedef const void* pointer; |
| 54 |
typedef const void* const_pointer; |
| 55 |
typedef const void value_type; |
| 56 |
|
| 57 |
template <class _Up> |
| 58 |
struct rebind { |
| 59 |
typedef allocator<_Up> other; |
| 60 |
}; |
| 61 |
}; |
| 62 |
#endif // _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST |
| 63 |
|
| 64 |
// This class provides a non-trivial default constructor to the class that derives from it |
| 65 |
// if the condition is satisfied. |
| 66 |
// |
| 67 |
// The second template parameter exists to allow giving a unique type to __non_trivial_if, |
| 68 |
// which makes it possible to avoid breaking the ABI when making this a base class of an |
| 69 |
// existing class. Without that, imagine we have classes D1 and D2, both of which used to |
| 70 |
// have no base classes, but which now derive from __non_trivial_if. The layout of a class |
| 71 |
// that inherits from both D1 and D2 will change because the two __non_trivial_if base |
| 72 |
// classes are not allowed to share the same address. |
| 73 |
// |
| 74 |
// By making those __non_trivial_if base classes unique, we work around this problem and |
| 75 |
// it is safe to start deriving from __non_trivial_if in existing classes. |
| 76 |
template <bool _Cond, class _Unique> |
| 77 |
struct __non_trivial_if {}; |
| 78 |
|
| 79 |
template <class _Unique> |
| 80 |
struct __non_trivial_if<true, _Unique> { |
| 81 |
_LIBCPP_HIDE_FROM_ABI __non_trivial_if() _NOEXCEPT {} |
| 82 |
}; |
| 83 |
|
| 84 |
// allocator |
| 85 |
// |
| 86 |
// Note: For ABI compatibility between C++20 and previous standards, we make |
| 87 |
// allocator<void> trivial in C++20. |
| 88 |
|
| 89 |
template <class _Tp> |
| 90 |
class _LIBCPP_TEMPLATE_VIS allocator : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> > { |
| 91 |
static_assert(!is_const<_Tp>::value, "std::allocator does not support const types"); |
| 92 |
static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types"); |
| 93 |
|
| 94 |
public: |
| 95 |
typedef size_t size_type; |
| 96 |
typedef ptrdiff_t difference_type; |
| 97 |
typedef _Tp value_type; |
| 98 |
typedef true_type propagate_on_container_move_assignment; |
| 99 |
typedef true_type is_always_equal; |
| 100 |
|
| 101 |
_LIBCPP_HIDE_FROM_ABI allocator() _NOEXCEPT = default; |
| 102 |
|
| 103 |
template <class _Up> |
| 104 |
_LIBCPP_HIDE_FROM_ABI allocator(const allocator<_Up>&) _NOEXCEPT {} |
| 105 |
|
| 106 |
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _Tp* allocate(size_t __n) { |
| 107 |
if (__n > allocator_traits<allocator>::max_size(*this)) |
| 108 |
__throw_bad_array_new_length(); |
| 109 |
if (__libcpp_is_constant_evaluated()) { |
| 110 |
return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); |
| 111 |
} else { |
| 112 |
return static_cast<_Tp*>(std::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp))); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
_LIBCPP_HIDE_FROM_ABI void deallocate(_Tp* __p, size_t __n) _NOEXCEPT { |
| 117 |
if (__libcpp_is_constant_evaluated()) { |
| 118 |
::operator delete(__p); |
| 119 |
} else { |
| 120 |
std::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// C++20 Removed members |
| 125 |
typedef _Tp* pointer; |
| 126 |
typedef const _Tp* const_pointer; |
| 127 |
typedef _Tp& reference; |
| 128 |
typedef const _Tp& const_reference; |
| 129 |
|
| 130 |
template <class _Up> |
| 131 |
struct rebind { |
| 132 |
typedef allocator<_Up> other; |
| 133 |
}; |
| 134 |
|
| 135 |
_LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT { return std::addressof(__x); } |
| 136 |
_LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT { return std::addressof(__x); } |
| 137 |
|
| 138 |
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _Tp* allocate(size_t __n, const void*) { return allocate(__n); } |
| 139 |
|
| 140 |
_LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return size_type(~0) / sizeof(_Tp); } |
| 141 |
|
| 142 |
template <class _Up, class... _Args> |
| 143 |
_LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) { |
| 144 |
::new ((void*)__p) _Up(std::forward<_Args>(__args)...); |
| 145 |
} |
| 146 |
|
| 147 |
_LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); } |
| 148 |
}; |
| 149 |
|
| 150 |
template <class _Tp, class _Up> |
| 151 |
inline _LIBCPP_HIDE_FROM_ABI bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT { |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
template <class _Tp, class _Up> |
| 156 |
inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
_LIBCPP_END_NAMESPACE_STD |
| 161 |
|
| 162 |
#endif // _LIBCPP___CXX03___MEMORY_ALLOCATOR_H |