1*38fd1498Szrj // <extptr_allocator.h> -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 2008-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj /**
26*38fd1498Szrj * @file ext/extptr_allocator.h
27*38fd1498Szrj * This file is a GNU extension to the Standard C++ Library.
28*38fd1498Szrj *
29*38fd1498Szrj * @author Bob Walters
30*38fd1498Szrj *
31*38fd1498Szrj * An example allocator which uses an alternative pointer type from
32*38fd1498Szrj * bits/pointer.h. Supports test cases which confirm container support
33*38fd1498Szrj * for alternative pointers.
34*38fd1498Szrj */
35*38fd1498Szrj
36*38fd1498Szrj #ifndef _EXTPTR_ALLOCATOR_H
37*38fd1498Szrj #define _EXTPTR_ALLOCATOR_H 1
38*38fd1498Szrj
39*38fd1498Szrj #include <memory>
40*38fd1498Szrj #include <ext/numeric_traits.h>
41*38fd1498Szrj #include <ext/pointer.h>
42*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)43*38fd1498Szrj namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
44*38fd1498Szrj {
45*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
46*38fd1498Szrj
47*38fd1498Szrj /**
48*38fd1498Szrj * @brief An example allocator which uses a non-standard pointer type.
49*38fd1498Szrj * @ingroup allocators
50*38fd1498Szrj *
51*38fd1498Szrj * This allocator specifies that containers use a 'relative pointer' as it's
52*38fd1498Szrj * pointer type. (See ext/pointer.h) Memory allocation in this example
53*38fd1498Szrj * is still performed using std::allocator.
54*38fd1498Szrj */
55*38fd1498Szrj template<typename _Tp>
56*38fd1498Szrj class _ExtPtr_allocator
57*38fd1498Szrj {
58*38fd1498Szrj public:
59*38fd1498Szrj typedef std::size_t size_type;
60*38fd1498Szrj typedef std::ptrdiff_t difference_type;
61*38fd1498Szrj
62*38fd1498Szrj // Note the non-standard pointer types.
63*38fd1498Szrj typedef _Pointer_adapter<_Relative_pointer_impl<_Tp> > pointer;
64*38fd1498Szrj typedef _Pointer_adapter<_Relative_pointer_impl<const _Tp> >
65*38fd1498Szrj const_pointer;
66*38fd1498Szrj
67*38fd1498Szrj typedef _Tp& reference;
68*38fd1498Szrj typedef const _Tp& const_reference;
69*38fd1498Szrj typedef _Tp value_type;
70*38fd1498Szrj
71*38fd1498Szrj template<typename _Up>
72*38fd1498Szrj struct rebind
73*38fd1498Szrj { typedef _ExtPtr_allocator<_Up> other; };
74*38fd1498Szrj
75*38fd1498Szrj _ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT
76*38fd1498Szrj : _M_real_alloc() { }
77*38fd1498Szrj
78*38fd1498Szrj _ExtPtr_allocator(const _ExtPtr_allocator& __rarg) _GLIBCXX_USE_NOEXCEPT
79*38fd1498Szrj : _M_real_alloc(__rarg._M_real_alloc) { }
80*38fd1498Szrj
81*38fd1498Szrj template<typename _Up>
82*38fd1498Szrj _ExtPtr_allocator(const _ExtPtr_allocator<_Up>& __rarg)
83*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT
84*38fd1498Szrj : _M_real_alloc(__rarg._M_getUnderlyingImp()) { }
85*38fd1498Szrj
86*38fd1498Szrj ~_ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT
87*38fd1498Szrj { }
88*38fd1498Szrj
89*38fd1498Szrj pointer address(reference __x) const _GLIBCXX_NOEXCEPT
90*38fd1498Szrj { return std::__addressof(__x); }
91*38fd1498Szrj
92*38fd1498Szrj const_pointer address(const_reference __x) const _GLIBCXX_NOEXCEPT
93*38fd1498Szrj { return std::__addressof(__x); }
94*38fd1498Szrj
95*38fd1498Szrj pointer allocate(size_type __n, void* __hint = 0)
96*38fd1498Szrj { return _M_real_alloc.allocate(__n,__hint); }
97*38fd1498Szrj
98*38fd1498Szrj void deallocate(pointer __p, size_type __n)
99*38fd1498Szrj { _M_real_alloc.deallocate(__p.get(), __n); }
100*38fd1498Szrj
101*38fd1498Szrj size_type max_size() const _GLIBCXX_USE_NOEXCEPT
102*38fd1498Szrj { return __numeric_traits<size_type>::__max / sizeof(_Tp); }
103*38fd1498Szrj
104*38fd1498Szrj #if __cplusplus >= 201103L
105*38fd1498Szrj template<typename _Up, typename... _Args>
106*38fd1498Szrj void
107*38fd1498Szrj construct(_Up* __p, _Args&&... __args)
108*38fd1498Szrj { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
109*38fd1498Szrj
110*38fd1498Szrj template<typename... _Args>
111*38fd1498Szrj void
112*38fd1498Szrj construct(pointer __p, _Args&&... __args)
113*38fd1498Szrj { construct(__p.get(), std::forward<_Args>(__args)...); }
114*38fd1498Szrj
115*38fd1498Szrj template<typename _Up>
116*38fd1498Szrj void
117*38fd1498Szrj destroy(_Up* __p)
118*38fd1498Szrj { __p->~_Up(); }
119*38fd1498Szrj
120*38fd1498Szrj void destroy(pointer __p)
121*38fd1498Szrj { destroy(__p.get()); }
122*38fd1498Szrj
123*38fd1498Szrj #else
124*38fd1498Szrj
125*38fd1498Szrj void construct(pointer __p, const _Tp& __val)
126*38fd1498Szrj { ::new(__p.get()) _Tp(__val); }
127*38fd1498Szrj
128*38fd1498Szrj void destroy(pointer __p)
129*38fd1498Szrj { __p->~_Tp(); }
130*38fd1498Szrj #endif
131*38fd1498Szrj
132*38fd1498Szrj template<typename _Up>
133*38fd1498Szrj inline bool
134*38fd1498Szrj operator==(const _ExtPtr_allocator<_Up>& __rarg)
135*38fd1498Szrj { return _M_real_alloc == __rarg._M_getUnderlyingImp(); }
136*38fd1498Szrj
137*38fd1498Szrj inline bool
138*38fd1498Szrj operator==(const _ExtPtr_allocator& __rarg)
139*38fd1498Szrj { return _M_real_alloc == __rarg._M_real_alloc; }
140*38fd1498Szrj
141*38fd1498Szrj template<typename _Up>
142*38fd1498Szrj inline bool
143*38fd1498Szrj operator!=(const _ExtPtr_allocator<_Up>& __rarg)
144*38fd1498Szrj { return _M_real_alloc != __rarg._M_getUnderlyingImp(); }
145*38fd1498Szrj
146*38fd1498Szrj inline bool
147*38fd1498Szrj operator!=(const _ExtPtr_allocator& __rarg)
148*38fd1498Szrj { return _M_real_alloc != __rarg._M_real_alloc; }
149*38fd1498Szrj
150*38fd1498Szrj template<typename _Up>
151*38fd1498Szrj inline friend void
152*38fd1498Szrj swap(_ExtPtr_allocator<_Up>&, _ExtPtr_allocator<_Up>&);
153*38fd1498Szrj
154*38fd1498Szrj // A method specific to this implementation.
155*38fd1498Szrj const std::allocator<_Tp>&
156*38fd1498Szrj _M_getUnderlyingImp() const
157*38fd1498Szrj { return _M_real_alloc; }
158*38fd1498Szrj
159*38fd1498Szrj private:
160*38fd1498Szrj std::allocator<_Tp> _M_real_alloc;
161*38fd1498Szrj };
162*38fd1498Szrj
163*38fd1498Szrj // _ExtPtr_allocator<void> specialization.
164*38fd1498Szrj template<>
165*38fd1498Szrj class _ExtPtr_allocator<void>
166*38fd1498Szrj {
167*38fd1498Szrj public:
168*38fd1498Szrj typedef std::size_t size_type;
169*38fd1498Szrj typedef std::ptrdiff_t difference_type;
170*38fd1498Szrj typedef void value_type;
171*38fd1498Szrj
172*38fd1498Szrj // Note the non-standard pointer types
173*38fd1498Szrj typedef _Pointer_adapter<_Relative_pointer_impl<void> > pointer;
174*38fd1498Szrj typedef _Pointer_adapter<_Relative_pointer_impl<const void> >
175*38fd1498Szrj const_pointer;
176*38fd1498Szrj
177*38fd1498Szrj template<typename _Up>
178*38fd1498Szrj struct rebind
179*38fd1498Szrj { typedef _ExtPtr_allocator<_Up> other; };
180*38fd1498Szrj
181*38fd1498Szrj private:
182*38fd1498Szrj std::allocator<void> _M_real_alloc;
183*38fd1498Szrj };
184*38fd1498Szrj
185*38fd1498Szrj template<typename _Tp>
186*38fd1498Szrj inline void
187*38fd1498Szrj swap(_ExtPtr_allocator<_Tp>& __larg, _ExtPtr_allocator<_Tp>& __rarg)
188*38fd1498Szrj {
189*38fd1498Szrj std::allocator<_Tp> __tmp( __rarg._M_real_alloc );
190*38fd1498Szrj __rarg._M_real_alloc = __larg._M_real_alloc;
191*38fd1498Szrj __larg._M_real_alloc = __tmp;
192*38fd1498Szrj }
193*38fd1498Szrj
194*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
195*38fd1498Szrj } // namespace
196*38fd1498Szrj
197*38fd1498Szrj #endif /* _EXTPTR_ALLOCATOR_H */
198