pool_allocator.h 8.8 KB

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