[rtsan] Remove mkfifoat interceptor (#116997)
[llvm-project.git] / libcxx / include / __memory / allocator.h
blobddb4179940b8b19c78f30d422c168abb884a3963
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___MEMORY_ALLOCATOR_H
11 #define _LIBCPP___MEMORY_ALLOCATOR_H
13 #include <__config>
14 #include <__cstddef/ptrdiff_t.h>
15 #include <__memory/addressof.h>
16 #include <__memory/allocate_at_least.h>
17 #include <__memory/allocator_traits.h>
18 #include <__type_traits/is_const.h>
19 #include <__type_traits/is_constant_evaluated.h>
20 #include <__type_traits/is_same.h>
21 #include <__type_traits/is_void.h>
22 #include <__type_traits/is_volatile.h>
23 #include <__utility/forward.h>
24 #include <new>
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 # pragma GCC system_header
28 #endif
30 _LIBCPP_BEGIN_NAMESPACE_STD
32 template <class _Tp>
33 class allocator;
35 #if _LIBCPP_STD_VER <= 17
36 // These specializations shouldn't be marked _LIBCPP_DEPRECATED_IN_CXX17.
37 // Specializing allocator<void> is deprecated, but not using it.
38 template <>
39 class _LIBCPP_TEMPLATE_VIS allocator<void> {
40 public:
41 _LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer;
42 _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
43 _LIBCPP_DEPRECATED_IN_CXX17 typedef void value_type;
45 template <class _Up>
46 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
47 typedef allocator<_Up> other;
50 #endif // _LIBCPP_STD_VER <= 17
52 // This class provides a non-trivial default constructor to the class that derives from it
53 // if the condition is satisfied.
55 // The second template parameter exists to allow giving a unique type to __non_trivial_if,
56 // which makes it possible to avoid breaking the ABI when making this a base class of an
57 // existing class. Without that, imagine we have classes D1 and D2, both of which used to
58 // have no base classes, but which now derive from __non_trivial_if. The layout of a class
59 // that inherits from both D1 and D2 will change because the two __non_trivial_if base
60 // classes are not allowed to share the same address.
62 // By making those __non_trivial_if base classes unique, we work around this problem and
63 // it is safe to start deriving from __non_trivial_if in existing classes.
64 template <bool _Cond, class _Unique>
65 struct __non_trivial_if {};
67 template <class _Unique>
68 struct __non_trivial_if<true, _Unique> {
69 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT {}
72 // allocator
74 // Note: For ABI compatibility between C++20 and previous standards, we make
75 // allocator<void> trivial in C++20.
77 template <class _Tp>
78 class _LIBCPP_TEMPLATE_VIS allocator : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> > {
79 static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
80 static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
82 public:
83 typedef size_t size_type;
84 typedef ptrdiff_t difference_type;
85 typedef _Tp value_type;
86 typedef true_type propagate_on_container_move_assignment;
87 #if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)
88 _LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;
89 #endif
91 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
93 template <class _Up>
94 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}
96 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* allocate(size_t __n) {
97 static_assert(sizeof(_Tp) >= 0, "cannot allocate memory for an incomplete type");
98 if (__n > allocator_traits<allocator>::max_size(*this))
99 __throw_bad_array_new_length();
100 if (__libcpp_is_constant_evaluated()) {
101 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
102 } else {
103 return static_cast<_Tp*>(std::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
107 #if _LIBCPP_STD_VER >= 23
108 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<_Tp*> allocate_at_least(size_t __n) {
109 static_assert(sizeof(_Tp) >= 0, "cannot allocate memory for an incomplete type");
110 return {allocate(__n), __n};
112 #endif
114 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
115 if (__libcpp_is_constant_evaluated()) {
116 ::operator delete(__p);
117 } else {
118 std::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
122 // C++20 Removed members
123 #if _LIBCPP_STD_VER <= 17
124 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
125 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
126 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
127 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
129 template <class _Up>
130 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
131 typedef allocator<_Up> other;
134 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
135 return std::addressof(__x);
137 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {
138 return std::addressof(__x);
141 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 _Tp* allocate(size_t __n, const void*) {
142 return allocate(__n);
145 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
146 return size_type(~0) / sizeof(_Tp);
149 template <class _Up, class... _Args>
150 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {
151 ::new ((void*)__p) _Up(std::forward<_Args>(__args)...);
154 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }
155 #endif
158 template <class _Tp, class _Up>
159 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
160 operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
161 return true;
164 #if _LIBCPP_STD_VER <= 17
166 template <class _Tp, class _Up>
167 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
168 return false;
171 #endif
173 _LIBCPP_END_NAMESPACE_STD
175 #endif // _LIBCPP___MEMORY_ALLOCATOR_H