| 1 |
// 2004-10-10 Paolo Carlini <[email protected]> |
| 2 |
|
| 3 |
// Copyright (C) 2004-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
// |
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
// |
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// 20.4.1.1 allocator members |
| 21 |
|
| 22 |
#include <ext/pool_allocator.h> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
|
| 25 |
struct small |
| 26 |
{ |
| 27 |
char c[16]; |
| 28 |
}; |
| 29 |
|
| 30 |
struct big |
| 31 |
{ |
| 32 |
char c[64]; |
| 33 |
}; |
| 34 |
|
| 35 |
bool started = false; |
| 36 |
|
| 37 |
void* |
| 38 |
operator new(size_t n) THROW(std::bad_alloc) |
| 39 |
{ |
| 40 |
if (started) |
| 41 |
{ |
| 42 |
static bool first = true; |
| 43 |
if (!first) |
| 44 |
throw std::bad_alloc(); |
| 45 |
first = false; |
| 46 |
} |
| 47 |
|
| 48 |
return std::malloc(n); |
| 49 |
} |
| 50 |
|
| 51 |
void |
| 52 |
operator delete(void* p) throw() |
| 53 |
{ |
| 54 |
if (p) |
| 55 |
std::free(p); |
| 56 |
} |
| 57 |
|
| 58 |
// http://gcc.gnu.org/ml/libstdc++/2004-10/msg00098.html |
| 59 |
void test01() |
| 60 |
{ |
| 61 |
using __gnu_cxx::__pool_alloc; |
| 62 |
|
| 63 |
__pool_alloc<big> alloc_big; |
| 64 |
(void) alloc_big.allocate(1); |
| 65 |
|
| 66 |
// The constant 20 comes from __pool_alloc_base::_M_refill. See |
| 67 |
// also __pool_alloc_base::_M_allocate_chunk. |
| 68 |
__pool_alloc<small> alloc_small; |
| 69 |
for (unsigned int i = 0; i < 20 * sizeof(big) / sizeof(small) + 1; ++i) |
| 70 |
(void) alloc_small.allocate(1); |
| 71 |
} |
| 72 |
|
| 73 |
int main() |
| 74 |
{ |
| 75 |
started = true; |
| 76 |
test01(); |
| 77 |
started = false; |
| 78 |
} |