]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/new_allocator.h
Relocation (= move+destroy)
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / new_allocator.h
CommitLineData
d38d4e5d 1// Allocator that wraps operator new -*- C++ -*-
42526146 2
85ec4feb 3// Copyright (C) 2001-2018 Free Software Foundation, Inc.
42526146
PE
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
42526146
PE
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
748086b7
JJ
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/>.
42526146 24
0aa06b18
BK
25/** @file ext/new_allocator.h
26 * This file is a GNU extension to the Standard C++ Library.
27 */
28
1ff9402d
BK
29#ifndef _NEW_ALLOCATOR_H
30#define _NEW_ALLOCATOR_H 1
31
8fc81078 32#include <bits/c++config.h>
1ff9402d 33#include <new>
a063e891 34#include <bits/functexcept.h>
ca0f8fd1 35#include <bits/move.h>
1b5dc776
JW
36#if __cplusplus >= 201103L
37#include <type_traits>
38#endif
1ff9402d 39
12ffa228
BK
40namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
41{
42_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 43
05a2763e
MG
44 using std::size_t;
45 using std::ptrdiff_t;
46
1ff9402d 47 /**
d38d4e5d 48 * @brief An allocator that uses global new, as per [20.4].
5b9daa7e 49 * @ingroup allocators
d38d4e5d 50 *
88b3e631 51 * This is precisely the allocator defined in the C++ Standard.
d38d4e5d
BK
52 * - all allocation calls operator new
53 * - all deallocation calls operator delete
fa754848
BK
54 *
55 * @tparam _Tp Type of allocated object.
1ff9402d 56 */
d38d4e5d
BK
57 template<typename _Tp>
58 class new_allocator
59 {
60 public:
61 typedef size_t size_type;
62 typedef ptrdiff_t difference_type;
63 typedef _Tp* pointer;
64 typedef const _Tp* const_pointer;
65 typedef _Tp& reference;
66 typedef const _Tp& const_reference;
67 typedef _Tp value_type;
68
69 template<typename _Tp1>
88b3e631
JW
70 struct rebind
71 { typedef new_allocator<_Tp1> other; };
d38d4e5d 72
1b5dc776
JW
73#if __cplusplus >= 201103L
74 // _GLIBCXX_RESOLVE_LIB_DEFECTS
75 // 2103. propagate_on_container_move_assignment
76 typedef std::true_type propagate_on_container_move_assignment;
77#endif
78
3be9ded2 79 _GLIBCXX20_CONSTEXPR
7d9cb054 80 new_allocator() _GLIBCXX_USE_NOEXCEPT { }
d38d4e5d 81
3be9ded2 82 _GLIBCXX20_CONSTEXPR
7d9cb054 83 new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { }
d38d4e5d
BK
84
85 template<typename _Tp1>
3be9ded2 86 _GLIBCXX20_CONSTEXPR
88b3e631 87 new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { }
d38d4e5d 88
7d9cb054 89 ~new_allocator() _GLIBCXX_USE_NOEXCEPT { }
d38d4e5d
BK
90
91 pointer
7d9cb054
PC
92 address(reference __x) const _GLIBCXX_NOEXCEPT
93 { return std::__addressof(__x); }
d38d4e5d
BK
94
95 const_pointer
7d9cb054
PC
96 address(const_reference __x) const _GLIBCXX_NOEXCEPT
97 { return std::__addressof(__x); }
d38d4e5d
BK
98
99 // NB: __n is permitted to be 0. The C++ standard says nothing
100 // about what the return value is when __n == 0.
101 pointer
89c6ecfa 102 allocate(size_type __n, const void* = static_cast<const void*>(0))
88b3e631 103 {
e762c6f4 104 if (__n > this->max_size())
a063e891
PC
105 std::__throw_bad_alloc();
106
ace4c2f0
JW
107#if __cpp_aligned_new
108 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
109 {
110 std::align_val_t __al = std::align_val_t(alignof(_Tp));
111 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al));
112 }
113#endif
a063e891
PC
114 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
115 }
d38d4e5d
BK
116
117 // __p is not permitted to be a null pointer.
118 void
dcec0389 119 deallocate(pointer __p, size_type)
ace4c2f0
JW
120 {
121#if __cpp_aligned_new
122 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
123 {
124 ::operator delete(__p, std::align_val_t(alignof(_Tp)));
125 return;
126 }
127#endif
128 ::operator delete(__p);
129 }
d38d4e5d
BK
130
131 size_type
7d9cb054 132 max_size() const _GLIBCXX_USE_NOEXCEPT
422a9f77
JW
133 {
134#if __PTRDIFF_MAX__ < __SIZE_MAX__
135 return size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
136#else
137 return size_t(-1) / sizeof(_Tp);
138#endif
139 }
d38d4e5d 140
734f5023 141#if __cplusplus >= 201103L
45ba8f9f 142 template<typename _Up, typename... _Args>
88b3e631
JW
143 void
144 construct(_Up* __p, _Args&&... __args)
0f317ef7
MG
145 noexcept(noexcept(::new((void *)__p)
146 _Up(std::forward<_Args>(__args)...)))
45ba8f9f
JW
147 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
148
149 template<typename _Up>
88b3e631 150 void
0f317ef7
MG
151 destroy(_Up* __p)
152 noexcept(noexcept( __p->~_Up()))
153 { __p->~_Up(); }
45ba8f9f 154#else
d38d4e5d
BK
155 // _GLIBCXX_RESOLVE_LIB_DEFECTS
156 // 402. wrong new expression in [some_] allocator::construct
88b3e631
JW
157 void
158 construct(pointer __p, const _Tp& __val)
61fcb9fb
PC
159 { ::new((void *)__p) _Tp(__val); }
160
88b3e631 161 void
d38d4e5d 162 destroy(pointer __p) { __p->~_Tp(); }
45ba8f9f 163#endif
f263b26e 164
c7790bdb
JW
165 template<typename _Up>
166 friend bool
167 operator==(const new_allocator&, const new_allocator<_Up>&)
168 _GLIBCXX_NOTHROW
169 { return true; }
88b3e631 170
c7790bdb
JW
171 template<typename _Up>
172 friend bool
173 operator!=(const new_allocator&, const new_allocator<_Up>&)
174 _GLIBCXX_NOTHROW
175 { return false; }
176 };
3cbc7af0 177
12ffa228
BK
178_GLIBCXX_END_NAMESPACE_VERSION
179} // namespace
1ff9402d
BK
180
181#endif