Import gcc-4.4.1
[dragonfly.git] / contrib / gcc-4.4 / libstdc++-v3 / include / ext / array_allocator.h
1 // array allocator -*- C++ -*-
2
3 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /** @file ext/array_allocator.h
27  *  This file is a GNU extension to the Standard C++ Library.
28  */
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 #include <bits/move.h>
38
39 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
40
41  using std::size_t;
42  using std::ptrdiff_t;
43
44   /// Base class.
45  template<typename _Tp>
46     class array_allocator_base
47     {
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;
56
57       pointer
58       address(reference __x) const { return &__x; }
59
60       const_pointer
61       address(const_reference __x) const { return &__x; }
62
63       void
64       deallocate(pointer, size_type)
65       { 
66         // Does nothing.
67       }
68
69       size_type
70       max_size() const throw() 
71       { return size_t(-1) / sizeof(_Tp); }
72
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((void *)__p) value_type(__val); }
78
79 #ifdef __GXX_EXPERIMENTAL_CXX0X__
80       template<typename... _Args>
81         void
82         construct(pointer __p, _Args&&... __args)
83         { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
84 #endif
85
86       void 
87       destroy(pointer __p) { __p->~_Tp(); }
88     };  
89
90   /**
91    *  @brief  An allocator that uses previously allocated memory.
92    *  This memory can be externally, globally, or otherwise allocated.
93    *  @ingroup allocators
94    */
95   template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> >
96     class array_allocator : public array_allocator_base<_Tp>
97     {
98     public:
99       typedef size_t            size_type;
100       typedef ptrdiff_t         difference_type;
101       typedef _Tp*              pointer;
102       typedef const _Tp*        const_pointer;
103       typedef _Tp&              reference;
104       typedef const _Tp&        const_reference;
105       typedef _Tp               value_type;
106       typedef _Array            array_type;
107
108     private:
109       array_type*       _M_array;
110       size_type         _M_used;
111
112     public:
113      template<typename _Tp1, typename _Array1 = _Array>
114         struct rebind
115         { typedef array_allocator<_Tp1, _Array1> other; };
116
117       array_allocator(array_type* __array = NULL) throw() 
118       : _M_array(__array), _M_used(size_type()) { }
119
120       array_allocator(const array_allocator& __o)  throw() 
121       : _M_array(__o._M_array), _M_used(__o._M_used) { }
122
123       template<typename _Tp1, typename _Array1>
124         array_allocator(const array_allocator<_Tp1, _Array1>&) throw()
125         : _M_array(NULL), _M_used(size_type()) { }
126
127       ~array_allocator() throw() { }
128
129       pointer
130       allocate(size_type __n, const void* = 0)
131       {
132         if (_M_array == 0 || _M_used + __n > _M_array->size())
133           std::__throw_bad_alloc();
134         pointer __ret = _M_array->begin() + _M_used;
135         _M_used += __n;
136         return __ret;
137       }
138     };
139
140   template<typename _Tp, typename _Array>
141     inline bool
142     operator==(const array_allocator<_Tp, _Array>&,
143                const array_allocator<_Tp, _Array>&)
144     { return true; }
145   
146   template<typename _Tp, typename _Array>
147     inline bool
148     operator!=(const array_allocator<_Tp, _Array>&, 
149                const array_allocator<_Tp, _Array>&)
150     { return false; }
151
152 _GLIBCXX_END_NAMESPACE
153
154 #endif