1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 // TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed
11 // UNSUPPORTED: availability-pmr-missing
12 
13 // <memory_resource>
14 
15 // class monotonic_buffer_resource
16 
17 #include <memory_resource>
18 #include <cassert>
19 
20 #include "count_new.h"
21 #include "test_macros.h"
22 
main(int,char **)23 int main(int, char**) {
24   globalMemCounter.reset();
25   char buffer[100];
26   auto mono1 = std::pmr::monotonic_buffer_resource(buffer, sizeof buffer, std::pmr::new_delete_resource());
27   std::pmr::memory_resource& r1 = mono1;
28 
29   // Check that construction with a buffer does not allocate anything from the upstream
30   assert(globalMemCounter.checkNewCalledEq(0));
31 
32   // Check that an allocation that fits in the buffer does not allocate anything from the upstream
33   void* ret = r1.allocate(50);
34   assert(ret);
35   assert(globalMemCounter.checkNewCalledEq(0));
36 
37   // Check a second allocation
38   ret = r1.allocate(20);
39   assert(ret);
40   assert(globalMemCounter.checkNewCalledEq(0));
41 
42   r1.deallocate(ret, 50);
43   assert(globalMemCounter.checkDeleteCalledEq(0));
44 
45   // Check an allocation that doesn't fit in the original buffer
46   ret = r1.allocate(50);
47   assert(ret);
48   ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledEq(1));
49 
50   r1.deallocate(ret, 50);
51   assert(globalMemCounter.checkDeleteCalledEq(0));
52 
53   mono1.release();
54   ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkDeleteCalledEq(1));
55   assert(globalMemCounter.checkOutstandingNewEq(0));
56 
57   return 0;
58 }
59