controlled_allocators.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 SUPPORT_CONTROLLED_ALLOCATORS_H
  9. #define SUPPORT_CONTROLLED_ALLOCATORS_H
  10. #include <memory>
  11. #include <type_traits>
  12. #include <cstddef>
  13. #include <cstdlib>
  14. #include <cstring>
  15. #include <cstdint>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "type_id.h"
  19. #if TEST_STD_VER < 11
  20. #error This header requires C++11 or greater
  21. #endif
  22. struct AllocController;
  23. // 'AllocController' is a concrete type that instruments and controls the
  24. // behavior of test allocators.
  25. template <class T, size_t ID = 0>
  26. class CountingAllocator;
  27. // 'CountingAllocator' is an basic implementation of the 'Allocator'
  28. // requirements that use the 'AllocController' interface.
  29. template <class T>
  30. class MinAlignAllocator;
  31. // 'MinAlignAllocator' is an instrumented test type which implements the
  32. // 'Allocator' requirements. 'MinAlignAllocator' ensures that it *never*
  33. // returns a pointer to over-aligned storage. For example
  34. // 'MinAlignPointer<char>{}.allocate(...)' will never a 2-byte aligned
  35. // pointer.
  36. template <class T>
  37. class NullAllocator;
  38. // 'NullAllocator' is an instrumented test type which implements the
  39. // 'Allocator' requirements except that 'allocator' and 'deallocate' are
  40. // nops.
  41. #define DISALLOW_COPY(Type) \
  42. Type(Type const&) = delete; \
  43. Type& operator=(Type const&) = delete
  44. constexpr std::size_t MaxAlignV = alignof(std::max_align_t);
  45. struct TestException {};
  46. struct AllocController {
  47. int copy_constructed = 0;
  48. int move_constructed = 0;
  49. int alive = 0;
  50. int alloc_count = 0;
  51. int dealloc_count = 0;
  52. int is_equal_count = 0;
  53. std::size_t alive_size;
  54. std::size_t allocated_size;
  55. std::size_t deallocated_size;
  56. std::size_t last_size = 0;
  57. std::size_t last_align = 0;
  58. void * last_pointer = 0;
  59. std::size_t last_alloc_size = 0;
  60. std::size_t last_alloc_align = 0;
  61. void * last_alloc_pointer = nullptr;
  62. std::size_t last_dealloc_size = 0;
  63. std::size_t last_dealloc_align = 0;
  64. void * last_dealloc_pointer = nullptr;
  65. bool throw_on_alloc = false;
  66. int construct_called = 0;
  67. void *last_construct_pointer = nullptr;
  68. TypeID const* last_construct_alloc = nullptr;
  69. TypeID const* last_construct_type = nullptr;
  70. TypeID const* last_construct_args = nullptr;
  71. int destroy_called = 0;
  72. void *last_destroy_pointer = nullptr;
  73. TypeID const* last_destroy_alloc = nullptr;
  74. TypeID const* last_destroy_type = nullptr;
  75. AllocController() = default;
  76. void countAlloc(void* p, size_t s, size_t a) {
  77. ++alive;
  78. ++alloc_count;
  79. alive_size += s;
  80. allocated_size += s;
  81. last_pointer = last_alloc_pointer = p;
  82. last_size = last_alloc_size = s;
  83. last_align = last_alloc_align = a;
  84. }
  85. void countDealloc(void* p, size_t s, size_t a) {
  86. --alive;
  87. ++dealloc_count;
  88. alive_size -= s;
  89. deallocated_size += s;
  90. last_pointer = last_dealloc_pointer = p;
  91. last_size = last_dealloc_size = s;
  92. last_align = last_dealloc_align = a;
  93. }
  94. template <class ...Args, class Alloc, class Tp>
  95. void countConstruct(Alloc const&, Tp *p) {
  96. ++construct_called;
  97. last_construct_pointer = p;
  98. last_construct_alloc = &makeTypeID<Alloc>();
  99. last_construct_type = &makeTypeID<Tp>();
  100. last_construct_args = &makeArgumentID<Args...>();
  101. }
  102. template <class Alloc, class Tp>
  103. void countDestroy(Alloc const&, Tp *p) {
  104. ++destroy_called;
  105. last_destroy_alloc = &makeTypeID<Alloc>();
  106. last_destroy_type = &makeTypeID<Tp>();
  107. last_destroy_pointer = p;
  108. }
  109. void reset() { std::memset(this, 0, sizeof(*this)); }
  110. void resetConstructDestroy() {
  111. construct_called = 0;
  112. last_construct_pointer = nullptr;
  113. last_construct_alloc = last_construct_args = last_construct_type = nullptr;
  114. destroy_called = 0;
  115. last_destroy_alloc = nullptr;
  116. last_destroy_pointer = nullptr;
  117. }
  118. public:
  119. bool checkAlloc(void* p, size_t s, size_t a) const {
  120. return p == last_alloc_pointer &&
  121. s == last_alloc_size &&
  122. a == last_alloc_align;
  123. }
  124. bool checkAlloc(void* p, size_t s) const {
  125. return p == last_alloc_pointer &&
  126. s == last_alloc_size;
  127. }
  128. bool checkAllocAtLeast(void* p, size_t s, size_t a) const {
  129. return p == last_alloc_pointer &&
  130. s <= last_alloc_size &&
  131. a <= last_alloc_align;
  132. }
  133. bool checkAllocAtLeast(void* p, size_t s) const {
  134. return p == last_alloc_pointer &&
  135. s <= last_alloc_size;
  136. }
  137. bool checkDealloc(void* p, size_t s, size_t a) const {
  138. return p == last_dealloc_pointer &&
  139. s == last_dealloc_size &&
  140. a == last_dealloc_align;
  141. }
  142. bool checkDealloc(void* p, size_t s) const {
  143. return p == last_dealloc_pointer &&
  144. s == last_dealloc_size;
  145. }
  146. bool checkDeallocMatchesAlloc() const {
  147. return last_dealloc_pointer == last_alloc_pointer &&
  148. last_dealloc_size == last_alloc_size &&
  149. last_dealloc_align == last_alloc_align;
  150. }
  151. template <class ...Args, class Alloc, class Tp>
  152. bool checkConstruct(Alloc const&, Tp *p) const {
  153. auto expectAlloc = &makeTypeID<Alloc>();
  154. auto expectTp = &makeTypeID<Tp>();
  155. auto expectArgs = &makeArgumentID<Args...>();
  156. return last_construct_pointer == p &&
  157. COMPARE_TYPEID(last_construct_alloc, expectAlloc) &&
  158. COMPARE_TYPEID(last_construct_type, expectTp) &&
  159. COMPARE_TYPEID(last_construct_args, expectArgs);
  160. }
  161. template <class Alloc, class Tp>
  162. bool checkDestroy(Alloc const&, Tp *p) const {
  163. return last_destroy_pointer == p &&
  164. last_destroy_alloc == &makeTypeID<Alloc>() &&
  165. last_destroy_type == &makeTypeID<Tp>();
  166. }
  167. bool checkDestroyMatchesConstruct() const {
  168. return last_destroy_pointer == last_construct_pointer &&
  169. last_destroy_type == last_construct_type;
  170. }
  171. void countIsEqual() {
  172. ++is_equal_count;
  173. }
  174. bool checkIsEqualCalledEq(int n) const {
  175. return is_equal_count == n;
  176. }
  177. private:
  178. DISALLOW_COPY(AllocController);
  179. };
  180. template <class T, size_t ID>
  181. class CountingAllocator
  182. {
  183. public:
  184. typedef T value_type;
  185. typedef T* pointer;
  186. template <class U>
  187. struct rebind { using other = CountingAllocator<U, ID>; };
  188. CountingAllocator() = delete;
  189. explicit CountingAllocator(AllocController& PP) : P(&PP) {}
  190. CountingAllocator(CountingAllocator const& other) : P(other.P) {
  191. P->copy_constructed += 1;
  192. }
  193. CountingAllocator(CountingAllocator&& other) : P(other.P) {
  194. P->move_constructed += 1;
  195. }
  196. template <class U>
  197. CountingAllocator(CountingAllocator<U, ID> const& other) TEST_NOEXCEPT : P(other.P) {
  198. P->copy_constructed += 1;
  199. }
  200. template <class U>
  201. CountingAllocator(CountingAllocator<U, ID>&& other) TEST_NOEXCEPT : P(other.P) {
  202. P->move_constructed += 1;
  203. }
  204. T* allocate(std::size_t n)
  205. {
  206. void* ret = ::operator new(n*sizeof(T));
  207. P->countAlloc(ret, n*sizeof(T), alignof(T));
  208. return static_cast<T*>(ret);
  209. }
  210. void deallocate(T* p, std::size_t n)
  211. {
  212. void* vp = static_cast<void*>(p);
  213. P->countDealloc(vp, n*sizeof(T), alignof(T));
  214. ::operator delete(vp);
  215. }
  216. template <class U, class ...Args>
  217. void construct(U *p, Args&&... args) {
  218. ::new ((void*)p) U(std::forward<Args>(args)...);
  219. P->countConstruct<Args&&...>(*this, p);
  220. }
  221. template <class U>
  222. void destroy(U* p) {
  223. p->~U();
  224. P->countDestroy(*this, p);
  225. }
  226. AllocController& getController() const { return *P; }
  227. private:
  228. template <class Tp, size_t XID> friend class CountingAllocator;
  229. AllocController *P;
  230. };
  231. template <size_t ID>
  232. class CountingAllocator<void, ID>
  233. {
  234. public:
  235. typedef void* pointer;
  236. typedef const void* const_pointer;
  237. typedef void value_type;
  238. template <class U>
  239. struct rebind { using other = CountingAllocator<U, ID>; };
  240. CountingAllocator() = delete;
  241. explicit CountingAllocator(AllocController& PP) : P(&PP) {}
  242. CountingAllocator(CountingAllocator const& other) : P(other.P) {
  243. P->copy_constructed += 1;
  244. }
  245. CountingAllocator(CountingAllocator&& other) : P(other.P) {
  246. P->move_constructed += 1;
  247. }
  248. template <class U>
  249. CountingAllocator(CountingAllocator<U, ID> const& other) TEST_NOEXCEPT : P(other.P) {
  250. P->copy_constructed += 1;
  251. }
  252. template <class U>
  253. CountingAllocator(CountingAllocator<U, ID>&& other) TEST_NOEXCEPT : P(other.P) {
  254. P->move_constructed += 1;
  255. }
  256. void construct(...) = delete;
  257. void destroy(void*) = delete;
  258. AllocController& getController() const { return *P; }
  259. private:
  260. template <class Tp, size_t> friend class CountingAllocator;
  261. AllocController *P;
  262. };
  263. template <class T, class U, size_t ID>
  264. inline bool operator==(CountingAllocator<T, ID> const& x,
  265. CountingAllocator<U, ID> const& y) {
  266. return &x.getController() == &y.getController();
  267. }
  268. template <class T, class U, size_t ID>
  269. inline bool operator!=(CountingAllocator<T, ID> const& x,
  270. CountingAllocator<U, ID> const& y) {
  271. return !(x == y);
  272. }
  273. template <class T>
  274. class MinAlignedAllocator
  275. {
  276. public:
  277. typedef T value_type;
  278. typedef T* pointer;
  279. MinAlignedAllocator() = delete;
  280. explicit MinAlignedAllocator(AllocController& R) : P(&R) {}
  281. MinAlignedAllocator(MinAlignedAllocator const& other) : P(other.P) {
  282. P->copy_constructed += 1;
  283. }
  284. MinAlignedAllocator(MinAlignedAllocator&& other) : P(other.P) {
  285. P->move_constructed += 1;
  286. }
  287. template <class U>
  288. MinAlignedAllocator(MinAlignedAllocator<U> const& other) TEST_NOEXCEPT : P(other.P) {
  289. P->copy_constructed += 1;
  290. }
  291. template <class U>
  292. MinAlignedAllocator(MinAlignedAllocator<U>&& other) TEST_NOEXCEPT : P(other.P) {
  293. P->move_constructed += 1;
  294. }
  295. T* allocate(std::size_t n) {
  296. char* aligned_ptr = (char*)::operator new(alloc_size(n*sizeof(T)));
  297. assert(is_max_aligned(aligned_ptr));
  298. char* unaligned_ptr = aligned_ptr + alignof(T);
  299. assert(is_min_aligned(unaligned_ptr));
  300. P->countAlloc(unaligned_ptr, n * sizeof(T), alignof(T));
  301. return ((T*)unaligned_ptr);
  302. }
  303. void deallocate(T* p, std::size_t n) {
  304. assert(is_min_aligned(p));
  305. char* aligned_ptr = ((char*)p) - alignof(T);
  306. assert(is_max_aligned(aligned_ptr));
  307. P->countDealloc(p, n*sizeof(T), alignof(T));
  308. return ::operator delete(static_cast<void*>(aligned_ptr));
  309. }
  310. template <class U, class ...Args>
  311. void construct(U *p, Args&&... args) {
  312. auto *c = ::new ((void*)p) U(std::forward<Args>(args)...);
  313. P->countConstruct<Args&&...>(*this, p);
  314. }
  315. template <class U>
  316. void destroy(U* p) {
  317. p->~U();
  318. P->countDestroy(*this, p);
  319. }
  320. AllocController& getController() const { return *P; }
  321. private:
  322. static const std::size_t BlockSize = alignof(std::max_align_t);
  323. static std::size_t alloc_size(std::size_t s) {
  324. std::size_t bytes = (s + BlockSize - 1) & ~(BlockSize - 1);
  325. bytes += BlockSize;
  326. assert(bytes % BlockSize == 0);
  327. return bytes;
  328. }
  329. static bool is_max_aligned(void* p) {
  330. return reinterpret_cast<std::uintptr_t>(p) % BlockSize == 0;
  331. }
  332. static bool is_min_aligned(void* p) {
  333. if (alignof(T) == BlockSize) {
  334. return is_max_aligned(p);
  335. } else {
  336. return reinterpret_cast<std::uintptr_t>(p) % BlockSize == alignof(T);
  337. }
  338. }
  339. template <class Tp> friend class MinAlignedAllocator;
  340. mutable AllocController *P;
  341. };
  342. template <class T, class U>
  343. inline bool operator==(MinAlignedAllocator<T> const& x,
  344. MinAlignedAllocator<U> const& y) {
  345. return &x.getController() == &y.getController();
  346. }
  347. template <class T, class U>
  348. inline bool operator!=(MinAlignedAllocator<T> const& x,
  349. MinAlignedAllocator<U> const& y) {
  350. return !(x == y);
  351. }
  352. template <class T>
  353. class NullAllocator
  354. {
  355. public:
  356. typedef T value_type;
  357. typedef T* pointer;
  358. NullAllocator() = delete;
  359. explicit NullAllocator(AllocController& PP) : P(&PP) {}
  360. NullAllocator(NullAllocator const& other) : P(other.P) {
  361. P->copy_constructed += 1;
  362. }
  363. NullAllocator(NullAllocator&& other) : P(other.P) {
  364. P->move_constructed += 1;
  365. }
  366. template <class U>
  367. NullAllocator(NullAllocator<U> const& other) TEST_NOEXCEPT : P(other.P) {
  368. P->copy_constructed += 1;
  369. }
  370. template <class U>
  371. NullAllocator(NullAllocator<U>&& other) TEST_NOEXCEPT : P(other.P) {
  372. P->move_constructed += 1;
  373. }
  374. T* allocate(std::size_t n)
  375. {
  376. P->countAlloc(nullptr, n*sizeof(T), alignof(T));
  377. return nullptr;
  378. }
  379. void deallocate(T* p, std::size_t n)
  380. {
  381. void* vp = static_cast<void*>(p);
  382. P->countDealloc(vp, n*sizeof(T), alignof(T));
  383. }
  384. AllocController& getController() const { return *P; }
  385. private:
  386. template <class Tp> friend class NullAllocator;
  387. AllocController *P;
  388. };
  389. template <class T, class U>
  390. inline bool operator==(NullAllocator<T> const& x,
  391. NullAllocator<U> const& y) {
  392. return &x.getController() == &y.getController();
  393. }
  394. template <class T, class U>
  395. inline bool operator!=(NullAllocator<T> const& x,
  396. NullAllocator<U> const& y) {
  397. return !(x == y);
  398. }
  399. #endif /* SUPPORT_CONTROLLED_ALLOCATORS_H */