pool_alloc.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. #ifdef BOOST_POOL_INSTRUMENT
  65. #include <iostream>
  66. #include <iomanip>
  67. #endif
  68. // The following code will be put into Boost.Config in a later revision
  69. #if defined(_RWSTD_VER) || defined(__SGI_STL_PORT) || \
  70. BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  71. #define BOOST_NO_PROPER_STL_DEALLOCATE
  72. #endif
  73. namespace boost {
  74. #ifdef BOOST_POOL_INSTRUMENT
  75. template <bool b>
  76. struct debug_info
  77. {
  78. static unsigned allocated;
  79. };
  80. template <bool b>
  81. unsigned debug_info<b>::allocated = 0;
  82. #endif
  83. //! Simple tag type used by pool_allocator as an argument to the
  84. //! underlying singleton_pool.
  85. struct pool_allocator_tag
  86. {
  87. };
  88. /*! \brief A C++ Standard Library conforming allocator, based on an underlying pool.
  89. Template parameters for pool_allocator are defined as follows:
  90. <b>T</b> Type of object to allocate/deallocate.
  91. <b>UserAllocator</B>. Defines the method that the underlying Pool will use to allocate memory from the system. See
  92. <a href="boost_pool/pool/pooling.html#boost_pool.pool.pooling.user_allocator">User Allocators</a> for details.
  93. <b>Mutex</b> Allows the user to determine the type of synchronization to be used on the underlying singleton_pool.
  94. <b>NextSize</b> The value of this parameter is passed to the underlying singleton_pool when it is created.
  95. <b>MaxSize</b> Limit on the maximum size used.
  96. \attention
  97. The underlying singleton_pool used by the this allocator
  98. constructs a pool instance that
  99. <b>is never freed</b>. This means that memory allocated
  100. by the allocator can be still used after main() has
  101. completed, but may mean that some memory checking programs
  102. will complain about leaks.
  103. */
  104. template <typename T,
  105. typename UserAllocator,
  106. typename Mutex,
  107. unsigned NextSize,
  108. unsigned MaxSize >
  109. class pool_allocator
  110. {
  111. public:
  112. typedef T value_type; //!< value_type of template parameter T.
  113. typedef UserAllocator user_allocator; //!< allocator that defines the method that the underlying Pool will use to allocate memory from the system.
  114. typedef Mutex mutex; //!< typedef mutex publishes the value of the template parameter Mutex.
  115. BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize); //!< next_size publishes the values of the template parameter NextSize.
  116. typedef value_type * pointer; //!<
  117. typedef const value_type * const_pointer;
  118. typedef value_type & reference;
  119. typedef const value_type & const_reference;
  120. typedef typename pool<UserAllocator>::size_type size_type;
  121. typedef typename pool<UserAllocator>::difference_type difference_type;
  122. //! \brief Nested class rebind allows for transformation from
  123. //! pool_allocator<T> to pool_allocator<U>.
  124. //!
  125. //! Nested class rebind allows for transformation from
  126. //! pool_allocator<T> to pool_allocator<U> via the member
  127. //! typedef other.
  128. template <typename U>
  129. struct rebind
  130. { //
  131. typedef pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  132. };
  133. public:
  134. pool_allocator()
  135. { /*! Results in default construction of the underlying singleton_pool IFF an
  136. instance of this allocator is constructed during global initialization (
  137. required to ensure construction of singleton_pool IFF an
  138. instance of this allocator is constructed during global
  139. initialization. See ticket #2359 for a complete explanation at
  140. http://svn.boost.org/trac/boost/ticket/2359) .
  141. */
  142. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  143. NextSize, MaxSize>::is_from(0);
  144. }
  145. // default copy constructor.
  146. // default assignment operator.
  147. // not explicit, mimicking std::allocator [20.4.1]
  148. template <typename U>
  149. pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
  150. { /*! Results in the default construction of the underlying singleton_pool, this
  151. is required to ensure construction of singleton_pool IFF an
  152. instance of this allocator is constructed during global
  153. initialization. See ticket #2359 for a complete explanation
  154. at http://svn.boost.org/trac/boost/ticket/2359 .
  155. */
  156. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  157. NextSize, MaxSize>::is_from(0);
  158. }
  159. // default destructor
  160. static pointer address(reference r)
  161. { return &r; }
  162. static const_pointer address(const_reference s)
  163. { return &s; }
  164. static size_type max_size()
  165. { return (std::numeric_limits<size_type>::max)(); }
  166. static void construct(const pointer ptr, const value_type & t)
  167. { new (ptr) T(t); }
  168. static void destroy(const pointer ptr)
  169. {
  170. ptr->~T();
  171. (void) ptr; // avoid unused variable warning.
  172. }
  173. bool operator==(const pool_allocator &) const
  174. { return true; }
  175. bool operator!=(const pool_allocator &) const
  176. { return false; }
  177. static pointer allocate(const size_type n)
  178. {
  179. #ifdef BOOST_POOL_INSTRUMENT
  180. debug_info<true>::allocated += n * sizeof(T);
  181. std::cout << "Allocating " << n << " * " << sizeof(T) << " bytes...\n"
  182. "Total allocated is now " << debug_info<true>::allocated << std::endl;
  183. #endif
  184. const pointer ret = static_cast<pointer>(
  185. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  186. NextSize, MaxSize>::ordered_malloc(n) );
  187. if ((ret == 0) && n)
  188. boost::throw_exception(std::bad_alloc());
  189. return ret;
  190. }
  191. static pointer allocate(const size_type n, const void * const)
  192. { //! allocate n bytes
  193. //! \param n bytes to allocate.
  194. //! \param unused.
  195. return allocate(n);
  196. }
  197. static void deallocate(const pointer ptr, const size_type n)
  198. { //! Deallocate n bytes from ptr
  199. //! \param ptr location to deallocate from.
  200. //! \param n number of bytes to deallocate.
  201. #ifdef BOOST_POOL_INSTRUMENT
  202. debug_info<true>::allocated -= n * sizeof(T);
  203. std::cout << "Deallocating " << n << " * " << sizeof(T) << " bytes...\n"
  204. "Total allocated is now " << debug_info<true>::allocated << std::endl;
  205. #endif
  206. #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
  207. if (ptr == 0 || n == 0)
  208. return;
  209. #endif
  210. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  211. NextSize, MaxSize>::ordered_free(ptr, n);
  212. }
  213. };
  214. /*! \brief Specialization of pool_allocator<void>.
  215. Specialization of pool_allocator for type void: required by the standard to make this a conforming allocator type.
  216. */
  217. template<
  218. typename UserAllocator,
  219. typename Mutex,
  220. unsigned NextSize,
  221. unsigned MaxSize>
  222. class pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>
  223. {
  224. public:
  225. typedef void* pointer;
  226. typedef const void* const_pointer;
  227. typedef void value_type;
  228. //! \brief Nested class rebind allows for transformation from
  229. //! pool_allocator<T> to pool_allocator<U>.
  230. //!
  231. //! Nested class rebind allows for transformation from
  232. //! pool_allocator<T> to pool_allocator<U> via the member
  233. //! typedef other.
  234. template <class U>
  235. struct rebind
  236. {
  237. typedef pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  238. };
  239. };
  240. //! Simple tag type used by fast_pool_allocator as a template parameter to the underlying singleton_pool.
  241. struct fast_pool_allocator_tag
  242. {
  243. };
  244. /*! \brief A C++ Standard Library conforming allocator geared towards allocating single chunks.
  245. While class template <tt>pool_allocator</tt> is a more general-purpose solution geared towards
  246. efficiently servicing requests for any number of contiguous chunks,
  247. <tt>fast_pool_allocator</tt> is also a general-purpose solution,
  248. but is geared towards efficiently servicing requests for one chunk at a time;
  249. it will work for contiguous chunks, but not as well as <tt>pool_allocator</tt>.
  250. If you are seriously concerned about performance,
  251. use <tt>fast_pool_allocator</tt> when dealing with containers such as <tt>std::list</tt>,
  252. and use <tt>pool_allocator</tt> when dealing with containers such as <tt>std::vector</tt>.
  253. The template parameters are defined as follows:
  254. <b>T</b> Type of object to allocate/deallocate.
  255. <b>UserAllocator</b>. Defines the method that the underlying Pool will use to allocate memory from the system.
  256. See <a href="boost_pool/pool/pooling.html#boost_pool.pool.pooling.user_allocator">User Allocators</a> for details.
  257. <b>Mutex</b> Allows the user to determine the type of synchronization to be used on the underlying <tt>singleton_pool</tt>.
  258. <b>NextSize</b> The value of this parameter is passed to the underlying Pool when it is created.
  259. <b>MaxSize</b> Limit on the maximum size used.
  260. \attention
  261. The underlying singleton_pool used by the this allocator
  262. constructs a pool instance that
  263. <b>is never freed</b>. This means that memory allocated
  264. by the allocator can be still used after main() has
  265. completed, but may mean that some memory checking programs
  266. will complain about leaks.
  267. */
  268. template <typename T,
  269. typename UserAllocator,
  270. typename Mutex,
  271. unsigned NextSize,
  272. unsigned MaxSize >
  273. class fast_pool_allocator
  274. {
  275. public:
  276. typedef T value_type;
  277. typedef UserAllocator user_allocator;
  278. typedef Mutex mutex;
  279. BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
  280. typedef value_type * pointer;
  281. typedef const value_type * const_pointer;
  282. typedef value_type & reference;
  283. typedef const value_type & const_reference;
  284. typedef typename pool<UserAllocator>::size_type size_type;
  285. typedef typename pool<UserAllocator>::difference_type difference_type;
  286. //! \brief Nested class rebind allows for transformation from
  287. //! fast_pool_allocator<T> to fast_pool_allocator<U>.
  288. //!
  289. //! Nested class rebind allows for transformation from
  290. //! fast_pool_allocator<T> to fast_pool_allocator<U> via the member
  291. //! typedef other.
  292. template <typename U>
  293. struct rebind
  294. {
  295. typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  296. };
  297. public:
  298. fast_pool_allocator()
  299. {
  300. //! Ensures construction of the underlying singleton_pool IFF an
  301. //! instance of this allocator is constructed during global
  302. //! initialization. See ticket #2359 for a complete explanation
  303. //! at http://svn.boost.org/trac/boost/ticket/2359 .
  304. singleton_pool<fast_pool_allocator_tag, sizeof(T),
  305. UserAllocator, Mutex, NextSize, MaxSize>::is_from(0);
  306. }
  307. // Default copy constructor used.
  308. // Default assignment operator used.
  309. // Not explicit, mimicking std::allocator [20.4.1]
  310. template <typename U>
  311. fast_pool_allocator(
  312. const fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
  313. {
  314. //! Ensures construction of the underlying singleton_pool IFF an
  315. //! instance of this allocator is constructed during global
  316. //! initialization. See ticket #2359 for a complete explanation
  317. //! at http://svn.boost.org/trac/boost/ticket/2359 .
  318. singleton_pool<fast_pool_allocator_tag, sizeof(T),
  319. UserAllocator, Mutex, NextSize, MaxSize>::is_from(0);
  320. }
  321. // Default destructor used.
  322. static pointer address(reference r)
  323. {
  324. return &r;
  325. }
  326. static const_pointer address(const_reference s)
  327. { return &s; }
  328. static size_type max_size()
  329. { return (std::numeric_limits<size_type>::max)(); }
  330. void construct(const pointer ptr, const value_type & t)
  331. { new (ptr) T(t); }
  332. void destroy(const pointer ptr)
  333. { //! Destroy ptr using destructor.
  334. ptr->~T();
  335. (void) ptr; // Avoid unused variable warning.
  336. }
  337. bool operator==(const fast_pool_allocator &) const
  338. { return true; }
  339. bool operator!=(const fast_pool_allocator &) const
  340. { return false; }
  341. static pointer allocate(const size_type n)
  342. {
  343. const pointer ret = (n == 1) ?
  344. static_cast<pointer>(
  345. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  346. UserAllocator, Mutex, NextSize, MaxSize>::malloc)() ) :
  347. static_cast<pointer>(
  348. singleton_pool<fast_pool_allocator_tag, sizeof(T),
  349. UserAllocator, Mutex, NextSize, MaxSize>::ordered_malloc(n) );
  350. if (ret == 0)
  351. boost::throw_exception(std::bad_alloc());
  352. return ret;
  353. }
  354. static pointer allocate(const size_type n, const void * const)
  355. { //! Allocate memory .
  356. return allocate(n);
  357. }
  358. static pointer allocate()
  359. { //! Allocate memory.
  360. const pointer ret = static_cast<pointer>(
  361. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  362. UserAllocator, Mutex, NextSize, MaxSize>::malloc)() );
  363. if (ret == 0)
  364. boost::throw_exception(std::bad_alloc());
  365. return ret;
  366. }
  367. static void deallocate(const pointer ptr, const size_type n)
  368. { //! Deallocate memory.
  369. #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
  370. if (ptr == 0 || n == 0)
  371. return;
  372. #endif
  373. if (n == 1)
  374. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  375. UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr);
  376. else
  377. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  378. UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr, n);
  379. }
  380. static void deallocate(const pointer ptr)
  381. { //! deallocate/free
  382. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  383. UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr);
  384. }
  385. };
  386. /*! \brief Specialization of fast_pool_allocator<void>.
  387. Specialization of fast_pool_allocator<void> required to make the allocator standard-conforming.
  388. */
  389. template<
  390. typename UserAllocator,
  391. typename Mutex,
  392. unsigned NextSize,
  393. unsigned MaxSize >
  394. class fast_pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>
  395. {
  396. public:
  397. typedef void* pointer;
  398. typedef const void* const_pointer;
  399. typedef void value_type;
  400. //! \brief Nested class rebind allows for transformation from
  401. //! fast_pool_allocator<T> to fast_pool_allocator<U>.
  402. //!
  403. //! Nested class rebind allows for transformation from
  404. //! fast_pool_allocator<T> to fast_pool_allocator<U> via the member
  405. //! typedef other.
  406. template <class U> struct rebind
  407. {
  408. typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  409. };
  410. };
  411. } // namespace boost
  412. #endif