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 // UNSUPPORTED: c++98, c++03
12 // <experimental/memory_resource>
14 // template <class Alloc> class resource_adaptor_imp;
16 // void * do_allocate(size_t size, size_t align)
17 // void do_deallocate(void*, size_t, size_t)
20 #include <experimental/memory_resource>
21 #include <type_traits>
26 #include "test_macros.h"
27 #include "test_memory_resource.hpp"
29 namespace ex = std::experimental::pmr;
31 template <class Alloc>
32 void check_allocate_deallocate()
34 typedef ex::resource_adaptor<Alloc> R1;
35 const std::size_t max_align = alignof(std::max_align_t);
37 for (std::size_t s = 1; s < 5012; ++s)
39 for(std::size_t align_req = 1; align_req <= (max_align * 2); align_req *= 2)
41 const std::size_t align_exp = align_req > max_align
42 ? max_align : align_req;
45 ex::memory_resource & m1 = r;
47 void * const ret = m1.allocate(s, align_req);
49 assert(P.alloc_count == 1);
50 assert(P.checkAllocAtLeast(ret, s, align_exp));
52 assert(((std::size_t)ret % align_exp) == 0);
54 m1.deallocate(ret, s, align_req);
56 assert(P.dealloc_count == 1);
57 assert(P.checkDeallocMatchesAlloc());
62 void check_alloc_max_size() {
63 using Alloc = NullAllocator<char>;
64 using R1 = ex::resource_adaptor<Alloc>;
65 const std::size_t max_align = alignof(std::max_align_t);
67 auto check = [=](std::size_t s, std::size_t align_req) {
68 const std::size_t align_exp = align_req > max_align
69 ? max_align : align_req;
72 ex::memory_resource & m1 = r;
74 void * const ret = m1.allocate(s, align_req);
76 assert(P.alloc_count == 1);
77 assert(P.checkAllocAtLeast(ret, s, align_exp));
79 m1.deallocate(ret, s, align_req);
81 assert(P.dealloc_count == 1);
82 assert(P.checkDeallocMatchesAlloc());
85 const std::size_t sizeTypeMax = ~0;
86 const std::size_t testSizeStart = sizeTypeMax - (max_align * 3);
87 const std::size_t testSizeEnd = sizeTypeMax - max_align;
89 for (std::size_t size = testSizeStart; size <= testSizeEnd; ++size) {
90 for (std::size_t align=1; align <= (max_align * 2); align *= 2) {
95 #ifndef TEST_HAS_NO_EXCEPTIONS
96 for (std::size_t size = sizeTypeMax; size > testSizeEnd; --size) {
99 ex::memory_resource & m1 = r;
104 } catch (std::exception const&) {
112 check_allocate_deallocate<CountingAllocator<char>>();
113 check_allocate_deallocate<MinAlignedAllocator<char>>();
114 check_alloc_max_size();