singleton_pool.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright (C) 2000, 2001 Stephen Cleary
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org for updates, documentation, and revision history.
  8. #ifndef BOOST_SINGLETON_POOL_HPP
  9. #define BOOST_SINGLETON_POOL_HPP
  10. /*!
  11. \file
  12. \brief The <tt>singleton_pool</tt> class allows other pool interfaces
  13. for types of the same size to share the same underlying pool.
  14. \details Header singleton_pool.hpp provides a template class <tt>singleton_pool</tt>,
  15. which provides access to a pool as a singleton object.
  16. */
  17. #include <boost/pool/poolfwd.hpp>
  18. // boost::pool
  19. #include <boost/pool/pool.hpp>
  20. // boost::details::pool::guard
  21. #include <boost/pool/detail/guard.hpp>
  22. #include <boost/type_traits/aligned_storage.hpp>
  23. namespace boost {
  24. /*!
  25. The singleton_pool class allows other pool interfaces
  26. for types of the same size to share the same pool. Template
  27. parameters are as follows:
  28. <b>Tag</b> User-specified type to uniquely identify this pool: allows different unbounded sets of singleton pools to exist.
  29. <b>RequestedSize</b> The size of each chunk returned by member function <tt>malloc()</tt>.
  30. <B>UserAllocator</b> User allocator, default = default_user_allocator_new_delete.
  31. <b>Mutex</B> This class is the type of mutex to use to protect simultaneous access to the underlying Pool.
  32. Can be any Boost.Thread Mutex type or <tt>boost::details::pool::null_mutex</tt>.
  33. It is exposed so that users may declare some singleton pools normally (i.e., with synchronization), but
  34. some singleton pools without synchronization (by specifying <tt>boost::details::pool::null_mutex</tt>) for efficiency reasons.
  35. The member typedef <tt>mutex</tt> exposes the value of this template parameter. The default for this
  36. parameter is boost::details::pool::default_mutex which is a synonym for either <tt>boost::details::pool::null_mutex</tt>
  37. (when threading support is turned off in the compiler (so BOOST_HAS_THREADS is not set), or threading support
  38. has ben explicitly disabled with BOOST_DISABLE_THREADS (Boost-wide disabling of threads) or BOOST_POOL_NO_MT (this library only))
  39. or for <tt>boost::mutex</tt> (when threading support is enabled in the compiler).
  40. <B>NextSize</b> The value of this parameter is passed to the underlying Pool when it is created and
  41. specifies the number of chunks to allocate in the first allocation request (defaults to 32).
  42. The member typedef <tt>static const value next_size</tt> exposes the value of this template parameter.
  43. <b>MaxSize</B>The value of this parameter is passed to the underlying Pool when it is created and
  44. specifies the maximum number of chunks to allocate in any single allocation request (defaults to 0).
  45. <b>Notes:</b>
  46. The underlying pool <i>p</i> referenced by the static functions
  47. in singleton_pool is actually declared in a way that is:
  48. 1 Thread-safe if there is only one thread running before main() begins and after main() ends
  49. -- all of the static functions of singleton_pool synchronize their access to p.
  50. 2 Guaranteed to be constructed before it is used --
  51. thus, the simple static object in the synopsis above would actually be an incorrect implementation.
  52. The actual implementation to guarantee this is considerably more complicated.
  53. 3 Note too that a different underlying pool p exists
  54. for each different set of template parameters,
  55. including implementation-specific ones.
  56. 4 The underlying pool is constructed "as if" by:
  57. pool<UserAllocator> p(RequestedSize, NextSize, MaxSize);
  58. \attention
  59. The underlying pool constructed by the singleton
  60. <b>is never freed</b>. This means that memory allocated
  61. by a singleton_pool can be still used after main() has
  62. completed, but may mean that some memory checking programs
  63. will complain about leaks from singleton_pool.
  64. */
  65. template <typename Tag,
  66. unsigned RequestedSize,
  67. typename UserAllocator,
  68. typename Mutex,
  69. unsigned NextSize,
  70. unsigned MaxSize >
  71. class singleton_pool
  72. {
  73. public:
  74. typedef Tag tag; /*!< The Tag template parameter uniquely
  75. identifies this pool and allows
  76. different unbounded sets of singleton pools to exist.
  77. For example, the pool allocators use two tag classes to ensure that the
  78. two different allocator types never share the same underlying singleton pool.
  79. Tag is never actually used by singleton_pool.
  80. */
  81. typedef Mutex mutex; //!< The type of mutex used to synchonise access to this pool (default <tt>details::pool::default_mutex</tt>).
  82. typedef UserAllocator user_allocator; //!< The user-allocator used by this pool, default = <tt>default_user_allocator_new_delete</tt>.
  83. typedef typename pool<UserAllocator>::size_type size_type; //!< size_type of user allocator.
  84. typedef typename pool<UserAllocator>::difference_type difference_type; //!< difference_type of user allocator.
  85. BOOST_STATIC_CONSTANT(unsigned, requested_size = RequestedSize); //!< The size of each chunk allocated by this pool.
  86. BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize); //!< The number of chunks to allocate on the first allocation.
  87. private:
  88. singleton_pool();
  89. #ifndef BOOST_DOXYGEN
  90. struct pool_type: public Mutex, public pool<UserAllocator>
  91. {
  92. pool_type() : pool<UserAllocator>(RequestedSize, NextSize, MaxSize) {}
  93. }; // struct pool_type: Mutex
  94. #else
  95. //
  96. // This is invoked when we build with Doxygen only:
  97. //
  98. public:
  99. static pool<UserAllocator> p; //!< For exposition only!
  100. #endif
  101. public:
  102. static void * malloc BOOST_PREVENT_MACRO_SUBSTITUTION()
  103. { //! Equivalent to SingletonPool::p.malloc(); synchronized.
  104. pool_type & p = get_pool();
  105. details::pool::guard<Mutex> g(p);
  106. return (p.malloc)();
  107. }
  108. static void * ordered_malloc()
  109. { //! Equivalent to SingletonPool::p.ordered_malloc(); synchronized.
  110. pool_type & p = get_pool();
  111. details::pool::guard<Mutex> g(p);
  112. return p.ordered_malloc();
  113. }
  114. static void * ordered_malloc(const size_type n)
  115. { //! Equivalent to SingletonPool::p.ordered_malloc(n); synchronized.
  116. pool_type & p = get_pool();
  117. details::pool::guard<Mutex> g(p);
  118. return p.ordered_malloc(n);
  119. }
  120. static bool is_from(void * const ptr)
  121. { //! Equivalent to SingletonPool::p.is_from(chunk); synchronized.
  122. //! \returns true if chunk is from SingletonPool::is_from(chunk)
  123. pool_type & p = get_pool();
  124. details::pool::guard<Mutex> g(p);
  125. return p.is_from(ptr);
  126. }
  127. static void free BOOST_PREVENT_MACRO_SUBSTITUTION(void * const ptr)
  128. { //! Equivalent to SingletonPool::p.free(chunk); synchronized.
  129. pool_type & p = get_pool();
  130. details::pool::guard<Mutex> g(p);
  131. (p.free)(ptr);
  132. }
  133. static void ordered_free(void * const ptr)
  134. { //! Equivalent to SingletonPool::p.ordered_free(chunk); synchronized.
  135. pool_type & p = get_pool();
  136. details::pool::guard<Mutex> g(p);
  137. p.ordered_free(ptr);
  138. }
  139. static void free BOOST_PREVENT_MACRO_SUBSTITUTION(void * const ptr, const size_type n)
  140. { //! Equivalent to SingletonPool::p.free(chunk, n); synchronized.
  141. pool_type & p = get_pool();
  142. details::pool::guard<Mutex> g(p);
  143. (p.free)(ptr, n);
  144. }
  145. static void ordered_free(void * const ptr, const size_type n)
  146. { //! Equivalent to SingletonPool::p.ordered_free(chunk, n); synchronized.
  147. pool_type & p = get_pool();
  148. details::pool::guard<Mutex> g(p);
  149. p.ordered_free(ptr, n);
  150. }
  151. static bool release_memory()
  152. { //! Equivalent to SingletonPool::p.release_memory(); synchronized.
  153. pool_type & p = get_pool();
  154. details::pool::guard<Mutex> g(p);
  155. return p.release_memory();
  156. }
  157. static bool purge_memory()
  158. { //! Equivalent to SingletonPool::p.purge_memory(); synchronized.
  159. pool_type & p = get_pool();
  160. details::pool::guard<Mutex> g(p);
  161. return p.purge_memory();
  162. }
  163. private:
  164. typedef boost::aligned_storage<sizeof(pool_type), boost::alignment_of<pool_type>::value> storage_type;
  165. static storage_type storage;
  166. static pool_type& get_pool()
  167. {
  168. static bool f = false;
  169. if(!f)
  170. {
  171. // This code *must* be called before main() starts,
  172. // and when only one thread is executing.
  173. f = true;
  174. new (&storage) pool_type;
  175. }
  176. // The following line does nothing else than force the instantiation
  177. // of singleton<T>::create_object, whose constructor is
  178. // called before main() begins.
  179. create_object.do_nothing();
  180. return *static_cast<pool_type*>(static_cast<void*>(&storage));
  181. }
  182. struct object_creator
  183. {
  184. object_creator()
  185. { // This constructor does nothing more than ensure that instance()
  186. // is called before main() begins, thus creating the static
  187. // T object before multithreading race issues can come up.
  188. singleton_pool<Tag, RequestedSize, UserAllocator, Mutex, NextSize, MaxSize>::get_pool();
  189. }
  190. inline void do_nothing() const
  191. {
  192. }
  193. };
  194. static object_creator create_object;
  195. }; // struct singleton_pool
  196. template <typename Tag,
  197. unsigned RequestedSize,
  198. typename UserAllocator,
  199. typename Mutex,
  200. unsigned NextSize,
  201. unsigned MaxSize >
  202. typename singleton_pool<Tag, RequestedSize, UserAllocator, Mutex, NextSize, MaxSize>::storage_type singleton_pool<Tag, RequestedSize, UserAllocator, Mutex, NextSize, MaxSize>::storage;
  203. template <typename Tag,
  204. unsigned RequestedSize,
  205. typename UserAllocator,
  206. typename Mutex,
  207. unsigned NextSize,
  208. unsigned MaxSize >
  209. typename singleton_pool<Tag, RequestedSize, UserAllocator, Mutex, NextSize, MaxSize>::object_creator singleton_pool<Tag, RequestedSize, UserAllocator, Mutex, NextSize, MaxSize>::create_object;
  210. } // namespace boost
  211. #endif