libstdc++
array_allocator.h
Go to the documentation of this file.
1 // array allocator -*- C++ -*-
2 
3 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 <bits/c++config.h>
34 #include <new>
35 #include <bits/functexcept.h>
36 #include <tr1/array>
37 #include <bits/move.h>
38 
39 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 
43  using std::size_t;
44  using std::ptrdiff_t;
45 
46  /// Base class.
47  template<typename _Tp>
49  {
50  public:
51  typedef size_t size_type;
52  typedef ptrdiff_t difference_type;
53  typedef _Tp* pointer;
54  typedef const _Tp* const_pointer;
55  typedef _Tp& reference;
56  typedef const _Tp& const_reference;
57  typedef _Tp value_type;
58 
59  pointer
60  address(reference __x) const _GLIBCXX_NOEXCEPT
61  { return std::__addressof(__x); }
62 
63  const_pointer
64  address(const_reference __x) const _GLIBCXX_NOEXCEPT
65  { return std::__addressof(__x); }
66 
67  void
68  deallocate(pointer, size_type)
69  {
70  // Does nothing.
71  }
72 
73  size_type
74  max_size() const _GLIBCXX_USE_NOEXCEPT
75  { return size_t(-1) / sizeof(_Tp); }
76 
77 #ifdef __GXX_EXPERIMENTAL_CXX0X__
78  template<typename _Up, typename... _Args>
79  void
80  construct(_Up* __p, _Args&&... __args)
81  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
82 
83  template<typename _Up>
84  void
85  destroy(_Up* __p) { __p->~_Up(); }
86 #else
87  // _GLIBCXX_RESOLVE_LIB_DEFECTS
88  // 402. wrong new expression in [some_] allocator::construct
89  void
90  construct(pointer __p, const _Tp& __val)
91  { ::new((void *)__p) value_type(__val); }
92 
93  void
94  destroy(pointer __p) { __p->~_Tp(); }
95 #endif
96  };
97 
98  /**
99  * @brief An allocator that uses previously allocated memory.
100  * This memory can be externally, globally, or otherwise allocated.
101  * @ingroup allocators
102  */
103  template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> >
105  {
106  public:
107  typedef size_t size_type;
108  typedef ptrdiff_t difference_type;
109  typedef _Tp* pointer;
110  typedef const _Tp* const_pointer;
111  typedef _Tp& reference;
112  typedef const _Tp& const_reference;
113  typedef _Tp value_type;
114  typedef _Array array_type;
115 
116  private:
117  array_type* _M_array;
118  size_type _M_used;
119 
120  public:
121  template<typename _Tp1, typename _Array1 = _Array>
122  struct rebind
123  { typedef array_allocator<_Tp1, _Array1> other; };
124 
125  array_allocator(array_type* __array = 0) _GLIBCXX_USE_NOEXCEPT
126  : _M_array(__array), _M_used(size_type()) { }
127 
128  array_allocator(const array_allocator& __o) _GLIBCXX_USE_NOEXCEPT
129  : _M_array(__o._M_array), _M_used(__o._M_used) { }
130 
131  template<typename _Tp1, typename _Array1>
133  _GLIBCXX_USE_NOEXCEPT
134  : _M_array(0), _M_used(size_type()) { }
135 
136  ~array_allocator() _GLIBCXX_USE_NOEXCEPT { }
137 
138  pointer
139  allocate(size_type __n, const void* = 0)
140  {
141  if (_M_array == 0 || _M_used + __n > _M_array->size())
142  std::__throw_bad_alloc();
143  pointer __ret = _M_array->begin() + _M_used;
144  _M_used += __n;
145  return __ret;
146  }
147  };
148 
149  template<typename _Tp, typename _Array>
150  inline bool
151  operator==(const array_allocator<_Tp, _Array>&,
153  { return true; }
154 
155  template<typename _Tp, typename _Array>
156  inline bool
157  operator!=(const array_allocator<_Tp, _Array>&,
158  const array_allocator<_Tp, _Array>&)
159  { return false; }
160 
161 _GLIBCXX_END_NAMESPACE_VERSION
162 } // namespace
163 
164 #endif