test_memory_resource.hpp 13 KB

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