]>
Commit | Line | Data |
---|---|---|
30a54583 | 1 | // array allocator -*- C++ -*- |
2 | ||
fbd26352 | 3 | // Copyright (C) 2004-2019 Free Software Foundation, Inc. |
30a54583 | 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 | |
6bc9506f | 8 | // Free Software Foundation; either version 3, or (at your option) |
30a54583 | 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 | ||
6bc9506f | 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/>. | |
30a54583 | 24 | |
b2a66747 | 25 | /** @file ext/array_allocator.h |
26 | * This file is a GNU extension to the Standard C++ Library. | |
27 | */ | |
28 | ||
30a54583 | 29 | #ifndef _ARRAY_ALLOCATOR_H |
30 | #define _ARRAY_ALLOCATOR_H 1 | |
31 | ||
e4bb1925 | 32 | #include <bits/c++config.h> |
30a54583 | 33 | #include <new> |
4aa4288b | 34 | #include <bits/functexcept.h> |
30a54583 | 35 | #include <tr1/array> |
359d05c3 | 36 | #include <bits/move.h> |
5f304128 | 37 | #if __cplusplus >= 201103L |
ad62a02b | 38 | #include <type_traits> |
39 | #endif | |
30a54583 | 40 | |
59edbc2a | 41 | // Suppress deprecated warning for this file. |
42 | #pragma GCC diagnostic push | |
43 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
44 | ||
2948dd21 | 45 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
46 | { | |
47 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
1069247d | 48 | |
a57562d3 | 49 | using std::size_t; |
50 | using std::ptrdiff_t; | |
51 | ||
4a77b438 | 52 | /// Base class. |
30a54583 | 53 | template<typename _Tp> |
54 | class array_allocator_base | |
55 | { | |
56 | public: | |
10eb30f0 | 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; | |
30a54583 | 64 | |
65 | pointer | |
6cb05617 | 66 | address(reference __x) const _GLIBCXX_NOEXCEPT |
67 | { return std::__addressof(__x); } | |
30a54583 | 68 | |
69 | const_pointer | |
6cb05617 | 70 | address(const_reference __x) const _GLIBCXX_NOEXCEPT |
71 | { return std::__addressof(__x); } | |
30a54583 | 72 | |
73 | void | |
eb942953 | 74 | deallocate(pointer, size_type) |
30a54583 | 75 | { |
76 | // Does nothing. | |
77 | } | |
78 | ||
79 | size_type | |
6cb05617 | 80 | max_size() const _GLIBCXX_USE_NOEXCEPT |
30a54583 | 81 | { return size_t(-1) / sizeof(_Tp); } |
82 | ||
0c8766b1 | 83 | #if __cplusplus >= 201103L |
2414d57e | 84 | template<typename _Up, typename... _Args> |
85 | void | |
86 | construct(_Up* __p, _Args&&... __args) | |
87 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } | |
88 | ||
89 | template<typename _Up> | |
90 | void | |
91 | destroy(_Up* __p) { __p->~_Up(); } | |
92 | #else | |
30a54583 | 93 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
94 | // 402. wrong new expression in [some_] allocator::construct | |
95 | void | |
96 | construct(pointer __p, const _Tp& __val) | |
282986bf | 97 | { ::new((void *)__p) value_type(__val); } |
98 | ||
30a54583 | 99 | void |
100 | destroy(pointer __p) { __p->~_Tp(); } | |
2414d57e | 101 | #endif |
f2b08775 | 102 | } _GLIBCXX_DEPRECATED; |
30a54583 | 103 | |
104 | /** | |
105 | * @brief An allocator that uses previously allocated memory. | |
106 | * This memory can be externally, globally, or otherwise allocated. | |
97a32de0 | 107 | * @ingroup allocators |
30a54583 | 108 | */ |
cd23dcfc | 109 | template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> > |
30a54583 | 110 | class array_allocator : public array_allocator_base<_Tp> |
111 | { | |
112 | public: | |
10eb30f0 | 113 | typedef size_t size_type; |
114 | typedef ptrdiff_t difference_type; | |
115 | typedef _Tp* pointer; | |
116 | typedef const _Tp* const_pointer; | |
117 | typedef _Tp& reference; | |
118 | typedef const _Tp& const_reference; | |
119 | typedef _Tp value_type; | |
120 | typedef _Array array_type; | |
121 | ||
5f304128 | 122 | #if __cplusplus >= 201103L |
ad62a02b | 123 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
124 | // 2103. std::allocator propagate_on_container_move_assignment | |
125 | typedef std::true_type propagate_on_container_move_assignment; | |
141d9a97 | 126 | |
127 | typedef std::true_type is_always_equal; | |
ad62a02b | 128 | #endif |
129 | ||
10eb30f0 | 130 | private: |
131 | array_type* _M_array; | |
132 | size_type _M_used; | |
133 | ||
134 | public: | |
30a54583 | 135 | template<typename _Tp1, typename _Array1 = _Array> |
136 | struct rebind | |
f2b08775 | 137 | { |
138 | typedef array_allocator<_Tp1, _Array1> other _GLIBCXX_DEPRECATED; | |
139 | } _GLIBCXX_DEPRECATED; | |
30a54583 | 140 | |
6cb05617 | 141 | array_allocator(array_type* __array = 0) _GLIBCXX_USE_NOEXCEPT |
10eb30f0 | 142 | : _M_array(__array), _M_used(size_type()) { } |
30a54583 | 143 | |
6cb05617 | 144 | array_allocator(const array_allocator& __o) _GLIBCXX_USE_NOEXCEPT |
10eb30f0 | 145 | : _M_array(__o._M_array), _M_used(__o._M_used) { } |
30a54583 | 146 | |
147 | template<typename _Tp1, typename _Array1> | |
6cb05617 | 148 | array_allocator(const array_allocator<_Tp1, _Array1>&) |
149 | _GLIBCXX_USE_NOEXCEPT | |
e4bb1925 | 150 | : _M_array(0), _M_used(size_type()) { } |
30a54583 | 151 | |
6cb05617 | 152 | ~array_allocator() _GLIBCXX_USE_NOEXCEPT { } |
30a54583 | 153 | |
154 | pointer | |
155 | allocate(size_type __n, const void* = 0) | |
156 | { | |
10eb30f0 | 157 | if (_M_array == 0 || _M_used + __n > _M_array->size()) |
4aa4288b | 158 | std::__throw_bad_alloc(); |
10eb30f0 | 159 | pointer __ret = _M_array->begin() + _M_used; |
160 | _M_used += __n; | |
30a54583 | 161 | return __ret; |
162 | } | |
f2b08775 | 163 | } _GLIBCXX_DEPRECATED; |
30a54583 | 164 | |
165 | template<typename _Tp, typename _Array> | |
166 | inline bool | |
167 | operator==(const array_allocator<_Tp, _Array>&, | |
168 | const array_allocator<_Tp, _Array>&) | |
169 | { return true; } | |
170 | ||
171 | template<typename _Tp, typename _Array> | |
172 | inline bool | |
173 | operator!=(const array_allocator<_Tp, _Array>&, | |
174 | const array_allocator<_Tp, _Array>&) | |
175 | { return false; } | |
1069247d | 176 | |
2948dd21 | 177 | _GLIBCXX_END_NAMESPACE_VERSION |
178 | } // namespace | |
30a54583 | 179 | |
59edbc2a | 180 | #pragma GCC diagnostic pop |
181 | ||
30a54583 | 182 | #endif |