cxx11_allocator.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Copyright 2006-2011 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #if !defined(BOOST_UNORDERED_TEST_CXX11_ALLOCATOR_HEADER)
  5. #define BOOST_UNORDERED_TEST_CXX11_ALLOCATOR_HEADER
  6. #include <boost/config.hpp>
  7. #include <boost/limits.hpp>
  8. #include <cstddef>
  9. #include "../helpers/fwd.hpp"
  10. #include "../helpers/memory.hpp"
  11. namespace test {
  12. struct allocator_false
  13. {
  14. enum
  15. {
  16. is_select_on_copy = 0,
  17. is_propagate_on_swap = 0,
  18. is_propagate_on_assign = 0,
  19. is_propagate_on_move = 0,
  20. cxx11_construct = 0
  21. };
  22. };
  23. struct allocator_flags_all
  24. {
  25. enum
  26. {
  27. is_select_on_copy = 1,
  28. is_propagate_on_swap = 1,
  29. is_propagate_on_assign = 1,
  30. is_propagate_on_move = 1,
  31. cxx11_construct = 1
  32. };
  33. };
  34. struct select_copy : allocator_false
  35. {
  36. enum
  37. {
  38. is_select_on_copy = 1
  39. };
  40. };
  41. struct propagate_swap : allocator_false
  42. {
  43. enum
  44. {
  45. is_propagate_on_swap = 1
  46. };
  47. };
  48. struct propagate_assign : allocator_false
  49. {
  50. enum
  51. {
  52. is_propagate_on_assign = 1
  53. };
  54. };
  55. struct propagate_move : allocator_false
  56. {
  57. enum
  58. {
  59. is_propagate_on_move = 1
  60. };
  61. };
  62. struct no_select_copy : allocator_flags_all
  63. {
  64. enum
  65. {
  66. is_select_on_copy = 0
  67. };
  68. };
  69. struct no_propagate_swap : allocator_flags_all
  70. {
  71. enum
  72. {
  73. is_propagate_on_swap = 0
  74. };
  75. };
  76. struct no_propagate_assign : allocator_flags_all
  77. {
  78. enum
  79. {
  80. is_propagate_on_assign = 0
  81. };
  82. };
  83. struct no_propagate_move : allocator_flags_all
  84. {
  85. enum
  86. {
  87. is_propagate_on_move = 0
  88. };
  89. };
  90. template <typename Flag> struct swap_allocator_base
  91. {
  92. struct propagate_on_container_swap
  93. {
  94. enum
  95. {
  96. value = Flag::is_propagate_on_swap
  97. };
  98. };
  99. };
  100. template <typename Flag> struct assign_allocator_base
  101. {
  102. struct propagate_on_container_copy_assignment
  103. {
  104. enum
  105. {
  106. value = Flag::is_propagate_on_assign
  107. };
  108. };
  109. };
  110. template <typename Flag> struct move_allocator_base
  111. {
  112. struct propagate_on_container_move_assignment
  113. {
  114. enum
  115. {
  116. value = Flag::is_propagate_on_move
  117. };
  118. };
  119. };
  120. namespace {
  121. // boostinspect:nounnamed
  122. bool force_equal_allocator_value = false;
  123. }
  124. struct force_equal_allocator
  125. {
  126. bool old_value_;
  127. explicit force_equal_allocator(bool value)
  128. : old_value_(force_equal_allocator_value)
  129. {
  130. force_equal_allocator_value = value;
  131. }
  132. ~force_equal_allocator() { force_equal_allocator_value = old_value_; }
  133. };
  134. template <typename T> struct cxx11_allocator_base
  135. {
  136. int tag_;
  137. int selected_;
  138. typedef std::size_t size_type;
  139. typedef std::ptrdiff_t difference_type;
  140. typedef T* pointer;
  141. typedef T const* const_pointer;
  142. typedef T& reference;
  143. typedef T const& const_reference;
  144. typedef T value_type;
  145. explicit cxx11_allocator_base(int t) : tag_(t), selected_(0)
  146. {
  147. detail::tracker.allocator_ref();
  148. }
  149. template <typename Y>
  150. cxx11_allocator_base(cxx11_allocator_base<Y> const& x)
  151. : tag_(x.tag_), selected_(x.selected_)
  152. {
  153. detail::tracker.allocator_ref();
  154. }
  155. cxx11_allocator_base(cxx11_allocator_base const& x)
  156. : tag_(x.tag_), selected_(x.selected_)
  157. {
  158. detail::tracker.allocator_ref();
  159. }
  160. ~cxx11_allocator_base() { detail::tracker.allocator_unref(); }
  161. pointer address(reference r) { return pointer(&r); }
  162. const_pointer address(const_reference r) { return const_pointer(&r); }
  163. pointer allocate(size_type n)
  164. {
  165. pointer ptr(static_cast<T*>(::operator new(n * sizeof(T))));
  166. detail::tracker.track_allocate((void*)ptr, n, sizeof(T), tag_);
  167. return ptr;
  168. }
  169. pointer allocate(size_type n, void const*)
  170. {
  171. pointer ptr(static_cast<T*>(::operator new(n * sizeof(T))));
  172. detail::tracker.track_allocate((void*)ptr, n, sizeof(T), tag_);
  173. return ptr;
  174. }
  175. void deallocate(pointer p, size_type n)
  176. {
  177. // Only checking tags when propagating swap.
  178. // Note that tags will be tested
  179. // properly in the normal allocator.
  180. detail::tracker.track_deallocate(
  181. (void*)p, n, sizeof(T), tag_, !force_equal_allocator_value);
  182. ::operator delete((void*)p);
  183. }
  184. void construct(T* p, T const& t)
  185. {
  186. detail::tracker.track_construct((void*)p, sizeof(T), tag_);
  187. new (p) T(t);
  188. }
  189. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  190. template <typename... Args>
  191. void construct(T* p, BOOST_FWD_REF(Args)... args)
  192. {
  193. detail::tracker.track_construct((void*)p, sizeof(T), tag_);
  194. new (p) T(boost::forward<Args>(args)...);
  195. }
  196. #endif
  197. void destroy(T* p)
  198. {
  199. detail::tracker.track_destroy((void*)p, sizeof(T), tag_);
  200. p->~T();
  201. }
  202. size_type max_size() const
  203. {
  204. return (std::numeric_limits<size_type>::max)();
  205. }
  206. };
  207. template <typename T, typename Flags = propagate_swap, typename Enable = void>
  208. struct cxx11_allocator;
  209. template <typename T, typename Flags>
  210. struct cxx11_allocator<T, Flags,
  211. typename boost::disable_if_c<Flags::is_select_on_copy>::type>
  212. : public cxx11_allocator_base<T>,
  213. public swap_allocator_base<Flags>,
  214. public assign_allocator_base<Flags>,
  215. public move_allocator_base<Flags>,
  216. Flags
  217. {
  218. #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 402000)
  219. template <typename U> struct rebind
  220. {
  221. typedef cxx11_allocator<U, Flags> other;
  222. };
  223. #endif
  224. explicit cxx11_allocator(int t = 0) : cxx11_allocator_base<T>(t) {}
  225. template <typename Y>
  226. cxx11_allocator(cxx11_allocator<Y, Flags> const& x)
  227. : cxx11_allocator_base<T>(x)
  228. {
  229. }
  230. cxx11_allocator(cxx11_allocator const& x) : cxx11_allocator_base<T>(x) {}
  231. // When not propagating swap, allocators are always equal
  232. // to avoid undefined behaviour.
  233. bool operator==(cxx11_allocator const& x) const
  234. {
  235. return force_equal_allocator_value || (this->tag_ == x.tag_);
  236. }
  237. bool operator!=(cxx11_allocator const& x) const { return !(*this == x); }
  238. };
  239. template <typename T, typename Flags>
  240. struct cxx11_allocator<T, Flags,
  241. typename boost::enable_if_c<Flags::is_select_on_copy>::type>
  242. : public cxx11_allocator_base<T>,
  243. public swap_allocator_base<Flags>,
  244. public assign_allocator_base<Flags>,
  245. public move_allocator_base<Flags>,
  246. Flags
  247. {
  248. cxx11_allocator select_on_container_copy_construction() const
  249. {
  250. cxx11_allocator tmp(*this);
  251. ++tmp.selected_;
  252. return tmp;
  253. }
  254. #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 402000)
  255. template <typename U> struct rebind
  256. {
  257. typedef cxx11_allocator<U, Flags> other;
  258. };
  259. #endif
  260. explicit cxx11_allocator(int t = 0) : cxx11_allocator_base<T>(t) {}
  261. template <typename Y>
  262. cxx11_allocator(cxx11_allocator<Y, Flags> const& x)
  263. : cxx11_allocator_base<T>(x)
  264. {
  265. }
  266. cxx11_allocator(cxx11_allocator const& x) : cxx11_allocator_base<T>(x) {}
  267. // When not propagating swap, allocators are always equal
  268. // to avoid undefined behaviour.
  269. bool operator==(cxx11_allocator const& x) const
  270. {
  271. return force_equal_allocator_value || (this->tag_ == x.tag_);
  272. }
  273. bool operator!=(cxx11_allocator const& x) const { return !(*this == x); }
  274. };
  275. template <typename T, typename Flags>
  276. bool equivalent_impl(cxx11_allocator<T, Flags> const& x,
  277. cxx11_allocator<T, Flags> const& y, test::derived_type)
  278. {
  279. return x.tag_ == y.tag_;
  280. }
  281. // Function to check how many times an allocator has been selected,
  282. // return 0 for other allocators.
  283. struct convert_from_anything
  284. {
  285. template <typename T> convert_from_anything(T const&) {}
  286. };
  287. inline int selected_count(convert_from_anything) { return 0; }
  288. template <typename T, typename Flags>
  289. int selected_count(cxx11_allocator<T, Flags> const& x)
  290. {
  291. return x.selected_;
  292. }
  293. }
  294. #endif