1*38fd1498Szrj // Bitmap 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/bitmap_allocator.h
26*38fd1498Szrj  *  This file is a GNU extension to the Standard C++ Library.
27*38fd1498Szrj  */
28*38fd1498Szrj 
29*38fd1498Szrj #ifndef _BITMAP_ALLOCATOR_H
30*38fd1498Szrj #define _BITMAP_ALLOCATOR_H 1
31*38fd1498Szrj 
32*38fd1498Szrj #include <utility> // For std::pair.
33*38fd1498Szrj #include <bits/functexcept.h> // For __throw_bad_alloc().
34*38fd1498Szrj #include <functional> // For greater_equal, and less_equal.
35*38fd1498Szrj #include <new> // For operator new.
36*38fd1498Szrj #include <debug/debug.h> // _GLIBCXX_DEBUG_ASSERT
37*38fd1498Szrj #include <ext/concurrence.h>
38*38fd1498Szrj #include <bits/move.h>
39*38fd1498Szrj 
40*38fd1498Szrj /** @brief The constant in the expression below is the alignment
41*38fd1498Szrj  * required in bytes.
42*38fd1498Szrj  */
43*38fd1498Szrj #define _BALLOC_ALIGN_BYTES 8
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   namespace __detail
53*38fd1498Szrj   {
54*38fd1498Szrj     /** @class  __mini_vector bitmap_allocator.h bitmap_allocator.h
55*38fd1498Szrj      *
56*38fd1498Szrj      *  @brief  __mini_vector<> is a stripped down version of the
57*38fd1498Szrj      *  full-fledged std::vector<>.
58*38fd1498Szrj      *
59*38fd1498Szrj      *  It is to be used only for built-in types or PODs. Notable
60*38fd1498Szrj      *  differences are:
61*38fd1498Szrj      *
62*38fd1498Szrj      *  1. Not all accessor functions are present.
63*38fd1498Szrj      *  2. Used ONLY for PODs.
64*38fd1498Szrj      *  3. No Allocator template argument. Uses ::operator new() to get
65*38fd1498Szrj      *  memory, and ::operator delete() to free it.
66*38fd1498Szrj      *  Caveat: The dtor does NOT free the memory allocated, so this a
67*38fd1498Szrj      *  memory-leaking vector!
68*38fd1498Szrj      */
69*38fd1498Szrj     template<typename _Tp>
70*38fd1498Szrj       class __mini_vector
71*38fd1498Szrj       {
72*38fd1498Szrj 	__mini_vector(const __mini_vector&);
73*38fd1498Szrj 	__mini_vector& operator=(const __mini_vector&);
74*38fd1498Szrj 
75*38fd1498Szrj       public:
76*38fd1498Szrj 	typedef _Tp value_type;
77*38fd1498Szrj 	typedef _Tp* pointer;
78*38fd1498Szrj 	typedef _Tp& reference;
79*38fd1498Szrj 	typedef const _Tp& const_reference;
80*38fd1498Szrj 	typedef size_t size_type;
81*38fd1498Szrj 	typedef ptrdiff_t difference_type;
82*38fd1498Szrj 	typedef pointer iterator;
83*38fd1498Szrj 
84*38fd1498Szrj       private:
85*38fd1498Szrj 	pointer _M_start;
86*38fd1498Szrj 	pointer _M_finish;
87*38fd1498Szrj 	pointer _M_end_of_storage;
88*38fd1498Szrj 
89*38fd1498Szrj 	size_type
90*38fd1498Szrj 	_M_space_left() const throw()
91*38fd1498Szrj 	{ return _M_end_of_storage - _M_finish; }
92*38fd1498Szrj 
93*38fd1498Szrj 	pointer
94*38fd1498Szrj 	allocate(size_type __n)
95*38fd1498Szrj 	{ return static_cast<pointer>(::operator new(__n * sizeof(_Tp))); }
96*38fd1498Szrj 
97*38fd1498Szrj 	void
98*38fd1498Szrj 	deallocate(pointer __p, size_type)
99*38fd1498Szrj 	{ ::operator delete(__p); }
100*38fd1498Szrj 
101*38fd1498Szrj       public:
102*38fd1498Szrj 	// Members used: size(), push_back(), pop_back(),
103*38fd1498Szrj 	// insert(iterator, const_reference), erase(iterator),
104*38fd1498Szrj 	// begin(), end(), back(), operator[].
105*38fd1498Szrj 
106*38fd1498Szrj 	__mini_vector()
107*38fd1498Szrj         : _M_start(0), _M_finish(0), _M_end_of_storage(0) { }
108*38fd1498Szrj 
109*38fd1498Szrj 	size_type
110*38fd1498Szrj 	size() const throw()
111*38fd1498Szrj 	{ return _M_finish - _M_start; }
112*38fd1498Szrj 
113*38fd1498Szrj 	iterator
114*38fd1498Szrj 	begin() const throw()
115*38fd1498Szrj 	{ return this->_M_start; }
116*38fd1498Szrj 
117*38fd1498Szrj 	iterator
118*38fd1498Szrj 	end() const throw()
119*38fd1498Szrj 	{ return this->_M_finish; }
120*38fd1498Szrj 
121*38fd1498Szrj 	reference
122*38fd1498Szrj 	back() const throw()
123*38fd1498Szrj 	{ return *(this->end() - 1); }
124*38fd1498Szrj 
125*38fd1498Szrj 	reference
126*38fd1498Szrj 	operator[](const size_type __pos) const throw()
127*38fd1498Szrj 	{ return this->_M_start[__pos]; }
128*38fd1498Szrj 
129*38fd1498Szrj 	void
130*38fd1498Szrj 	insert(iterator __pos, const_reference __x);
131*38fd1498Szrj 
132*38fd1498Szrj 	void
133*38fd1498Szrj 	push_back(const_reference __x)
134*38fd1498Szrj 	{
135*38fd1498Szrj 	  if (this->_M_space_left())
136*38fd1498Szrj 	    {
137*38fd1498Szrj 	      *this->end() = __x;
138*38fd1498Szrj 	      ++this->_M_finish;
139*38fd1498Szrj 	    }
140*38fd1498Szrj 	  else
141*38fd1498Szrj 	    this->insert(this->end(), __x);
142*38fd1498Szrj 	}
143*38fd1498Szrj 
144*38fd1498Szrj 	void
145*38fd1498Szrj 	pop_back() throw()
146*38fd1498Szrj 	{ --this->_M_finish; }
147*38fd1498Szrj 
148*38fd1498Szrj 	void
149*38fd1498Szrj 	erase(iterator __pos) throw();
150*38fd1498Szrj 
151*38fd1498Szrj 	void
152*38fd1498Szrj 	clear() throw()
153*38fd1498Szrj 	{ this->_M_finish = this->_M_start; }
154*38fd1498Szrj       };
155*38fd1498Szrj 
156*38fd1498Szrj     // Out of line function definitions.
157*38fd1498Szrj     template<typename _Tp>
158*38fd1498Szrj       void __mini_vector<_Tp>::
159*38fd1498Szrj       insert(iterator __pos, const_reference __x)
160*38fd1498Szrj       {
161*38fd1498Szrj 	if (this->_M_space_left())
162*38fd1498Szrj 	  {
163*38fd1498Szrj 	    size_type __to_move = this->_M_finish - __pos;
164*38fd1498Szrj 	    iterator __dest = this->end();
165*38fd1498Szrj 	    iterator __src = this->end() - 1;
166*38fd1498Szrj 
167*38fd1498Szrj 	    ++this->_M_finish;
168*38fd1498Szrj 	    while (__to_move)
169*38fd1498Szrj 	      {
170*38fd1498Szrj 		*__dest = *__src;
171*38fd1498Szrj 		--__dest; --__src; --__to_move;
172*38fd1498Szrj 	      }
173*38fd1498Szrj 	    *__pos = __x;
174*38fd1498Szrj 	  }
175*38fd1498Szrj 	else
176*38fd1498Szrj 	  {
177*38fd1498Szrj 	    size_type __new_size = this->size() ? this->size() * 2 : 1;
178*38fd1498Szrj 	    iterator __new_start = this->allocate(__new_size);
179*38fd1498Szrj 	    iterator __first = this->begin();
180*38fd1498Szrj 	    iterator __start = __new_start;
181*38fd1498Szrj 	    while (__first != __pos)
182*38fd1498Szrj 	      {
183*38fd1498Szrj 		*__start = *__first;
184*38fd1498Szrj 		++__start; ++__first;
185*38fd1498Szrj 	      }
186*38fd1498Szrj 	    *__start = __x;
187*38fd1498Szrj 	    ++__start;
188*38fd1498Szrj 	    while (__first != this->end())
189*38fd1498Szrj 	      {
190*38fd1498Szrj 		*__start = *__first;
191*38fd1498Szrj 		++__start; ++__first;
192*38fd1498Szrj 	      }
193*38fd1498Szrj 	    if (this->_M_start)
194*38fd1498Szrj 	      this->deallocate(this->_M_start, this->size());
195*38fd1498Szrj 
196*38fd1498Szrj 	    this->_M_start = __new_start;
197*38fd1498Szrj 	    this->_M_finish = __start;
198*38fd1498Szrj 	    this->_M_end_of_storage = this->_M_start + __new_size;
199*38fd1498Szrj 	  }
200*38fd1498Szrj       }
201*38fd1498Szrj 
202*38fd1498Szrj     template<typename _Tp>
203*38fd1498Szrj       void __mini_vector<_Tp>::
204*38fd1498Szrj       erase(iterator __pos) throw()
205*38fd1498Szrj       {
206*38fd1498Szrj 	while (__pos + 1 != this->end())
207*38fd1498Szrj 	  {
208*38fd1498Szrj 	    *__pos = __pos[1];
209*38fd1498Szrj 	    ++__pos;
210*38fd1498Szrj 	  }
211*38fd1498Szrj 	--this->_M_finish;
212*38fd1498Szrj       }
213*38fd1498Szrj 
214*38fd1498Szrj 
215*38fd1498Szrj     template<typename _Tp>
216*38fd1498Szrj       struct __mv_iter_traits
217*38fd1498Szrj       {
218*38fd1498Szrj 	typedef typename _Tp::value_type value_type;
219*38fd1498Szrj 	typedef typename _Tp::difference_type difference_type;
220*38fd1498Szrj       };
221*38fd1498Szrj 
222*38fd1498Szrj     template<typename _Tp>
223*38fd1498Szrj       struct __mv_iter_traits<_Tp*>
224*38fd1498Szrj       {
225*38fd1498Szrj 	typedef _Tp value_type;
226*38fd1498Szrj 	typedef ptrdiff_t difference_type;
227*38fd1498Szrj       };
228*38fd1498Szrj 
229*38fd1498Szrj     enum
230*38fd1498Szrj       {
231*38fd1498Szrj 	bits_per_byte = 8,
232*38fd1498Szrj 	bits_per_block = sizeof(size_t) * size_t(bits_per_byte)
233*38fd1498Szrj       };
234*38fd1498Szrj 
235*38fd1498Szrj     template<typename _ForwardIterator, typename _Tp, typename _Compare>
236*38fd1498Szrj       _ForwardIterator
237*38fd1498Szrj       __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
238*38fd1498Szrj 		    const _Tp& __val, _Compare __comp)
239*38fd1498Szrj       {
240*38fd1498Szrj 	typedef typename __mv_iter_traits<_ForwardIterator>::difference_type
241*38fd1498Szrj 	  _DistanceType;
242*38fd1498Szrj 
243*38fd1498Szrj 	_DistanceType __len = __last - __first;
244*38fd1498Szrj 	_DistanceType __half;
245*38fd1498Szrj 	_ForwardIterator __middle;
246*38fd1498Szrj 
247*38fd1498Szrj 	while (__len > 0)
248*38fd1498Szrj 	  {
249*38fd1498Szrj 	    __half = __len >> 1;
250*38fd1498Szrj 	    __middle = __first;
251*38fd1498Szrj 	    __middle += __half;
252*38fd1498Szrj 	    if (__comp(*__middle, __val))
253*38fd1498Szrj 	      {
254*38fd1498Szrj 		__first = __middle;
255*38fd1498Szrj 		++__first;
256*38fd1498Szrj 		__len = __len - __half - 1;
257*38fd1498Szrj 	      }
258*38fd1498Szrj 	    else
259*38fd1498Szrj 	      __len = __half;
260*38fd1498Szrj 	  }
261*38fd1498Szrj 	return __first;
262*38fd1498Szrj       }
263*38fd1498Szrj 
264*38fd1498Szrj     /** @brief The number of Blocks pointed to by the address pair
265*38fd1498Szrj      *  passed to the function.
266*38fd1498Szrj      */
267*38fd1498Szrj     template<typename _AddrPair>
268*38fd1498Szrj       inline size_t
269*38fd1498Szrj       __num_blocks(_AddrPair __ap)
270*38fd1498Szrj       { return (__ap.second - __ap.first) + 1; }
271*38fd1498Szrj 
272*38fd1498Szrj     /** @brief The number of Bit-maps pointed to by the address pair
273*38fd1498Szrj      *  passed to the function.
274*38fd1498Szrj      */
275*38fd1498Szrj     template<typename _AddrPair>
276*38fd1498Szrj       inline size_t
277*38fd1498Szrj       __num_bitmaps(_AddrPair __ap)
278*38fd1498Szrj       { return __num_blocks(__ap) / size_t(bits_per_block); }
279*38fd1498Szrj 
280*38fd1498Szrj     // _Tp should be a pointer type.
281*38fd1498Szrj     template<typename _Tp>
282*38fd1498Szrj       class _Inclusive_between
283*38fd1498Szrj       : public std::unary_function<typename std::pair<_Tp, _Tp>, bool>
284*38fd1498Szrj       {
285*38fd1498Szrj 	typedef _Tp pointer;
286*38fd1498Szrj 	pointer _M_ptr_value;
287*38fd1498Szrj 	typedef typename std::pair<_Tp, _Tp> _Block_pair;
288*38fd1498Szrj 
289*38fd1498Szrj       public:
290*38fd1498Szrj 	_Inclusive_between(pointer __ptr) : _M_ptr_value(__ptr)
291*38fd1498Szrj 	{ }
292*38fd1498Szrj 
293*38fd1498Szrj 	bool
294*38fd1498Szrj 	operator()(_Block_pair __bp) const throw()
295*38fd1498Szrj 	{
296*38fd1498Szrj 	  if (std::less_equal<pointer>()(_M_ptr_value, __bp.second)
297*38fd1498Szrj 	      && std::greater_equal<pointer>()(_M_ptr_value, __bp.first))
298*38fd1498Szrj 	    return true;
299*38fd1498Szrj 	  else
300*38fd1498Szrj 	    return false;
301*38fd1498Szrj 	}
302*38fd1498Szrj       };
303*38fd1498Szrj 
304*38fd1498Szrj     // Used to pass a Functor to functions by reference.
305*38fd1498Szrj     template<typename _Functor>
306*38fd1498Szrj       class _Functor_Ref
307*38fd1498Szrj       : public std::unary_function<typename _Functor::argument_type,
308*38fd1498Szrj 				   typename _Functor::result_type>
309*38fd1498Szrj       {
310*38fd1498Szrj 	_Functor& _M_fref;
311*38fd1498Szrj 
312*38fd1498Szrj       public:
313*38fd1498Szrj 	typedef typename _Functor::argument_type argument_type;
314*38fd1498Szrj 	typedef typename _Functor::result_type result_type;
315*38fd1498Szrj 
316*38fd1498Szrj 	_Functor_Ref(_Functor& __fref) : _M_fref(__fref)
317*38fd1498Szrj 	{ }
318*38fd1498Szrj 
319*38fd1498Szrj 	result_type
320*38fd1498Szrj 	operator()(argument_type __arg)
321*38fd1498Szrj 	{ return _M_fref(__arg); }
322*38fd1498Szrj       };
323*38fd1498Szrj 
324*38fd1498Szrj     /** @class  _Ffit_finder bitmap_allocator.h bitmap_allocator.h
325*38fd1498Szrj      *
326*38fd1498Szrj      *  @brief  The class which acts as a predicate for applying the
327*38fd1498Szrj      *  first-fit memory allocation policy for the bitmap allocator.
328*38fd1498Szrj      */
329*38fd1498Szrj     // _Tp should be a pointer type, and _Alloc is the Allocator for
330*38fd1498Szrj     // the vector.
331*38fd1498Szrj     template<typename _Tp>
332*38fd1498Szrj       class _Ffit_finder
333*38fd1498Szrj       : public std::unary_function<typename std::pair<_Tp, _Tp>, bool>
334*38fd1498Szrj       {
335*38fd1498Szrj 	typedef typename std::pair<_Tp, _Tp> _Block_pair;
336*38fd1498Szrj 	typedef typename __detail::__mini_vector<_Block_pair> _BPVector;
337*38fd1498Szrj 	typedef typename _BPVector::difference_type _Counter_type;
338*38fd1498Szrj 
339*38fd1498Szrj 	size_t* _M_pbitmap;
340*38fd1498Szrj 	_Counter_type _M_data_offset;
341*38fd1498Szrj 
342*38fd1498Szrj       public:
343*38fd1498Szrj 	_Ffit_finder() : _M_pbitmap(0), _M_data_offset(0)
344*38fd1498Szrj 	{ }
345*38fd1498Szrj 
346*38fd1498Szrj 	bool
347*38fd1498Szrj 	operator()(_Block_pair __bp) throw()
348*38fd1498Szrj 	{
349*38fd1498Szrj 	  // Set the _rover to the last physical location bitmap,
350*38fd1498Szrj 	  // which is the bitmap which belongs to the first free
351*38fd1498Szrj 	  // block. Thus, the bitmaps are in exact reverse order of
352*38fd1498Szrj 	  // the actual memory layout. So, we count down the bitmaps,
353*38fd1498Szrj 	  // which is the same as moving up the memory.
354*38fd1498Szrj 
355*38fd1498Szrj 	  // If the used count stored at the start of the Bit Map headers
356*38fd1498Szrj 	  // is equal to the number of Objects that the current Block can
357*38fd1498Szrj 	  // store, then there is definitely no space for another single
358*38fd1498Szrj 	  // object, so just return false.
359*38fd1498Szrj 	  _Counter_type __diff = __detail::__num_bitmaps(__bp);
360*38fd1498Szrj 
361*38fd1498Szrj 	  if (*(reinterpret_cast<size_t*>
362*38fd1498Szrj 		(__bp.first) - (__diff + 1)) == __detail::__num_blocks(__bp))
363*38fd1498Szrj 	    return false;
364*38fd1498Szrj 
365*38fd1498Szrj 	  size_t* __rover = reinterpret_cast<size_t*>(__bp.first) - 1;
366*38fd1498Szrj 
367*38fd1498Szrj 	  for (_Counter_type __i = 0; __i < __diff; ++__i)
368*38fd1498Szrj 	    {
369*38fd1498Szrj 	      _M_data_offset = __i;
370*38fd1498Szrj 	      if (*__rover)
371*38fd1498Szrj 		{
372*38fd1498Szrj 		  _M_pbitmap = __rover;
373*38fd1498Szrj 		  return true;
374*38fd1498Szrj 		}
375*38fd1498Szrj 	      --__rover;
376*38fd1498Szrj 	    }
377*38fd1498Szrj 	  return false;
378*38fd1498Szrj 	}
379*38fd1498Szrj 
380*38fd1498Szrj 	size_t*
381*38fd1498Szrj 	_M_get() const throw()
382*38fd1498Szrj 	{ return _M_pbitmap; }
383*38fd1498Szrj 
384*38fd1498Szrj 	_Counter_type
385*38fd1498Szrj 	_M_offset() const throw()
386*38fd1498Szrj 	{ return _M_data_offset * size_t(bits_per_block); }
387*38fd1498Szrj       };
388*38fd1498Szrj 
389*38fd1498Szrj     /** @class  _Bitmap_counter bitmap_allocator.h bitmap_allocator.h
390*38fd1498Szrj      *
391*38fd1498Szrj      *  @brief  The bitmap counter which acts as the bitmap
392*38fd1498Szrj      *  manipulator, and manages the bit-manipulation functions and
393*38fd1498Szrj      *  the searching and identification functions on the bit-map.
394*38fd1498Szrj      */
395*38fd1498Szrj     // _Tp should be a pointer type.
396*38fd1498Szrj     template<typename _Tp>
397*38fd1498Szrj       class _Bitmap_counter
398*38fd1498Szrj       {
399*38fd1498Szrj 	typedef typename
400*38fd1498Szrj 	__detail::__mini_vector<typename std::pair<_Tp, _Tp> > _BPVector;
401*38fd1498Szrj 	typedef typename _BPVector::size_type _Index_type;
402*38fd1498Szrj 	typedef _Tp pointer;
403*38fd1498Szrj 
404*38fd1498Szrj 	_BPVector& _M_vbp;
405*38fd1498Szrj 	size_t* _M_curr_bmap;
406*38fd1498Szrj 	size_t* _M_last_bmap_in_block;
407*38fd1498Szrj 	_Index_type _M_curr_index;
408*38fd1498Szrj 
409*38fd1498Szrj       public:
410*38fd1498Szrj 	// Use the 2nd parameter with care. Make sure that such an
411*38fd1498Szrj 	// entry exists in the vector before passing that particular
412*38fd1498Szrj 	// index to this ctor.
413*38fd1498Szrj 	_Bitmap_counter(_BPVector& Rvbp, long __index = -1) : _M_vbp(Rvbp)
414*38fd1498Szrj 	{ this->_M_reset(__index); }
415*38fd1498Szrj 
416*38fd1498Szrj 	void
417*38fd1498Szrj 	_M_reset(long __index = -1) throw()
418*38fd1498Szrj 	{
419*38fd1498Szrj 	  if (__index == -1)
420*38fd1498Szrj 	    {
421*38fd1498Szrj 	      _M_curr_bmap = 0;
422*38fd1498Szrj 	      _M_curr_index = static_cast<_Index_type>(-1);
423*38fd1498Szrj 	      return;
424*38fd1498Szrj 	    }
425*38fd1498Szrj 
426*38fd1498Szrj 	  _M_curr_index = __index;
427*38fd1498Szrj 	  _M_curr_bmap = reinterpret_cast<size_t*>
428*38fd1498Szrj 	    (_M_vbp[_M_curr_index].first) - 1;
429*38fd1498Szrj 
430*38fd1498Szrj 	  _GLIBCXX_DEBUG_ASSERT(__index <= (long)_M_vbp.size() - 1);
431*38fd1498Szrj 
432*38fd1498Szrj 	  _M_last_bmap_in_block = _M_curr_bmap
433*38fd1498Szrj 	    - ((_M_vbp[_M_curr_index].second
434*38fd1498Szrj 		- _M_vbp[_M_curr_index].first + 1)
435*38fd1498Szrj 	       / size_t(bits_per_block) - 1);
436*38fd1498Szrj 	}
437*38fd1498Szrj 
438*38fd1498Szrj 	// Dangerous Function! Use with extreme care. Pass to this
439*38fd1498Szrj 	// function ONLY those values that are known to be correct,
440*38fd1498Szrj 	// otherwise this will mess up big time.
441*38fd1498Szrj 	void
442*38fd1498Szrj 	_M_set_internal_bitmap(size_t* __new_internal_marker) throw()
443*38fd1498Szrj 	{ _M_curr_bmap = __new_internal_marker; }
444*38fd1498Szrj 
445*38fd1498Szrj 	bool
446*38fd1498Szrj 	_M_finished() const throw()
447*38fd1498Szrj 	{ return(_M_curr_bmap == 0); }
448*38fd1498Szrj 
449*38fd1498Szrj 	_Bitmap_counter&
450*38fd1498Szrj 	operator++() throw()
451*38fd1498Szrj 	{
452*38fd1498Szrj 	  if (_M_curr_bmap == _M_last_bmap_in_block)
453*38fd1498Szrj 	    {
454*38fd1498Szrj 	      if (++_M_curr_index == _M_vbp.size())
455*38fd1498Szrj 		_M_curr_bmap = 0;
456*38fd1498Szrj 	      else
457*38fd1498Szrj 		this->_M_reset(_M_curr_index);
458*38fd1498Szrj 	    }
459*38fd1498Szrj 	  else
460*38fd1498Szrj 	    --_M_curr_bmap;
461*38fd1498Szrj 	  return *this;
462*38fd1498Szrj 	}
463*38fd1498Szrj 
464*38fd1498Szrj 	size_t*
465*38fd1498Szrj 	_M_get() const throw()
466*38fd1498Szrj 	{ return _M_curr_bmap; }
467*38fd1498Szrj 
468*38fd1498Szrj 	pointer
469*38fd1498Szrj 	_M_base() const throw()
470*38fd1498Szrj 	{ return _M_vbp[_M_curr_index].first; }
471*38fd1498Szrj 
472*38fd1498Szrj 	_Index_type
473*38fd1498Szrj 	_M_offset() const throw()
474*38fd1498Szrj 	{
475*38fd1498Szrj 	  return size_t(bits_per_block)
476*38fd1498Szrj 	    * ((reinterpret_cast<size_t*>(this->_M_base())
477*38fd1498Szrj 		- _M_curr_bmap) - 1);
478*38fd1498Szrj 	}
479*38fd1498Szrj 
480*38fd1498Szrj 	_Index_type
481*38fd1498Szrj 	_M_where() const throw()
482*38fd1498Szrj 	{ return _M_curr_index; }
483*38fd1498Szrj       };
484*38fd1498Szrj 
485*38fd1498Szrj     /** @brief  Mark a memory address as allocated by re-setting the
486*38fd1498Szrj      *  corresponding bit in the bit-map.
487*38fd1498Szrj      */
488*38fd1498Szrj     inline void
489*38fd1498Szrj     __bit_allocate(size_t* __pbmap, size_t __pos) throw()
490*38fd1498Szrj     {
491*38fd1498Szrj       size_t __mask = 1 << __pos;
492*38fd1498Szrj       __mask = ~__mask;
493*38fd1498Szrj       *__pbmap &= __mask;
494*38fd1498Szrj     }
495*38fd1498Szrj 
496*38fd1498Szrj     /** @brief  Mark a memory address as free by setting the
497*38fd1498Szrj      *  corresponding bit in the bit-map.
498*38fd1498Szrj      */
499*38fd1498Szrj     inline void
500*38fd1498Szrj     __bit_free(size_t* __pbmap, size_t __pos) throw()
501*38fd1498Szrj     {
502*38fd1498Szrj       size_t __mask = 1 << __pos;
503*38fd1498Szrj       *__pbmap |= __mask;
504*38fd1498Szrj     }
505*38fd1498Szrj   } // namespace __detail
506*38fd1498Szrj 
507*38fd1498Szrj   /** @brief  Generic Version of the bsf instruction.
508*38fd1498Szrj    */
509*38fd1498Szrj   inline size_t
510*38fd1498Szrj   _Bit_scan_forward(size_t __num)
511*38fd1498Szrj   { return static_cast<size_t>(__builtin_ctzl(__num)); }
512*38fd1498Szrj 
513*38fd1498Szrj   /** @class  free_list bitmap_allocator.h bitmap_allocator.h
514*38fd1498Szrj    *
515*38fd1498Szrj    *  @brief  The free list class for managing chunks of memory to be
516*38fd1498Szrj    *  given to and returned by the bitmap_allocator.
517*38fd1498Szrj    */
518*38fd1498Szrj   class free_list
519*38fd1498Szrj   {
520*38fd1498Szrj   public:
521*38fd1498Szrj     typedef size_t* 				value_type;
522*38fd1498Szrj     typedef __detail::__mini_vector<value_type> vector_type;
523*38fd1498Szrj     typedef vector_type::iterator 		iterator;
524*38fd1498Szrj     typedef __mutex				__mutex_type;
525*38fd1498Szrj 
526*38fd1498Szrj   private:
527*38fd1498Szrj     struct _LT_pointer_compare
528*38fd1498Szrj     {
529*38fd1498Szrj       bool
530*38fd1498Szrj       operator()(const size_t* __pui,
531*38fd1498Szrj 		 const size_t __cui) const throw()
532*38fd1498Szrj       { return *__pui < __cui; }
533*38fd1498Szrj     };
534*38fd1498Szrj 
535*38fd1498Szrj #if defined __GTHREADS
536*38fd1498Szrj     __mutex_type&
537*38fd1498Szrj     _M_get_mutex()
538*38fd1498Szrj     {
539*38fd1498Szrj       static __mutex_type _S_mutex;
540*38fd1498Szrj       return _S_mutex;
541*38fd1498Szrj     }
542*38fd1498Szrj #endif
543*38fd1498Szrj 
544*38fd1498Szrj     vector_type&
545*38fd1498Szrj     _M_get_free_list()
546*38fd1498Szrj     {
547*38fd1498Szrj       static vector_type _S_free_list;
548*38fd1498Szrj       return _S_free_list;
549*38fd1498Szrj     }
550*38fd1498Szrj 
551*38fd1498Szrj     /** @brief  Performs validation of memory based on their size.
552*38fd1498Szrj      *
553*38fd1498Szrj      *  @param  __addr The pointer to the memory block to be
554*38fd1498Szrj      *  validated.
555*38fd1498Szrj      *
556*38fd1498Szrj      *  Validates the memory block passed to this function and
557*38fd1498Szrj      *  appropriately performs the action of managing the free list of
558*38fd1498Szrj      *  blocks by adding this block to the free list or deleting this
559*38fd1498Szrj      *  or larger blocks from the free list.
560*38fd1498Szrj      */
561*38fd1498Szrj     void
562*38fd1498Szrj     _M_validate(size_t* __addr) throw()
563*38fd1498Szrj     {
564*38fd1498Szrj       vector_type& __free_list = _M_get_free_list();
565*38fd1498Szrj       const vector_type::size_type __max_size = 64;
566*38fd1498Szrj       if (__free_list.size() >= __max_size)
567*38fd1498Szrj 	{
568*38fd1498Szrj 	  // Ok, the threshold value has been reached.  We determine
569*38fd1498Szrj 	  // which block to remove from the list of free blocks.
570*38fd1498Szrj 	  if (*__addr >= *__free_list.back())
571*38fd1498Szrj 	    {
572*38fd1498Szrj 	      // Ok, the new block is greater than or equal to the
573*38fd1498Szrj 	      // last block in the list of free blocks. We just free
574*38fd1498Szrj 	      // the new block.
575*38fd1498Szrj 	      ::operator delete(static_cast<void*>(__addr));
576*38fd1498Szrj 	      return;
577*38fd1498Szrj 	    }
578*38fd1498Szrj 	  else
579*38fd1498Szrj 	    {
580*38fd1498Szrj 	      // Deallocate the last block in the list of free lists,
581*38fd1498Szrj 	      // and insert the new one in its correct position.
582*38fd1498Szrj 	      ::operator delete(static_cast<void*>(__free_list.back()));
583*38fd1498Szrj 	      __free_list.pop_back();
584*38fd1498Szrj 	    }
585*38fd1498Szrj 	}
586*38fd1498Szrj 
587*38fd1498Szrj       // Just add the block to the list of free lists unconditionally.
588*38fd1498Szrj       iterator __temp = __detail::__lower_bound
589*38fd1498Szrj 	(__free_list.begin(), __free_list.end(),
590*38fd1498Szrj 	 *__addr, _LT_pointer_compare());
591*38fd1498Szrj 
592*38fd1498Szrj       // We may insert the new free list before _temp;
593*38fd1498Szrj       __free_list.insert(__temp, __addr);
594*38fd1498Szrj     }
595*38fd1498Szrj 
596*38fd1498Szrj     /** @brief  Decides whether the wastage of memory is acceptable for
597*38fd1498Szrj      *  the current memory request and returns accordingly.
598*38fd1498Szrj      *
599*38fd1498Szrj      *  @param __block_size The size of the block available in the free
600*38fd1498Szrj      *  list.
601*38fd1498Szrj      *
602*38fd1498Szrj      *  @param __required_size The required size of the memory block.
603*38fd1498Szrj      *
604*38fd1498Szrj      *  @return true if the wastage incurred is acceptable, else returns
605*38fd1498Szrj      *  false.
606*38fd1498Szrj      */
607*38fd1498Szrj     bool
608*38fd1498Szrj     _M_should_i_give(size_t __block_size,
609*38fd1498Szrj 		     size_t __required_size) throw()
610*38fd1498Szrj     {
611*38fd1498Szrj       const size_t __max_wastage_percentage = 36;
612*38fd1498Szrj       if (__block_size >= __required_size &&
613*38fd1498Szrj 	  (((__block_size - __required_size) * 100 / __block_size)
614*38fd1498Szrj 	   < __max_wastage_percentage))
615*38fd1498Szrj 	return true;
616*38fd1498Szrj       else
617*38fd1498Szrj 	return false;
618*38fd1498Szrj     }
619*38fd1498Szrj 
620*38fd1498Szrj   public:
621*38fd1498Szrj     /** @brief This function returns the block of memory to the
622*38fd1498Szrj      *  internal free list.
623*38fd1498Szrj      *
624*38fd1498Szrj      *  @param  __addr The pointer to the memory block that was given
625*38fd1498Szrj      *  by a call to the _M_get function.
626*38fd1498Szrj      */
627*38fd1498Szrj     inline void
628*38fd1498Szrj     _M_insert(size_t* __addr) throw()
629*38fd1498Szrj     {
630*38fd1498Szrj #if defined __GTHREADS
631*38fd1498Szrj       __scoped_lock __bfl_lock(_M_get_mutex());
632*38fd1498Szrj #endif
633*38fd1498Szrj       // Call _M_validate to decide what should be done with
634*38fd1498Szrj       // this particular free list.
635*38fd1498Szrj       this->_M_validate(reinterpret_cast<size_t*>(__addr) - 1);
636*38fd1498Szrj       // See discussion as to why this is 1!
637*38fd1498Szrj     }
638*38fd1498Szrj 
639*38fd1498Szrj     /** @brief  This function gets a block of memory of the specified
640*38fd1498Szrj      *  size from the free list.
641*38fd1498Szrj      *
642*38fd1498Szrj      *  @param  __sz The size in bytes of the memory required.
643*38fd1498Szrj      *
644*38fd1498Szrj      *  @return  A pointer to the new memory block of size at least
645*38fd1498Szrj      *  equal to that requested.
646*38fd1498Szrj      */
647*38fd1498Szrj     size_t*
648*38fd1498Szrj     _M_get(size_t __sz) _GLIBCXX_THROW(std::bad_alloc);
649*38fd1498Szrj 
650*38fd1498Szrj     /** @brief  This function just clears the internal Free List, and
651*38fd1498Szrj      *  gives back all the memory to the OS.
652*38fd1498Szrj      */
653*38fd1498Szrj     void
654*38fd1498Szrj     _M_clear();
655*38fd1498Szrj   };
656*38fd1498Szrj 
657*38fd1498Szrj 
658*38fd1498Szrj   // Forward declare the class.
659*38fd1498Szrj   template<typename _Tp>
660*38fd1498Szrj     class bitmap_allocator;
661*38fd1498Szrj 
662*38fd1498Szrj   // Specialize for void:
663*38fd1498Szrj   template<>
664*38fd1498Szrj     class bitmap_allocator<void>
665*38fd1498Szrj     {
666*38fd1498Szrj     public:
667*38fd1498Szrj       typedef void*       pointer;
668*38fd1498Szrj       typedef const void* const_pointer;
669*38fd1498Szrj 
670*38fd1498Szrj       // Reference-to-void members are impossible.
671*38fd1498Szrj       typedef void  value_type;
672*38fd1498Szrj       template<typename _Tp1>
673*38fd1498Szrj         struct rebind
674*38fd1498Szrj 	{
675*38fd1498Szrj 	  typedef bitmap_allocator<_Tp1> other;
676*38fd1498Szrj 	};
677*38fd1498Szrj     };
678*38fd1498Szrj 
679*38fd1498Szrj   /**
680*38fd1498Szrj    * @brief Bitmap Allocator, primary template.
681*38fd1498Szrj    * @ingroup allocators
682*38fd1498Szrj    */
683*38fd1498Szrj   template<typename _Tp>
684*38fd1498Szrj     class bitmap_allocator : private free_list
685*38fd1498Szrj     {
686*38fd1498Szrj     public:
687*38fd1498Szrj       typedef size_t    		size_type;
688*38fd1498Szrj       typedef ptrdiff_t 		difference_type;
689*38fd1498Szrj       typedef _Tp*        		pointer;
690*38fd1498Szrj       typedef const _Tp*  		const_pointer;
691*38fd1498Szrj       typedef _Tp&        		reference;
692*38fd1498Szrj       typedef const _Tp&  		const_reference;
693*38fd1498Szrj       typedef _Tp         		value_type;
694*38fd1498Szrj       typedef free_list::__mutex_type 	__mutex_type;
695*38fd1498Szrj 
696*38fd1498Szrj       template<typename _Tp1>
697*38fd1498Szrj         struct rebind
698*38fd1498Szrj 	{
699*38fd1498Szrj 	  typedef bitmap_allocator<_Tp1> other;
700*38fd1498Szrj 	};
701*38fd1498Szrj 
702*38fd1498Szrj #if __cplusplus >= 201103L
703*38fd1498Szrj       // _GLIBCXX_RESOLVE_LIB_DEFECTS
704*38fd1498Szrj       // 2103. propagate_on_container_move_assignment
705*38fd1498Szrj       typedef std::true_type propagate_on_container_move_assignment;
706*38fd1498Szrj #endif
707*38fd1498Szrj 
708*38fd1498Szrj     private:
709*38fd1498Szrj       template<size_t _BSize, size_t _AlignSize>
710*38fd1498Szrj         struct aligned_size
711*38fd1498Szrj 	{
712*38fd1498Szrj 	  enum
713*38fd1498Szrj 	    {
714*38fd1498Szrj 	      modulus = _BSize % _AlignSize,
715*38fd1498Szrj 	      value = _BSize + (modulus ? _AlignSize - (modulus) : 0)
716*38fd1498Szrj 	    };
717*38fd1498Szrj 	};
718*38fd1498Szrj 
719*38fd1498Szrj       struct _Alloc_block
720*38fd1498Szrj       {
721*38fd1498Szrj 	char __M_unused[aligned_size<sizeof(value_type),
722*38fd1498Szrj 			_BALLOC_ALIGN_BYTES>::value];
723*38fd1498Szrj       };
724*38fd1498Szrj 
725*38fd1498Szrj 
726*38fd1498Szrj       typedef typename std::pair<_Alloc_block*, _Alloc_block*> _Block_pair;
727*38fd1498Szrj 
728*38fd1498Szrj       typedef typename __detail::__mini_vector<_Block_pair> _BPVector;
729*38fd1498Szrj       typedef typename _BPVector::iterator _BPiter;
730*38fd1498Szrj 
731*38fd1498Szrj       template<typename _Predicate>
732*38fd1498Szrj         static _BPiter
733*38fd1498Szrj         _S_find(_Predicate __p)
734*38fd1498Szrj         {
735*38fd1498Szrj 	  _BPiter __first = _S_mem_blocks.begin();
736*38fd1498Szrj 	  while (__first != _S_mem_blocks.end() && !__p(*__first))
737*38fd1498Szrj 	    ++__first;
738*38fd1498Szrj 	  return __first;
739*38fd1498Szrj 	}
740*38fd1498Szrj 
741*38fd1498Szrj #if defined _GLIBCXX_DEBUG
742*38fd1498Szrj       // Complexity: O(lg(N)). Where, N is the number of block of size
743*38fd1498Szrj       // sizeof(value_type).
744*38fd1498Szrj       void
745*38fd1498Szrj       _S_check_for_free_blocks() throw()
746*38fd1498Szrj       {
747*38fd1498Szrj 	typedef typename __detail::_Ffit_finder<_Alloc_block*> _FFF;
748*38fd1498Szrj 	_BPiter __bpi = _S_find(_FFF());
749*38fd1498Szrj 
750*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT(__bpi == _S_mem_blocks.end());
751*38fd1498Szrj       }
752*38fd1498Szrj #endif
753*38fd1498Szrj 
754*38fd1498Szrj       /** @brief  Responsible for exponentially growing the internal
755*38fd1498Szrj        *  memory pool.
756*38fd1498Szrj        *
757*38fd1498Szrj        *  @throw  std::bad_alloc. If memory can not be allocated.
758*38fd1498Szrj        *
759*38fd1498Szrj        *  Complexity: O(1), but internally depends upon the
760*38fd1498Szrj        *  complexity of the function free_list::_M_get. The part where
761*38fd1498Szrj        *  the bitmap headers are written has complexity: O(X),where X
762*38fd1498Szrj        *  is the number of blocks of size sizeof(value_type) within
763*38fd1498Szrj        *  the newly acquired block. Having a tight bound.
764*38fd1498Szrj        */
765*38fd1498Szrj       void
766*38fd1498Szrj       _S_refill_pool() _GLIBCXX_THROW(std::bad_alloc)
767*38fd1498Szrj       {
768*38fd1498Szrj #if defined _GLIBCXX_DEBUG
769*38fd1498Szrj 	_S_check_for_free_blocks();
770*38fd1498Szrj #endif
771*38fd1498Szrj 
772*38fd1498Szrj 	const size_t __num_bitmaps = (_S_block_size
773*38fd1498Szrj 				      / size_t(__detail::bits_per_block));
774*38fd1498Szrj 	const size_t __size_to_allocate = sizeof(size_t)
775*38fd1498Szrj 	  + _S_block_size * sizeof(_Alloc_block)
776*38fd1498Szrj 	  + __num_bitmaps * sizeof(size_t);
777*38fd1498Szrj 
778*38fd1498Szrj 	size_t* __temp =
779*38fd1498Szrj 	  reinterpret_cast<size_t*>(this->_M_get(__size_to_allocate));
780*38fd1498Szrj 	*__temp = 0;
781*38fd1498Szrj 	++__temp;
782*38fd1498Szrj 
783*38fd1498Szrj 	// The Header information goes at the Beginning of the Block.
784*38fd1498Szrj 	_Block_pair __bp =
785*38fd1498Szrj 	  std::make_pair(reinterpret_cast<_Alloc_block*>
786*38fd1498Szrj 			 (__temp + __num_bitmaps),
787*38fd1498Szrj 			 reinterpret_cast<_Alloc_block*>
788*38fd1498Szrj 			 (__temp + __num_bitmaps)
789*38fd1498Szrj 			 + _S_block_size - 1);
790*38fd1498Szrj 
791*38fd1498Szrj 	// Fill the Vector with this information.
792*38fd1498Szrj 	_S_mem_blocks.push_back(__bp);
793*38fd1498Szrj 
794*38fd1498Szrj 	for (size_t __i = 0; __i < __num_bitmaps; ++__i)
795*38fd1498Szrj 	  __temp[__i] = ~static_cast<size_t>(0); // 1 Indicates all Free.
796*38fd1498Szrj 
797*38fd1498Szrj 	_S_block_size *= 2;
798*38fd1498Szrj       }
799*38fd1498Szrj 
800*38fd1498Szrj       static _BPVector _S_mem_blocks;
801*38fd1498Szrj       static size_t _S_block_size;
802*38fd1498Szrj       static __detail::_Bitmap_counter<_Alloc_block*> _S_last_request;
803*38fd1498Szrj       static typename _BPVector::size_type _S_last_dealloc_index;
804*38fd1498Szrj #if defined __GTHREADS
805*38fd1498Szrj       static __mutex_type _S_mut;
806*38fd1498Szrj #endif
807*38fd1498Szrj 
808*38fd1498Szrj     public:
809*38fd1498Szrj 
810*38fd1498Szrj       /** @brief  Allocates memory for a single object of size
811*38fd1498Szrj        *  sizeof(_Tp).
812*38fd1498Szrj        *
813*38fd1498Szrj        *  @throw  std::bad_alloc. If memory can not be allocated.
814*38fd1498Szrj        *
815*38fd1498Szrj        *  Complexity: Worst case complexity is O(N), but that
816*38fd1498Szrj        *  is hardly ever hit. If and when this particular case is
817*38fd1498Szrj        *  encountered, the next few cases are guaranteed to have a
818*38fd1498Szrj        *  worst case complexity of O(1)!  That's why this function
819*38fd1498Szrj        *  performs very well on average. You can consider this
820*38fd1498Szrj        *  function to have a complexity referred to commonly as:
821*38fd1498Szrj        *  Amortized Constant time.
822*38fd1498Szrj        */
823*38fd1498Szrj       pointer
824*38fd1498Szrj       _M_allocate_single_object() _GLIBCXX_THROW(std::bad_alloc)
825*38fd1498Szrj       {
826*38fd1498Szrj #if defined __GTHREADS
827*38fd1498Szrj 	__scoped_lock __bit_lock(_S_mut);
828*38fd1498Szrj #endif
829*38fd1498Szrj 
830*38fd1498Szrj 	// The algorithm is something like this: The last_request
831*38fd1498Szrj 	// variable points to the last accessed Bit Map. When such a
832*38fd1498Szrj 	// condition occurs, we try to find a free block in the
833*38fd1498Szrj 	// current bitmap, or succeeding bitmaps until the last bitmap
834*38fd1498Szrj 	// is reached. If no free block turns up, we resort to First
835*38fd1498Szrj 	// Fit method.
836*38fd1498Szrj 
837*38fd1498Szrj 	// WARNING: Do not re-order the condition in the while
838*38fd1498Szrj 	// statement below, because it relies on C++'s short-circuit
839*38fd1498Szrj 	// evaluation. The return from _S_last_request->_M_get() will
840*38fd1498Szrj 	// NOT be dereference able if _S_last_request->_M_finished()
841*38fd1498Szrj 	// returns true. This would inevitably lead to a NULL pointer
842*38fd1498Szrj 	// dereference if tinkered with.
843*38fd1498Szrj 	while (_S_last_request._M_finished() == false
844*38fd1498Szrj 	       && (*(_S_last_request._M_get()) == 0))
845*38fd1498Szrj 	  _S_last_request.operator++();
846*38fd1498Szrj 
847*38fd1498Szrj 	if (__builtin_expect(_S_last_request._M_finished() == true, false))
848*38fd1498Szrj 	  {
849*38fd1498Szrj 	    // Fall Back to First Fit algorithm.
850*38fd1498Szrj 	    typedef typename __detail::_Ffit_finder<_Alloc_block*> _FFF;
851*38fd1498Szrj 	    _FFF __fff;
852*38fd1498Szrj 	    _BPiter __bpi = _S_find(__detail::_Functor_Ref<_FFF>(__fff));
853*38fd1498Szrj 
854*38fd1498Szrj 	    if (__bpi != _S_mem_blocks.end())
855*38fd1498Szrj 	      {
856*38fd1498Szrj 		// Search was successful. Ok, now mark the first bit from
857*38fd1498Szrj 		// the right as 0, meaning Allocated. This bit is obtained
858*38fd1498Szrj 		// by calling _M_get() on __fff.
859*38fd1498Szrj 		size_t __nz_bit = _Bit_scan_forward(*__fff._M_get());
860*38fd1498Szrj 		__detail::__bit_allocate(__fff._M_get(), __nz_bit);
861*38fd1498Szrj 
862*38fd1498Szrj 		_S_last_request._M_reset(__bpi - _S_mem_blocks.begin());
863*38fd1498Szrj 
864*38fd1498Szrj 		// Now, get the address of the bit we marked as allocated.
865*38fd1498Szrj 		pointer __ret = reinterpret_cast<pointer>
866*38fd1498Szrj 		  (__bpi->first + __fff._M_offset() + __nz_bit);
867*38fd1498Szrj 		size_t* __puse_count =
868*38fd1498Szrj 		  reinterpret_cast<size_t*>
869*38fd1498Szrj 		  (__bpi->first) - (__detail::__num_bitmaps(*__bpi) + 1);
870*38fd1498Szrj 
871*38fd1498Szrj 		++(*__puse_count);
872*38fd1498Szrj 		return __ret;
873*38fd1498Szrj 	      }
874*38fd1498Szrj 	    else
875*38fd1498Szrj 	      {
876*38fd1498Szrj 		// Search was unsuccessful. We Add more memory to the
877*38fd1498Szrj 		// pool by calling _S_refill_pool().
878*38fd1498Szrj 		_S_refill_pool();
879*38fd1498Szrj 
880*38fd1498Szrj 		// _M_Reset the _S_last_request structure to the first
881*38fd1498Szrj 		// free block's bit map.
882*38fd1498Szrj 		_S_last_request._M_reset(_S_mem_blocks.size() - 1);
883*38fd1498Szrj 
884*38fd1498Szrj 		// Now, mark that bit as allocated.
885*38fd1498Szrj 	      }
886*38fd1498Szrj 	  }
887*38fd1498Szrj 
888*38fd1498Szrj 	// _S_last_request holds a pointer to a valid bit map, that
889*38fd1498Szrj 	// points to a free block in memory.
890*38fd1498Szrj 	size_t __nz_bit = _Bit_scan_forward(*_S_last_request._M_get());
891*38fd1498Szrj 	__detail::__bit_allocate(_S_last_request._M_get(), __nz_bit);
892*38fd1498Szrj 
893*38fd1498Szrj 	pointer __ret = reinterpret_cast<pointer>
894*38fd1498Szrj 	  (_S_last_request._M_base() + _S_last_request._M_offset() + __nz_bit);
895*38fd1498Szrj 
896*38fd1498Szrj 	size_t* __puse_count = reinterpret_cast<size_t*>
897*38fd1498Szrj 	  (_S_mem_blocks[_S_last_request._M_where()].first)
898*38fd1498Szrj 	  - (__detail::
899*38fd1498Szrj 	     __num_bitmaps(_S_mem_blocks[_S_last_request._M_where()]) + 1);
900*38fd1498Szrj 
901*38fd1498Szrj 	++(*__puse_count);
902*38fd1498Szrj 	return __ret;
903*38fd1498Szrj       }
904*38fd1498Szrj 
905*38fd1498Szrj       /** @brief  Deallocates memory that belongs to a single object of
906*38fd1498Szrj        *  size sizeof(_Tp).
907*38fd1498Szrj        *
908*38fd1498Szrj        *  Complexity: O(lg(N)), but the worst case is not hit
909*38fd1498Szrj        *  often!  This is because containers usually deallocate memory
910*38fd1498Szrj        *  close to each other and this case is handled in O(1) time by
911*38fd1498Szrj        *  the deallocate function.
912*38fd1498Szrj        */
913*38fd1498Szrj       void
914*38fd1498Szrj       _M_deallocate_single_object(pointer __p) throw()
915*38fd1498Szrj       {
916*38fd1498Szrj #if defined __GTHREADS
917*38fd1498Szrj 	__scoped_lock __bit_lock(_S_mut);
918*38fd1498Szrj #endif
919*38fd1498Szrj 	_Alloc_block* __real_p = reinterpret_cast<_Alloc_block*>(__p);
920*38fd1498Szrj 
921*38fd1498Szrj 	typedef typename _BPVector::iterator _Iterator;
922*38fd1498Szrj 	typedef typename _BPVector::difference_type _Difference_type;
923*38fd1498Szrj 
924*38fd1498Szrj 	_Difference_type __diff;
925*38fd1498Szrj 	long __displacement;
926*38fd1498Szrj 
927*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0);
928*38fd1498Szrj 
929*38fd1498Szrj 	__detail::_Inclusive_between<_Alloc_block*> __ibt(__real_p);
930*38fd1498Szrj 	if (__ibt(_S_mem_blocks[_S_last_dealloc_index]))
931*38fd1498Szrj 	  {
932*38fd1498Szrj 	    _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index
933*38fd1498Szrj 				  <= _S_mem_blocks.size() - 1);
934*38fd1498Szrj 
935*38fd1498Szrj 	    // Initial Assumption was correct!
936*38fd1498Szrj 	    __diff = _S_last_dealloc_index;
937*38fd1498Szrj 	    __displacement = __real_p - _S_mem_blocks[__diff].first;
938*38fd1498Szrj 	  }
939*38fd1498Szrj 	else
940*38fd1498Szrj 	  {
941*38fd1498Szrj 	    _Iterator _iter = _S_find(__ibt);
942*38fd1498Szrj 
943*38fd1498Szrj 	    _GLIBCXX_DEBUG_ASSERT(_iter != _S_mem_blocks.end());
944*38fd1498Szrj 
945*38fd1498Szrj 	    __diff = _iter - _S_mem_blocks.begin();
946*38fd1498Szrj 	    __displacement = __real_p - _S_mem_blocks[__diff].first;
947*38fd1498Szrj 	    _S_last_dealloc_index = __diff;
948*38fd1498Szrj 	  }
949*38fd1498Szrj 
950*38fd1498Szrj 	// Get the position of the iterator that has been found.
951*38fd1498Szrj 	const size_t __rotate = (__displacement
952*38fd1498Szrj 				 % size_t(__detail::bits_per_block));
953*38fd1498Szrj 	size_t* __bitmapC =
954*38fd1498Szrj 	  reinterpret_cast<size_t*>
955*38fd1498Szrj 	  (_S_mem_blocks[__diff].first) - 1;
956*38fd1498Szrj 	__bitmapC -= (__displacement / size_t(__detail::bits_per_block));
957*38fd1498Szrj 
958*38fd1498Szrj 	__detail::__bit_free(__bitmapC, __rotate);
959*38fd1498Szrj 	size_t* __puse_count = reinterpret_cast<size_t*>
960*38fd1498Szrj 	  (_S_mem_blocks[__diff].first)
961*38fd1498Szrj 	  - (__detail::__num_bitmaps(_S_mem_blocks[__diff]) + 1);
962*38fd1498Szrj 
963*38fd1498Szrj 	_GLIBCXX_DEBUG_ASSERT(*__puse_count != 0);
964*38fd1498Szrj 
965*38fd1498Szrj 	--(*__puse_count);
966*38fd1498Szrj 
967*38fd1498Szrj 	if (__builtin_expect(*__puse_count == 0, false))
968*38fd1498Szrj 	  {
969*38fd1498Szrj 	    _S_block_size /= 2;
970*38fd1498Szrj 
971*38fd1498Szrj 	    // We can safely remove this block.
972*38fd1498Szrj 	    // _Block_pair __bp = _S_mem_blocks[__diff];
973*38fd1498Szrj 	    this->_M_insert(__puse_count);
974*38fd1498Szrj 	    _S_mem_blocks.erase(_S_mem_blocks.begin() + __diff);
975*38fd1498Szrj 
976*38fd1498Szrj 	    // Reset the _S_last_request variable to reflect the
977*38fd1498Szrj 	    // erased block. We do this to protect future requests
978*38fd1498Szrj 	    // after the last block has been removed from a particular
979*38fd1498Szrj 	    // memory Chunk, which in turn has been returned to the
980*38fd1498Szrj 	    // free list, and hence had been erased from the vector,
981*38fd1498Szrj 	    // so the size of the vector gets reduced by 1.
982*38fd1498Szrj 	    if ((_Difference_type)_S_last_request._M_where() >= __diff--)
983*38fd1498Szrj 	      _S_last_request._M_reset(__diff);
984*38fd1498Szrj 
985*38fd1498Szrj 	    // If the Index into the vector of the region of memory
986*38fd1498Szrj 	    // that might hold the next address that will be passed to
987*38fd1498Szrj 	    // deallocated may have been invalidated due to the above
988*38fd1498Szrj 	    // erase procedure being called on the vector, hence we
989*38fd1498Szrj 	    // try to restore this invariant too.
990*38fd1498Szrj 	    if (_S_last_dealloc_index >= _S_mem_blocks.size())
991*38fd1498Szrj 	      {
992*38fd1498Szrj 		_S_last_dealloc_index =(__diff != -1 ? __diff : 0);
993*38fd1498Szrj 		_GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0);
994*38fd1498Szrj 	      }
995*38fd1498Szrj 	  }
996*38fd1498Szrj       }
997*38fd1498Szrj 
998*38fd1498Szrj     public:
999*38fd1498Szrj       bitmap_allocator() _GLIBCXX_USE_NOEXCEPT
1000*38fd1498Szrj       { }
1001*38fd1498Szrj 
1002*38fd1498Szrj       bitmap_allocator(const bitmap_allocator&) _GLIBCXX_USE_NOEXCEPT
1003*38fd1498Szrj       { }
1004*38fd1498Szrj 
1005*38fd1498Szrj       template<typename _Tp1>
1006*38fd1498Szrj         bitmap_allocator(const bitmap_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT
1007*38fd1498Szrj         { }
1008*38fd1498Szrj 
1009*38fd1498Szrj       ~bitmap_allocator() _GLIBCXX_USE_NOEXCEPT
1010*38fd1498Szrj       { }
1011*38fd1498Szrj 
1012*38fd1498Szrj       pointer
1013*38fd1498Szrj       allocate(size_type __n)
1014*38fd1498Szrj       {
1015*38fd1498Szrj 	if (__n > this->max_size())
1016*38fd1498Szrj 	  std::__throw_bad_alloc();
1017*38fd1498Szrj 
1018*38fd1498Szrj #if __cpp_aligned_new
1019*38fd1498Szrj 	if (alignof(value_type) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
1020*38fd1498Szrj 	  {
1021*38fd1498Szrj 	    const size_type __b = __n * sizeof(value_type);
1022*38fd1498Szrj 	    std::align_val_t __al = std::align_val_t(alignof(value_type));
1023*38fd1498Szrj 	    return static_cast<pointer>(::operator new(__b, __al));
1024*38fd1498Szrj 	  }
1025*38fd1498Szrj #endif
1026*38fd1498Szrj 
1027*38fd1498Szrj 	if (__builtin_expect(__n == 1, true))
1028*38fd1498Szrj 	  return this->_M_allocate_single_object();
1029*38fd1498Szrj 	else
1030*38fd1498Szrj 	  {
1031*38fd1498Szrj 	    const size_type __b = __n * sizeof(value_type);
1032*38fd1498Szrj 	    return reinterpret_cast<pointer>(::operator new(__b));
1033*38fd1498Szrj 	  }
1034*38fd1498Szrj       }
1035*38fd1498Szrj 
1036*38fd1498Szrj       pointer
1037*38fd1498Szrj       allocate(size_type __n, typename bitmap_allocator<void>::const_pointer)
1038*38fd1498Szrj       { return allocate(__n); }
1039*38fd1498Szrj 
1040*38fd1498Szrj       void
1041*38fd1498Szrj       deallocate(pointer __p, size_type __n) throw()
1042*38fd1498Szrj       {
1043*38fd1498Szrj 	if (__builtin_expect(__p != 0, true))
1044*38fd1498Szrj 	  {
1045*38fd1498Szrj #if __cpp_aligned_new
1046*38fd1498Szrj 	    // Types with extended alignment are handled by operator delete.
1047*38fd1498Szrj 	    if (alignof(value_type) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
1048*38fd1498Szrj 	      {
1049*38fd1498Szrj 		::operator delete(__p, std::align_val_t(alignof(value_type)));
1050*38fd1498Szrj 		return;
1051*38fd1498Szrj 	      }
1052*38fd1498Szrj #endif
1053*38fd1498Szrj 
1054*38fd1498Szrj 	    if (__builtin_expect(__n == 1, true))
1055*38fd1498Szrj 	      this->_M_deallocate_single_object(__p);
1056*38fd1498Szrj 	    else
1057*38fd1498Szrj 	      ::operator delete(__p);
1058*38fd1498Szrj 	  }
1059*38fd1498Szrj       }
1060*38fd1498Szrj 
1061*38fd1498Szrj       pointer
1062*38fd1498Szrj       address(reference __r) const _GLIBCXX_NOEXCEPT
1063*38fd1498Szrj       { return std::__addressof(__r); }
1064*38fd1498Szrj 
1065*38fd1498Szrj       const_pointer
1066*38fd1498Szrj       address(const_reference __r) const _GLIBCXX_NOEXCEPT
1067*38fd1498Szrj       { return std::__addressof(__r); }
1068*38fd1498Szrj 
1069*38fd1498Szrj       size_type
1070*38fd1498Szrj       max_size() const _GLIBCXX_USE_NOEXCEPT
1071*38fd1498Szrj       { return size_type(-1) / sizeof(value_type); }
1072*38fd1498Szrj 
1073*38fd1498Szrj #if __cplusplus >= 201103L
1074*38fd1498Szrj       template<typename _Up, typename... _Args>
1075*38fd1498Szrj         void
1076*38fd1498Szrj         construct(_Up* __p, _Args&&... __args)
1077*38fd1498Szrj 	{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
1078*38fd1498Szrj 
1079*38fd1498Szrj       template<typename _Up>
1080*38fd1498Szrj         void
1081*38fd1498Szrj         destroy(_Up* __p)
1082*38fd1498Szrj         { __p->~_Up(); }
1083*38fd1498Szrj #else
1084*38fd1498Szrj       void
1085*38fd1498Szrj       construct(pointer __p, const_reference __data)
1086*38fd1498Szrj       { ::new((void *)__p) value_type(__data); }
1087*38fd1498Szrj 
1088*38fd1498Szrj       void
1089*38fd1498Szrj       destroy(pointer __p)
1090*38fd1498Szrj       { __p->~value_type(); }
1091*38fd1498Szrj #endif
1092*38fd1498Szrj     };
1093*38fd1498Szrj 
1094*38fd1498Szrj   template<typename _Tp1, typename _Tp2>
1095*38fd1498Szrj     bool
1096*38fd1498Szrj     operator==(const bitmap_allocator<_Tp1>&,
1097*38fd1498Szrj 	       const bitmap_allocator<_Tp2>&) throw()
1098*38fd1498Szrj     { return true; }
1099*38fd1498Szrj 
1100*38fd1498Szrj   template<typename _Tp1, typename _Tp2>
1101*38fd1498Szrj     bool
1102*38fd1498Szrj     operator!=(const bitmap_allocator<_Tp1>&,
1103*38fd1498Szrj 	       const bitmap_allocator<_Tp2>&) throw()
1104*38fd1498Szrj   { return false; }
1105*38fd1498Szrj 
1106*38fd1498Szrj   // Static member definitions.
1107*38fd1498Szrj   template<typename _Tp>
1108*38fd1498Szrj     typename bitmap_allocator<_Tp>::_BPVector
1109*38fd1498Szrj     bitmap_allocator<_Tp>::_S_mem_blocks;
1110*38fd1498Szrj 
1111*38fd1498Szrj   template<typename _Tp>
1112*38fd1498Szrj     size_t bitmap_allocator<_Tp>::_S_block_size =
1113*38fd1498Szrj     2 * size_t(__detail::bits_per_block);
1114*38fd1498Szrj 
1115*38fd1498Szrj   template<typename _Tp>
1116*38fd1498Szrj     typename bitmap_allocator<_Tp>::_BPVector::size_type
1117*38fd1498Szrj     bitmap_allocator<_Tp>::_S_last_dealloc_index = 0;
1118*38fd1498Szrj 
1119*38fd1498Szrj   template<typename _Tp>
1120*38fd1498Szrj     __detail::_Bitmap_counter
1121*38fd1498Szrj       <typename bitmap_allocator<_Tp>::_Alloc_block*>
1122*38fd1498Szrj     bitmap_allocator<_Tp>::_S_last_request(_S_mem_blocks);
1123*38fd1498Szrj 
1124*38fd1498Szrj #if defined __GTHREADS
1125*38fd1498Szrj   template<typename _Tp>
1126*38fd1498Szrj     typename bitmap_allocator<_Tp>::__mutex_type
1127*38fd1498Szrj     bitmap_allocator<_Tp>::_S_mut;
1128*38fd1498Szrj #endif
1129*38fd1498Szrj 
1130*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
1131*38fd1498Szrj } // namespace __gnu_cxx
1132*38fd1498Szrj 
1133*38fd1498Szrj #endif
1134