136ac495dSmrg // <extptr_allocator.h> -*- C++ -*-
236ac495dSmrg
3*8feb0f0bSmrg // Copyright (C) 2008-2020 Free Software Foundation, Inc.
436ac495dSmrg //
536ac495dSmrg // This file is part of the GNU ISO C++ Library. This library is free
636ac495dSmrg // software; you can redistribute it and/or modify it under the
736ac495dSmrg // terms of the GNU General Public License as published by the
836ac495dSmrg // Free Software Foundation; either version 3, or (at your option)
936ac495dSmrg // any later version.
1036ac495dSmrg
1136ac495dSmrg // This library is distributed in the hope that it will be useful,
1236ac495dSmrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
1336ac495dSmrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1436ac495dSmrg // GNU General Public License for more details.
1536ac495dSmrg
1636ac495dSmrg // Under Section 7 of GPL version 3, you are granted additional
1736ac495dSmrg // permissions described in the GCC Runtime Library Exception, version
1836ac495dSmrg // 3.1, as published by the Free Software Foundation.
1936ac495dSmrg
2036ac495dSmrg // You should have received a copy of the GNU General Public License and
2136ac495dSmrg // a copy of the GCC Runtime Library Exception along with this program;
2236ac495dSmrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2336ac495dSmrg // <http://www.gnu.org/licenses/>.
2436ac495dSmrg
2536ac495dSmrg /**
2636ac495dSmrg * @file ext/extptr_allocator.h
2736ac495dSmrg * This file is a GNU extension to the Standard C++ Library.
2836ac495dSmrg *
2936ac495dSmrg * @author Bob Walters
3036ac495dSmrg *
3136ac495dSmrg * An example allocator which uses an alternative pointer type from
3236ac495dSmrg * bits/pointer.h. Supports test cases which confirm container support
3336ac495dSmrg * for alternative pointers.
3436ac495dSmrg */
3536ac495dSmrg
3636ac495dSmrg #ifndef _EXTPTR_ALLOCATOR_H
3736ac495dSmrg #define _EXTPTR_ALLOCATOR_H 1
3836ac495dSmrg
3936ac495dSmrg #include <memory>
4036ac495dSmrg #include <ext/numeric_traits.h>
4136ac495dSmrg #include <ext/pointer.h>
4236ac495dSmrg
_GLIBCXX_VISIBILITY(default)4336ac495dSmrg namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
4436ac495dSmrg {
4536ac495dSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
4636ac495dSmrg
4736ac495dSmrg /**
4836ac495dSmrg * @brief An example allocator which uses a non-standard pointer type.
4936ac495dSmrg * @ingroup allocators
5036ac495dSmrg *
5136ac495dSmrg * This allocator specifies that containers use a 'relative pointer' as it's
5236ac495dSmrg * pointer type. (See ext/pointer.h) Memory allocation in this example
5336ac495dSmrg * is still performed using std::allocator.
5436ac495dSmrg */
5536ac495dSmrg template<typename _Tp>
5636ac495dSmrg class _ExtPtr_allocator
5736ac495dSmrg {
5836ac495dSmrg public:
5936ac495dSmrg typedef std::size_t size_type;
6036ac495dSmrg typedef std::ptrdiff_t difference_type;
6136ac495dSmrg
6236ac495dSmrg // Note the non-standard pointer types.
6336ac495dSmrg typedef _Pointer_adapter<_Relative_pointer_impl<_Tp> > pointer;
6436ac495dSmrg typedef _Pointer_adapter<_Relative_pointer_impl<const _Tp> >
6536ac495dSmrg const_pointer;
6636ac495dSmrg
6736ac495dSmrg typedef _Tp& reference;
6836ac495dSmrg typedef const _Tp& const_reference;
6936ac495dSmrg typedef _Tp value_type;
7036ac495dSmrg
7136ac495dSmrg template<typename _Up>
7236ac495dSmrg struct rebind
7336ac495dSmrg { typedef _ExtPtr_allocator<_Up> other; };
7436ac495dSmrg
7536ac495dSmrg _ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT
7636ac495dSmrg : _M_real_alloc() { }
7736ac495dSmrg
7836ac495dSmrg _ExtPtr_allocator(const _ExtPtr_allocator& __rarg) _GLIBCXX_USE_NOEXCEPT
7936ac495dSmrg : _M_real_alloc(__rarg._M_real_alloc) { }
8036ac495dSmrg
8136ac495dSmrg template<typename _Up>
8236ac495dSmrg _ExtPtr_allocator(const _ExtPtr_allocator<_Up>& __rarg)
8336ac495dSmrg _GLIBCXX_USE_NOEXCEPT
8436ac495dSmrg : _M_real_alloc(__rarg._M_getUnderlyingImp()) { }
8536ac495dSmrg
8636ac495dSmrg ~_ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT
8736ac495dSmrg { }
8836ac495dSmrg
8936ac495dSmrg pointer address(reference __x) const _GLIBCXX_NOEXCEPT
9036ac495dSmrg { return std::__addressof(__x); }
9136ac495dSmrg
9236ac495dSmrg const_pointer address(const_reference __x) const _GLIBCXX_NOEXCEPT
9336ac495dSmrg { return std::__addressof(__x); }
9436ac495dSmrg
95*8feb0f0bSmrg _GLIBCXX_NODISCARD pointer allocate(size_type __n, const void* = 0)
96*8feb0f0bSmrg { return _M_real_alloc.allocate(__n); }
9736ac495dSmrg
9836ac495dSmrg void deallocate(pointer __p, size_type __n)
9936ac495dSmrg { _M_real_alloc.deallocate(__p.get(), __n); }
10036ac495dSmrg
10136ac495dSmrg size_type max_size() const _GLIBCXX_USE_NOEXCEPT
10236ac495dSmrg { return __numeric_traits<size_type>::__max / sizeof(_Tp); }
10336ac495dSmrg
10436ac495dSmrg #if __cplusplus >= 201103L
10536ac495dSmrg template<typename _Up, typename... _Args>
10636ac495dSmrg void
10736ac495dSmrg construct(_Up* __p, _Args&&... __args)
10836ac495dSmrg { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
10936ac495dSmrg
11036ac495dSmrg template<typename... _Args>
11136ac495dSmrg void
11236ac495dSmrg construct(pointer __p, _Args&&... __args)
11336ac495dSmrg { construct(__p.get(), std::forward<_Args>(__args)...); }
11436ac495dSmrg
11536ac495dSmrg template<typename _Up>
11636ac495dSmrg void
11736ac495dSmrg destroy(_Up* __p)
11836ac495dSmrg { __p->~_Up(); }
11936ac495dSmrg
12036ac495dSmrg void destroy(pointer __p)
12136ac495dSmrg { destroy(__p.get()); }
12236ac495dSmrg
12336ac495dSmrg #else
12436ac495dSmrg
12536ac495dSmrg void construct(pointer __p, const _Tp& __val)
12636ac495dSmrg { ::new(__p.get()) _Tp(__val); }
12736ac495dSmrg
12836ac495dSmrg void destroy(pointer __p)
12936ac495dSmrg { __p->~_Tp(); }
13036ac495dSmrg #endif
13136ac495dSmrg
13236ac495dSmrg template<typename _Up>
13336ac495dSmrg inline bool
134*8feb0f0bSmrg operator==(const _ExtPtr_allocator<_Up>& __rarg) const
13536ac495dSmrg { return _M_real_alloc == __rarg._M_getUnderlyingImp(); }
13636ac495dSmrg
13736ac495dSmrg inline bool
138*8feb0f0bSmrg operator==(const _ExtPtr_allocator& __rarg) const
13936ac495dSmrg { return _M_real_alloc == __rarg._M_real_alloc; }
14036ac495dSmrg
141*8feb0f0bSmrg #if __cpp_impl_three_way_comparison < 201907L
14236ac495dSmrg template<typename _Up>
14336ac495dSmrg inline bool
144*8feb0f0bSmrg operator!=(const _ExtPtr_allocator<_Up>& __rarg) const
14536ac495dSmrg { return _M_real_alloc != __rarg._M_getUnderlyingImp(); }
14636ac495dSmrg
14736ac495dSmrg inline bool
148*8feb0f0bSmrg operator!=(const _ExtPtr_allocator& __rarg) const
14936ac495dSmrg { return _M_real_alloc != __rarg._M_real_alloc; }
150*8feb0f0bSmrg #endif
15136ac495dSmrg
15236ac495dSmrg template<typename _Up>
15336ac495dSmrg inline friend void
15436ac495dSmrg swap(_ExtPtr_allocator<_Up>&, _ExtPtr_allocator<_Up>&);
15536ac495dSmrg
15636ac495dSmrg // A method specific to this implementation.
15736ac495dSmrg const std::allocator<_Tp>&
15836ac495dSmrg _M_getUnderlyingImp() const
15936ac495dSmrg { return _M_real_alloc; }
16036ac495dSmrg
16136ac495dSmrg private:
16236ac495dSmrg std::allocator<_Tp> _M_real_alloc;
16336ac495dSmrg };
16436ac495dSmrg
16536ac495dSmrg // _ExtPtr_allocator<void> specialization.
16636ac495dSmrg template<>
16736ac495dSmrg class _ExtPtr_allocator<void>
16836ac495dSmrg {
16936ac495dSmrg public:
17036ac495dSmrg typedef std::size_t size_type;
17136ac495dSmrg typedef std::ptrdiff_t difference_type;
17236ac495dSmrg typedef void value_type;
17336ac495dSmrg
17436ac495dSmrg // Note the non-standard pointer types
17536ac495dSmrg typedef _Pointer_adapter<_Relative_pointer_impl<void> > pointer;
17636ac495dSmrg typedef _Pointer_adapter<_Relative_pointer_impl<const void> >
17736ac495dSmrg const_pointer;
17836ac495dSmrg
179*8feb0f0bSmrg _ExtPtr_allocator() { }
180*8feb0f0bSmrg
181*8feb0f0bSmrg template<typename _Up>
182*8feb0f0bSmrg _ExtPtr_allocator(const _ExtPtr_allocator<_Up>&) { }
183*8feb0f0bSmrg
18436ac495dSmrg template<typename _Up>
18536ac495dSmrg struct rebind
18636ac495dSmrg { typedef _ExtPtr_allocator<_Up> other; };
18736ac495dSmrg
18836ac495dSmrg private:
18936ac495dSmrg std::allocator<void> _M_real_alloc;
19036ac495dSmrg };
19136ac495dSmrg
19236ac495dSmrg template<typename _Tp>
19336ac495dSmrg inline void
19436ac495dSmrg swap(_ExtPtr_allocator<_Tp>& __larg, _ExtPtr_allocator<_Tp>& __rarg)
19536ac495dSmrg {
19636ac495dSmrg std::allocator<_Tp> __tmp( __rarg._M_real_alloc );
19736ac495dSmrg __rarg._M_real_alloc = __larg._M_real_alloc;
19836ac495dSmrg __larg._M_real_alloc = __tmp;
19936ac495dSmrg }
20036ac495dSmrg
20136ac495dSmrg _GLIBCXX_END_NAMESPACE_VERSION
20236ac495dSmrg } // namespace
20336ac495dSmrg
20436ac495dSmrg #endif /* _EXTPTR_ALLOCATOR_H */
205