1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef SUPPORT_TEST_MEMORY_RESOURCE_HPP
11 #define SUPPORT_TEST_MEMORY_RESOURCE_HPP
13 #include <experimental/memory_resource>
14 #include <experimental/utility>
16 #include <type_traits>
22 #include "test_macros.h"
23 #include "controlled_allocators.hpp"
24 #include "uses_alloc_types.hpp"
26 // FIXME: This is a hack to allow uses_allocator_types.hpp to work with
27 // erased_type. However we can't define that behavior directly in the header
28 // because it con't include <experimental/memory_resource>
30 struct TransformErasedTypeAlloc<std::experimental::erased_type> {
31 using type = std::experimental::pmr::memory_resource*;
34 template <class ProviderT, int = 0>
35 class TestResourceImp : public std::experimental::pmr::memory_resource
38 static int resource_alive;
39 static int resource_constructed;
40 static int resource_destructed;
42 static void resetStatics() {
43 assert(resource_alive == 0);
45 resource_constructed = 0;
46 resource_destructed = 0;
49 using memory_resource = std::experimental::pmr::memory_resource;
50 using Provider = ProviderT;
54 explicit TestResourceImp(int val = 0) : value(val) {
56 ++resource_constructed;
59 ~TestResourceImp() noexcept {
61 ++resource_destructed;
64 void reset() { C.reset(); P.reset(); }
65 AllocController& getController() { return C; }
67 bool checkAlloc(void* p, std::size_t s, std::size_t a) const
68 { return C.checkAlloc(p, s, a); }
70 bool checkDealloc(void* p, std::size_t s, std::size_t a) const
71 { return C.checkDealloc(p, s, a); }
73 bool checkIsEqualCalledEq(int n) const { return C.checkIsEqualCalledEq(n); }
76 virtual void * do_allocate(std::size_t s, std::size_t a) {
77 if (C.throw_on_alloc) {
78 #ifndef TEST_HAS_NO_EXCEPTIONS
79 throw TestException{};
84 void* ret = P.allocate(s, a);
85 C.countAlloc(ret, s, a);
89 virtual void do_deallocate(void * p, std::size_t s, std::size_t a) {
90 C.countDealloc(p, s, a);
91 P.deallocate(p, s, a);
94 virtual bool do_is_equal(memory_resource const & other) const noexcept {
96 TestResourceImp const * o = dynamic_cast<TestResourceImp const *>(&other);
97 return o && o->value == value;
100 mutable AllocController C;
102 DISALLOW_COPY(TestResourceImp);
105 template <class Provider, int N>
106 int TestResourceImp<Provider, N>::resource_alive = 0;
108 template <class Provider, int N>
109 int TestResourceImp<Provider, N>::resource_constructed = 0;
111 template <class Provider, int N>
112 int TestResourceImp<Provider, N>::resource_destructed = 0;
115 struct NullProvider {
117 void* allocate(size_t, size_t) { return nullptr; }
118 void deallocate(void*, size_t, size_t) {}
121 DISALLOW_COPY(NullProvider);
124 struct NewDeleteProvider {
125 NewDeleteProvider() {}
126 void* allocate(size_t s, size_t) { return ::operator new(s); }
127 void deallocate(void* p, size_t, size_t) { ::operator delete(p); }
130 DISALLOW_COPY(NewDeleteProvider);
133 template <size_t Size = 4096 * 10> // 10 pages worth of memory.
134 struct BufferProvider {
136 void* next = &buffer;
141 void* allocate(size_t s, size_t a) {
142 void* ret = std::align(s, a, next, space);
143 if (ret == nullptr) {
144 #ifndef TEST_HAS_NO_EXCEPTIONS
145 throw std::bad_alloc();
154 void deallocate(void*, size_t, size_t) {}
161 DISALLOW_COPY(BufferProvider);
164 using NullResource = TestResourceImp<NullProvider, 0>;
165 using NewDeleteResource = TestResourceImp<NewDeleteProvider, 0>;
166 using TestResource = TestResourceImp<BufferProvider<>, 0>;
167 using TestResource1 = TestResourceImp<BufferProvider<>, 1>;
168 using TestResource2 = TestResourceImp<BufferProvider<>, 2>;
171 #endif /* SUPPORT_TEST_MEMORY_RESOURCE_HPP */