libstdc++
extptr_allocator.h
Go to the documentation of this file.
1 // <extptr_allocator.h> -*- C++ -*-
2 
3 // Copyright (C) 2008, 2009, 2010 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 <limits>
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() throw()
76  : _M_real_alloc() { }
77 
78  _ExtPtr_allocator(const _ExtPtr_allocator &__rarg) throw()
79  : _M_real_alloc(__rarg._M_real_alloc) { }
80 
81  template<typename _Up>
82  _ExtPtr_allocator(const _ExtPtr_allocator<_Up>& __rarg) throw()
83  : _M_real_alloc(__rarg._M_getUnderlyingImp()) { }
84 
85  ~_ExtPtr_allocator() throw()
86  { }
87 
88  pointer address(reference __x) const
89  { return std::__addressof(__x); }
90 
91  const_pointer address(const_reference __x) const
92  { return std::__addressof(__x); }
93 
94  pointer allocate(size_type __n, void* __hint = 0)
95  { return _M_real_alloc.allocate(__n,__hint); }
96 
97  void deallocate(pointer __p, size_type __n)
98  { _M_real_alloc.deallocate(__p.get(), __n); }
99 
100  size_type max_size() const throw()
101  { return std::numeric_limits<size_type>::max() / sizeof(_Tp); }
102 
103  void construct(pointer __p, const _Tp& __val)
104  { ::new(__p.get()) _Tp(__val); }
105 
106 #ifdef __GXX_EXPERIMENTAL_CXX0X__
107  template<typename... _Args>
108  void
109  construct(pointer __p, _Args&&... __args)
110  { ::new(__p.get()) _Tp(std::forward<_Args>(__args)...); }
111 #endif
112 
113  void destroy(pointer __p)
114  { __p->~_Tp(); }
115 
116  template<typename _Up>
117  inline bool
118  operator==(const _ExtPtr_allocator<_Up>& __rarg)
119  { return _M_real_alloc == __rarg._M_getUnderlyingImp(); }
120 
121  inline bool
122  operator==(const _ExtPtr_allocator& __rarg)
123  { return _M_real_alloc == __rarg._M_real_alloc; }
124 
125  template<typename _Up>
126  inline bool
127  operator!=(const _ExtPtr_allocator<_Up>& __rarg)
128  { return _M_real_alloc != __rarg._M_getUnderlyingImp(); }
129 
130  inline bool
131  operator!=(const _ExtPtr_allocator& __rarg)
132  { return _M_real_alloc != __rarg._M_real_alloc; }
133 
134  template<typename _Up>
135  inline friend void
137 
138  // A method specific to this implementation.
139  const std::allocator<_Tp>&
140  _M_getUnderlyingImp() const
141  { return _M_real_alloc; }
142 
143  private:
144  std::allocator<_Tp> _M_real_alloc;
145  };
146 
147  // _ExtPtr_allocator<void> specialization.
148  template<>
149  class _ExtPtr_allocator<void>
150  {
151  public:
152  typedef std::size_t size_type;
153  typedef std::ptrdiff_t difference_type;
154  typedef void value_type;
155 
156  // Note the non-standard pointer types
159  const_pointer;
160 
161  template<typename _Up>
162  struct rebind
163  { typedef _ExtPtr_allocator<_Up> other; };
164 
165  private:
166  std::allocator<void> _M_real_alloc;
167  };
168 
169  template<typename _Tp>
170  inline void
171  swap(_ExtPtr_allocator<_Tp>& __larg, _ExtPtr_allocator<_Tp>& __rarg)
172  {
173  std::allocator<_Tp> __tmp( __rarg._M_real_alloc );
174  __rarg._M_real_alloc = __larg._M_real_alloc;
175  __larg._M_real_alloc = __tmp;
176  }
177 
178 _GLIBCXX_END_NAMESPACE_VERSION
179 } // namespace
180 
181 #endif /* _EXTPTR_ALLOCATOR_H */
An example allocator which uses a non-standard pointer type.This allocator specifies that containers ...
static constexpr _Tp max()
Definition: limits:313
allocator<void> specialization.
Definition: allocator.h:70
The standard allocator, as per [20.4].Further details: http://gcc.gnu.org/onlinedocs/libstdc++/manual...
Definition: allocator.h:66