xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/ext/array_allocator.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // array allocator -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2004-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj /** @file ext/array_allocator.h
26*38fd1498Szrj  *  This file is a GNU extension to the Standard C++ Library.
27*38fd1498Szrj  */
28*38fd1498Szrj 
29*38fd1498Szrj #ifndef _ARRAY_ALLOCATOR_H
30*38fd1498Szrj #define _ARRAY_ALLOCATOR_H 1
31*38fd1498Szrj 
32*38fd1498Szrj #include <bits/c++config.h>
33*38fd1498Szrj #include <new>
34*38fd1498Szrj #include <bits/functexcept.h>
35*38fd1498Szrj #include <tr1/array>
36*38fd1498Szrj #include <bits/move.h>
37*38fd1498Szrj #if __cplusplus >= 201103L
38*38fd1498Szrj #include <type_traits>
39*38fd1498Szrj #endif
40*38fd1498Szrj 
41*38fd1498Szrj // Suppress deprecated warning for this file.
42*38fd1498Szrj #pragma GCC diagnostic push
43*38fd1498Szrj #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
44*38fd1498Szrj 
_GLIBCXX_VISIBILITY(default)45*38fd1498Szrj namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
46*38fd1498Szrj {
47*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
48*38fd1498Szrj 
49*38fd1498Szrj  using std::size_t;
50*38fd1498Szrj  using std::ptrdiff_t;
51*38fd1498Szrj 
52*38fd1498Szrj   /// Base class.
53*38fd1498Szrj  template<typename _Tp>
54*38fd1498Szrj     class array_allocator_base
55*38fd1498Szrj     {
56*38fd1498Szrj     public:
57*38fd1498Szrj       typedef size_t     	size_type;
58*38fd1498Szrj       typedef ptrdiff_t  	difference_type;
59*38fd1498Szrj       typedef _Tp*       	pointer;
60*38fd1498Szrj       typedef const _Tp* 	const_pointer;
61*38fd1498Szrj       typedef _Tp&       	reference;
62*38fd1498Szrj       typedef const _Tp&	const_reference;
63*38fd1498Szrj       typedef _Tp        	value_type;
64*38fd1498Szrj 
65*38fd1498Szrj       pointer
66*38fd1498Szrj       address(reference __x) const _GLIBCXX_NOEXCEPT
67*38fd1498Szrj       { return std::__addressof(__x); }
68*38fd1498Szrj 
69*38fd1498Szrj       const_pointer
70*38fd1498Szrj       address(const_reference __x) const _GLIBCXX_NOEXCEPT
71*38fd1498Szrj       { return std::__addressof(__x); }
72*38fd1498Szrj 
73*38fd1498Szrj       void
74*38fd1498Szrj       deallocate(pointer, size_type)
75*38fd1498Szrj       {
76*38fd1498Szrj 	// Does nothing.
77*38fd1498Szrj       }
78*38fd1498Szrj 
79*38fd1498Szrj       size_type
80*38fd1498Szrj       max_size() const _GLIBCXX_USE_NOEXCEPT
81*38fd1498Szrj       { return size_t(-1) / sizeof(_Tp); }
82*38fd1498Szrj 
83*38fd1498Szrj #if __cplusplus >= 201103L
84*38fd1498Szrj       template<typename _Up, typename... _Args>
85*38fd1498Szrj         void
86*38fd1498Szrj         construct(_Up* __p, _Args&&... __args)
87*38fd1498Szrj 	{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
88*38fd1498Szrj 
89*38fd1498Szrj       template<typename _Up>
90*38fd1498Szrj         void
91*38fd1498Szrj         destroy(_Up* __p) { __p->~_Up(); }
92*38fd1498Szrj #else
93*38fd1498Szrj       // _GLIBCXX_RESOLVE_LIB_DEFECTS
94*38fd1498Szrj       // 402. wrong new expression in [some_] allocator::construct
95*38fd1498Szrj       void
96*38fd1498Szrj       construct(pointer __p, const _Tp& __val)
97*38fd1498Szrj       { ::new((void *)__p) value_type(__val); }
98*38fd1498Szrj 
99*38fd1498Szrj       void
100*38fd1498Szrj       destroy(pointer __p) { __p->~_Tp(); }
101*38fd1498Szrj #endif
102*38fd1498Szrj     } _GLIBCXX_DEPRECATED;
103*38fd1498Szrj 
104*38fd1498Szrj   /**
105*38fd1498Szrj    *  @brief  An allocator that uses previously allocated memory.
106*38fd1498Szrj    *  This memory can be externally, globally, or otherwise allocated.
107*38fd1498Szrj    *  @ingroup allocators
108*38fd1498Szrj    */
109*38fd1498Szrj   template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> >
110*38fd1498Szrj     class array_allocator : public array_allocator_base<_Tp>
111*38fd1498Szrj     {
112*38fd1498Szrj     public:
113*38fd1498Szrj       typedef size_t     	size_type;
114*38fd1498Szrj       typedef ptrdiff_t  	difference_type;
115*38fd1498Szrj       typedef _Tp*       	pointer;
116*38fd1498Szrj       typedef const _Tp* 	const_pointer;
117*38fd1498Szrj       typedef _Tp&       	reference;
118*38fd1498Szrj       typedef const _Tp& 	const_reference;
119*38fd1498Szrj       typedef _Tp        	value_type;
120*38fd1498Szrj       typedef _Array		array_type;
121*38fd1498Szrj 
122*38fd1498Szrj #if __cplusplus >= 201103L
123*38fd1498Szrj       // _GLIBCXX_RESOLVE_LIB_DEFECTS
124*38fd1498Szrj       // 2103. std::allocator propagate_on_container_move_assignment
125*38fd1498Szrj       typedef std::true_type propagate_on_container_move_assignment;
126*38fd1498Szrj 
127*38fd1498Szrj       typedef std::true_type is_always_equal;
128*38fd1498Szrj #endif
129*38fd1498Szrj 
130*38fd1498Szrj     private:
131*38fd1498Szrj       array_type* 	_M_array;
132*38fd1498Szrj       size_type 	_M_used;
133*38fd1498Szrj 
134*38fd1498Szrj     public:
135*38fd1498Szrj      template<typename _Tp1, typename _Array1 = _Array>
136*38fd1498Szrj         struct rebind
137*38fd1498Szrj         {
138*38fd1498Szrj 	  typedef array_allocator<_Tp1, _Array1> other _GLIBCXX_DEPRECATED;
139*38fd1498Szrj 	} _GLIBCXX_DEPRECATED;
140*38fd1498Szrj 
141*38fd1498Szrj       array_allocator(array_type* __array = 0) _GLIBCXX_USE_NOEXCEPT
142*38fd1498Szrj       : _M_array(__array), _M_used(size_type()) { }
143*38fd1498Szrj 
144*38fd1498Szrj       array_allocator(const array_allocator& __o) _GLIBCXX_USE_NOEXCEPT
145*38fd1498Szrj       : _M_array(__o._M_array), _M_used(__o._M_used) { }
146*38fd1498Szrj 
147*38fd1498Szrj       template<typename _Tp1, typename _Array1>
148*38fd1498Szrj         array_allocator(const array_allocator<_Tp1, _Array1>&)
149*38fd1498Szrj 	_GLIBCXX_USE_NOEXCEPT
150*38fd1498Szrj 	: _M_array(0), _M_used(size_type()) { }
151*38fd1498Szrj 
152*38fd1498Szrj       ~array_allocator() _GLIBCXX_USE_NOEXCEPT { }
153*38fd1498Szrj 
154*38fd1498Szrj       pointer
155*38fd1498Szrj       allocate(size_type __n, const void* = 0)
156*38fd1498Szrj       {
157*38fd1498Szrj 	if (_M_array == 0 || _M_used + __n > _M_array->size())
158*38fd1498Szrj 	  std::__throw_bad_alloc();
159*38fd1498Szrj 	pointer __ret = _M_array->begin() + _M_used;
160*38fd1498Szrj 	_M_used += __n;
161*38fd1498Szrj 	return __ret;
162*38fd1498Szrj       }
163*38fd1498Szrj     } _GLIBCXX_DEPRECATED;
164*38fd1498Szrj 
165*38fd1498Szrj   template<typename _Tp, typename _Array>
166*38fd1498Szrj     inline bool
167*38fd1498Szrj     operator==(const array_allocator<_Tp, _Array>&,
168*38fd1498Szrj 	       const array_allocator<_Tp, _Array>&)
169*38fd1498Szrj     { return true; }
170*38fd1498Szrj 
171*38fd1498Szrj   template<typename _Tp, typename _Array>
172*38fd1498Szrj     inline bool
173*38fd1498Szrj     operator!=(const array_allocator<_Tp, _Array>&,
174*38fd1498Szrj 	       const array_allocator<_Tp, _Array>&)
175*38fd1498Szrj     { return false; }
176*38fd1498Szrj 
177*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
178*38fd1498Szrj } // namespace
179*38fd1498Szrj 
180*38fd1498Szrj #pragma GCC diagnostic pop
181*38fd1498Szrj 
182*38fd1498Szrj #endif
183