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