aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/experimental/type_erased_allocator/1.cc
blob: 924d728aa4ed7b74d580d1984afd514168acf852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// { dg-options "-std=gnu++14" }

// Copyright (C) 2015-2016 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include <memory>
#include <experimental/memory_resource>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>

using std::experimental::pmr::polymorphic_allocator;
using std::experimental::pmr::memory_resource;
using std::experimental::pmr::new_delete_resource;
using std::experimental::pmr::get_default_resource;
using std::experimental::pmr::set_default_resource;

struct A
{
  A() { ++ctor_count; }
  ~A() { ++dtor_count; }
  static int ctor_count;
  static int dtor_count;
};
int A::ctor_count = 0;
int A::dtor_count = 0;

struct CountedResource : public memory_resource
{
public:
  CountedResource() = default;
  ~ CountedResource() = default;

  static size_t get_alloc_count()  { return alloc_count;  }
  static size_t get_dalloc_count() { return dalloc_count; }

  static size_t  alloc_count;
  static size_t  dalloc_count;
protected:
  void* do_allocate(size_t bytes, size_t alignment)
  {
    alloc_count += bytes;
    if (auto ptr = std::malloc(bytes)) {
      return ptr;
    }
    throw std::bad_alloc();
  }

  void do_deallocate(void *p, size_t bytes, size_t alignment)
  {
    dalloc_count += bytes;
    free(p);
  }

  bool do_is_equal(const memory_resource& __other) const noexcept
  { return this == &__other; }
};
  size_t  CountedResource::alloc_count  = 0;
  size_t  CountedResource::dalloc_count = 0;

void clear()
{
  CountedResource::alloc_count  = 0;
  CountedResource::dalloc_count = 0;
  A::ctor_count = 0;
  A::dtor_count = 0;
}

// memory resource
void test01()
{
  memory_resource* r = new_delete_resource();
  VERIFY(get_default_resource() == r);
  void *p = get_default_resource()->allocate(5);
  VERIFY(p);
  get_default_resource()->deallocate(p, 5);

  clear();
  CountedResource* cr = new CountedResource();
  set_default_resource(cr);
  VERIFY(get_default_resource() == cr);
  void *pc = get_default_resource()->allocate(5);
  VERIFY(pc);
  get_default_resource()->deallocate(pc, 5);
  VERIFY(CountedResource::get_alloc_count()  == 5);
  VERIFY(CountedResource::get_dalloc_count() == 5);
}

// polymorphic_allocator
void test02()
{
  clear();
  {
    CountedResource cr;
    polymorphic_allocator<A> pa(&cr);
    std::vector<A, polymorphic_allocator<A>> v(5, A(), pa);
  }
  VERIFY(A::ctor_count == 1);
  VERIFY(A::dtor_count == 6);
  VERIFY(CountedResource::get_alloc_count()  == 5);
  VERIFY(CountedResource::get_dalloc_count() == 5);
}

void test03() {
  clear();
  CountedResource cr;
  polymorphic_allocator<A> pa(&cr);
  A* p = pa.allocate(1);
  pa.construct(p);
  pa.destroy(p);
  pa.deallocate(p, 1);
  VERIFY(A::ctor_count == 1);
  VERIFY(A::dtor_count == 1);
  VERIFY(CountedResource::get_alloc_count()  == 1);
  VERIFY(CountedResource::get_dalloc_count() == 1);
}

void test04() {
  polymorphic_allocator<A> pa1(get_default_resource());
  polymorphic_allocator<A> pa2(get_default_resource());
  VERIFY(pa1 == pa2);
  polymorphic_allocator<A> pa3 = pa2.select_on_container_copy_construction();
  VERIFY(pa1 == pa3);
}

int main() {
  test01();
  test02();
  test03();
  test04();
  return 0;
}