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() {}
33  POSType(ex::memory_resource* r, void* p, int s) : res(r), ptr(p), n(s) {}
34  ~POSType() {
35      if (ptr) {
36          if (!res) res = ex::get_default_resource();
37          res->deallocate(ptr, n);
38      }
39  }
40};
41
42void swap(POSType & L, POSType & R) {
43    std::swap(L.res, R.res);
44    std::swap(L.ptr, R.ptr);
45    std::swap(L.n, R.n);
46}
47
48POSType constructed_before_resources;
49POSType constructed_before_resources2;
50
51// Constructs resources
52ex::memory_resource* resource = ex::get_default_resource();
53
54POSType constructed_after_resources(resource, resource->allocate(1024), 1024);
55POSType constructed_after_resources2(nullptr, resource->allocate(1024), 1024);
56
57int main()
58{
59    swap(constructed_after_resources, constructed_before_resources);
60    swap(constructed_before_resources2, constructed_after_resources2);
61}
62