]>
Commit | Line | Data |
---|---|---|
3febde35 BK |
1 | // array allocator -*- C++ -*- |
2 | ||
1b5dc776 | 3 | // Copyright (C) 2004-2012 Free Software Foundation, Inc. |
3febde35 BK |
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) |
3febde35 BK |
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/>. | |
3febde35 | 24 | |
0aa06b18 BK |
25 | /** @file ext/array_allocator.h |
26 | * This file is a GNU extension to the Standard C++ Library. | |
27 | */ | |
28 | ||
3febde35 BK |
29 | #ifndef _ARRAY_ALLOCATOR_H |
30 | #define _ARRAY_ALLOCATOR_H 1 | |
31 | ||
8fc81078 | 32 | #include <bits/c++config.h> |
3febde35 | 33 | #include <new> |
a063e891 | 34 | #include <bits/functexcept.h> |
3febde35 | 35 | #include <tr1/array> |
ca0f8fd1 | 36 | #include <bits/move.h> |
1b5dc776 JW |
37 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
38 | #include <type_traits> | |
39 | #endif | |
3febde35 | 40 | |
12ffa228 BK |
41 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
42 | { | |
43 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 44 | |
05a2763e MG |
45 | using std::size_t; |
46 | using std::ptrdiff_t; | |
47 | ||
939759fc | 48 | /// Base class. |
3febde35 BK |
49 | template<typename _Tp> |
50 | class array_allocator_base | |
51 | { | |
52 | public: | |
0c092147 BK |
53 | typedef size_t size_type; |
54 | typedef ptrdiff_t difference_type; | |
55 | typedef _Tp* pointer; | |
56 | typedef const _Tp* const_pointer; | |
57 | typedef _Tp& reference; | |
58 | typedef const _Tp& const_reference; | |
59 | typedef _Tp value_type; | |
3febde35 BK |
60 | |
61 | pointer | |
7d9cb054 PC |
62 | address(reference __x) const _GLIBCXX_NOEXCEPT |
63 | { return std::__addressof(__x); } | |
3febde35 BK |
64 | |
65 | const_pointer | |
7d9cb054 PC |
66 | address(const_reference __x) const _GLIBCXX_NOEXCEPT |
67 | { return std::__addressof(__x); } | |
3febde35 BK |
68 | |
69 | void | |
5d1b2a1e | 70 | deallocate(pointer, size_type) |
3febde35 BK |
71 | { |
72 | // Does nothing. | |
73 | } | |
74 | ||
75 | size_type | |
7d9cb054 | 76 | max_size() const _GLIBCXX_USE_NOEXCEPT |
3febde35 BK |
77 | { return size_t(-1) / sizeof(_Tp); } |
78 | ||
734f5023 | 79 | #if __cplusplus >= 201103L |
45ba8f9f JW |
80 | template<typename _Up, typename... _Args> |
81 | void | |
82 | construct(_Up* __p, _Args&&... __args) | |
83 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } | |
84 | ||
85 | template<typename _Up> | |
86 | void | |
87 | destroy(_Up* __p) { __p->~_Up(); } | |
88 | #else | |
3febde35 BK |
89 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
90 | // 402. wrong new expression in [some_] allocator::construct | |
91 | void | |
92 | construct(pointer __p, const _Tp& __val) | |
61fcb9fb PC |
93 | { ::new((void *)__p) value_type(__val); } |
94 | ||
3febde35 BK |
95 | void |
96 | destroy(pointer __p) { __p->~_Tp(); } | |
45ba8f9f | 97 | #endif |
3febde35 BK |
98 | }; |
99 | ||
100 | /** | |
101 | * @brief An allocator that uses previously allocated memory. | |
102 | * This memory can be externally, globally, or otherwise allocated. | |
5b9daa7e | 103 | * @ingroup allocators |
3febde35 | 104 | */ |
e7457c3e | 105 | template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> > |
3febde35 BK |
106 | class array_allocator : public array_allocator_base<_Tp> |
107 | { | |
108 | public: | |
0c092147 BK |
109 | typedef size_t size_type; |
110 | typedef ptrdiff_t difference_type; | |
111 | typedef _Tp* pointer; | |
112 | typedef const _Tp* const_pointer; | |
113 | typedef _Tp& reference; | |
114 | typedef const _Tp& const_reference; | |
115 | typedef _Tp value_type; | |
116 | typedef _Array array_type; | |
117 | ||
1b5dc776 JW |
118 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
119 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
120 | // 2103. std::allocator propagate_on_container_move_assignment | |
121 | typedef std::true_type propagate_on_container_move_assignment; | |
122 | #endif | |
123 | ||
0c092147 BK |
124 | private: |
125 | array_type* _M_array; | |
126 | size_type _M_used; | |
127 | ||
128 | public: | |
3febde35 BK |
129 | template<typename _Tp1, typename _Array1 = _Array> |
130 | struct rebind | |
131 | { typedef array_allocator<_Tp1, _Array1> other; }; | |
132 | ||
7d9cb054 | 133 | array_allocator(array_type* __array = 0) _GLIBCXX_USE_NOEXCEPT |
0c092147 | 134 | : _M_array(__array), _M_used(size_type()) { } |
3febde35 | 135 | |
7d9cb054 | 136 | array_allocator(const array_allocator& __o) _GLIBCXX_USE_NOEXCEPT |
0c092147 | 137 | : _M_array(__o._M_array), _M_used(__o._M_used) { } |
3febde35 BK |
138 | |
139 | template<typename _Tp1, typename _Array1> | |
7d9cb054 PC |
140 | array_allocator(const array_allocator<_Tp1, _Array1>&) |
141 | _GLIBCXX_USE_NOEXCEPT | |
8fc81078 | 142 | : _M_array(0), _M_used(size_type()) { } |
3febde35 | 143 | |
7d9cb054 | 144 | ~array_allocator() _GLIBCXX_USE_NOEXCEPT { } |
3febde35 BK |
145 | |
146 | pointer | |
147 | allocate(size_type __n, const void* = 0) | |
148 | { | |
0c092147 | 149 | if (_M_array == 0 || _M_used + __n > _M_array->size()) |
a063e891 | 150 | std::__throw_bad_alloc(); |
0c092147 BK |
151 | pointer __ret = _M_array->begin() + _M_used; |
152 | _M_used += __n; | |
3febde35 BK |
153 | return __ret; |
154 | } | |
155 | }; | |
156 | ||
157 | template<typename _Tp, typename _Array> | |
158 | inline bool | |
159 | operator==(const array_allocator<_Tp, _Array>&, | |
160 | const array_allocator<_Tp, _Array>&) | |
161 | { return true; } | |
162 | ||
163 | template<typename _Tp, typename _Array> | |
164 | inline bool | |
165 | operator!=(const array_allocator<_Tp, _Array>&, | |
166 | const array_allocator<_Tp, _Array>&) | |
167 | { return false; } | |
3cbc7af0 | 168 | |
12ffa228 BK |
169 | _GLIBCXX_END_NAMESPACE_VERSION |
170 | } // namespace | |
3febde35 BK |
171 | |
172 | #endif |