/Src/Dependencies/Boost/boost/pool/pool_alloc.hpp

http://hadesmem.googlecode.com/ · C++ Header · 286 lines · 216 code · 35 blank · 35 comment · 19 complexity · 3ee8ee3fef952a23d87afc7574ccf2ae MD5 · raw file

  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_POOL_ALLOC_HPP
  9. #define BOOST_POOL_ALLOC_HPP
  10. // std::numeric_limits
  11. #include <boost/limits.hpp>
  12. // new, std::bad_alloc
  13. #include <new>
  14. #include <boost/throw_exception.hpp>
  15. #include <boost/pool/poolfwd.hpp>
  16. // boost::singleton_pool
  17. #include <boost/pool/singleton_pool.hpp>
  18. #include <boost/detail/workaround.hpp>
  19. // The following code will be put into Boost.Config in a later revision
  20. #if defined(_RWSTD_VER) || defined(__SGI_STL_PORT) || \
  21. BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  22. #define BOOST_NO_PROPER_STL_DEALLOCATE
  23. #endif
  24. namespace boost {
  25. struct pool_allocator_tag { };
  26. template <typename T,
  27. typename UserAllocator,
  28. typename Mutex,
  29. unsigned NextSize,
  30. unsigned MaxSize>
  31. class pool_allocator
  32. {
  33. public:
  34. typedef T value_type;
  35. typedef UserAllocator user_allocator;
  36. typedef Mutex mutex;
  37. BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
  38. typedef value_type * pointer;
  39. typedef const value_type * const_pointer;
  40. typedef value_type & reference;
  41. typedef const value_type & const_reference;
  42. typedef typename pool<UserAllocator>::size_type size_type;
  43. typedef typename pool<UserAllocator>::difference_type difference_type;
  44. template <typename U>
  45. struct rebind
  46. {
  47. typedef pool_allocator<U, UserAllocator, Mutex, NextSize,MaxSize> other;
  48. };
  49. public:
  50. pool_allocator()
  51. {
  52. // Required to ensure construction of singleton_pool IFF an
  53. // instace of this allocator is constructed during global
  54. // initialization. See ticket #2359 for a complete explaination
  55. // ( http://svn.boost.org/trac/boost/ticket/2359 )
  56. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  57. NextSize, MaxSize>::is_from(0);
  58. }
  59. // default copy constructor
  60. // default assignment operator
  61. // not explicit, mimicking std::allocator [20.4.1]
  62. template <typename U>
  63. pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
  64. {
  65. // Required to ensure construction of singleton_pool IFF an
  66. // instace of this allocator is constructed during global
  67. // initialization. See ticket #2359 for a complete explaination
  68. // ( http://svn.boost.org/trac/boost/ticket/2359 )
  69. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  70. NextSize, MaxSize>::is_from(0);
  71. }
  72. // default destructor
  73. static pointer address(reference r)
  74. { return &r; }
  75. static const_pointer address(const_reference s)
  76. { return &s; }
  77. static size_type max_size()
  78. { return (std::numeric_limits<size_type>::max)(); }
  79. static void construct(const pointer ptr, const value_type & t)
  80. { new (ptr) T(t); }
  81. static void destroy(const pointer ptr)
  82. {
  83. ptr->~T();
  84. (void) ptr; // avoid unused variable warning
  85. }
  86. bool operator==(const pool_allocator &) const
  87. { return true; }
  88. bool operator!=(const pool_allocator &) const
  89. { return false; }
  90. static pointer allocate(const size_type n)
  91. {
  92. const pointer ret = static_cast<pointer>(
  93. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  94. NextSize, MaxSize>::ordered_malloc(n) );
  95. if (ret == 0)
  96. boost::throw_exception(std::bad_alloc());
  97. return ret;
  98. }
  99. static pointer allocate(const size_type n, const void * const)
  100. { return allocate(n); }
  101. static void deallocate(const pointer ptr, const size_type n)
  102. {
  103. #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
  104. if (ptr == 0 || n == 0)
  105. return;
  106. #endif
  107. singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
  108. NextSize, MaxSize>::ordered_free(ptr, n);
  109. }
  110. };
  111. template<
  112. typename UserAllocator,
  113. typename Mutex,
  114. unsigned NextSize,
  115. unsigned MaxSize>
  116. class pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>
  117. {
  118. public:
  119. typedef void* pointer;
  120. typedef const void* const_pointer;
  121. typedef void value_type;
  122. template <class U> struct rebind {
  123. typedef pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  124. };
  125. };
  126. struct fast_pool_allocator_tag { };
  127. template <typename T,
  128. typename UserAllocator,
  129. typename Mutex,
  130. unsigned NextSize,
  131. unsigned MaxSize>
  132. class fast_pool_allocator
  133. {
  134. public:
  135. typedef T value_type;
  136. typedef UserAllocator user_allocator;
  137. typedef Mutex mutex;
  138. BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
  139. typedef value_type * pointer;
  140. typedef const value_type * const_pointer;
  141. typedef value_type & reference;
  142. typedef const value_type & const_reference;
  143. typedef typename pool<UserAllocator>::size_type size_type;
  144. typedef typename pool<UserAllocator>::difference_type difference_type;
  145. template <typename U>
  146. struct rebind
  147. {
  148. typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  149. };
  150. public:
  151. fast_pool_allocator()
  152. {
  153. // Required to ensure construction of singleton_pool IFF an
  154. // instace of this allocator is constructed during global
  155. // initialization. See ticket #2359 for a complete explaination
  156. // ( http://svn.boost.org/trac/boost/ticket/2359 )
  157. singleton_pool<fast_pool_allocator_tag, sizeof(T),
  158. UserAllocator, Mutex, NextSize, MaxSize>::is_from(0);
  159. }
  160. // default copy constructor
  161. // default assignment operator
  162. // not explicit, mimicking std::allocator [20.4.1]
  163. template <typename U>
  164. fast_pool_allocator(
  165. const fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
  166. {
  167. // Required to ensure construction of singleton_pool IFF an
  168. // instace of this allocator is constructed during global
  169. // initialization. See ticket #2359 for a complete explaination
  170. // ( http://svn.boost.org/trac/boost/ticket/2359 )
  171. singleton_pool<fast_pool_allocator_tag, sizeof(T),
  172. UserAllocator, Mutex, NextSize, MaxSize>::is_from(0);
  173. }
  174. // default destructor
  175. static pointer address(reference r)
  176. { return &r; }
  177. static const_pointer address(const_reference s)
  178. { return &s; }
  179. static size_type max_size()
  180. { return (std::numeric_limits<size_type>::max)(); }
  181. void construct(const pointer ptr, const value_type & t)
  182. { new (ptr) T(t); }
  183. void destroy(const pointer ptr)
  184. {
  185. ptr->~T();
  186. (void) ptr; // avoid unused variable warning
  187. }
  188. bool operator==(const fast_pool_allocator &) const
  189. { return true; }
  190. bool operator!=(const fast_pool_allocator &) const
  191. { return false; }
  192. static pointer allocate(const size_type n)
  193. {
  194. const pointer ret = (n == 1) ?
  195. static_cast<pointer>(
  196. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  197. UserAllocator, Mutex, NextSize, MaxSize>::malloc)() ) :
  198. static_cast<pointer>(
  199. singleton_pool<fast_pool_allocator_tag, sizeof(T),
  200. UserAllocator, Mutex, NextSize, MaxSize>::ordered_malloc(n) );
  201. if (ret == 0)
  202. boost::throw_exception(std::bad_alloc());
  203. return ret;
  204. }
  205. static pointer allocate(const size_type n, const void * const)
  206. { return allocate(n); }
  207. static pointer allocate()
  208. {
  209. const pointer ret = static_cast<pointer>(
  210. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  211. UserAllocator, Mutex, NextSize, MaxSize>::malloc)() );
  212. if (ret == 0)
  213. boost::throw_exception(std::bad_alloc());
  214. return ret;
  215. }
  216. static void deallocate(const pointer ptr, const size_type n)
  217. {
  218. #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
  219. if (ptr == 0 || n == 0)
  220. return;
  221. #endif
  222. if (n == 1)
  223. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  224. UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr);
  225. else
  226. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  227. UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr, n);
  228. }
  229. static void deallocate(const pointer ptr)
  230. {
  231. (singleton_pool<fast_pool_allocator_tag, sizeof(T),
  232. UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr);
  233. }
  234. };
  235. template<
  236. typename UserAllocator,
  237. typename Mutex,
  238. unsigned NextSize,
  239. unsigned MaxSize>
  240. class fast_pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>
  241. {
  242. public:
  243. typedef void* pointer;
  244. typedef const void* const_pointer;
  245. typedef void value_type;
  246. template <class U> struct rebind {
  247. typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
  248. };
  249. };
  250. } // namespace boost
  251. #endif