pool_allocator.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 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. using std::size_t;
  51. using std::ptrdiff_t;
  52. /**
  53. * @brief Base class for __pool_alloc.
  54. *
  55. * Uses various allocators to fulfill underlying requests (and makes as
  56. * few requests as possible when in default high-speed pool mode).
  57. *
  58. * Important implementation properties:
  59. * 0. If globally mandated, then allocate objects from new
  60. * 1. If the clients request an object of size > _S_max_bytes, the resulting
  61. * object will be obtained directly from new
  62. * 2. In all other cases, we allocate an object of size exactly
  63. * _S_round_up(requested_size). Thus the client has enough size
  64. * information that we can return the object to the proper free list
  65. * without permanently losing part of the object.
  66. */
  67. class __pool_alloc_base
  68. {
  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 size_t size_type;
  110. typedef 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 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. 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. template<typename _Tp>
  165. inline bool
  166. operator!=(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
  167. { return false; }
  168. template<typename _Tp>
  169. _Atomic_word
  170. __pool_alloc<_Tp>::_S_force_new;
  171. template<typename _Tp>
  172. _Tp*
  173. __pool_alloc<_Tp>::allocate(size_type __n, const void*)
  174. {
  175. pointer __ret = 0;
  176. if (__builtin_expect(__n != 0, true))
  177. {
  178. if (__n > this->max_size())
  179. std::__throw_bad_alloc();
  180. const size_t __bytes = __n * sizeof(_Tp);
  181. #if __cpp_aligned_new
  182. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  183. {
  184. std::align_val_t __al = std::align_val_t(alignof(_Tp));
  185. return static_cast<_Tp*>(::operator new(__bytes, __al));
  186. }
  187. #endif
  188. // If there is a race through here, assume answer from getenv
  189. // will resolve in same direction. Inspired by techniques
  190. // to efficiently support threading found in basic_string.h.
  191. if (_S_force_new == 0)
  192. {
  193. if (std::getenv("GLIBCXX_FORCE_NEW"))
  194. __atomic_add_dispatch(&_S_force_new, 1);
  195. else
  196. __atomic_add_dispatch(&_S_force_new, -1);
  197. }
  198. if (__bytes > size_t(_S_max_bytes) || _S_force_new > 0)
  199. __ret = static_cast<_Tp*>(::operator new(__bytes));
  200. else
  201. {
  202. _Obj* volatile* __free_list = _M_get_free_list(__bytes);
  203. __scoped_lock sentry(_M_get_mutex());
  204. _Obj* __restrict__ __result = *__free_list;
  205. if (__builtin_expect(__result == 0, 0))
  206. __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
  207. else
  208. {
  209. *__free_list = __result->_M_free_list_link;
  210. __ret = reinterpret_cast<_Tp*>(__result);
  211. }
  212. if (__ret == 0)
  213. std::__throw_bad_alloc();
  214. }
  215. }
  216. return __ret;
  217. }
  218. template<typename _Tp>
  219. void
  220. __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
  221. {
  222. if (__builtin_expect(__n != 0 && __p != 0, true))
  223. {
  224. #if __cpp_aligned_new
  225. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  226. {
  227. ::operator delete(__p, std::align_val_t(alignof(_Tp)));
  228. return;
  229. }
  230. #endif
  231. const size_t __bytes = __n * sizeof(_Tp);
  232. if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new > 0)
  233. ::operator delete(__p);
  234. else
  235. {
  236. _Obj* volatile* __free_list = _M_get_free_list(__bytes);
  237. _Obj* __q = reinterpret_cast<_Obj*>(__p);
  238. __scoped_lock sentry(_M_get_mutex());
  239. __q ->_M_free_list_link = *__free_list;
  240. *__free_list = __q;
  241. }
  242. }
  243. }
  244. _GLIBCXX_END_NAMESPACE_VERSION
  245. } // namespace
  246. #endif