| 1 | // 2001-06-14 Benjamin Kosnik <[email protected]> |
| 2 | |
| 3 | // Copyright (C) 2001-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 | // { dg-require-effective-target std_allocator_new } |
| 21 | |
| 22 | // 20.4.1.1 allocator members |
| 23 | |
| 24 | #include <memory> |
| 25 | #include <stdexcept> |
| 26 | #include <cstdlib> |
| 27 | #include <testsuite_hooks.h> |
| 28 | |
| 29 | #if __cplusplus >= 201103L |
| 30 | # define NOTHROW noexcept |
| 31 | #else |
| 32 | # define NOTHROW throw() |
| 33 | #endif |
| 34 | |
| 35 | struct gnu { }; |
| 36 | |
| 37 | bool check_new = false; |
| 38 | bool check_delete = false; |
| 39 | |
| 40 | void* |
| 41 | operator new(std::size_t n) THROW(std::bad_alloc) |
| 42 | { |
| 43 | check_new = true; |
| 44 | return std::malloc(n); |
| 45 | } |
| 46 | |
| 47 | void operator delete(void *v) NOTHROW |
| 48 | { |
| 49 | check_delete = true; |
| 50 | return std::free(v); |
| 51 | } |
| 52 | |
| 53 | #if __cpp_sized_deallocation |
| 54 | void operator delete(void *v, std::size_t) NOTHROW |
| 55 | { |
| 56 | ::operator delete(v); |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | void test01() |
| 61 | { |
| 62 | std::allocator<gnu> obj; |
| 63 | |
| 64 | gnu* pobj = obj.allocate(256); |
| 65 | VERIFY( check_new ); |
| 66 | |
| 67 | obj.deallocate(pobj, 256); |
| 68 | VERIFY( check_delete ); |
| 69 | } |
| 70 | |
| 71 | int main() |
| 72 | { |
| 73 | test01(); |
| 74 | return 0; |
| 75 | } |
| 76 |