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