1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // XFAIL: availability-aligned_allocation-missing
13 // <experimental/memory_resource>
15 // memory_resource * new_delete_resource()
17 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
19 #include <experimental/memory_resource>
20 #include <type_traits>
23 #include "count_new.h"
25 #include "test_macros.h"
27 namespace ex
= std::experimental::pmr
;
29 struct assert_on_compare
: public ex::memory_resource
32 void * do_allocate(std::size_t, size_t) override
33 { assert(false); return nullptr; }
35 void do_deallocate(void *, std::size_t, size_t) override
38 bool do_is_equal(ex::memory_resource
const &) const noexcept override
39 { assert(false); return false; }
45 static_assert(std::is_same
<
46 decltype(ex::new_delete_resource()), ex::memory_resource
*
51 assert(ex::new_delete_resource());
53 // assert same return value
55 assert(ex::new_delete_resource() == ex::new_delete_resource());
63 ex::memory_resource
& r1
= *ex::new_delete_resource();
64 ex::memory_resource
& r2
= *ex::new_delete_resource();
65 // check both calls returned the same object
67 // check for proper equality semantics
75 ex::memory_resource
& r1
= *ex::new_delete_resource();
77 ex::memory_resource
& r2
= c
;
83 void test_allocate_deallocate()
85 ex::memory_resource
& r1
= *ex::new_delete_resource();
87 globalMemCounter
.reset();
89 void *ret
= r1
.allocate(50);
91 assert(globalMemCounter
.checkOutstandingNewEq(1));
92 assert(globalMemCounter
.checkLastNewSizeEq(50));
94 r1
.deallocate(ret
, 1);
95 assert(globalMemCounter
.checkOutstandingNewEq(0));
96 assert(globalMemCounter
.checkDeleteCalledEq(1));
100 int main(int, char**)
102 static_assert(noexcept(ex::new_delete_resource()), "Must be noexcept");
105 test_allocate_deallocate();