allocator.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Allocators -*- C++ -*-
  2. // Copyright (C) 2001-2019 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. * Copyright (c) 1996-1997
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. */
  32. /** @file bits/allocator.h
  33. * This is an internal header file, included by other library headers.
  34. * Do not attempt to use it directly. @headername{memory}
  35. */
  36. #ifndef _ALLOCATOR_H
  37. #define _ALLOCATOR_H 1
  38. #include <bits/c++allocator.h> // Define the base class to std::allocator.
  39. #include <bits/memoryfwd.h>
  40. #if __cplusplus >= 201103L
  41. #include <type_traits>
  42. #endif
  43. #define __cpp_lib_incomplete_container_elements 201505
  44. #if __cplusplus >= 201103L
  45. # define __cpp_lib_allocator_is_always_equal 201411
  46. #endif
  47. namespace std _GLIBCXX_VISIBILITY(default)
  48. {
  49. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  50. /**
  51. * @addtogroup allocators
  52. * @{
  53. */
  54. /// allocator<void> specialization.
  55. template<>
  56. class allocator<void>
  57. {
  58. public:
  59. typedef size_t size_type;
  60. typedef ptrdiff_t difference_type;
  61. typedef void* pointer;
  62. typedef const void* const_pointer;
  63. typedef void value_type;
  64. template<typename _Tp1>
  65. struct rebind
  66. { typedef allocator<_Tp1> other; };
  67. #if __cplusplus >= 201103L
  68. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  69. // 2103. std::allocator propagate_on_container_move_assignment
  70. typedef true_type propagate_on_container_move_assignment;
  71. typedef true_type is_always_equal;
  72. template<typename _Up, typename... _Args>
  73. void
  74. construct(_Up* __p, _Args&&... __args)
  75. noexcept(noexcept(::new((void *)__p)
  76. _Up(std::forward<_Args>(__args)...)))
  77. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  78. template<typename _Up>
  79. void
  80. destroy(_Up* __p)
  81. noexcept(noexcept(__p->~_Up()))
  82. { __p->~_Up(); }
  83. #endif
  84. };
  85. /**
  86. * @brief The @a standard allocator, as per [20.4].
  87. *
  88. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
  89. * for further details.
  90. *
  91. * @tparam _Tp Type of allocated object.
  92. */
  93. template<typename _Tp>
  94. class allocator : public __allocator_base<_Tp>
  95. {
  96. public:
  97. typedef size_t size_type;
  98. typedef ptrdiff_t difference_type;
  99. typedef _Tp* pointer;
  100. typedef const _Tp* const_pointer;
  101. typedef _Tp& reference;
  102. typedef const _Tp& const_reference;
  103. typedef _Tp value_type;
  104. template<typename _Tp1>
  105. struct rebind
  106. { typedef allocator<_Tp1> other; };
  107. #if __cplusplus >= 201103L
  108. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  109. // 2103. std::allocator propagate_on_container_move_assignment
  110. typedef true_type propagate_on_container_move_assignment;
  111. typedef true_type is_always_equal;
  112. #endif
  113. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  114. // 3035. std::allocator's constructors should be constexpr
  115. _GLIBCXX20_CONSTEXPR
  116. allocator() _GLIBCXX_NOTHROW { }
  117. _GLIBCXX20_CONSTEXPR
  118. allocator(const allocator& __a) _GLIBCXX_NOTHROW
  119. : __allocator_base<_Tp>(__a) { }
  120. #if __cplusplus >= 201103L
  121. // Avoid implicit deprecation.
  122. allocator& operator=(const allocator&) = default;
  123. #endif
  124. template<typename _Tp1>
  125. _GLIBCXX20_CONSTEXPR
  126. allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { }
  127. ~allocator() _GLIBCXX_NOTHROW { }
  128. friend bool
  129. operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
  130. { return true; }
  131. friend bool
  132. operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
  133. { return false; }
  134. // Inherit everything else.
  135. };
  136. template<typename _T1, typename _T2>
  137. inline bool
  138. operator==(const allocator<_T1>&, const allocator<_T2>&)
  139. _GLIBCXX_NOTHROW
  140. { return true; }
  141. template<typename _T1, typename _T2>
  142. inline bool
  143. operator!=(const allocator<_T1>&, const allocator<_T2>&)
  144. _GLIBCXX_NOTHROW
  145. { return false; }
  146. // Invalid allocator<cv T> partial specializations.
  147. // allocator_traits::rebind_alloc can be used to form a valid allocator type.
  148. template<typename _Tp>
  149. class allocator<const _Tp>
  150. {
  151. public:
  152. typedef _Tp value_type;
  153. template<typename _Up> allocator(const allocator<_Up>&) { }
  154. };
  155. template<typename _Tp>
  156. class allocator<volatile _Tp>
  157. {
  158. public:
  159. typedef _Tp value_type;
  160. template<typename _Up> allocator(const allocator<_Up>&) { }
  161. };
  162. template<typename _Tp>
  163. class allocator<const volatile _Tp>
  164. {
  165. public:
  166. typedef _Tp value_type;
  167. template<typename _Up> allocator(const allocator<_Up>&) { }
  168. };
  169. /// @} group allocator
  170. // Inhibit implicit instantiations for required instantiations,
  171. // which are defined via explicit instantiations elsewhere.
  172. #if _GLIBCXX_EXTERN_TEMPLATE
  173. extern template class allocator<char>;
  174. extern template class allocator<wchar_t>;
  175. #endif
  176. // Undefine.
  177. #undef __allocator_base
  178. // To implement Option 3 of DR 431.
  179. template<typename _Alloc, bool = __is_empty(_Alloc)>
  180. struct __alloc_swap
  181. { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
  182. template<typename _Alloc>
  183. struct __alloc_swap<_Alloc, false>
  184. {
  185. static void
  186. _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
  187. {
  188. // Precondition: swappable allocators.
  189. if (__one != __two)
  190. swap(__one, __two);
  191. }
  192. };
  193. // Optimize for stateless allocators.
  194. template<typename _Alloc, bool = __is_empty(_Alloc)>
  195. struct __alloc_neq
  196. {
  197. static bool
  198. _S_do_it(const _Alloc&, const _Alloc&)
  199. { return false; }
  200. };
  201. template<typename _Alloc>
  202. struct __alloc_neq<_Alloc, false>
  203. {
  204. static bool
  205. _S_do_it(const _Alloc& __one, const _Alloc& __two)
  206. { return __one != __two; }
  207. };
  208. #if __cplusplus >= 201103L
  209. template<typename _Tp, bool
  210. = __or_<is_copy_constructible<typename _Tp::value_type>,
  211. is_nothrow_move_constructible<typename _Tp::value_type>>::value>
  212. struct __shrink_to_fit_aux
  213. { static bool _S_do_it(_Tp&) noexcept { return false; } };
  214. template<typename _Tp>
  215. struct __shrink_to_fit_aux<_Tp, true>
  216. {
  217. static bool
  218. _S_do_it(_Tp& __c) noexcept
  219. {
  220. #if __cpp_exceptions
  221. try
  222. {
  223. _Tp(__make_move_if_noexcept_iterator(__c.begin()),
  224. __make_move_if_noexcept_iterator(__c.end()),
  225. __c.get_allocator()).swap(__c);
  226. return true;
  227. }
  228. catch(...)
  229. { return false; }
  230. #else
  231. return false;
  232. #endif
  233. }
  234. };
  235. #endif
  236. _GLIBCXX_END_NAMESPACE_VERSION
  237. } // namespace std
  238. #endif