]>
Commit | Line | Data |
---|---|---|
3febde35 BK |
1 | // array allocator -*- C++ -*- |
2 | ||
5b9daa7e BK |
3 | // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 |
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 | |
9 | // Free Software Foundation; either version 2, 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 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING. If not, write to the Free | |
83f51799 | 19 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
3febde35 BK |
20 | // USA. |
21 | ||
22 | // As a special exception, you may use this file as part of a free software | |
23 | // library without restriction. Specifically, if other files instantiate | |
24 | // templates or use macros or inline functions from this file, or you compile | |
25 | // this file and link it with other files to produce an executable, this | |
26 | // file does not by itself cause the resulting executable to be covered by | |
27 | // the GNU General Public License. This exception does not however | |
28 | // invalidate any other reasons why the executable file might be covered by | |
29 | // the GNU General Public License. | |
30 | ||
0aa06b18 BK |
31 | /** @file ext/array_allocator.h |
32 | * This file is a GNU extension to the Standard C++ Library. | |
33 | */ | |
34 | ||
3febde35 BK |
35 | #ifndef _ARRAY_ALLOCATOR_H |
36 | #define _ARRAY_ALLOCATOR_H 1 | |
37 | ||
38 | #include <cstddef> | |
39 | #include <new> | |
a063e891 | 40 | #include <bits/functexcept.h> |
3febde35 | 41 | #include <tr1/array> |
ca0f8fd1 | 42 | #include <bits/move.h> |
3febde35 | 43 | |
3cbc7af0 BK |
44 | _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) |
45 | ||
05a2763e MG |
46 | using std::size_t; |
47 | using std::ptrdiff_t; | |
48 | ||
939759fc | 49 | /// Base class. |
3febde35 BK |
50 | template<typename _Tp> |
51 | class array_allocator_base | |
52 | { | |
53 | public: | |
0c092147 BK |
54 | typedef size_t size_type; |
55 | typedef ptrdiff_t difference_type; | |
56 | typedef _Tp* pointer; | |
57 | typedef const _Tp* const_pointer; | |
58 | typedef _Tp& reference; | |
59 | typedef const _Tp& const_reference; | |
60 | typedef _Tp value_type; | |
3febde35 BK |
61 | |
62 | pointer | |
63 | address(reference __x) const { return &__x; } | |
64 | ||
65 | const_pointer | |
66 | address(const_reference __x) const { return &__x; } | |
67 | ||
68 | void | |
5d1b2a1e | 69 | deallocate(pointer, size_type) |
3febde35 BK |
70 | { |
71 | // Does nothing. | |
72 | } | |
73 | ||
74 | size_type | |
75 | max_size() const throw() | |
76 | { return size_t(-1) / sizeof(_Tp); } | |
77 | ||
78 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
79 | // 402. wrong new expression in [some_] allocator::construct | |
80 | void | |
81 | construct(pointer __p, const _Tp& __val) | |
61fcb9fb PC |
82 | { ::new((void *)__p) value_type(__val); } |
83 | ||
84 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ | |
85 | template<typename... _Args> | |
86 | void | |
87 | construct(pointer __p, _Args&&... __args) | |
88 | { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); } | |
89 | #endif | |
3febde35 BK |
90 | |
91 | void | |
92 | destroy(pointer __p) { __p->~_Tp(); } | |
93 | }; | |
94 | ||
95 | /** | |
96 | * @brief An allocator that uses previously allocated memory. | |
97 | * This memory can be externally, globally, or otherwise allocated. | |
5b9daa7e | 98 | * @ingroup allocators |
3febde35 | 99 | */ |
e7457c3e | 100 | template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> > |
3febde35 BK |
101 | class array_allocator : public array_allocator_base<_Tp> |
102 | { | |
103 | public: | |
0c092147 BK |
104 | typedef size_t size_type; |
105 | typedef ptrdiff_t difference_type; | |
106 | typedef _Tp* pointer; | |
107 | typedef const _Tp* const_pointer; | |
108 | typedef _Tp& reference; | |
109 | typedef const _Tp& const_reference; | |
110 | typedef _Tp value_type; | |
111 | typedef _Array array_type; | |
112 | ||
113 | private: | |
114 | array_type* _M_array; | |
115 | size_type _M_used; | |
116 | ||
117 | public: | |
3febde35 BK |
118 | template<typename _Tp1, typename _Array1 = _Array> |
119 | struct rebind | |
120 | { typedef array_allocator<_Tp1, _Array1> other; }; | |
121 | ||
122 | array_allocator(array_type* __array = NULL) throw() | |
0c092147 | 123 | : _M_array(__array), _M_used(size_type()) { } |
3febde35 BK |
124 | |
125 | array_allocator(const array_allocator& __o) throw() | |
0c092147 | 126 | : _M_array(__o._M_array), _M_used(__o._M_used) { } |
3febde35 BK |
127 | |
128 | template<typename _Tp1, typename _Array1> | |
129 | array_allocator(const array_allocator<_Tp1, _Array1>&) throw() | |
0c092147 | 130 | : _M_array(NULL), _M_used(size_type()) { } |
3febde35 BK |
131 | |
132 | ~array_allocator() throw() { } | |
133 | ||
134 | pointer | |
135 | allocate(size_type __n, const void* = 0) | |
136 | { | |
0c092147 | 137 | if (_M_array == 0 || _M_used + __n > _M_array->size()) |
a063e891 | 138 | std::__throw_bad_alloc(); |
0c092147 BK |
139 | pointer __ret = _M_array->begin() + _M_used; |
140 | _M_used += __n; | |
3febde35 BK |
141 | return __ret; |
142 | } | |
143 | }; | |
144 | ||
145 | template<typename _Tp, typename _Array> | |
146 | inline bool | |
147 | operator==(const array_allocator<_Tp, _Array>&, | |
148 | const array_allocator<_Tp, _Array>&) | |
149 | { return true; } | |
150 | ||
151 | template<typename _Tp, typename _Array> | |
152 | inline bool | |
153 | operator!=(const array_allocator<_Tp, _Array>&, | |
154 | const array_allocator<_Tp, _Array>&) | |
155 | { return false; } | |
3cbc7af0 BK |
156 | |
157 | _GLIBCXX_END_NAMESPACE | |
3febde35 BK |
158 | |
159 | #endif |