]>
Commit | Line | Data |
---|---|---|
3febde35 BK |
1 | // array allocator -*- C++ -*- |
2 | ||
3 | // Copyright (C) 2004 Free Software Foundation, Inc. | |
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 | |
18 | // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
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 | ||
30 | #ifndef _ARRAY_ALLOCATOR_H | |
31 | #define _ARRAY_ALLOCATOR_H 1 | |
32 | ||
33 | #include <cstddef> | |
34 | #include <new> | |
35 | #include <tr1/array> | |
36 | ||
37 | namespace __gnu_cxx | |
38 | { | |
39 | template<typename _Tp> | |
40 | class array_allocator_base | |
41 | { | |
42 | public: | |
43 | typedef size_t size_type; | |
44 | typedef ptrdiff_t difference_type; | |
45 | typedef _Tp* pointer; | |
46 | typedef const _Tp* const_pointer; | |
47 | typedef _Tp& reference; | |
48 | typedef const _Tp& const_reference; | |
49 | typedef _Tp value_type; | |
50 | ||
51 | pointer | |
52 | address(reference __x) const { return &__x; } | |
53 | ||
54 | const_pointer | |
55 | address(const_reference __x) const { return &__x; } | |
56 | ||
57 | void | |
5d1b2a1e | 58 | deallocate(pointer, size_type) |
3febde35 BK |
59 | { |
60 | // Does nothing. | |
61 | } | |
62 | ||
63 | size_type | |
64 | max_size() const throw() | |
65 | { return size_t(-1) / sizeof(_Tp); } | |
66 | ||
67 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
68 | // 402. wrong new expression in [some_] allocator::construct | |
69 | void | |
70 | construct(pointer __p, const _Tp& __val) | |
71 | { ::new(__p) value_type(__val); } | |
72 | ||
73 | void | |
74 | destroy(pointer __p) { __p->~_Tp(); } | |
75 | }; | |
76 | ||
77 | /** | |
78 | * @brief An allocator that uses previously allocated memory. | |
79 | * This memory can be externally, globally, or otherwise allocated. | |
80 | * | |
81 | * (See @link Allocators allocators info @endlink for more.) | |
82 | */ | |
83 | template<typename _Tp, typename _Array = std::tr1::array<_Tp> > | |
84 | class array_allocator : public array_allocator_base<_Tp> | |
85 | { | |
86 | public: | |
87 | typedef size_t size_type; | |
88 | typedef ptrdiff_t difference_type; | |
89 | typedef _Tp* pointer; | |
90 | typedef const _Tp* const_pointer; | |
91 | typedef _Tp& reference; | |
92 | typedef const _Tp& const_reference; | |
93 | typedef _Tp value_type; | |
94 | ||
95 | typedef _Array array_type; | |
96 | ||
97 | array_type* _M_array; | |
98 | ||
99 | template<typename _Tp1, typename _Array1 = _Array> | |
100 | struct rebind | |
101 | { typedef array_allocator<_Tp1, _Array1> other; }; | |
102 | ||
103 | array_allocator(array_type* __array = NULL) throw() | |
104 | : _M_array(__array) | |
105 | { } | |
106 | ||
107 | array_allocator(const array_allocator& __o) throw() | |
108 | : _M_array(__o._M_array) { } | |
109 | ||
110 | template<typename _Tp1, typename _Array1> | |
111 | array_allocator(const array_allocator<_Tp1, _Array1>&) throw() | |
112 | : _M_array(NULL) { } | |
113 | ||
114 | ~array_allocator() throw() { } | |
115 | ||
116 | pointer | |
117 | allocate(size_type __n, const void* = 0) | |
118 | { | |
cbb0dcef PC |
119 | static size_type __used; |
120 | if (__builtin_expect(__used + __n > array_type::_S_index, false)) | |
3febde35 | 121 | throw std::bad_alloc(); |
cbb0dcef PC |
122 | pointer __ret = _M_array->begin() + __used; |
123 | __used += __n; | |
3febde35 BK |
124 | return __ret; |
125 | } | |
126 | }; | |
127 | ||
128 | template<typename _Tp, typename _Array> | |
129 | inline bool | |
130 | operator==(const array_allocator<_Tp, _Array>&, | |
131 | const array_allocator<_Tp, _Array>&) | |
132 | { return true; } | |
133 | ||
134 | template<typename _Tp, typename _Array> | |
135 | inline bool | |
136 | operator!=(const array_allocator<_Tp, _Array>&, | |
137 | const array_allocator<_Tp, _Array>&) | |
138 | { return false; } | |
139 | } // namespace __gnu_cxx | |
140 | ||
141 | #endif |