pool_alloc.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. // Copyright (C) 2000, 2001 Stephen Cleary
  2. // Copyright (C) 2010 Paul A. Bristow added Doxygen comments.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org for updates, documentation, and revision history.
  9. #ifndef BOOST_POOL_ALLOC_HPP
  10. #define BOOST_POOL_ALLOC_HPP
  11. /*!
  12. \file
  13. \brief C++ Standard Library compatible pool-based allocators.
  14. \details This header provides two template types -
  15. \ref pool_allocator and \ref fast_pool_allocator -
  16. that can be used for fast and efficient memory allocation
  17. in conjunction with the C++ Standard Library containers.
  18. These types both satisfy the Standard Allocator requirements [20.1.5]
  19. and the additional requirements in [20.1.5/4],
  20. so they can be used with either Standard or user-supplied containers.
  21. In addition, the fast_pool_allocator also provides an additional allocation
  22. and an additional deallocation function:
  23. <table>
  24. <tr><th>Expression</th><th>Return Type</th><th>Semantic Equivalence<th></tr>
  25. <tr><td><tt>PoolAlloc::allocate()</tt></td><td><tt>T *</tt></td><td><tt>PoolAlloc::allocate(1)</tt></tr>
  26. <tr><td><tt>PoolAlloc::deallocate(p)</tt></td><td>void</tt></td><td><tt>PoolAlloc::deallocate(p, 1)</tt></tr>
  27. </table>
  28. The typedef user_allocator publishes the value of the UserAllocator template parameter.
  29. <b>Notes</b>
  30. If the allocation functions run out of memory, they will throw <tt>std::bad_alloc</tt>.
  31. The underlying Pool type used by the allocators is accessible through the Singleton Pool Interface.
  32. The identifying tag used for pool_allocator is pool_allocator_tag,
  33. and the tag used for fast_pool_allocator is fast_pool_allocator_tag.
  34. All template parameters of the allocators (including implementation-specific ones)
  35. determine the type of the underlying Pool,
  36. with the exception of the first parameter T, whose size is used instead.
  37. Since the size of T is used to determine the type of the underlying Pool,
  38. each allocator for different types of the same size will share the same underlying pool.
  39. The tag class prevents pools from being shared between pool_allocator and fast_pool_allocator.
  40. For example, on a system where
  41. <tt>sizeof(int) == sizeof(void *)</tt>, <tt>pool_allocator<int></tt> and <tt>pool_allocator<void *></tt>
  42. will both allocate/deallocate from/to the same pool.
  43. If there is only one thread running before main() starts and after main() ends,
  44. then both allocators are completely thread-safe.
  45. <b>Compiler and STL Notes</b>
  46. A number of common STL libraries contain bugs in their using of allocators.
  47. Specifically, they pass null pointers to the deallocate function,
  48. which is explicitly forbidden by the Standard [20.1.5 Table 32].
  49. PoolAlloc will work around these libraries if it detects them;
  50. currently, workarounds are in place for:
  51. Borland C++ (Builder and command-line compiler)
  52. with default (RogueWave) library, ver. 5 and earlier,
  53. STLport (with any compiler), ver. 4.0 and earlier.
  54. */
  55. // std::numeric_limits
  56. #include <boost/limits.hpp>
  57. // new, std::bad_alloc
  58. #include <new>
  59. #include <boost/throw_exception.hpp>
  60. #include <boost/pool/poolfwd.hpp>
  61. // boost::singleton_pool
  62. #include <boost/pool/singleton_pool.hpp>
  63. #include <boost/detail/workaround.hpp>
  64. // C++11 features detection
  65. #include <boost/config.hpp>
  66. // std::forward
  67. #ifdef BOOST_HAS_VARIADIC_TMPL
  68. #include <utility>
  69. #endif
  70. #ifdef BOOST_POOL_INSTRUMENT
  71. #include <iostream>
  72. #include <iomanip>
  73. #endif
  74. // The following code will be put into Boost.Config in a later revision
  75. #if defined(_RWSTD_VER) || defined(__SGI_STL_PORT) || \
  76. BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x582))
  77. #define BOOST_NO_PROPER_STL_DEALLOCATE
  78. #endif
  79. namespace boost {
  80. #ifdef BOOST_POOL_INSTRUMENT
  81. template <bool b>
  82. struct debug_info
  83. {
  84. static unsigned allocated;
  85. };
  86. template <bool b>
  87. unsigned debug_info<b>::allocated = 0;
  88. #endif
  89. //! Simple tag type used by pool_allocator as an argument to the
  90. //! underlying singleton_pool.
  91. struct pool_allocator_tag
  92. {
  93. };
  94. /*! \brief A C++ Standard Library conforming allocator, based on an underlying pool.
  95. Template parameters for pool_allocator are defined as follows:
  96. <b>T</b> Type of object to allocate/deallocate.
  97. <b>UserAllocator</B>. Defines the method that the underlying Pool will use to allocate memory from the system. See
  98. <a href="boost_pool/pool/pooling.html#boost_pool.pool.pooling.user_allocator">User Allocators</a> for details.
  99. <b>Mutex</b> Allows the user to determine the type of synchronization to be used on the underlying singleton_pool.
  100. <b>NextSize</b> The value of this parameter is passed to the underlying singleton_pool when it is created.
  101. <b>MaxSize</b> Limit on the maximum size used.
  102. \attention
  103. The underlying singleton_pool used by the this allocator
  104. constructs a pool instance that
  105. <b>is never freed</b>. This means that memory allocated
  106. by the allocator can be still used after main() has
  107. completed, but may mean that some memory checking programs
  108. will complain about leaks.
  109. */
  110. template <typename T,
  111. typename UserAllocator,
  112. typename Mutex,
  113. unsigned NextSize,
  114. unsigned MaxSize >
  115. class pool_allocator
  116. {
  117. public:
  118. typedef T value_type; //!< value_type of template parameter T.
  119. typedef UserAllocator user_allocator; //!< allocator that defines the method that the underlying Pool will use to allocate memory from the system.
  120. typedef Mutex mutex; //!< typedef mutex publishes the value of the template parameter Mutex.
  121. BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize); //!< next_size publishes the values of the template parameter NextSize.
  122. typedef value_type * pointer; //!<
  123. typedef const value_type * const_pointer;
  124. typedef value_type & reference;
  125. typedef const value_type & const_reference;
  126. typedef typename pool<UserAllocator>::size_type size_type;
  127. typedef typename pool<UserAllocator>::difference_type difference_type;
  128. //! \brief Nested class rebind allows for transformation from
  129. //! pool_allocator<T> to pool_allocator<U>.
  130. //!
  131. //! Nested class rebind allows for transformation from
  132. //! pool_allocator<T> to pool_allocator<U> via the member
  133. //! typedef other.
  134. template <typename U>
  135. struct rebind
  136. { //
  137. typedef pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  138. };
  139. public:
  140. pool_allocator()
  141. { /*! Results in default construction of the underlying singleton_pool IFF an
  142. instance of this allocator is constructed during global initialization (
  143. required to ensure construction of singleton_pool IFF an
  144. instance of this allocator is constructed during global
  145. initialization. See ticket #2359 for a complete explanation at
  146. http://svn.boost.org/trac/boost/ticket/2359) .
  147. */
  148. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  149. NextSize, MaxSize>::is_from(0);
  150. }
  151. // default copy constructor.
  152. // default assignment operator.
  153. // not explicit, mimicking std::allocator [20.4.1]
  154. template <typename U>
  155. pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
  156. { /*! Results in the default construction of the underlying singleton_pool, this
  157. is required to ensure construction of singleton_pool IFF an
  158. instance of this allocator is constructed during global
  159. initialization. See ticket #2359 for a complete explanation
  160. at http://svn.boost.org/trac/boost/ticket/2359 .
  161. */
  162. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  163. NextSize, MaxSize>::is_from(0);
  164. }
  165. // default destructor
  166. static pointer address(reference r)
  167. { return &r; }
  168. static const_pointer address(const_reference s)
  169. { return &s; }
  170. static size_type max_size()
  171. { return (std::numeric_limits<size_type>::max)(); }
  172. #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
  173. template <typename U, typename... Args>
  174. static void construct(U* ptr, Args&&... args)
  175. { new (ptr) U(std::forward<Args>(args)...); }
  176. #else
  177. static void construct(const pointer ptr, const value_type & t)
  178. { new (ptr) T(t); }
  179. #endif
  180. static void destroy(const pointer ptr)
  181. {
  182. ptr->~T();
  183. (void) ptr; // avoid unused variable warning.
  184. }
  185. bool operator==(const pool_allocator &) const
  186. { return true; }
  187. bool operator!=(const pool_allocator &) const
  188. { return false; }
  189. static pointer allocate(const size_type n)
  190. {
  191. #ifdef BOOST_POOL_INSTRUMENT
  192. debug_info<true>::allocated += n * sizeof(T);
  193. std::cout << "Allocating " << n << " * " << sizeof(T) << " bytes...\n"
  194. "Total allocated is now " << debug_info<true>::allocated << std::endl;
  195. #endif
  196. const pointer ret = static_cast<pointer>(
  197. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  198. NextSize, MaxSize>::ordered_malloc(n) );
  199. if ((ret == 0) && n)
  200. boost::throw_exception(std::bad_alloc());
  201. return ret;
  202. }
  203. static pointer allocate(const size_type n, const void * const)
  204. { //! allocate n bytes
  205. //! \param n bytes to allocate.
  206. //! \param unused.
  207. return allocate(n);
  208. }
  209. static void deallocate(const pointer ptr, const size_type n)
  210. { //! Deallocate n bytes from ptr
  211. //! \param ptr location to deallocate from.
  212. //! \param n number of bytes to deallocate.
  213. #ifdef BOOST_POOL_INSTRUMENT
  214. debug_info<true>::allocated -= n * sizeof(T);
  215. std::cout << "Deallocating " << n << " * " << sizeof(T) << " bytes...\n"
  216. "Total allocated is now " << debug_info<true>::allocated << std::endl;
  217. #endif
  218. #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
  219. if (ptr == 0 || n == 0)
  220. return;
  221. #endif
  222. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  223. NextSize, MaxSize>::ordered_free(ptr, n);
  224. }
  225. };
  226. /*! \brief Specialization of pool_allocator<void>.
  227. Specialization of pool_allocator for type void: required by the standard to make this a conforming allocator type.
  228. */
  229. template<
  230. typename UserAllocator,
  231. typename Mutex,
  232. unsigned NextSize,
  233. unsigned MaxSize>
  234. class pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>
  235. {
  236. public:
  237. typedef void* pointer;
  238. typedef const void* const_pointer;
  239. typedef void value_type;
  240. //! \brief Nested class rebind allows for transformation from
  241. //! pool_allocator<T> to pool_allocator<U>.
  242. //!
  243. //! Nested class rebind allows for transformation from
  244. //! pool_allocator<T> to pool_allocator<U> via the member
  245. //! typedef other.
  246. template <class U>
  247. struct rebind
  248. {
  249. typedef pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  250. };
  251. };
  252. //! Simple tag type used by fast_pool_allocator as a template parameter to the underlying singleton_pool.
  253. struct fast_pool_allocator_tag
  254. {
  255. };
  256. /*! \brief A C++ Standard Library conforming allocator geared towards allocating single chunks.
  257. While class template <tt>pool_allocator</tt> is a more general-purpose solution geared towards
  258. efficiently servicing requests for any number of contiguous chunks,
  259. <tt>fast_pool_allocator</tt> is also a general-purpose solution,
  260. but is geared towards efficiently servicing requests for one chunk at a time;
  261. it will work for contiguous chunks, but not as well as <tt>pool_allocator</tt>.
  262. If you are seriously concerned about performance,
  263. use <tt>fast_pool_allocator</tt> when dealing with containers such as <tt>std::list</tt>,
  264. and use <tt>pool_allocator</tt> when dealing with containers such as <tt>std::vector</tt>.
  265. The template parameters are defined as follows:
  266. <b>T</b> Type of object to allocate/deallocate.
  267. <b>UserAllocator</b>. Defines the method that the underlying Pool will use to allocate memory from the system.
  268. See <a href="boost_pool/pool/pooling.html#boost_pool.pool.pooling.user_allocator">User Allocators</a> for details.
  269. <b>Mutex</b> Allows the user to determine the type of synchronization to be used on the underlying <tt>singleton_pool</tt>.
  270. <b>NextSize</b> The value of this parameter is passed to the underlying Pool when it is created.
  271. <b>MaxSize</b> Limit on the maximum size used.
  272. \attention
  273. The underlying singleton_pool used by the this allocator
  274. constructs a pool instance that
  275. <b>is never freed</b>. This means that memory allocated
  276. by the allocator can be still used after main() has
  277. completed, but may mean that some memory checking programs
  278. will complain about leaks.
  279. */
  280. template <typename T,
  281. typename UserAllocator,
  282. typename Mutex,
  283. unsigned NextSize,
  284. unsigned MaxSize >
  285. class fast_pool_allocator
  286. {
  287. public:
  288. typedef T value_type;
  289. typedef UserAllocator user_allocator;
  290. typedef Mutex mutex;
  291. BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
  292. typedef value_type * pointer;
  293. typedef const value_type * const_pointer;
  294. typedef value_type & reference;
  295. typedef const value_type & const_reference;
  296. typedef typename pool<UserAllocator>::size_type size_type;
  297. typedef typename pool<UserAllocator>::difference_type difference_type;
  298. //! \brief Nested class rebind allows for transformation from
  299. //! fast_pool_allocator<T> to fast_pool_allocator<U>.
  300. //!
  301. //! Nested class rebind allows for transformation from
  302. //! fast_pool_allocator<T> to fast_pool_allocator<U> via the member
  303. //! typedef other.
  304. template <typename U>
  305. struct rebind
  306. {
  307. typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  308. };
  309. public:
  310. fast_pool_allocator()
  311. {
  312. //! Ensures construction of the underlying singleton_pool IFF an
  313. //! instance of this allocator is constructed during global
  314. //! initialization. See ticket #2359 for a complete explanation
  315. //! at http://svn.boost.org/trac/boost/ticket/2359 .
  316. singleton_pool<fast_pool_allocator_tag, sizeof(T),
  317. UserAllocator, Mutex, NextSize, MaxSize>::is_from(0);
  318. }
  319. // Default copy constructor used.
  320. // Default assignment operator used.
  321. // Not explicit, mimicking std::allocator [20.4.1]
  322. template <typename U>
  323. fast_pool_allocator(
  324. const fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
  325. {
  326. //! Ensures construction of the underlying singleton_pool IFF an
  327. //! instance of this allocator is constructed during global
  328. //! initialization. See ticket #2359 for a complete explanation
  329. //! at http://svn.boost.org/trac/boost/ticket/2359 .
  330. singleton_pool<fast_pool_allocator_tag, sizeof(T),
  331. UserAllocator, Mutex, NextSize, MaxSize>::is_from(0);
  332. }
  333. // Default destructor used.
  334. static pointer address(reference r)
  335. {
  336. return &r;
  337. }
  338. static const_pointer address(const_reference s)
  339. { return &s; }
  340. static size_type max_size()
  341. { return (std::numeric_limits<size_type>::max)(); }
  342. #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
  343. template <typename U, typename... Args>
  344. void construct(U* ptr, Args&&... args)
  345. { new (ptr) U(std::forward<Args>(args)...); }
  346. #else
  347. void construct(const pointer ptr, const value_type & t)
  348. { new (ptr) T(t); }
  349. #endif
  350. void destroy(const pointer ptr)
  351. { //! Destroy ptr using destructor.
  352. ptr->~T();
  353. (void) ptr; // Avoid unused variable warning.
  354. }
  355. bool operator==(const fast_pool_allocator &) const
  356. { return true; }
  357. bool operator!=(const fast_pool_allocator &) const
  358. { return false; }
  359. static pointer allocate(const size_type n)
  360. {
  361. const pointer ret = (n == 1) ?
  362. static_cast<pointer>(
  363. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  364. UserAllocator, Mutex, NextSize, MaxSize>::malloc)() ) :
  365. static_cast<pointer>(
  366. singleton_pool<fast_pool_allocator_tag, sizeof(T),
  367. UserAllocator, Mutex, NextSize, MaxSize>::ordered_malloc(n) );
  368. if (ret == 0)
  369. boost::throw_exception(std::bad_alloc());
  370. return ret;
  371. }
  372. static pointer allocate(const size_type n, const void * const)
  373. { //! Allocate memory .
  374. return allocate(n);
  375. }
  376. static pointer allocate()
  377. { //! Allocate memory.
  378. const pointer ret = static_cast<pointer>(
  379. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  380. UserAllocator, Mutex, NextSize, MaxSize>::malloc)() );
  381. if (ret == 0)
  382. boost::throw_exception(std::bad_alloc());
  383. return ret;
  384. }
  385. static void deallocate(const pointer ptr, const size_type n)
  386. { //! Deallocate memory.
  387. #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
  388. if (ptr == 0 || n == 0)
  389. return;
  390. #endif
  391. if (n == 1)
  392. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  393. UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr);
  394. else
  395. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  396. UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr, n);
  397. }
  398. static void deallocate(const pointer ptr)
  399. { //! deallocate/free
  400. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  401. UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr);
  402. }
  403. };
  404. /*! \brief Specialization of fast_pool_allocator<void>.
  405. Specialization of fast_pool_allocator<void> required to make the allocator standard-conforming.
  406. */
  407. template<
  408. typename UserAllocator,
  409. typename Mutex,
  410. unsigned NextSize,
  411. unsigned MaxSize >
  412. class fast_pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>
  413. {
  414. public:
  415. typedef void* pointer;
  416. typedef const void* const_pointer;
  417. typedef void value_type;
  418. //! \brief Nested class rebind allows for transformation from
  419. //! fast_pool_allocator<T> to fast_pool_allocator<U>.
  420. //!
  421. //! Nested class rebind allows for transformation from
  422. //! fast_pool_allocator<T> to fast_pool_allocator<U> via the member
  423. //! typedef other.
  424. template <class U> struct rebind
  425. {
  426. typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  427. };
  428. };
  429. } // namespace boost
  430. #endif