Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libstdc++-v3 / include / ext / array_allocator.h
blob8689d9da26a6cafc5b840ac0888a2a15b2e4f530
1 // array allocator -*- C++ -*-
3 // Copyright (C) 2004, 2005 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.
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.
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.
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.
30 /** @file ext/array_allocator.h
31 * This file is a GNU extension to the Standard C++ Library.
34 #ifndef _ARRAY_ALLOCATOR_H
35 #define _ARRAY_ALLOCATOR_H 1
37 #include <cstddef>
38 #include <new>
39 #include <bits/functexcept.h>
40 #include <tr1/array>
42 namespace __gnu_cxx
44 /// @brief Base class.
45 template<typename _Tp>
46 class array_allocator_base
48 public:
49 typedef size_t size_type;
50 typedef ptrdiff_t difference_type;
51 typedef _Tp* pointer;
52 typedef const _Tp* const_pointer;
53 typedef _Tp& reference;
54 typedef const _Tp& const_reference;
55 typedef _Tp value_type;
57 pointer
58 address(reference __x) const { return &__x; }
60 const_pointer
61 address(const_reference __x) const { return &__x; }
63 void
64 deallocate(pointer, size_type)
66 // Does nothing.
69 size_type
70 max_size() const throw()
71 { return size_t(-1) / sizeof(_Tp); }
73 // _GLIBCXX_RESOLVE_LIB_DEFECTS
74 // 402. wrong new expression in [some_] allocator::construct
75 void
76 construct(pointer __p, const _Tp& __val)
77 { ::new(__p) value_type(__val); }
79 void
80 destroy(pointer __p) { __p->~_Tp(); }
81 };
83 /**
84 * @brief An allocator that uses previously allocated memory.
85 * This memory can be externally, globally, or otherwise allocated.
87 template<typename _Tp, typename _Array = std::tr1::array<_Tp> >
88 class array_allocator : public array_allocator_base<_Tp>
90 public:
91 typedef size_t size_type;
92 typedef ptrdiff_t difference_type;
93 typedef _Tp* pointer;
94 typedef const _Tp* const_pointer;
95 typedef _Tp& reference;
96 typedef const _Tp& const_reference;
97 typedef _Tp value_type;
99 typedef _Array array_type;
101 array_type* _M_array;
103 template<typename _Tp1, typename _Array1 = _Array>
104 struct rebind
105 { typedef array_allocator<_Tp1, _Array1> other; };
107 array_allocator(array_type* __array = NULL) throw()
108 : _M_array(__array)
111 array_allocator(const array_allocator& __o) throw()
112 : _M_array(__o._M_array) { }
114 template<typename _Tp1, typename _Array1>
115 array_allocator(const array_allocator<_Tp1, _Array1>&) throw()
116 : _M_array(NULL) { }
118 ~array_allocator() throw() { }
120 pointer
121 allocate(size_type __n, const void* = 0)
123 static size_type __array_used;
124 if (_M_array == 0 || __array_used + __n > _M_array->size())
125 std::__throw_bad_alloc();
126 pointer __ret = _M_array->begin() + __array_used;
127 __array_used += __n;
128 return __ret;
132 template<typename _Tp, typename _Array>
133 inline bool
134 operator==(const array_allocator<_Tp, _Array>&,
135 const array_allocator<_Tp, _Array>&)
136 { return true; }
138 template<typename _Tp, typename _Array>
139 inline bool
140 operator!=(const array_allocator<_Tp, _Array>&,
141 const array_allocator<_Tp, _Array>&)
142 { return false; }
143 } // namespace __gnu_cxx
145 #endif