allocator.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Allocators -*- C++ -*-
  2. // Copyright (C) 2001-2018 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. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  76. template<typename _Up>
  77. void
  78. destroy(_Up* __p) { __p->~_Up(); }
  79. #endif
  80. };
  81. /**
  82. * @brief The @a standard allocator, as per [20.4].
  83. *
  84. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
  85. * for further details.
  86. *
  87. * @tparam _Tp Type of allocated object.
  88. */
  89. template<typename _Tp>
  90. class allocator : public __allocator_base<_Tp>
  91. {
  92. public:
  93. typedef size_t size_type;
  94. typedef ptrdiff_t difference_type;
  95. typedef _Tp* pointer;
  96. typedef const _Tp* const_pointer;
  97. typedef _Tp& reference;
  98. typedef const _Tp& const_reference;
  99. typedef _Tp value_type;
  100. template<typename _Tp1>
  101. struct rebind
  102. { typedef allocator<_Tp1> other; };
  103. #if __cplusplus >= 201103L
  104. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  105. // 2103. std::allocator propagate_on_container_move_assignment
  106. typedef true_type propagate_on_container_move_assignment;
  107. typedef true_type is_always_equal;
  108. #endif
  109. allocator() throw() { }
  110. allocator(const allocator& __a) throw()
  111. : __allocator_base<_Tp>(__a) { }
  112. template<typename _Tp1>
  113. allocator(const allocator<_Tp1>&) throw() { }
  114. ~allocator() throw() { }
  115. // Inherit everything else.
  116. };
  117. template<typename _T1, typename _T2>
  118. inline bool
  119. operator==(const allocator<_T1>&, const allocator<_T2>&)
  120. _GLIBCXX_USE_NOEXCEPT
  121. { return true; }
  122. template<typename _Tp>
  123. inline bool
  124. operator==(const allocator<_Tp>&, const allocator<_Tp>&)
  125. _GLIBCXX_USE_NOEXCEPT
  126. { return true; }
  127. template<typename _T1, typename _T2>
  128. inline bool
  129. operator!=(const allocator<_T1>&, const allocator<_T2>&)
  130. _GLIBCXX_USE_NOEXCEPT
  131. { return false; }
  132. template<typename _Tp>
  133. inline bool
  134. operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
  135. _GLIBCXX_USE_NOEXCEPT
  136. { return false; }
  137. // Invalid allocator<cv T> partial specializations.
  138. // allocator_traits::rebind_alloc can be used to form a valid allocator type.
  139. template<typename _Tp>
  140. class allocator<const _Tp>
  141. {
  142. public:
  143. typedef _Tp value_type;
  144. template<typename _Up> allocator(const allocator<_Up>&) { }
  145. };
  146. template<typename _Tp>
  147. class allocator<volatile _Tp>
  148. {
  149. public:
  150. typedef _Tp value_type;
  151. template<typename _Up> allocator(const allocator<_Up>&) { }
  152. };
  153. template<typename _Tp>
  154. class allocator<const volatile _Tp>
  155. {
  156. public:
  157. typedef _Tp value_type;
  158. template<typename _Up> allocator(const allocator<_Up>&) { }
  159. };
  160. /// @} group allocator
  161. // Inhibit implicit instantiations for required instantiations,
  162. // which are defined via explicit instantiations elsewhere.
  163. #if _GLIBCXX_EXTERN_TEMPLATE
  164. extern template class allocator<char>;
  165. extern template class allocator<wchar_t>;
  166. #endif
  167. // Undefine.
  168. #undef __allocator_base
  169. // To implement Option 3 of DR 431.
  170. template<typename _Alloc, bool = __is_empty(_Alloc)>
  171. struct __alloc_swap
  172. { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
  173. template<typename _Alloc>
  174. struct __alloc_swap<_Alloc, false>
  175. {
  176. static void
  177. _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
  178. {
  179. // Precondition: swappable allocators.
  180. if (__one != __two)
  181. swap(__one, __two);
  182. }
  183. };
  184. // Optimize for stateless allocators.
  185. template<typename _Alloc, bool = __is_empty(_Alloc)>
  186. struct __alloc_neq
  187. {
  188. static bool
  189. _S_do_it(const _Alloc&, const _Alloc&)
  190. { return false; }
  191. };
  192. template<typename _Alloc>
  193. struct __alloc_neq<_Alloc, false>
  194. {
  195. static bool
  196. _S_do_it(const _Alloc& __one, const _Alloc& __two)
  197. { return __one != __two; }
  198. };
  199. #if __cplusplus >= 201103L
  200. template<typename _Tp, bool
  201. = __or_<is_copy_constructible<typename _Tp::value_type>,
  202. is_nothrow_move_constructible<typename _Tp::value_type>>::value>
  203. struct __shrink_to_fit_aux
  204. { static bool _S_do_it(_Tp&) noexcept { return false; } };
  205. template<typename _Tp>
  206. struct __shrink_to_fit_aux<_Tp, true>
  207. {
  208. static bool
  209. _S_do_it(_Tp& __c) noexcept
  210. {
  211. #if __cpp_exceptions
  212. try
  213. {
  214. _Tp(__make_move_if_noexcept_iterator(__c.begin()),
  215. __make_move_if_noexcept_iterator(__c.end()),
  216. __c.get_allocator()).swap(__c);
  217. return true;
  218. }
  219. catch(...)
  220. { return false; }
  221. #else
  222. return false;
  223. #endif
  224. }
  225. };
  226. #endif
  227. _GLIBCXX_END_NAMESPACE_VERSION
  228. } // namespace std
  229. #endif