libstdc++
extptr_allocator.h
Go to the documentation of this file.
1 // <extptr_allocator.h> -*- C++ -*-
2 
3 // Copyright (C) 2008-2018 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 // 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.
19 
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/>.
24 
25 /**
26  * @file ext/extptr_allocator.h
27  * This file is a GNU extension to the Standard C++ Library.
28  *
29  * @author Bob Walters
30  *
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.
34  */
35 
36 #ifndef _EXTPTR_ALLOCATOR_H
37 #define _EXTPTR_ALLOCATOR_H 1
38 
39 #include <memory>
40 #include <ext/numeric_traits.h>
41 #include <ext/pointer.h>
42 
43 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 
47  /**
48  * @brief An example allocator which uses a non-standard pointer type.
49  * @ingroup allocators
50  *
51  * This allocator specifies that containers use a 'relative pointer' as it's
52  * pointer type. (See ext/pointer.h) Memory allocation in this example
53  * is still performed using std::allocator.
54  */
55  template<typename _Tp>
57  {
58  public:
59  typedef std::size_t size_type;
60  typedef std::ptrdiff_t difference_type;
61 
62  // Note the non-standard pointer types.
66 
67  typedef _Tp& reference;
68  typedef const _Tp& const_reference;
69  typedef _Tp value_type;
70 
71  template<typename _Up>
72  struct rebind
73  { typedef _ExtPtr_allocator<_Up> other; };
74 
75  _ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT
76  : _M_real_alloc() { }
77 
78  _ExtPtr_allocator(const _ExtPtr_allocator& __rarg) _GLIBCXX_USE_NOEXCEPT
79  : _M_real_alloc(__rarg._M_real_alloc) { }
80 
81  template<typename _Up>
83  _GLIBCXX_USE_NOEXCEPT
84  : _M_real_alloc(__rarg._M_getUnderlyingImp()) { }
85 
86  ~_ExtPtr_allocator() _GLIBCXX_USE_NOEXCEPT
87  { }
88 
89  pointer address(reference __x) const _GLIBCXX_NOEXCEPT
90  { return std::__addressof(__x); }
91 
92  const_pointer address(const_reference __x) const _GLIBCXX_NOEXCEPT
93  { return std::__addressof(__x); }
94 
95  pointer allocate(size_type __n, void* __hint = 0)
96  { return _M_real_alloc.allocate(__n,__hint); }
97 
98  void deallocate(pointer __p, size_type __n)
99  { _M_real_alloc.deallocate(__p.get(), __n); }
100 
101  size_type max_size() const _GLIBCXX_USE_NOEXCEPT
102  { return __numeric_traits<size_type>::__max / sizeof(_Tp); }
103 
104 #if __cplusplus >= 201103L
105  template<typename _Up, typename... _Args>
106  void
107  construct(_Up* __p, _Args&&... __args)
108  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
109 
110  template<typename... _Args>
111  void
112  construct(pointer __p, _Args&&... __args)
113  { construct(__p.get(), std::forward<_Args>(__args)...); }
114 
115  template<typename _Up>
116  void
117  destroy(_Up* __p)
118  { __p->~_Up(); }
119 
120  void destroy(pointer __p)
121  { destroy(__p.get()); }
122 
123 #else
124 
125  void construct(pointer __p, const _Tp& __val)
126  { ::new(__p.get()) _Tp(__val); }
127 
128  void destroy(pointer __p)
129  { __p->~_Tp(); }
130 #endif
131 
132  template<typename _Up>
133  inline bool
134  operator==(const _ExtPtr_allocator<_Up>& __rarg)
135  { return _M_real_alloc == __rarg._M_getUnderlyingImp(); }
136 
137  inline bool
138  operator==(const _ExtPtr_allocator& __rarg)
139  { return _M_real_alloc == __rarg._M_real_alloc; }
140 
141  template<typename _Up>
142  inline bool
143  operator!=(const _ExtPtr_allocator<_Up>& __rarg)
144  { return _M_real_alloc != __rarg._M_getUnderlyingImp(); }
145 
146  inline bool
147  operator!=(const _ExtPtr_allocator& __rarg)
148  { return _M_real_alloc != __rarg._M_real_alloc; }
149 
150  template<typename _Up>
151  inline friend void
153 
154  // A method specific to this implementation.
155  const std::allocator<_Tp>&
156  _M_getUnderlyingImp() const
157  { return _M_real_alloc; }
158 
159  private:
160  std::allocator<_Tp> _M_real_alloc;
161  };
162 
163  // _ExtPtr_allocator<void> specialization.
164  template<>
165  class _ExtPtr_allocator<void>
166  {
167  public:
168  typedef std::size_t size_type;
169  typedef std::ptrdiff_t difference_type;
170  typedef void value_type;
171 
172  // Note the non-standard pointer types
175  const_pointer;
176 
177  template<typename _Up>
178  struct rebind
179  { typedef _ExtPtr_allocator<_Up> other; };
180 
181  private:
182  std::allocator<void> _M_real_alloc;
183  };
184 
185  template<typename _Tp>
186  inline void
187  swap(_ExtPtr_allocator<_Tp>& __larg, _ExtPtr_allocator<_Tp>& __rarg)
188  {
189  std::allocator<_Tp> __tmp( __rarg._M_real_alloc );
190  __rarg._M_real_alloc = __larg._M_real_alloc;
191  __larg._M_real_alloc = __tmp;
192  }
193 
194 _GLIBCXX_END_NAMESPACE_VERSION
195 } // namespace
196 
197 #endif /* _EXTPTR_ALLOCATOR_H */
The standard allocator, as per [20.4].
Definition: allocator.h:108
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:47
GNU extensions for public use.
allocator<void> specialization.
Definition: allocator.h:68
An example allocator which uses a non-standard pointer type.This allocator specifies that containers ...