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