1 // <extptr_allocator.h> -*- C++ -*-
3 // Copyright (C) 2008-2024 Free Software Foundation, Inc.
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)
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.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * @file ext/extptr_allocator.h
27 * This file is a GNU extension to the Standard C++ Library.
31 * An example allocator which uses an alternative pointer type from
32 * bits/pointer.h. Supports test cases which confirm container support
33 * for alternative pointers.
36 #ifndef _EXTPTR_ALLOCATOR_H
37 #define _EXTPTR_ALLOCATOR_H 1
39 #include <bits/requires_hosted.h> // GNU extensions are currently omitted
42 #include <ext/numeric_traits.h>
43 #include <ext/pointer.h>
45 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 * @brief An example allocator which uses a non-standard pointer type.
53 * This allocator specifies that containers use a 'relative pointer' as it's
54 * pointer type. (See ext/pointer.h) Memory allocation in this example
55 * is still performed using std::allocator.
57 template<typename _Tp
>
58 class _ExtPtr_allocator
61 typedef std::size_t size_type
;
62 typedef std::ptrdiff_t difference_type
;
64 // Note the non-standard pointer types.
65 typedef _Pointer_adapter
<_Relative_pointer_impl
<_Tp
> > pointer
;
66 typedef _Pointer_adapter
<_Relative_pointer_impl
<const _Tp
> >
69 typedef _Tp
& reference
;
70 typedef const _Tp
& const_reference
;
71 typedef _Tp value_type
;
73 template<typename _Up
>
75 { typedef _ExtPtr_allocator
<_Up
> other
; };
77 _ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT
80 _ExtPtr_allocator(const _ExtPtr_allocator
& __rarg
) _GLIBCXX_USE_NOEXCEPT
81 : _M_real_alloc(__rarg
._M_real_alloc
) { }
83 template<typename _Up
>
84 _ExtPtr_allocator(const _ExtPtr_allocator
<_Up
>& __rarg
)
86 : _M_real_alloc(__rarg
._M_getUnderlyingImp()) { }
88 ~_ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT
91 pointer
address(reference __x
) const _GLIBCXX_NOEXCEPT
92 { return std::__addressof(__x
); }
94 const_pointer
address(const_reference __x
) const _GLIBCXX_NOEXCEPT
95 { return std::__addressof(__x
); }
97 _GLIBCXX_NODISCARD pointer
allocate(size_type __n
, const void* = 0)
98 { return _M_real_alloc
.allocate(__n
); }
100 void deallocate(pointer __p
, size_type __n
)
101 { _M_real_alloc
.deallocate(__p
.get(), __n
); }
103 size_type
max_size() const _GLIBCXX_USE_NOEXCEPT
104 { return __numeric_traits
<size_type
>::__max
/ sizeof(_Tp
); }
106 #if __cplusplus >= 201103L
107 template<typename _Up
, typename
... _Args
>
109 construct(_Up
* __p
, _Args
&&... __args
)
110 { ::new((void *)__p
) _Up(std::forward
<_Args
>(__args
)...); }
112 template<typename
... _Args
>
114 construct(pointer __p
, _Args
&&... __args
)
115 { construct(__p
.get(), std::forward
<_Args
>(__args
)...); }
117 template<typename _Up
>
122 void destroy(pointer __p
)
123 { destroy(__p
.get()); }
127 void construct(pointer __p
, const _Tp
& __val
)
128 { ::new(__p
.get()) _Tp(__val
); }
130 void destroy(pointer __p
)
134 template<typename _Up
>
136 operator==(const _ExtPtr_allocator
<_Up
>& __rarg
) const
137 { return _M_real_alloc
== __rarg
._M_getUnderlyingImp(); }
140 operator==(const _ExtPtr_allocator
& __rarg
) const
141 { return _M_real_alloc
== __rarg
._M_real_alloc
; }
143 #if __cpp_impl_three_way_comparison < 201907L
144 template<typename _Up
>
146 operator!=(const _ExtPtr_allocator
<_Up
>& __rarg
) const
147 { return _M_real_alloc
!= __rarg
._M_getUnderlyingImp(); }
150 operator!=(const _ExtPtr_allocator
& __rarg
) const
151 { return _M_real_alloc
!= __rarg
._M_real_alloc
; }
154 template<typename _Up
>
156 swap(_ExtPtr_allocator
<_Up
>&, _ExtPtr_allocator
<_Up
>&);
158 // A method specific to this implementation.
159 const std::allocator
<_Tp
>&
160 _M_getUnderlyingImp() const
161 { return _M_real_alloc
; }
164 std::allocator
<_Tp
> _M_real_alloc
;
167 // _ExtPtr_allocator<void> specialization.
169 class _ExtPtr_allocator
<void>
172 typedef std::size_t size_type
;
173 typedef std::ptrdiff_t difference_type
;
174 typedef void value_type
;
176 // Note the non-standard pointer types
177 typedef _Pointer_adapter
<_Relative_pointer_impl
<void> > pointer
;
178 typedef _Pointer_adapter
<_Relative_pointer_impl
<const void> >
181 _ExtPtr_allocator() { }
183 template<typename _Up
>
184 _ExtPtr_allocator(const _ExtPtr_allocator
<_Up
>&) { }
186 template<typename _Up
>
188 { typedef _ExtPtr_allocator
<_Up
> other
; };
191 std::allocator
<void> _M_real_alloc
;
194 template<typename _Tp
>
196 swap(_ExtPtr_allocator
<_Tp
>& __larg
, _ExtPtr_allocator
<_Tp
>& __rarg
)
198 std::allocator
<_Tp
> __tmp( __rarg
._M_real_alloc
);
199 __rarg
._M_real_alloc
= __larg
._M_real_alloc
;
200 __larg
._M_real_alloc
= __tmp
;
203 _GLIBCXX_END_NAMESPACE_VERSION
206 #endif /* _EXTPTR_ALLOCATOR_H */