re PR fortran/18111 (spurious warnings with -W -Wunused)
[gcc.git] / libstdc++-v3 / include / ext / array_allocator.h
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 <bits/functexcept.h>
36 #include <tr1/array>
37
38 namespace __gnu_cxx
39 {
40 template<typename _Tp>
41 class array_allocator_base
42 {
43 public:
44 typedef size_t size_type;
45 typedef ptrdiff_t difference_type;
46 typedef _Tp* pointer;
47 typedef const _Tp* const_pointer;
48 typedef _Tp& reference;
49 typedef const _Tp& const_reference;
50 typedef _Tp value_type;
51
52 pointer
53 address(reference __x) const { return &__x; }
54
55 const_pointer
56 address(const_reference __x) const { return &__x; }
57
58 void
59 deallocate(pointer, size_type)
60 {
61 // Does nothing.
62 }
63
64 size_type
65 max_size() const throw()
66 { return size_t(-1) / sizeof(_Tp); }
67
68 // _GLIBCXX_RESOLVE_LIB_DEFECTS
69 // 402. wrong new expression in [some_] allocator::construct
70 void
71 construct(pointer __p, const _Tp& __val)
72 { ::new(__p) value_type(__val); }
73
74 void
75 destroy(pointer __p) { __p->~_Tp(); }
76 };
77
78 /**
79 * @brief An allocator that uses previously allocated memory.
80 * This memory can be externally, globally, or otherwise allocated.
81 *
82 * (See @link Allocators allocators info @endlink for more.)
83 */
84 template<typename _Tp, typename _Array = std::tr1::array<_Tp> >
85 class array_allocator : public array_allocator_base<_Tp>
86 {
87 public:
88 typedef size_t size_type;
89 typedef ptrdiff_t difference_type;
90 typedef _Tp* pointer;
91 typedef const _Tp* const_pointer;
92 typedef _Tp& reference;
93 typedef const _Tp& const_reference;
94 typedef _Tp value_type;
95
96 typedef _Array array_type;
97
98 array_type* _M_array;
99
100 template<typename _Tp1, typename _Array1 = _Array>
101 struct rebind
102 { typedef array_allocator<_Tp1, _Array1> other; };
103
104 array_allocator(array_type* __array = NULL) throw()
105 : _M_array(__array)
106 { }
107
108 array_allocator(const array_allocator& __o) throw()
109 : _M_array(__o._M_array) { }
110
111 template<typename _Tp1, typename _Array1>
112 array_allocator(const array_allocator<_Tp1, _Array1>&) throw()
113 : _M_array(NULL) { }
114
115 ~array_allocator() throw() { }
116
117 pointer
118 allocate(size_type __n, const void* = 0)
119 {
120 static size_type __used;
121 if (_M_array == 0 || __used + __n > _M_array->size())
122 std::__throw_bad_alloc();
123 pointer __ret = _M_array->begin() + __used;
124 __used += __n;
125 return __ret;
126 }
127 };
128
129 template<typename _Tp, typename _Array>
130 inline bool
131 operator==(const array_allocator<_Tp, _Array>&,
132 const array_allocator<_Tp, _Array>&)
133 { return true; }
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 false; }
140 } // namespace __gnu_cxx
141
142 #endif