libstdc++
new_allocator.h
Go to the documentation of this file.
1 // Allocator that wraps operator new -*- C++ -*-
2 
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 /** @file ext/new_allocator.h
27  * This file is a GNU extension to the Standard C++ Library.
28  */
29 
30 #ifndef _NEW_ALLOCATOR_H
31 #define _NEW_ALLOCATOR_H 1
32 
33 #include <bits/c++config.h>
34 #include <new>
35 #include <bits/functexcept.h>
36 #include <bits/move.h>
37 
38 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
39 {
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 
42  using std::size_t;
43  using std::ptrdiff_t;
44 
45  /**
46  * @brief An allocator that uses global new, as per [20.4].
47  * @ingroup allocators
48  *
49  * This is precisely the allocator defined in the C++ Standard.
50  * - all allocation calls operator new
51  * - all deallocation calls operator delete
52  */
53  template<typename _Tp>
54  class new_allocator
55  {
56  public:
57  typedef size_t size_type;
58  typedef ptrdiff_t difference_type;
59  typedef _Tp* pointer;
60  typedef const _Tp* const_pointer;
61  typedef _Tp& reference;
62  typedef const _Tp& const_reference;
63  typedef _Tp value_type;
64 
65  template<typename _Tp1>
66  struct rebind
67  { typedef new_allocator<_Tp1> other; };
68 
69  new_allocator() _GLIBCXX_USE_NOEXCEPT { }
70 
71  new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { }
72 
73  template<typename _Tp1>
74  new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { }
75 
76  ~new_allocator() _GLIBCXX_USE_NOEXCEPT { }
77 
78  pointer
79  address(reference __x) const _GLIBCXX_NOEXCEPT
80  { return std::__addressof(__x); }
81 
82  const_pointer
83  address(const_reference __x) const _GLIBCXX_NOEXCEPT
84  { return std::__addressof(__x); }
85 
86  // NB: __n is permitted to be 0. The C++ standard says nothing
87  // about what the return value is when __n == 0.
88  pointer
89  allocate(size_type __n, const void* = 0)
90  {
91  if (__n > this->max_size())
92  std::__throw_bad_alloc();
93 
94  return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
95  }
96 
97  // __p is not permitted to be a null pointer.
98  void
99  deallocate(pointer __p, size_type)
100  { ::operator delete(__p); }
101 
102  size_type
103  max_size() const _GLIBCXX_USE_NOEXCEPT
104  { return size_t(-1) / sizeof(_Tp); }
105 
106 #ifdef __GXX_EXPERIMENTAL_CXX0X__
107  template<typename _Up, typename... _Args>
108  void
109  construct(_Up* __p, _Args&&... __args)
110  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
111 
112  template<typename _Up>
113  void
114  destroy(_Up* __p) { __p->~_Up(); }
115 #else
116  // _GLIBCXX_RESOLVE_LIB_DEFECTS
117  // 402. wrong new expression in [some_] allocator::construct
118  void
119  construct(pointer __p, const _Tp& __val)
120  { ::new((void *)__p) _Tp(__val); }
121 
122  void
123  destroy(pointer __p) { __p->~_Tp(); }
124 #endif
125  };
126 
127  template<typename _Tp>
128  inline bool
129  operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
130  { return true; }
131 
132  template<typename _Tp>
133  inline bool
134  operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
135  { return false; }
136 
137 _GLIBCXX_END_NAMESPACE_VERSION
138 } // namespace
139 
140 #endif
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:77
_Tp * __addressof(_Tp &__r) _GLIBCXX_NOEXCEPT
Same as C++11 std::addressof.
Definition: move.h:47