| 1 |
// { dg-do run { target c++11 } } |
| 2 |
// { dg-require-effective-target hosted } |
| 3 |
|
| 4 |
// Copyright (C) 2013-2025 Free Software Foundation, Inc. |
| 5 |
// |
| 6 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 7 |
// software; you can redistribute it and/or modify it under the |
| 8 |
// terms of the GNU General Public License as published by the |
| 9 |
// Free Software Foundation; either version 3, or (at your option) |
| 10 |
// any later version. |
| 11 |
|
| 12 |
// This library is distributed in the hope that it will be useful, |
| 13 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
// GNU General Public License for more details. |
| 16 |
|
| 17 |
// You should have received a copy of the GNU General Public License along |
| 18 |
// with this library; see the file COPYING3. If not see |
| 19 |
// <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
#include <memory> |
| 22 |
#include <testsuite_hooks.h> |
| 23 |
|
| 24 |
struct X { }; |
| 25 |
|
| 26 |
using spcd = std::_Sp_counted_deleter<X*, std::default_delete<X>, |
| 27 |
std::allocator<void>, std::__default_lock_policy>; |
| 28 |
|
| 29 |
namespace std |
| 30 |
{ |
| 31 |
template<> |
| 32 |
struct allocator<spcd> |
| 33 |
{ |
| 34 |
using value_type = spcd; |
| 35 |
|
| 36 |
allocator() = default; |
| 37 |
|
| 38 |
template<typename U> |
| 39 |
allocator(const allocator<U>&) { } |
| 40 |
|
| 41 |
value_type* allocate(size_t n) |
| 42 |
{ |
| 43 |
if (n != 1) |
| 44 |
throw bad_alloc(); |
| 45 |
allocated = true; |
| 46 |
return static_cast<value_type*>((void*)(storage)); |
| 47 |
} |
| 48 |
|
| 49 |
void deallocate(value_type* p, size_t n) |
| 50 |
{ |
| 51 |
VERIFY(n == 1 && p == (void*)storage && allocated); |
| 52 |
allocated = false; |
| 53 |
} |
| 54 |
|
| 55 |
template<typename _Up, typename... _Args> |
| 56 |
void construct(_Up* __p, _Args&&... __args) |
| 57 |
{ ::new(__p) _Up(std::forward<_Args>(__args)...); } |
| 58 |
|
| 59 |
template<typename _Up> |
| 60 |
void destroy(_Up* __p) |
| 61 |
{ __p->~_Up(); } |
| 62 |
|
| 63 |
static char storage[sizeof(spcd)]; |
| 64 |
static bool allocated; |
| 65 |
}; |
| 66 |
|
| 67 |
char allocator<spcd>::storage[]; |
| 68 |
bool allocator<spcd>::allocated = false; |
| 69 |
} |
| 70 |
|
| 71 |
int main() |
| 72 |
{ |
| 73 |
std::shared_ptr<X> s( std::unique_ptr<X>(new X) ); |
| 74 |
VERIFY( std::allocator<spcd>::allocated ); |
| 75 |
s.reset(); |
| 76 |
VERIFY( !std::allocator<spcd>::allocated ); |
| 77 |
} |