test_memory_resource.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_TEST_MEMORY_RESOURCE_H
  9. #define SUPPORT_TEST_MEMORY_RESOURCE_H
  10. #include <experimental/memory_resource>
  11. #include <experimental/utility>
  12. #include <memory>
  13. #include <type_traits>
  14. #include <cstddef>
  15. #include <cstdlib>
  16. #include <cstring>
  17. #include <cstdint>
  18. #include <cassert>
  19. #include "test_macros.h"
  20. #include "controlled_allocators.h"
  21. #include "uses_alloc_types.h"
  22. // FIXME: This is a hack to allow uses_allocator_types.hpp to work with
  23. // erased_type. However we can't define that behavior directly in the header
  24. // because it can't include <experimental/memory_resource>
  25. template <>
  26. struct TransformErasedTypeAlloc<std::experimental::erased_type> {
  27. using type = std::experimental::pmr::polymorphic_allocator<int>;
  28. };
  29. template <class ProviderT, int = 0>
  30. class TestResourceImp : public std::experimental::pmr::memory_resource
  31. {
  32. public:
  33. static int resource_alive;
  34. static int resource_constructed;
  35. static int resource_destructed;
  36. static void resetStatics() {
  37. assert(resource_alive == 0);
  38. resource_alive = 0;
  39. resource_constructed = 0;
  40. resource_destructed = 0;
  41. }
  42. using memory_resource = std::experimental::pmr::memory_resource;
  43. using Provider = ProviderT;
  44. int value;
  45. explicit TestResourceImp(int val = 0) : value(val) {
  46. ++resource_alive;
  47. ++resource_constructed;
  48. }
  49. ~TestResourceImp() noexcept {
  50. --resource_alive;
  51. ++resource_destructed;
  52. }
  53. void reset() { C.reset(); P.reset(); }
  54. AllocController& getController() { return C; }
  55. bool checkAlloc(void* p, std::size_t s, std::size_t a) const
  56. { return C.checkAlloc(p, s, a); }
  57. bool checkDealloc(void* p, std::size_t s, std::size_t a) const
  58. { return C.checkDealloc(p, s, a); }
  59. bool checkIsEqualCalledEq(int n) const { return C.checkIsEqualCalledEq(n); }
  60. protected:
  61. virtual void * do_allocate(std::size_t s, std::size_t a) {
  62. if (C.throw_on_alloc) {
  63. #ifndef TEST_HAS_NO_EXCEPTIONS
  64. throw TestException{};
  65. #else
  66. assert(false);
  67. #endif
  68. }
  69. void* ret = P.allocate(s, a);
  70. C.countAlloc(ret, s, a);
  71. return ret;
  72. }
  73. virtual void do_deallocate(void * p, std::size_t s, std::size_t a) {
  74. C.countDealloc(p, s, a);
  75. P.deallocate(p, s, a);
  76. }
  77. virtual bool do_is_equal(memory_resource const & other) const noexcept {
  78. C.countIsEqual();
  79. TestResourceImp const * o = dynamic_cast<TestResourceImp const *>(&other);
  80. return o && o->value == value;
  81. }
  82. private:
  83. mutable AllocController C;
  84. mutable Provider P;
  85. DISALLOW_COPY(TestResourceImp);
  86. };
  87. template <class Provider, int N>
  88. int TestResourceImp<Provider, N>::resource_alive = 0;
  89. template <class Provider, int N>
  90. int TestResourceImp<Provider, N>::resource_constructed = 0;
  91. template <class Provider, int N>
  92. int TestResourceImp<Provider, N>::resource_destructed = 0;
  93. struct NullProvider {
  94. NullProvider() {}
  95. void* allocate(size_t, size_t) { return nullptr; }
  96. void deallocate(void*, size_t, size_t) {}
  97. void reset() {}
  98. private:
  99. DISALLOW_COPY(NullProvider);
  100. };
  101. struct NewDeleteProvider {
  102. NewDeleteProvider() {}
  103. void* allocate(size_t s, size_t) { return ::operator new(s); }
  104. void deallocate(void* p, size_t, size_t) { ::operator delete(p); }
  105. void reset() {}
  106. private:
  107. DISALLOW_COPY(NewDeleteProvider);
  108. };
  109. template <size_t Size = 4096 * 10> // 10 pages worth of memory.
  110. struct BufferProvider {
  111. char buffer[Size];
  112. void* next = &buffer;
  113. size_t space = Size;
  114. BufferProvider() {}
  115. void* allocate(size_t s, size_t a) {
  116. void* ret = std::align(s, a, next, space);
  117. if (ret == nullptr) {
  118. #ifndef TEST_HAS_NO_EXCEPTIONS
  119. throw std::bad_alloc();
  120. #else
  121. assert(false);
  122. #endif
  123. }
  124. return ret;
  125. }
  126. void deallocate(void*, size_t, size_t) {}
  127. void reset() {
  128. next = &buffer;
  129. space = Size;
  130. }
  131. private:
  132. DISALLOW_COPY(BufferProvider);
  133. };
  134. using NullResource = TestResourceImp<NullProvider, 0>;
  135. using NewDeleteResource = TestResourceImp<NewDeleteProvider, 0>;
  136. using TestResource = TestResourceImp<BufferProvider<>, 0>;
  137. using TestResource1 = TestResourceImp<BufferProvider<>, 1>;
  138. using TestResource2 = TestResourceImp<BufferProvider<>, 2>;
  139. #endif /* SUPPORT_TEST_MEMORY_RESOURCE_H */