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
10// REQUIRES: c++experimental
11// UNSUPPORTED: c++98, c++03
12
13// <experimental/memory_resource>
14
15// memory_resource * new_delete_resource()
16
17// The lifetime of the value returned by 'new_delete_resource()' should
18// never end, even very late into program termination. This test constructs
19// attempts to use 'new_delete_resource()' very late in program termination
20// to detect lifetime issues.
21
22#include <experimental/memory_resource>
23#include <type_traits>
24#include <cassert>
25
26namespace ex = std::experimental::pmr;
27
28struct POSType {
29  ex::memory_resource* res = nullptr;
30  void* ptr = nullptr;
31  int n = 0;
32  POSType() {res = ex::new_delete_resource(); ptr = res->allocate(42); n = 42; }
33  POSType(ex::memory_resource* r, void* p, int s) : res(r), ptr(p), n(s) {}
34  ~POSType() { if (ptr) res->deallocate(ptr, n); }
35};
36
37void swap(POSType & L, POSType & R) {
38    std::swap(L.res, R.res);
39    std::swap(L.ptr, R.ptr);
40    std::swap(L.n, R.n);
41}
42
43POSType constructed_before_resources;
44
45// Constructs resources
46ex::memory_resource* resource = ex::new_delete_resource();
47
48POSType constructed_after_resources(resource, resource->allocate(1024), 1024);
49
50int main()
51{
52    swap(constructed_after_resources, constructed_before_resources);
53}
54