summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/monotonic_buffer_resource/allocate.cc
blob: 637bdf1b30ec461b8b0d8e78266ea706a24e7024 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (C) 2018-2020 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/>.

// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }
// { dg-require-cstdint "" }

#include <memory_resource>
#include <testsuite_allocator.h>

void
test01()
{
  __gnu_test::memory_resource r;

  // test that it's possible to allocate after each of the constructors
  {
    std::pmr::monotonic_buffer_resource mr(&r);
    auto p = mr.allocate(1024);
    VERIFY( p != nullptr );
    auto q = mr.allocate(1024);
    VERIFY( q != nullptr );
    VERIFY( p != q );
  }
  VERIFY( r.number_of_active_allocations() == 0 );
  {
    std::pmr::monotonic_buffer_resource mr(128, &r);
    auto p = mr.allocate(1024);
    VERIFY( p != nullptr );
    auto q = mr.allocate(1024);
    VERIFY( q != nullptr );
    VERIFY( p != q );
  }
  VERIFY( r.number_of_active_allocations() == 0 );
  {
    unsigned char buf[64];
    std::pmr::monotonic_buffer_resource mr((void*)buf, sizeof(buf), &r);
    auto p = mr.allocate(1024);
    VERIFY( p != nullptr );
    auto q = mr.allocate(1024);
    VERIFY( q != nullptr );
    VERIFY( p != q );
  }
  VERIFY( r.number_of_active_allocations() == 0 );
  {
    std::pmr::monotonic_buffer_resource mr;
    auto p = mr.allocate(1024);
    VERIFY( p != nullptr );
    auto q = mr.allocate(1024);
    VERIFY( q != nullptr );
    VERIFY( p != q );
  }
  {
    std::pmr::monotonic_buffer_resource mr(64);
    auto p = mr.allocate(1024);
    VERIFY( p != nullptr );
    auto q = mr.allocate(1024);
    VERIFY( q != nullptr );
    VERIFY( p != q );
  }
  {
    unsigned char buf[64];
    std::pmr::monotonic_buffer_resource mr((void*)buf, sizeof(buf));
    auto p = mr.allocate(1024);
    VERIFY( p != nullptr );
    auto q = mr.allocate(1024);
    VERIFY( q != nullptr );
    VERIFY( p != q );
  }
}

void
test02()
{
  unsigned char buf[64];
  std::pmr::monotonic_buffer_resource mr(buf, sizeof(buf));

  auto p = mr.allocate(0);
  VERIFY( p != nullptr );
  auto q = mr.allocate(0);
  VERIFY( q != nullptr );
  VERIFY( p != q );

  p = mr.allocate(0, 1);
  VERIFY( p != nullptr );
  q = mr.allocate(0, 1);
  VERIFY( q != nullptr );
  VERIFY( p != q );
}

void
test03()
{
#if __cpp_exceptions
  {
    std::pmr::monotonic_buffer_resource mr(std::pmr::null_memory_resource());
    bool caught = false;
    try
    {
      (void) mr.allocate(1, 1);
    }
    catch (const std::bad_alloc&)
    {
      caught = true;
    }
    VERIFY( caught );
  }
  {
    unsigned char buf[16];
    std::pmr::monotonic_buffer_resource mr(buf, sizeof(buf),
					   std::pmr::null_memory_resource());
    (void) mr.allocate(16, 1);
    bool caught = false;
    try
    {
      (void) mr.allocate(1, 1);
    }
    catch (const std::bad_alloc&)
    {
      caught = true;
    }
    VERIFY( caught );
  }
#endif
}

void
test04()
{
  auto buf = new unsigned char[512];
  std::pmr::monotonic_buffer_resource mr(buf, 512,
					 std::pmr::null_memory_resource());
  std::size_t prev_size = 1;
  void* prev_ptr = mr.allocate(prev_size, 1);
  for (int i = 0; i < 9; ++i)
  {
    std::size_t size = 1 << i;
    void* ptr = mr.allocate(size, 1);
    VERIFY( ((char*)ptr - (char*)prev_ptr) == prev_size );
    prev_ptr = ptr;
    prev_size = size;
  }
}

void
test05()
{
  // test that returned pointer is correctly aligned
  auto is_aligned = [](void* p, size_t alignment) -> bool {
    return (reinterpret_cast<std::uintptr_t>(p) % alignment) == 0;
  };

  auto buf = new unsigned char[2048];
  std::pmr::monotonic_buffer_resource mr(buf+1, 2047);
  for (int i = 0; i < 9; ++i)
  {
    auto p = mr.allocate(1, 1 << i);
    VERIFY( is_aligned(p, 1 << i) );
    // Make next available byte misaligned:
    (void) mr.allocate(1 << i, 1);
  }
}

void
test06()
{
  // check for geometric progression in buffer sizes from upstream

  struct resource : __gnu_test::memory_resource
  {
    bool allocated = false;
    std::size_t last_size = 0;

    void*
    do_allocate(size_t bytes, size_t align) override
    {
      allocated = true;
      last_size = bytes;
      return __gnu_test::memory_resource::do_allocate(bytes, align);
    }
  };

  resource r;
  std::pmr::monotonic_buffer_resource mr(32, &r);
  std::size_t last_size = 0;

  for (int i = 0; i < 100; ++i)
  {
    (void) mr.allocate(16);
    if (r.allocated)
    {
      VERIFY(r.last_size >= last_size);
      last_size = r.last_size;
      r.allocated = false;
    }
  }
}

void
test07()
{
  // Custom exception thrown on expected allocation failure.
  struct very_bad_alloc : std::bad_alloc { };

  struct careful_resource : __gnu_test::memory_resource
  {
    void* do_allocate(std::size_t bytes, std::size_t alignment)
    {
      // pmr::monotonic_buffer_resource::do_allocate is not allowed to
      // throw an exception when asked for an allocation it can't satisfy.
      // The libstdc++ implementation will ask upstream to allocate
      // bytes=SIZE_MAX and alignment=bit_floor(SIZE_MAX) instead of throwing.
      // Verify that we got those values:
      if (bytes != std::numeric_limits<std::size_t>::max())
	VERIFY( !"upstream allocation should request maximum number of bytes" );
      if (alignment != (1 + std::numeric_limits<std::size_t>::max() / 2))
	VERIFY( !"upstream allocation should request maximum alignment" );

      // A successful failure:
      throw very_bad_alloc();
    }
  };

  careful_resource cr;
  std::pmr::monotonic_buffer_resource mbr(&cr);
  try
  {
    // Try to allocate a ridiculous size:
    void* p = mbr.allocate(std::size_t(-2), 1);
    // Should not reach here!
    VERIFY( !"attempt to allocate SIZE_MAX-1 should not have succeeded" );
    throw p;
  }
  catch (const very_bad_alloc&)
  {
    // Should catch this exception from careful_resource::do_allocate
  }
  catch (const std::bad_alloc&)
  {
    VERIFY( !"monotonic_buffer_resource::do_allocate is not allowed to throw" );
  }
}

int
main()
{
  test01();
  test02();
  test03();
  test04();
  test05();
  test06();
  test07();
}