]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/ext/array_allocator.h
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / ext / array_allocator.h
1 // array allocator -*- C++ -*-
2
3 // Copyright (C) 2004, 2005, 2006, 2007 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 /** @file ext/array_allocator.h
31  *  This file is a GNU extension to the Standard C++ Library.
32  */
33
34 #ifndef _ARRAY_ALLOCATOR_H
35 #define _ARRAY_ALLOCATOR_H 1
36
37 #include <cstddef>
38 #include <new>
39 #include <bits/functexcept.h>
40 #include <tr1/array>
41 #include <bits/stl_move.h>
42
43 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
44
45  using std::size_t;
46  using std::ptrdiff_t;
47
48   /// Base class.
49  template<typename _Tp>
50     class array_allocator_base
51     {
52     public:
53       typedef size_t            size_type;
54       typedef ptrdiff_t         difference_type;
55       typedef _Tp*              pointer;
56       typedef const _Tp*        const_pointer;
57       typedef _Tp&              reference;
58       typedef const _Tp&        const_reference;
59       typedef _Tp               value_type;
60
61       pointer
62       address(reference __x) const { return &__x; }
63
64       const_pointer
65       address(const_reference __x) const { return &__x; }
66
67       void
68       deallocate(pointer, size_type)
69       { 
70         // Does nothing.
71       }
72
73       size_type
74       max_size() const throw() 
75       { return size_t(-1) / sizeof(_Tp); }
76
77       // _GLIBCXX_RESOLVE_LIB_DEFECTS
78       // 402. wrong new expression in [some_] allocator::construct
79       void 
80       construct(pointer __p, const _Tp& __val) 
81       { ::new((void *)__p) value_type(__val); }
82
83 #ifdef __GXX_EXPERIMENTAL_CXX0X__
84       template<typename... _Args>
85         void
86         construct(pointer __p, _Args&&... __args)
87         { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
88 #endif
89
90       void 
91       destroy(pointer __p) { __p->~_Tp(); }
92     };  
93
94   /**
95    *  @brief  An allocator that uses previously allocated memory.
96    *  This memory can be externally, globally, or otherwise allocated.
97    */
98   template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> >
99     class array_allocator : public array_allocator_base<_Tp>
100     {
101     public:
102       typedef size_t            size_type;
103       typedef ptrdiff_t         difference_type;
104       typedef _Tp*              pointer;
105       typedef const _Tp*        const_pointer;
106       typedef _Tp&              reference;
107       typedef const _Tp&        const_reference;
108       typedef _Tp               value_type;
109       typedef _Array            array_type;
110
111     private:
112       array_type*       _M_array;
113       size_type         _M_used;
114
115     public:
116      template<typename _Tp1, typename _Array1 = _Array>
117         struct rebind
118         { typedef array_allocator<_Tp1, _Array1> other; };
119
120       array_allocator(array_type* __array = NULL) throw() 
121       : _M_array(__array), _M_used(size_type()) { }
122
123       array_allocator(const array_allocator& __o)  throw() 
124       : _M_array(__o._M_array), _M_used(__o._M_used) { }
125
126       template<typename _Tp1, typename _Array1>
127         array_allocator(const array_allocator<_Tp1, _Array1>&) throw()
128         : _M_array(NULL), _M_used(size_type()) { }
129
130       ~array_allocator() throw() { }
131
132       pointer
133       allocate(size_type __n, const void* = 0)
134       {
135         if (_M_array == 0 || _M_used + __n > _M_array->size())
136           std::__throw_bad_alloc();
137         pointer __ret = _M_array->begin() + _M_used;
138         _M_used += __n;
139         return __ret;
140       }
141     };
142
143   template<typename _Tp, typename _Array>
144     inline bool
145     operator==(const array_allocator<_Tp, _Array>&,
146                const array_allocator<_Tp, _Array>&)
147     { return true; }
148   
149   template<typename _Tp, typename _Array>
150     inline bool
151     operator!=(const array_allocator<_Tp, _Array>&, 
152                const array_allocator<_Tp, _Array>&)
153     { return false; }
154
155 _GLIBCXX_END_NAMESPACE
156
157 #endif