test_allocator.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef TEST_ALLOCATOR_H
  9. #define TEST_ALLOCATOR_H
  10. #include <type_traits>
  11. #include <new>
  12. #include <memory>
  13. #include <utility>
  14. #include <cstddef>
  15. #include <cstdlib>
  16. #include <climits>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. template <class Alloc>
  20. inline typename std::allocator_traits<Alloc>::size_type
  21. alloc_max_size(Alloc const &a) {
  22. typedef std::allocator_traits<Alloc> AT;
  23. return AT::max_size(a);
  24. }
  25. class test_alloc_base
  26. {
  27. protected:
  28. static int time_to_throw;
  29. public:
  30. static int throw_after;
  31. static int count;
  32. static int alloc_count;
  33. static int copied;
  34. static int moved;
  35. static int converted;
  36. const static int destructed_value = -1;
  37. const static int default_value = 0;
  38. const static int moved_value = INT_MAX;
  39. static void clear() {
  40. assert(count == 0 && "clearing leaking allocator data?");
  41. count = 0;
  42. time_to_throw = 0;
  43. alloc_count = 0;
  44. throw_after = INT_MAX;
  45. clear_ctor_counters();
  46. }
  47. static void clear_ctor_counters() {
  48. copied = 0;
  49. moved = 0;
  50. converted = 0;
  51. }
  52. };
  53. int test_alloc_base::count = 0;
  54. int test_alloc_base::time_to_throw = 0;
  55. int test_alloc_base::alloc_count = 0;
  56. int test_alloc_base::throw_after = INT_MAX;
  57. int test_alloc_base::copied = 0;
  58. int test_alloc_base::moved = 0;
  59. int test_alloc_base::converted = 0;
  60. template <class T>
  61. class test_allocator
  62. : public test_alloc_base
  63. {
  64. int data_; // participates in equality
  65. int id_; // unique identifier, doesn't participate in equality
  66. template <class U> friend class test_allocator;
  67. public:
  68. typedef unsigned size_type;
  69. typedef int difference_type;
  70. typedef T value_type;
  71. typedef value_type* pointer;
  72. typedef const value_type* const_pointer;
  73. typedef typename std::add_lvalue_reference<value_type>::type reference;
  74. typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
  75. template <class U> struct rebind {typedef test_allocator<U> other;};
  76. test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {++count;}
  77. explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id)
  78. {++count;}
  79. test_allocator(const test_allocator& a) TEST_NOEXCEPT : data_(a.data_),
  80. id_(a.id_) {
  81. ++count;
  82. ++copied;
  83. assert(a.data_ != destructed_value && a.id_ != destructed_value &&
  84. "copying from destroyed allocator");
  85. }
  86. #if TEST_STD_VER >= 11
  87. test_allocator(test_allocator&& a) TEST_NOEXCEPT : data_(a.data_),
  88. id_(a.id_) {
  89. ++count;
  90. ++moved;
  91. assert(a.data_ != destructed_value && a.id_ != destructed_value &&
  92. "moving from destroyed allocator");
  93. a.data_ = moved_value;
  94. a.id_ = moved_value;
  95. }
  96. #endif
  97. template <class U>
  98. test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT : data_(a.data_),
  99. id_(a.id_) {
  100. ++count;
  101. ++converted;
  102. }
  103. ~test_allocator() TEST_NOEXCEPT {
  104. assert(data_ >= 0); assert(id_ >= 0);
  105. --count;
  106. data_ = destructed_value;
  107. id_ = destructed_value;
  108. }
  109. pointer address(reference x) const {return &x;}
  110. const_pointer address(const_reference x) const {return &x;}
  111. pointer allocate(size_type n, const void* = 0)
  112. {
  113. assert(data_ >= 0);
  114. if (time_to_throw >= throw_after) {
  115. #ifndef TEST_HAS_NO_EXCEPTIONS
  116. throw std::bad_alloc();
  117. #else
  118. std::terminate();
  119. #endif
  120. }
  121. ++time_to_throw;
  122. ++alloc_count;
  123. return (pointer)::operator new(n * sizeof(T));
  124. }
  125. void deallocate(pointer p, size_type)
  126. {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p);}
  127. size_type max_size() const TEST_NOEXCEPT
  128. {return UINT_MAX / sizeof(T);}
  129. #if TEST_STD_VER < 11
  130. void construct(pointer p, const T& val)
  131. {::new(static_cast<void*>(p)) T(val);}
  132. #else
  133. template <class U> void construct(pointer p, U&& val)
  134. {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
  135. #endif
  136. void destroy(pointer p)
  137. {p->~T();}
  138. friend bool operator==(const test_allocator& x, const test_allocator& y)
  139. {return x.data_ == y.data_;}
  140. friend bool operator!=(const test_allocator& x, const test_allocator& y)
  141. {return !(x == y);}
  142. int get_data() const { return data_; }
  143. int get_id() const { return id_; }
  144. };
  145. template <class T>
  146. class non_default_test_allocator
  147. : public test_alloc_base
  148. {
  149. int data_;
  150. template <class U> friend class non_default_test_allocator;
  151. public:
  152. typedef unsigned size_type;
  153. typedef int difference_type;
  154. typedef T value_type;
  155. typedef value_type* pointer;
  156. typedef const value_type* const_pointer;
  157. typedef typename std::add_lvalue_reference<value_type>::type reference;
  158. typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
  159. template <class U> struct rebind {typedef non_default_test_allocator<U> other;};
  160. // non_default_test_allocator() TEST_NOEXCEPT : data_(0) {++count;}
  161. explicit non_default_test_allocator(int i) TEST_NOEXCEPT : data_(i) {++count;}
  162. non_default_test_allocator(const non_default_test_allocator& a) TEST_NOEXCEPT
  163. : data_(a.data_) {++count;}
  164. template <class U> non_default_test_allocator(const non_default_test_allocator<U>& a) TEST_NOEXCEPT
  165. : data_(a.data_) {++count;}
  166. ~non_default_test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;}
  167. pointer address(reference x) const {return &x;}
  168. const_pointer address(const_reference x) const {return &x;}
  169. pointer allocate(size_type n, const void* = 0)
  170. {
  171. assert(data_ >= 0);
  172. if (time_to_throw >= throw_after) {
  173. #ifndef TEST_HAS_NO_EXCEPTIONS
  174. throw std::bad_alloc();
  175. #else
  176. std::terminate();
  177. #endif
  178. }
  179. ++time_to_throw;
  180. ++alloc_count;
  181. return (pointer)::operator new (n * sizeof(T));
  182. }
  183. void deallocate(pointer p, size_type)
  184. {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); }
  185. size_type max_size() const TEST_NOEXCEPT
  186. {return UINT_MAX / sizeof(T);}
  187. #if TEST_STD_VER < 11
  188. void construct(pointer p, const T& val)
  189. {::new(static_cast<void*>(p)) T(val);}
  190. #else
  191. template <class U> void construct(pointer p, U&& val)
  192. {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
  193. #endif
  194. void destroy(pointer p) {p->~T();}
  195. friend bool operator==(const non_default_test_allocator& x, const non_default_test_allocator& y)
  196. {return x.data_ == y.data_;}
  197. friend bool operator!=(const non_default_test_allocator& x, const non_default_test_allocator& y)
  198. {return !(x == y);}
  199. };
  200. template <>
  201. class test_allocator<void>
  202. : public test_alloc_base
  203. {
  204. int data_;
  205. int id_;
  206. template <class U> friend class test_allocator;
  207. public:
  208. typedef unsigned size_type;
  209. typedef int difference_type;
  210. typedef void value_type;
  211. typedef value_type* pointer;
  212. typedef const value_type* const_pointer;
  213. template <class U> struct rebind {typedef test_allocator<U> other;};
  214. test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {}
  215. explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id) {}
  216. test_allocator(const test_allocator& a) TEST_NOEXCEPT
  217. : data_(a.data_), id_(a.id_) {}
  218. template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT
  219. : data_(a.data_), id_(a.id_) {}
  220. ~test_allocator() TEST_NOEXCEPT {data_ = -1; id_ = -1; }
  221. int get_id() const { return id_; }
  222. int get_data() const { return data_; }
  223. friend bool operator==(const test_allocator& x, const test_allocator& y)
  224. {return x.data_ == y.data_;}
  225. friend bool operator!=(const test_allocator& x, const test_allocator& y)
  226. {return !(x == y);}
  227. };
  228. template <class T>
  229. class other_allocator
  230. {
  231. int data_;
  232. template <class U> friend class other_allocator;
  233. public:
  234. typedef T value_type;
  235. other_allocator() : data_(-1) {}
  236. explicit other_allocator(int i) : data_(i) {}
  237. template <class U> other_allocator(const other_allocator<U>& a)
  238. : data_(a.data_) {}
  239. T* allocate(std::size_t n)
  240. {return (T*)::operator new(n * sizeof(T));}
  241. void deallocate(T* p, std::size_t)
  242. {::operator delete((void*)p);}
  243. other_allocator select_on_container_copy_construction() const
  244. {return other_allocator(-2);}
  245. friend bool operator==(const other_allocator& x, const other_allocator& y)
  246. {return x.data_ == y.data_;}
  247. friend bool operator!=(const other_allocator& x, const other_allocator& y)
  248. {return !(x == y);}
  249. typedef std::true_type propagate_on_container_copy_assignment;
  250. typedef std::true_type propagate_on_container_move_assignment;
  251. typedef std::true_type propagate_on_container_swap;
  252. #if TEST_STD_VER < 11
  253. std::size_t max_size() const
  254. {return UINT_MAX / sizeof(T);}
  255. #endif
  256. };
  257. #if TEST_STD_VER >= 11
  258. struct Ctor_Tag {};
  259. template <typename T> class TaggingAllocator;
  260. struct Tag_X {
  261. // All constructors must be passed the Tag type.
  262. // DefaultInsertable into vector<X, TaggingAllocator<X>>,
  263. Tag_X(Ctor_Tag) {}
  264. // CopyInsertable into vector<X, TaggingAllocator<X>>,
  265. Tag_X(Ctor_Tag, const Tag_X&) {}
  266. // MoveInsertable into vector<X, TaggingAllocator<X>>, and
  267. Tag_X(Ctor_Tag, Tag_X&&) {}
  268. // EmplaceConstructible into vector<X, TaggingAllocator<X>> from args.
  269. template<typename... Args>
  270. Tag_X(Ctor_Tag, Args&&...) { }
  271. // not DefaultConstructible, CopyConstructible or MoveConstructible.
  272. Tag_X() = delete;
  273. Tag_X(const Tag_X&) = delete;
  274. Tag_X(Tag_X&&) = delete;
  275. // CopyAssignable.
  276. Tag_X& operator=(const Tag_X&) { return *this; }
  277. // MoveAssignable.
  278. Tag_X& operator=(Tag_X&&) { return *this; }
  279. private:
  280. // Not Destructible.
  281. ~Tag_X() { }
  282. // Erasable from vector<X, TaggingAllocator<X>>.
  283. friend class TaggingAllocator<Tag_X>;
  284. };
  285. template<typename T>
  286. class TaggingAllocator {
  287. public:
  288. using value_type = T;
  289. TaggingAllocator() = default;
  290. template<typename U>
  291. TaggingAllocator(const TaggingAllocator<U>&) { }
  292. T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }
  293. void deallocate(T* p, std::size_t n) { std::allocator<T>{}.deallocate(p, n); }
  294. template<typename... Args>
  295. void construct(Tag_X* p, Args&&... args)
  296. { ::new((void*)p) Tag_X(Ctor_Tag{}, std::forward<Args>(args)...); }
  297. template<typename U, typename... Args>
  298. void construct(U* p, Args&&... args)
  299. { ::new((void*)p) U(std::forward<Args>(args)...); }
  300. template<typename U, typename... Args>
  301. void destroy(U* p)
  302. { p->~U(); }
  303. };
  304. template<typename T, typename U>
  305. bool
  306. operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
  307. { return true; }
  308. template<typename T, typename U>
  309. bool
  310. operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
  311. { return false; }
  312. #endif
  313. template <std::size_t MaxAllocs>
  314. struct limited_alloc_handle {
  315. std::size_t outstanding_;
  316. void* last_alloc_;
  317. limited_alloc_handle() : outstanding_(0), last_alloc_(nullptr) {}
  318. template <class T>
  319. T *allocate(std::size_t N) {
  320. if (N + outstanding_ > MaxAllocs)
  321. TEST_THROW(std::bad_alloc());
  322. last_alloc_ = ::operator new(N*sizeof(T));
  323. outstanding_ += N;
  324. return static_cast<T*>(last_alloc_);
  325. }
  326. void deallocate(void* ptr, std::size_t N) {
  327. if (ptr == last_alloc_) {
  328. last_alloc_ = nullptr;
  329. assert(outstanding_ >= N);
  330. outstanding_ -= N;
  331. }
  332. ::operator delete(ptr);
  333. }
  334. };
  335. template <class T, std::size_t N>
  336. class limited_allocator
  337. {
  338. template <class U, std::size_t UN> friend class limited_allocator;
  339. typedef limited_alloc_handle<N> BuffT;
  340. std::shared_ptr<BuffT> handle_;
  341. public:
  342. typedef T value_type;
  343. typedef value_type* pointer;
  344. typedef const value_type* const_pointer;
  345. typedef value_type& reference;
  346. typedef const value_type& const_reference;
  347. typedef std::size_t size_type;
  348. typedef std::ptrdiff_t difference_type;
  349. template <class U> struct rebind { typedef limited_allocator<U, N> other; };
  350. limited_allocator() : handle_(new BuffT) {}
  351. limited_allocator(limited_allocator const& other) : handle_(other.handle_) {}
  352. template <class U>
  353. explicit limited_allocator(limited_allocator<U, N> const& other)
  354. : handle_(other.handle_) {}
  355. private:
  356. limited_allocator& operator=(const limited_allocator&);// = delete;
  357. public:
  358. pointer allocate(size_type n) { return handle_->template allocate<T>(n); }
  359. void deallocate(pointer p, size_type n) { handle_->deallocate(p, n); }
  360. size_type max_size() const {return N;}
  361. BuffT* getHandle() const { return handle_.get(); }
  362. };
  363. template <class T, class U, std::size_t N>
  364. inline bool operator==(limited_allocator<T, N> const& LHS,
  365. limited_allocator<U, N> const& RHS) {
  366. return LHS.getHandle() == RHS.getHandle();
  367. }
  368. template <class T, class U, std::size_t N>
  369. inline bool operator!=(limited_allocator<T, N> const& LHS,
  370. limited_allocator<U, N> const& RHS) {
  371. return !(LHS == RHS);
  372. }
  373. #endif // TEST_ALLOCATOR_H