pool_allocator.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Allocators -*- C++ -*-
  2. // Copyright (C) 2001-2021 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 ext/pool_allocator.h
  33. * This file is a GNU extension to the Standard C++ Library.
  34. */
  35. #ifndef _POOL_ALLOCATOR_H
  36. #define _POOL_ALLOCATOR_H 1
  37. #include <bits/c++config.h>
  38. #include <cstdlib>
  39. #include <new>
  40. #include <bits/functexcept.h>
  41. #include <ext/atomicity.h>
  42. #include <ext/concurrence.h>
  43. #include <bits/move.h>
  44. #if __cplusplus >= 201103L
  45. #include <type_traits>
  46. #endif
  47. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  48. {
  49. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  50. /**
  51. * @brief Base class for __pool_alloc.
  52. *
  53. * Uses various allocators to fulfill underlying requests (and makes as
  54. * few requests as possible when in default high-speed pool mode).
  55. *
  56. * Important implementation properties:
  57. * 0. If globally mandated, then allocate objects from new
  58. * 1. If the clients request an object of size > _S_max_bytes, the resulting
  59. * object will be obtained directly from new
  60. * 2. In all other cases, we allocate an object of size exactly
  61. * _S_round_up(requested_size). Thus the client has enough size
  62. * information that we can return the object to the proper free list
  63. * without permanently losing part of the object.
  64. */
  65. class __pool_alloc_base
  66. {
  67. typedef std::size_t size_t;
  68. protected:
  69. enum { _S_align = 8 };
  70. enum { _S_max_bytes = 128 };
  71. enum { _S_free_list_size = (size_t)_S_max_bytes / (size_t)_S_align };
  72. union _Obj
  73. {
  74. union _Obj* _M_free_list_link;
  75. char _M_client_data[1]; // The client sees this.
  76. };
  77. static _Obj* volatile _S_free_list[_S_free_list_size];
  78. // Chunk allocation state.
  79. static char* _S_start_free;
  80. static char* _S_end_free;
  81. static size_t _S_heap_size;
  82. size_t
  83. _M_round_up(size_t __bytes)
  84. { return ((__bytes + (size_t)_S_align - 1) & ~((size_t)_S_align - 1)); }
  85. _GLIBCXX_CONST _Obj* volatile*
  86. _M_get_free_list(size_t __bytes) throw ();
  87. __mutex&
  88. _M_get_mutex() throw ();
  89. // Returns an object of size __n, and optionally adds to size __n
  90. // free list.
  91. void*
  92. _M_refill(size_t __n);
  93. // Allocates a chunk for nobjs of size size. nobjs may be reduced
  94. // if it is inconvenient to allocate the requested number.
  95. char*
  96. _M_allocate_chunk(size_t __n, int& __nobjs);
  97. };
  98. /**
  99. * @brief Allocator using a memory pool with a single lock.
  100. * @ingroup allocators
  101. */
  102. template<typename _Tp>
  103. class __pool_alloc : private __pool_alloc_base
  104. {
  105. private:
  106. static _Atomic_word _S_force_new;
  107. public:
  108. typedef std::size_t size_type;
  109. typedef std::ptrdiff_t difference_type;
  110. typedef _Tp* pointer;
  111. typedef const _Tp* const_pointer;
  112. typedef _Tp& reference;
  113. typedef const _Tp& const_reference;
  114. typedef _Tp value_type;
  115. template<typename _Tp1>
  116. struct rebind
  117. { typedef __pool_alloc<_Tp1> other; };
  118. #if __cplusplus >= 201103L
  119. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  120. // 2103. propagate_on_container_move_assignment
  121. typedef std::true_type propagate_on_container_move_assignment;
  122. #endif
  123. __pool_alloc() _GLIBCXX_USE_NOEXCEPT { }
  124. __pool_alloc(const __pool_alloc&) _GLIBCXX_USE_NOEXCEPT { }
  125. template<typename _Tp1>
  126. __pool_alloc(const __pool_alloc<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { }
  127. ~__pool_alloc() _GLIBCXX_USE_NOEXCEPT { }
  128. pointer
  129. address(reference __x) const _GLIBCXX_NOEXCEPT
  130. { return std::__addressof(__x); }
  131. const_pointer
  132. address(const_reference __x) const _GLIBCXX_NOEXCEPT
  133. { return std::__addressof(__x); }
  134. size_type
  135. max_size() const _GLIBCXX_USE_NOEXCEPT
  136. { return std::size_t(-1) / sizeof(_Tp); }
  137. #if __cplusplus >= 201103L
  138. template<typename _Up, typename... _Args>
  139. void
  140. construct(_Up* __p, _Args&&... __args)
  141. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  142. template<typename _Up>
  143. void
  144. destroy(_Up* __p) { __p->~_Up(); }
  145. #else
  146. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  147. // 402. wrong new expression in [some_] allocator::construct
  148. void
  149. construct(pointer __p, const _Tp& __val)
  150. { ::new((void *)__p) _Tp(__val); }
  151. void
  152. destroy(pointer __p) { __p->~_Tp(); }
  153. #endif
  154. _GLIBCXX_NODISCARD pointer
  155. allocate(size_type __n, const void* = 0);
  156. void
  157. deallocate(pointer __p, size_type __n);
  158. };
  159. template<typename _Tp>
  160. inline bool
  161. operator==(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
  162. { return true; }
  163. #if __cpp_impl_three_way_comparison < 201907L
  164. template<typename _Tp>
  165. inline bool
  166. operator!=(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
  167. { return false; }
  168. #endif
  169. template<typename _Tp>
  170. _Atomic_word
  171. __pool_alloc<_Tp>::_S_force_new;
  172. template<typename _Tp>
  173. _GLIBCXX_NODISCARD _Tp*
  174. __pool_alloc<_Tp>::allocate(size_type __n, const void*)
  175. {
  176. using std::size_t;
  177. pointer __ret = 0;
  178. if (__builtin_expect(__n != 0, true))
  179. {
  180. if (__n > this->max_size())
  181. std::__throw_bad_alloc();
  182. const size_t __bytes = __n * sizeof(_Tp);
  183. #if __cpp_aligned_new
  184. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  185. {
  186. std::align_val_t __al = std::align_val_t(alignof(_Tp));
  187. return static_cast<_Tp*>(::operator new(__bytes, __al));
  188. }
  189. #endif
  190. // If there is a race through here, assume answer from getenv
  191. // will resolve in same direction. Inspired by techniques
  192. // to efficiently support threading found in basic_string.h.
  193. if (_S_force_new == 0)
  194. {
  195. if (std::getenv("GLIBCXX_FORCE_NEW"))
  196. __atomic_add_dispatch(&_S_force_new, 1);
  197. else
  198. __atomic_add_dispatch(&_S_force_new, -1);
  199. }
  200. if (__bytes > size_t(_S_max_bytes) || _S_force_new > 0)
  201. __ret = static_cast<_Tp*>(::operator new(__bytes));
  202. else
  203. {
  204. _Obj* volatile* __free_list = _M_get_free_list(__bytes);
  205. __scoped_lock sentry(_M_get_mutex());
  206. _Obj* __restrict__ __result = *__free_list;
  207. if (__builtin_expect(__result == 0, 0))
  208. __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
  209. else
  210. {
  211. *__free_list = __result->_M_free_list_link;
  212. __ret = reinterpret_cast<_Tp*>(__result);
  213. }
  214. if (__ret == 0)
  215. std::__throw_bad_alloc();
  216. }
  217. }
  218. return __ret;
  219. }
  220. template<typename _Tp>
  221. void
  222. __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
  223. {
  224. using std::size_t;
  225. if (__builtin_expect(__n != 0 && __p != 0, true))
  226. {
  227. #if __cpp_aligned_new
  228. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  229. {
  230. ::operator delete(__p, std::align_val_t(alignof(_Tp)));
  231. return;
  232. }
  233. #endif
  234. const size_t __bytes = __n * sizeof(_Tp);
  235. if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new > 0)
  236. ::operator delete(__p);
  237. else
  238. {
  239. _Obj* volatile* __free_list = _M_get_free_list(__bytes);
  240. _Obj* __q = reinterpret_cast<_Obj*>(__p);
  241. __scoped_lock sentry(_M_get_mutex());
  242. __q ->_M_free_list_link = *__free_list;
  243. *__free_list = __q;
  244. }
  245. }
  246. }
  247. _GLIBCXX_END_NAMESPACE_VERSION
  248. } // namespace
  249. #endif