xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/ext/bitmap_allocator.h (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg // Bitmap Allocator. -*- C++ -*-
236ac495dSmrg 
3*8feb0f0bSmrg // Copyright (C) 2004-2020 Free Software Foundation, Inc.
436ac495dSmrg //
536ac495dSmrg // This file is part of the GNU ISO C++ Library.  This library is free
636ac495dSmrg // software; you can redistribute it and/or modify it under the
736ac495dSmrg // terms of the GNU General Public License as published by the
836ac495dSmrg // Free Software Foundation; either version 3, or (at your option)
936ac495dSmrg // any later version.
1036ac495dSmrg 
1136ac495dSmrg // This library is distributed in the hope that it will be useful,
1236ac495dSmrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
1336ac495dSmrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1436ac495dSmrg // GNU General Public License for more details.
1536ac495dSmrg 
1636ac495dSmrg // Under Section 7 of GPL version 3, you are granted additional
1736ac495dSmrg // permissions described in the GCC Runtime Library Exception, version
1836ac495dSmrg // 3.1, as published by the Free Software Foundation.
1936ac495dSmrg 
2036ac495dSmrg // You should have received a copy of the GNU General Public License and
2136ac495dSmrg // a copy of the GCC Runtime Library Exception along with this program;
2236ac495dSmrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2336ac495dSmrg // <http://www.gnu.org/licenses/>.
2436ac495dSmrg 
2536ac495dSmrg /** @file ext/bitmap_allocator.h
2636ac495dSmrg  *  This file is a GNU extension to the Standard C++ Library.
2736ac495dSmrg  */
2836ac495dSmrg 
2936ac495dSmrg #ifndef _BITMAP_ALLOCATOR_H
3036ac495dSmrg #define _BITMAP_ALLOCATOR_H 1
3136ac495dSmrg 
3236ac495dSmrg #include <utility> // For std::pair.
3336ac495dSmrg #include <bits/functexcept.h> // For __throw_bad_alloc().
34*8feb0f0bSmrg #include <bits/stl_function.h> // For greater_equal, and less_equal.
3536ac495dSmrg #include <new> // For operator new.
3636ac495dSmrg #include <debug/debug.h> // _GLIBCXX_DEBUG_ASSERT
3736ac495dSmrg #include <ext/concurrence.h>
3836ac495dSmrg #include <bits/move.h>
3936ac495dSmrg 
4036ac495dSmrg /** @brief The constant in the expression below is the alignment
4136ac495dSmrg  * required in bytes.
4236ac495dSmrg  */
4336ac495dSmrg #define _BALLOC_ALIGN_BYTES 8
4436ac495dSmrg 
_GLIBCXX_VISIBILITY(default)4536ac495dSmrg namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
4636ac495dSmrg {
47a2dc1f3fSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
48a2dc1f3fSmrg 
4936ac495dSmrg   namespace __detail
5036ac495dSmrg   {
5136ac495dSmrg     /** @class  __mini_vector bitmap_allocator.h bitmap_allocator.h
5236ac495dSmrg      *
5336ac495dSmrg      *  @brief  __mini_vector<> is a stripped down version of the
5436ac495dSmrg      *  full-fledged std::vector<>.
5536ac495dSmrg      *
5636ac495dSmrg      *  It is to be used only for built-in types or PODs. Notable
5736ac495dSmrg      *  differences are:
5836ac495dSmrg      *
5936ac495dSmrg      *  1. Not all accessor functions are present.
6036ac495dSmrg      *  2. Used ONLY for PODs.
6136ac495dSmrg      *  3. No Allocator template argument. Uses ::operator new() to get
6236ac495dSmrg      *  memory, and ::operator delete() to free it.
6336ac495dSmrg      *  Caveat: The dtor does NOT free the memory allocated, so this a
6436ac495dSmrg      *  memory-leaking vector!
6536ac495dSmrg      */
6636ac495dSmrg     template<typename _Tp>
6736ac495dSmrg       class __mini_vector
6836ac495dSmrg       {
6936ac495dSmrg 	__mini_vector(const __mini_vector&);
7036ac495dSmrg 	__mini_vector& operator=(const __mini_vector&);
7136ac495dSmrg 
7236ac495dSmrg       public:
7336ac495dSmrg 	typedef _Tp value_type;
7436ac495dSmrg 	typedef _Tp* pointer;
7536ac495dSmrg 	typedef _Tp& reference;
7636ac495dSmrg 	typedef const _Tp& const_reference;
77*8feb0f0bSmrg 	typedef std::size_t size_type;
78*8feb0f0bSmrg 	typedef std::ptrdiff_t difference_type;
7936ac495dSmrg 	typedef pointer iterator;
8036ac495dSmrg 
8136ac495dSmrg       private:
8236ac495dSmrg 	pointer _M_start;
8336ac495dSmrg 	pointer _M_finish;
8436ac495dSmrg 	pointer _M_end_of_storage;
8536ac495dSmrg 
8636ac495dSmrg 	size_type
8736ac495dSmrg 	_M_space_left() const throw()
8836ac495dSmrg 	{ return _M_end_of_storage - _M_finish; }
8936ac495dSmrg 
90c0a68be4Smrg 	_GLIBCXX_NODISCARD pointer
9136ac495dSmrg 	allocate(size_type __n)
9236ac495dSmrg 	{ return static_cast<pointer>(::operator new(__n * sizeof(_Tp))); }
9336ac495dSmrg 
9436ac495dSmrg 	void
9536ac495dSmrg 	deallocate(pointer __p, size_type)
9636ac495dSmrg 	{ ::operator delete(__p); }
9736ac495dSmrg 
9836ac495dSmrg       public:
9936ac495dSmrg 	// Members used: size(), push_back(), pop_back(),
10036ac495dSmrg 	// insert(iterator, const_reference), erase(iterator),
10136ac495dSmrg 	// begin(), end(), back(), operator[].
10236ac495dSmrg 
10336ac495dSmrg 	__mini_vector()
10436ac495dSmrg         : _M_start(0), _M_finish(0), _M_end_of_storage(0) { }
10536ac495dSmrg 
10636ac495dSmrg 	size_type
10736ac495dSmrg 	size() const throw()
10836ac495dSmrg 	{ return _M_finish - _M_start; }
10936ac495dSmrg 
11036ac495dSmrg 	iterator
11136ac495dSmrg 	begin() const throw()
11236ac495dSmrg 	{ return this->_M_start; }
11336ac495dSmrg 
11436ac495dSmrg 	iterator
11536ac495dSmrg 	end() const throw()
11636ac495dSmrg 	{ return this->_M_finish; }
11736ac495dSmrg 
11836ac495dSmrg 	reference
11936ac495dSmrg 	back() const throw()
12036ac495dSmrg 	{ return *(this->end() - 1); }
12136ac495dSmrg 
12236ac495dSmrg 	reference
12336ac495dSmrg 	operator[](const size_type __pos) const throw()
12436ac495dSmrg 	{ return this->_M_start[__pos]; }
12536ac495dSmrg 
12636ac495dSmrg 	void
12736ac495dSmrg 	insert(iterator __pos, const_reference __x);
12836ac495dSmrg 
12936ac495dSmrg 	void
13036ac495dSmrg 	push_back(const_reference __x)
13136ac495dSmrg 	{
13236ac495dSmrg 	  if (this->_M_space_left())
13336ac495dSmrg 	    {
13436ac495dSmrg 	      *this->end() = __x;
13536ac495dSmrg 	      ++this->_M_finish;
13636ac495dSmrg 	    }
13736ac495dSmrg 	  else
13836ac495dSmrg 	    this->insert(this->end(), __x);
13936ac495dSmrg 	}
14036ac495dSmrg 
14136ac495dSmrg 	void
14236ac495dSmrg 	pop_back() throw()
14336ac495dSmrg 	{ --this->_M_finish; }
14436ac495dSmrg 
14536ac495dSmrg 	void
14636ac495dSmrg 	erase(iterator __pos) throw();
14736ac495dSmrg 
14836ac495dSmrg 	void
14936ac495dSmrg 	clear() throw()
15036ac495dSmrg 	{ this->_M_finish = this->_M_start; }
15136ac495dSmrg       };
15236ac495dSmrg 
15336ac495dSmrg     // Out of line function definitions.
15436ac495dSmrg     template<typename _Tp>
15536ac495dSmrg       void __mini_vector<_Tp>::
15636ac495dSmrg       insert(iterator __pos, const_reference __x)
15736ac495dSmrg       {
15836ac495dSmrg 	if (this->_M_space_left())
15936ac495dSmrg 	  {
16036ac495dSmrg 	    size_type __to_move = this->_M_finish - __pos;
16136ac495dSmrg 	    iterator __dest = this->end();
16236ac495dSmrg 	    iterator __src = this->end() - 1;
16336ac495dSmrg 
16436ac495dSmrg 	    ++this->_M_finish;
16536ac495dSmrg 	    while (__to_move)
16636ac495dSmrg 	      {
16736ac495dSmrg 		*__dest = *__src;
16836ac495dSmrg 		--__dest; --__src; --__to_move;
16936ac495dSmrg 	      }
17036ac495dSmrg 	    *__pos = __x;
17136ac495dSmrg 	  }
17236ac495dSmrg 	else
17336ac495dSmrg 	  {
17436ac495dSmrg 	    size_type __new_size = this->size() ? this->size() * 2 : 1;
17536ac495dSmrg 	    iterator __new_start = this->allocate(__new_size);
17636ac495dSmrg 	    iterator __first = this->begin();
17736ac495dSmrg 	    iterator __start = __new_start;
17836ac495dSmrg 	    while (__first != __pos)
17936ac495dSmrg 	      {
18036ac495dSmrg 		*__start = *__first;
18136ac495dSmrg 		++__start; ++__first;
18236ac495dSmrg 	      }
18336ac495dSmrg 	    *__start = __x;
18436ac495dSmrg 	    ++__start;
18536ac495dSmrg 	    while (__first != this->end())
18636ac495dSmrg 	      {
18736ac495dSmrg 		*__start = *__first;
18836ac495dSmrg 		++__start; ++__first;
18936ac495dSmrg 	      }
19036ac495dSmrg 	    if (this->_M_start)
19136ac495dSmrg 	      this->deallocate(this->_M_start, this->size());
19236ac495dSmrg 
19336ac495dSmrg 	    this->_M_start = __new_start;
19436ac495dSmrg 	    this->_M_finish = __start;
19536ac495dSmrg 	    this->_M_end_of_storage = this->_M_start + __new_size;
19636ac495dSmrg 	  }
19736ac495dSmrg       }
19836ac495dSmrg 
19936ac495dSmrg     template<typename _Tp>
20036ac495dSmrg       void __mini_vector<_Tp>::
20136ac495dSmrg       erase(iterator __pos) throw()
20236ac495dSmrg       {
20336ac495dSmrg 	while (__pos + 1 != this->end())
20436ac495dSmrg 	  {
20536ac495dSmrg 	    *__pos = __pos[1];
20636ac495dSmrg 	    ++__pos;
20736ac495dSmrg 	  }
20836ac495dSmrg 	--this->_M_finish;
20936ac495dSmrg       }
21036ac495dSmrg 
21136ac495dSmrg 
21236ac495dSmrg     template<typename _Tp>
21336ac495dSmrg       struct __mv_iter_traits
21436ac495dSmrg       {
21536ac495dSmrg 	typedef typename _Tp::value_type value_type;
21636ac495dSmrg 	typedef typename _Tp::difference_type difference_type;
21736ac495dSmrg       };
21836ac495dSmrg 
21936ac495dSmrg     template<typename _Tp>
22036ac495dSmrg       struct __mv_iter_traits<_Tp*>
22136ac495dSmrg       {
22236ac495dSmrg 	typedef _Tp value_type;
223*8feb0f0bSmrg 	typedef std::ptrdiff_t difference_type;
22436ac495dSmrg       };
22536ac495dSmrg 
22636ac495dSmrg     enum
22736ac495dSmrg       {
22836ac495dSmrg 	bits_per_byte = 8,
229*8feb0f0bSmrg 	bits_per_block = sizeof(std::size_t) * std::size_t(bits_per_byte)
23036ac495dSmrg       };
23136ac495dSmrg 
23236ac495dSmrg     template<typename _ForwardIterator, typename _Tp, typename _Compare>
23336ac495dSmrg       _ForwardIterator
23436ac495dSmrg       __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
23536ac495dSmrg 		    const _Tp& __val, _Compare __comp)
23636ac495dSmrg       {
23736ac495dSmrg 	typedef typename __mv_iter_traits<_ForwardIterator>::difference_type
23836ac495dSmrg 	  _DistanceType;
23936ac495dSmrg 
24036ac495dSmrg 	_DistanceType __len = __last - __first;
24136ac495dSmrg 	_DistanceType __half;
24236ac495dSmrg 	_ForwardIterator __middle;
24336ac495dSmrg 
24436ac495dSmrg 	while (__len > 0)
24536ac495dSmrg 	  {
24636ac495dSmrg 	    __half = __len >> 1;
24736ac495dSmrg 	    __middle = __first;
24836ac495dSmrg 	    __middle += __half;
24936ac495dSmrg 	    if (__comp(*__middle, __val))
25036ac495dSmrg 	      {
25136ac495dSmrg 		__first = __middle;
25236ac495dSmrg 		++__first;
25336ac495dSmrg 		__len = __len - __half - 1;
25436ac495dSmrg 	      }
25536ac495dSmrg 	    else
25636ac495dSmrg 	      __len = __half;
25736ac495dSmrg 	  }
25836ac495dSmrg 	return __first;
25936ac495dSmrg       }
26036ac495dSmrg 
26136ac495dSmrg     /** @brief The number of Blocks pointed to by the address pair
26236ac495dSmrg      *  passed to the function.
26336ac495dSmrg      */
26436ac495dSmrg     template<typename _AddrPair>
265*8feb0f0bSmrg       inline std::size_t
26636ac495dSmrg       __num_blocks(_AddrPair __ap)
26736ac495dSmrg       { return (__ap.second - __ap.first) + 1; }
26836ac495dSmrg 
26936ac495dSmrg     /** @brief The number of Bit-maps pointed to by the address pair
27036ac495dSmrg      *  passed to the function.
27136ac495dSmrg      */
27236ac495dSmrg     template<typename _AddrPair>
273*8feb0f0bSmrg       inline std::size_t
27436ac495dSmrg       __num_bitmaps(_AddrPair __ap)
275*8feb0f0bSmrg       { return __num_blocks(__ap) / std::size_t(bits_per_block); }
27636ac495dSmrg 
27736ac495dSmrg     // _Tp should be a pointer type.
27836ac495dSmrg     template<typename _Tp>
27936ac495dSmrg       class _Inclusive_between
28036ac495dSmrg       : public std::unary_function<typename std::pair<_Tp, _Tp>, bool>
28136ac495dSmrg       {
28236ac495dSmrg 	typedef _Tp pointer;
28336ac495dSmrg 	pointer _M_ptr_value;
28436ac495dSmrg 	typedef typename std::pair<_Tp, _Tp> _Block_pair;
28536ac495dSmrg 
28636ac495dSmrg       public:
28736ac495dSmrg 	_Inclusive_between(pointer __ptr) : _M_ptr_value(__ptr)
28836ac495dSmrg 	{ }
28936ac495dSmrg 
29036ac495dSmrg 	bool
29136ac495dSmrg 	operator()(_Block_pair __bp) const throw()
29236ac495dSmrg 	{
29336ac495dSmrg 	  if (std::less_equal<pointer>()(_M_ptr_value, __bp.second)
29436ac495dSmrg 	      && std::greater_equal<pointer>()(_M_ptr_value, __bp.first))
29536ac495dSmrg 	    return true;
29636ac495dSmrg 	  else
29736ac495dSmrg 	    return false;
29836ac495dSmrg 	}
29936ac495dSmrg       };
30036ac495dSmrg 
30136ac495dSmrg     // Used to pass a Functor to functions by reference.
30236ac495dSmrg     template<typename _Functor>
30336ac495dSmrg       class _Functor_Ref
30436ac495dSmrg       : public std::unary_function<typename _Functor::argument_type,
30536ac495dSmrg 				   typename _Functor::result_type>
30636ac495dSmrg       {
30736ac495dSmrg 	_Functor& _M_fref;
30836ac495dSmrg 
30936ac495dSmrg       public:
31036ac495dSmrg 	typedef typename _Functor::argument_type argument_type;
31136ac495dSmrg 	typedef typename _Functor::result_type result_type;
31236ac495dSmrg 
31336ac495dSmrg 	_Functor_Ref(_Functor& __fref) : _M_fref(__fref)
31436ac495dSmrg 	{ }
31536ac495dSmrg 
31636ac495dSmrg 	result_type
31736ac495dSmrg 	operator()(argument_type __arg)
31836ac495dSmrg 	{ return _M_fref(__arg); }
31936ac495dSmrg       };
32036ac495dSmrg 
32136ac495dSmrg     /** @class  _Ffit_finder bitmap_allocator.h bitmap_allocator.h
32236ac495dSmrg      *
32336ac495dSmrg      *  @brief  The class which acts as a predicate for applying the
32436ac495dSmrg      *  first-fit memory allocation policy for the bitmap allocator.
32536ac495dSmrg      */
32636ac495dSmrg     // _Tp should be a pointer type, and _Alloc is the Allocator for
32736ac495dSmrg     // the vector.
32836ac495dSmrg     template<typename _Tp>
32936ac495dSmrg       class _Ffit_finder
33036ac495dSmrg       : public std::unary_function<typename std::pair<_Tp, _Tp>, bool>
33136ac495dSmrg       {
33236ac495dSmrg 	typedef typename std::pair<_Tp, _Tp> _Block_pair;
33336ac495dSmrg 	typedef typename __detail::__mini_vector<_Block_pair> _BPVector;
33436ac495dSmrg 	typedef typename _BPVector::difference_type _Counter_type;
33536ac495dSmrg 
336*8feb0f0bSmrg 	std::size_t* _M_pbitmap;
33736ac495dSmrg 	_Counter_type _M_data_offset;
33836ac495dSmrg 
33936ac495dSmrg       public:
34036ac495dSmrg 	_Ffit_finder() : _M_pbitmap(0), _M_data_offset(0)
34136ac495dSmrg 	{ }
34236ac495dSmrg 
34336ac495dSmrg 	bool
34436ac495dSmrg 	operator()(_Block_pair __bp) throw()
34536ac495dSmrg 	{
346*8feb0f0bSmrg 	  using std::size_t;
34736ac495dSmrg 	  // Set the _rover to the last physical location bitmap,
34836ac495dSmrg 	  // which is the bitmap which belongs to the first free
34936ac495dSmrg 	  // block. Thus, the bitmaps are in exact reverse order of
35036ac495dSmrg 	  // the actual memory layout. So, we count down the bitmaps,
35136ac495dSmrg 	  // which is the same as moving up the memory.
35236ac495dSmrg 
35336ac495dSmrg 	  // If the used count stored at the start of the Bit Map headers
35436ac495dSmrg 	  // is equal to the number of Objects that the current Block can
35536ac495dSmrg 	  // store, then there is definitely no space for another single
35636ac495dSmrg 	  // object, so just return false.
35736ac495dSmrg 	  _Counter_type __diff = __detail::__num_bitmaps(__bp);
35836ac495dSmrg 
35936ac495dSmrg 	  if (*(reinterpret_cast<size_t*>
36036ac495dSmrg 		(__bp.first) - (__diff + 1)) == __detail::__num_blocks(__bp))
36136ac495dSmrg 	    return false;
36236ac495dSmrg 
36336ac495dSmrg 	  size_t* __rover = reinterpret_cast<size_t*>(__bp.first) - 1;
36436ac495dSmrg 
36536ac495dSmrg 	  for (_Counter_type __i = 0; __i < __diff; ++__i)
36636ac495dSmrg 	    {
36736ac495dSmrg 	      _M_data_offset = __i;
36836ac495dSmrg 	      if (*__rover)
36936ac495dSmrg 		{
37036ac495dSmrg 		  _M_pbitmap = __rover;
37136ac495dSmrg 		  return true;
37236ac495dSmrg 		}
37336ac495dSmrg 	      --__rover;
37436ac495dSmrg 	    }
37536ac495dSmrg 	  return false;
37636ac495dSmrg 	}
37736ac495dSmrg 
378*8feb0f0bSmrg 	std::size_t*
37936ac495dSmrg 	_M_get() const throw()
38036ac495dSmrg 	{ return _M_pbitmap; }
38136ac495dSmrg 
38236ac495dSmrg 	_Counter_type
38336ac495dSmrg 	_M_offset() const throw()
384*8feb0f0bSmrg 	{ return _M_data_offset * std::size_t(bits_per_block); }
38536ac495dSmrg       };
38636ac495dSmrg 
38736ac495dSmrg     /** @class  _Bitmap_counter bitmap_allocator.h bitmap_allocator.h
38836ac495dSmrg      *
38936ac495dSmrg      *  @brief  The bitmap counter which acts as the bitmap
39036ac495dSmrg      *  manipulator, and manages the bit-manipulation functions and
39136ac495dSmrg      *  the searching and identification functions on the bit-map.
39236ac495dSmrg      */
39336ac495dSmrg     // _Tp should be a pointer type.
39436ac495dSmrg     template<typename _Tp>
39536ac495dSmrg       class _Bitmap_counter
39636ac495dSmrg       {
39736ac495dSmrg 	typedef typename
39836ac495dSmrg 	__detail::__mini_vector<typename std::pair<_Tp, _Tp> > _BPVector;
39936ac495dSmrg 	typedef typename _BPVector::size_type _Index_type;
40036ac495dSmrg 	typedef _Tp pointer;
40136ac495dSmrg 
40236ac495dSmrg 	_BPVector& _M_vbp;
403*8feb0f0bSmrg 	std::size_t* _M_curr_bmap;
404*8feb0f0bSmrg 	std::size_t* _M_last_bmap_in_block;
40536ac495dSmrg 	_Index_type _M_curr_index;
40636ac495dSmrg 
40736ac495dSmrg       public:
40836ac495dSmrg 	// Use the 2nd parameter with care. Make sure that such an
40936ac495dSmrg 	// entry exists in the vector before passing that particular
41036ac495dSmrg 	// index to this ctor.
41136ac495dSmrg 	_Bitmap_counter(_BPVector& Rvbp, long __index = -1) : _M_vbp(Rvbp)
41236ac495dSmrg 	{ this->_M_reset(__index); }
41336ac495dSmrg 
41436ac495dSmrg 	void
41536ac495dSmrg 	_M_reset(long __index = -1) throw()
41636ac495dSmrg 	{
41736ac495dSmrg 	  if (__index == -1)
41836ac495dSmrg 	    {
41936ac495dSmrg 	      _M_curr_bmap = 0;
42036ac495dSmrg 	      _M_curr_index = static_cast<_Index_type>(-1);
42136ac495dSmrg 	      return;
42236ac495dSmrg 	    }
42336ac495dSmrg 
42436ac495dSmrg 	  _M_curr_index = __index;
425*8feb0f0bSmrg 	  _M_curr_bmap = reinterpret_cast<std::size_t*>
42636ac495dSmrg 	    (_M_vbp[_M_curr_index].first) - 1;
42736ac495dSmrg 
42836ac495dSmrg 	  _GLIBCXX_DEBUG_ASSERT(__index <= (long)_M_vbp.size() - 1);
42936ac495dSmrg 
43036ac495dSmrg 	  _M_last_bmap_in_block = _M_curr_bmap
43136ac495dSmrg 	    - ((_M_vbp[_M_curr_index].second
43236ac495dSmrg 		- _M_vbp[_M_curr_index].first + 1)
433*8feb0f0bSmrg 	       / std::size_t(bits_per_block) - 1);
43436ac495dSmrg 	}
43536ac495dSmrg 
43636ac495dSmrg 	// Dangerous Function! Use with extreme care. Pass to this
43736ac495dSmrg 	// function ONLY those values that are known to be correct,
43836ac495dSmrg 	// otherwise this will mess up big time.
43936ac495dSmrg 	void
440*8feb0f0bSmrg 	_M_set_internal_bitmap(std::size_t* __new_internal_marker) throw()
44136ac495dSmrg 	{ _M_curr_bmap = __new_internal_marker; }
44236ac495dSmrg 
44336ac495dSmrg 	bool
44436ac495dSmrg 	_M_finished() const throw()
44536ac495dSmrg 	{ return(_M_curr_bmap == 0); }
44636ac495dSmrg 
44736ac495dSmrg 	_Bitmap_counter&
44836ac495dSmrg 	operator++() throw()
44936ac495dSmrg 	{
45036ac495dSmrg 	  if (_M_curr_bmap == _M_last_bmap_in_block)
45136ac495dSmrg 	    {
45236ac495dSmrg 	      if (++_M_curr_index == _M_vbp.size())
45336ac495dSmrg 		_M_curr_bmap = 0;
45436ac495dSmrg 	      else
45536ac495dSmrg 		this->_M_reset(_M_curr_index);
45636ac495dSmrg 	    }
45736ac495dSmrg 	  else
45836ac495dSmrg 	    --_M_curr_bmap;
45936ac495dSmrg 	  return *this;
46036ac495dSmrg 	}
46136ac495dSmrg 
462*8feb0f0bSmrg 	std::size_t*
46336ac495dSmrg 	_M_get() const throw()
46436ac495dSmrg 	{ return _M_curr_bmap; }
46536ac495dSmrg 
46636ac495dSmrg 	pointer
46736ac495dSmrg 	_M_base() const throw()
46836ac495dSmrg 	{ return _M_vbp[_M_curr_index].first; }
46936ac495dSmrg 
47036ac495dSmrg 	_Index_type
47136ac495dSmrg 	_M_offset() const throw()
47236ac495dSmrg 	{
473*8feb0f0bSmrg 	  return std::size_t(bits_per_block)
474*8feb0f0bSmrg 	    * ((reinterpret_cast<std::size_t*>(this->_M_base())
47536ac495dSmrg 		- _M_curr_bmap) - 1);
47636ac495dSmrg 	}
47736ac495dSmrg 
47836ac495dSmrg 	_Index_type
47936ac495dSmrg 	_M_where() const throw()
48036ac495dSmrg 	{ return _M_curr_index; }
48136ac495dSmrg       };
48236ac495dSmrg 
48336ac495dSmrg     /** @brief  Mark a memory address as allocated by re-setting the
48436ac495dSmrg      *  corresponding bit in the bit-map.
48536ac495dSmrg      */
48636ac495dSmrg     inline void
487*8feb0f0bSmrg     __bit_allocate(std::size_t* __pbmap, std::size_t __pos) throw()
48836ac495dSmrg     {
489*8feb0f0bSmrg       std::size_t __mask = 1 << __pos;
49036ac495dSmrg       __mask = ~__mask;
49136ac495dSmrg       *__pbmap &= __mask;
49236ac495dSmrg     }
49336ac495dSmrg 
49436ac495dSmrg     /** @brief  Mark a memory address as free by setting the
49536ac495dSmrg      *  corresponding bit in the bit-map.
49636ac495dSmrg      */
49736ac495dSmrg     inline void
498*8feb0f0bSmrg     __bit_free(std::size_t* __pbmap, std::size_t __pos) throw()
49936ac495dSmrg     {
500*8feb0f0bSmrg       std::size_t __mask = 1 << __pos;
50136ac495dSmrg       *__pbmap |= __mask;
50236ac495dSmrg     }
50336ac495dSmrg   } // namespace __detail
50436ac495dSmrg 
50536ac495dSmrg   /** @brief  Generic Version of the bsf instruction.
50636ac495dSmrg    */
507*8feb0f0bSmrg   inline std::size_t
508*8feb0f0bSmrg   _Bit_scan_forward(std::size_t __num)
509*8feb0f0bSmrg   { return static_cast<std::size_t>(__builtin_ctzl(__num)); }
51036ac495dSmrg 
51136ac495dSmrg   /** @class  free_list bitmap_allocator.h bitmap_allocator.h
51236ac495dSmrg    *
51336ac495dSmrg    *  @brief  The free list class for managing chunks of memory to be
51436ac495dSmrg    *  given to and returned by the bitmap_allocator.
51536ac495dSmrg    */
51636ac495dSmrg   class free_list
51736ac495dSmrg   {
51836ac495dSmrg   public:
519*8feb0f0bSmrg     typedef std::size_t* 			value_type;
52036ac495dSmrg     typedef __detail::__mini_vector<value_type> vector_type;
52136ac495dSmrg     typedef vector_type::iterator 		iterator;
52236ac495dSmrg     typedef __mutex				__mutex_type;
52336ac495dSmrg 
52436ac495dSmrg   private:
52536ac495dSmrg     struct _LT_pointer_compare
52636ac495dSmrg     {
52736ac495dSmrg       bool
528*8feb0f0bSmrg       operator()(const std::size_t* __pui,
529*8feb0f0bSmrg 		 const std::size_t __cui) const throw()
53036ac495dSmrg       { return *__pui < __cui; }
53136ac495dSmrg     };
53236ac495dSmrg 
53336ac495dSmrg #if defined __GTHREADS
53436ac495dSmrg     __mutex_type&
53536ac495dSmrg     _M_get_mutex()
53636ac495dSmrg     {
53736ac495dSmrg       static __mutex_type _S_mutex;
53836ac495dSmrg       return _S_mutex;
53936ac495dSmrg     }
54036ac495dSmrg #endif
54136ac495dSmrg 
54236ac495dSmrg     vector_type&
54336ac495dSmrg     _M_get_free_list()
54436ac495dSmrg     {
54536ac495dSmrg       static vector_type _S_free_list;
54636ac495dSmrg       return _S_free_list;
54736ac495dSmrg     }
54836ac495dSmrg 
54936ac495dSmrg     /** @brief  Performs validation of memory based on their size.
55036ac495dSmrg      *
55136ac495dSmrg      *  @param  __addr The pointer to the memory block to be
55236ac495dSmrg      *  validated.
55336ac495dSmrg      *
55436ac495dSmrg      *  Validates the memory block passed to this function and
55536ac495dSmrg      *  appropriately performs the action of managing the free list of
55636ac495dSmrg      *  blocks by adding this block to the free list or deleting this
55736ac495dSmrg      *  or larger blocks from the free list.
55836ac495dSmrg      */
55936ac495dSmrg     void
560*8feb0f0bSmrg     _M_validate(std::size_t* __addr) throw()
56136ac495dSmrg     {
56236ac495dSmrg       vector_type& __free_list = _M_get_free_list();
56336ac495dSmrg       const vector_type::size_type __max_size = 64;
56436ac495dSmrg       if (__free_list.size() >= __max_size)
56536ac495dSmrg 	{
56636ac495dSmrg 	  // Ok, the threshold value has been reached.  We determine
56736ac495dSmrg 	  // which block to remove from the list of free blocks.
56836ac495dSmrg 	  if (*__addr >= *__free_list.back())
56936ac495dSmrg 	    {
57036ac495dSmrg 	      // Ok, the new block is greater than or equal to the
57136ac495dSmrg 	      // last block in the list of free blocks. We just free
57236ac495dSmrg 	      // the new block.
57336ac495dSmrg 	      ::operator delete(static_cast<void*>(__addr));
57436ac495dSmrg 	      return;
57536ac495dSmrg 	    }
57636ac495dSmrg 	  else
57736ac495dSmrg 	    {
57836ac495dSmrg 	      // Deallocate the last block in the list of free lists,
57936ac495dSmrg 	      // and insert the new one in its correct position.
58036ac495dSmrg 	      ::operator delete(static_cast<void*>(__free_list.back()));
58136ac495dSmrg 	      __free_list.pop_back();
58236ac495dSmrg 	    }
58336ac495dSmrg 	}
58436ac495dSmrg 
58536ac495dSmrg       // Just add the block to the list of free lists unconditionally.
58636ac495dSmrg       iterator __temp = __detail::__lower_bound
58736ac495dSmrg 	(__free_list.begin(), __free_list.end(),
58836ac495dSmrg 	 *__addr, _LT_pointer_compare());
58936ac495dSmrg 
59036ac495dSmrg       // We may insert the new free list before _temp;
59136ac495dSmrg       __free_list.insert(__temp, __addr);
59236ac495dSmrg     }
59336ac495dSmrg 
59436ac495dSmrg     /** @brief  Decides whether the wastage of memory is acceptable for
59536ac495dSmrg      *  the current memory request and returns accordingly.
59636ac495dSmrg      *
59736ac495dSmrg      *  @param __block_size The size of the block available in the free
59836ac495dSmrg      *  list.
59936ac495dSmrg      *
60036ac495dSmrg      *  @param __required_size The required size of the memory block.
60136ac495dSmrg      *
60236ac495dSmrg      *  @return true if the wastage incurred is acceptable, else returns
60336ac495dSmrg      *  false.
60436ac495dSmrg      */
60536ac495dSmrg     bool
606*8feb0f0bSmrg     _M_should_i_give(std::size_t __block_size,
607*8feb0f0bSmrg 		     std::size_t __required_size) throw()
60836ac495dSmrg     {
609*8feb0f0bSmrg       const std::size_t __max_wastage_percentage = 36;
61036ac495dSmrg       if (__block_size >= __required_size &&
61136ac495dSmrg 	  (((__block_size - __required_size) * 100 / __block_size)
61236ac495dSmrg 	   < __max_wastage_percentage))
61336ac495dSmrg 	return true;
61436ac495dSmrg       else
61536ac495dSmrg 	return false;
61636ac495dSmrg     }
61736ac495dSmrg 
61836ac495dSmrg   public:
61936ac495dSmrg     /** @brief This function returns the block of memory to the
62036ac495dSmrg      *  internal free list.
62136ac495dSmrg      *
62236ac495dSmrg      *  @param  __addr The pointer to the memory block that was given
62336ac495dSmrg      *  by a call to the _M_get function.
62436ac495dSmrg      */
62536ac495dSmrg     inline void
626*8feb0f0bSmrg     _M_insert(std::size_t* __addr) throw()
62736ac495dSmrg     {
62836ac495dSmrg #if defined __GTHREADS
62936ac495dSmrg       __scoped_lock __bfl_lock(_M_get_mutex());
63036ac495dSmrg #endif
63136ac495dSmrg       // Call _M_validate to decide what should be done with
63236ac495dSmrg       // this particular free list.
633*8feb0f0bSmrg       this->_M_validate(reinterpret_cast<std::size_t*>(__addr) - 1);
63436ac495dSmrg       // See discussion as to why this is 1!
63536ac495dSmrg     }
63636ac495dSmrg 
63736ac495dSmrg     /** @brief  This function gets a block of memory of the specified
63836ac495dSmrg      *  size from the free list.
63936ac495dSmrg      *
64036ac495dSmrg      *  @param  __sz The size in bytes of the memory required.
64136ac495dSmrg      *
64236ac495dSmrg      *  @return  A pointer to the new memory block of size at least
64336ac495dSmrg      *  equal to that requested.
64436ac495dSmrg      */
645*8feb0f0bSmrg     std::size_t*
646*8feb0f0bSmrg     _M_get(std::size_t __sz) _GLIBCXX_THROW(std::bad_alloc);
64736ac495dSmrg 
64836ac495dSmrg     /** @brief  This function just clears the internal Free List, and
64936ac495dSmrg      *  gives back all the memory to the OS.
65036ac495dSmrg      */
65136ac495dSmrg     void
65236ac495dSmrg     _M_clear();
65336ac495dSmrg   };
65436ac495dSmrg 
65536ac495dSmrg 
65636ac495dSmrg   // Forward declare the class.
65736ac495dSmrg   template<typename _Tp>
65836ac495dSmrg     class bitmap_allocator;
65936ac495dSmrg 
66036ac495dSmrg   // Specialize for void:
66136ac495dSmrg   template<>
66236ac495dSmrg     class bitmap_allocator<void>
66336ac495dSmrg     {
66436ac495dSmrg     public:
66536ac495dSmrg       typedef void*       pointer;
66636ac495dSmrg       typedef const void* const_pointer;
66736ac495dSmrg 
66836ac495dSmrg       // Reference-to-void members are impossible.
66936ac495dSmrg       typedef void  value_type;
67036ac495dSmrg       template<typename _Tp1>
67136ac495dSmrg         struct rebind
67236ac495dSmrg 	{
67336ac495dSmrg 	  typedef bitmap_allocator<_Tp1> other;
67436ac495dSmrg 	};
67536ac495dSmrg     };
67636ac495dSmrg 
67736ac495dSmrg   /**
67836ac495dSmrg    * @brief Bitmap Allocator, primary template.
67936ac495dSmrg    * @ingroup allocators
68036ac495dSmrg    */
68136ac495dSmrg   template<typename _Tp>
68236ac495dSmrg     class bitmap_allocator : private free_list
68336ac495dSmrg     {
68436ac495dSmrg     public:
685*8feb0f0bSmrg       typedef std::size_t    		size_type;
686*8feb0f0bSmrg       typedef std::ptrdiff_t 		difference_type;
68736ac495dSmrg       typedef _Tp*        		pointer;
68836ac495dSmrg       typedef const _Tp*  		const_pointer;
68936ac495dSmrg       typedef _Tp&        		reference;
69036ac495dSmrg       typedef const _Tp&  		const_reference;
69136ac495dSmrg       typedef _Tp         		value_type;
69236ac495dSmrg       typedef free_list::__mutex_type 	__mutex_type;
69336ac495dSmrg 
69436ac495dSmrg       template<typename _Tp1>
69536ac495dSmrg         struct rebind
69636ac495dSmrg 	{
69736ac495dSmrg 	  typedef bitmap_allocator<_Tp1> other;
69836ac495dSmrg 	};
69936ac495dSmrg 
70036ac495dSmrg #if __cplusplus >= 201103L
70136ac495dSmrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
70236ac495dSmrg       // 2103. propagate_on_container_move_assignment
70336ac495dSmrg       typedef std::true_type propagate_on_container_move_assignment;
70436ac495dSmrg #endif
70536ac495dSmrg 
70636ac495dSmrg     private:
707*8feb0f0bSmrg       template<std::size_t _BSize, std::size_t _AlignSize>
70836ac495dSmrg         struct aligned_size
70936ac495dSmrg 	{
71036ac495dSmrg 	  enum
71136ac495dSmrg 	    {
71236ac495dSmrg 	      modulus = _BSize % _AlignSize,
71336ac495dSmrg 	      value = _BSize + (modulus ? _AlignSize - (modulus) : 0)
71436ac495dSmrg 	    };
71536ac495dSmrg 	};
71636ac495dSmrg 
71736ac495dSmrg       struct _Alloc_block
71836ac495dSmrg       {
71936ac495dSmrg 	char __M_unused[aligned_size<sizeof(value_type),
72036ac495dSmrg 			_BALLOC_ALIGN_BYTES>::value];
72136ac495dSmrg       };
72236ac495dSmrg 
72336ac495dSmrg 
72436ac495dSmrg       typedef typename std::pair<_Alloc_block*, _Alloc_block*> _Block_pair;
72536ac495dSmrg 
72636ac495dSmrg       typedef typename __detail::__mini_vector<_Block_pair> _BPVector;
72736ac495dSmrg       typedef typename _BPVector::iterator _BPiter;
72836ac495dSmrg 
72936ac495dSmrg       template<typename _Predicate>
73036ac495dSmrg         static _BPiter
73136ac495dSmrg         _S_find(_Predicate __p)
73236ac495dSmrg         {
73336ac495dSmrg 	  _BPiter __first = _S_mem_blocks.begin();
73436ac495dSmrg 	  while (__first != _S_mem_blocks.end() && !__p(*__first))
73536ac495dSmrg 	    ++__first;
73636ac495dSmrg 	  return __first;
73736ac495dSmrg 	}
73836ac495dSmrg 
73936ac495dSmrg #if defined _GLIBCXX_DEBUG
74036ac495dSmrg       // Complexity: O(lg(N)). Where, N is the number of block of size
74136ac495dSmrg       // sizeof(value_type).
74236ac495dSmrg       void
74336ac495dSmrg       _S_check_for_free_blocks() throw()
74436ac495dSmrg       {
74536ac495dSmrg 	typedef typename __detail::_Ffit_finder<_Alloc_block*> _FFF;
74636ac495dSmrg 	_BPiter __bpi = _S_find(_FFF());
74736ac495dSmrg 
74836ac495dSmrg 	_GLIBCXX_DEBUG_ASSERT(__bpi == _S_mem_blocks.end());
74936ac495dSmrg       }
75036ac495dSmrg #endif
75136ac495dSmrg 
75236ac495dSmrg       /** @brief  Responsible for exponentially growing the internal
75336ac495dSmrg        *  memory pool.
75436ac495dSmrg        *
75536ac495dSmrg        *  @throw  std::bad_alloc. If memory cannot be allocated.
75636ac495dSmrg        *
75736ac495dSmrg        *  Complexity: O(1), but internally depends upon the
75836ac495dSmrg        *  complexity of the function free_list::_M_get. The part where
75936ac495dSmrg        *  the bitmap headers are written has complexity: O(X),where X
76036ac495dSmrg        *  is the number of blocks of size sizeof(value_type) within
76136ac495dSmrg        *  the newly acquired block. Having a tight bound.
76236ac495dSmrg        */
76336ac495dSmrg       void
76436ac495dSmrg       _S_refill_pool() _GLIBCXX_THROW(std::bad_alloc)
76536ac495dSmrg       {
766*8feb0f0bSmrg 	using std::size_t;
76736ac495dSmrg #if defined _GLIBCXX_DEBUG
76836ac495dSmrg 	_S_check_for_free_blocks();
76936ac495dSmrg #endif
77036ac495dSmrg 
77136ac495dSmrg 	const size_t __num_bitmaps = (_S_block_size
77236ac495dSmrg 				      / size_t(__detail::bits_per_block));
77336ac495dSmrg 	const size_t __size_to_allocate = sizeof(size_t)
77436ac495dSmrg 	  + _S_block_size * sizeof(_Alloc_block)
77536ac495dSmrg 	  + __num_bitmaps * sizeof(size_t);
77636ac495dSmrg 
77736ac495dSmrg 	size_t* __temp =
77836ac495dSmrg 	  reinterpret_cast<size_t*>(this->_M_get(__size_to_allocate));
77936ac495dSmrg 	*__temp = 0;
78036ac495dSmrg 	++__temp;
78136ac495dSmrg 
78236ac495dSmrg 	// The Header information goes at the Beginning of the Block.
78336ac495dSmrg 	_Block_pair __bp =
78436ac495dSmrg 	  std::make_pair(reinterpret_cast<_Alloc_block*>
78536ac495dSmrg 			 (__temp + __num_bitmaps),
78636ac495dSmrg 			 reinterpret_cast<_Alloc_block*>
78736ac495dSmrg 			 (__temp + __num_bitmaps)
78836ac495dSmrg 			 + _S_block_size - 1);
78936ac495dSmrg 
79036ac495dSmrg 	// Fill the Vector with this information.
79136ac495dSmrg 	_S_mem_blocks.push_back(__bp);
79236ac495dSmrg 
79336ac495dSmrg 	for (size_t __i = 0; __i < __num_bitmaps; ++__i)
79436ac495dSmrg 	  __temp[__i] = ~static_cast<size_t>(0); // 1 Indicates all Free.
79536ac495dSmrg 
79636ac495dSmrg 	_S_block_size *= 2;
79736ac495dSmrg       }
79836ac495dSmrg 
79936ac495dSmrg       static _BPVector _S_mem_blocks;
800*8feb0f0bSmrg       static std::size_t _S_block_size;
80136ac495dSmrg       static __detail::_Bitmap_counter<_Alloc_block*> _S_last_request;
80236ac495dSmrg       static typename _BPVector::size_type _S_last_dealloc_index;
80336ac495dSmrg #if defined __GTHREADS
80436ac495dSmrg       static __mutex_type _S_mut;
80536ac495dSmrg #endif
80636ac495dSmrg 
80736ac495dSmrg     public:
80836ac495dSmrg 
80936ac495dSmrg       /** @brief  Allocates memory for a single object of size
81036ac495dSmrg        *  sizeof(_Tp).
81136ac495dSmrg        *
81236ac495dSmrg        *  @throw  std::bad_alloc. If memory cannot be allocated.
81336ac495dSmrg        *
81436ac495dSmrg        *  Complexity: Worst case complexity is O(N), but that
81536ac495dSmrg        *  is hardly ever hit. If and when this particular case is
81636ac495dSmrg        *  encountered, the next few cases are guaranteed to have a
81736ac495dSmrg        *  worst case complexity of O(1)!  That's why this function
81836ac495dSmrg        *  performs very well on average. You can consider this
81936ac495dSmrg        *  function to have a complexity referred to commonly as:
82036ac495dSmrg        *  Amortized Constant time.
82136ac495dSmrg        */
82236ac495dSmrg       pointer
82336ac495dSmrg       _M_allocate_single_object() _GLIBCXX_THROW(std::bad_alloc)
82436ac495dSmrg       {
825*8feb0f0bSmrg 	using std::size_t;
82636ac495dSmrg #if defined __GTHREADS
82736ac495dSmrg 	__scoped_lock __bit_lock(_S_mut);
82836ac495dSmrg #endif
82936ac495dSmrg 
83036ac495dSmrg 	// The algorithm is something like this: The last_request
83136ac495dSmrg 	// variable points to the last accessed Bit Map. When such a
83236ac495dSmrg 	// condition occurs, we try to find a free block in the
83336ac495dSmrg 	// current bitmap, or succeeding bitmaps until the last bitmap
83436ac495dSmrg 	// is reached. If no free block turns up, we resort to First
83536ac495dSmrg 	// Fit method.
83636ac495dSmrg 
83736ac495dSmrg 	// WARNING: Do not re-order the condition in the while
83836ac495dSmrg 	// statement below, because it relies on C++'s short-circuit
83936ac495dSmrg 	// evaluation. The return from _S_last_request->_M_get() will
84036ac495dSmrg 	// NOT be dereference able if _S_last_request->_M_finished()
84136ac495dSmrg 	// returns true. This would inevitably lead to a NULL pointer
84236ac495dSmrg 	// dereference if tinkered with.
84336ac495dSmrg 	while (_S_last_request._M_finished() == false
84436ac495dSmrg 	       && (*(_S_last_request._M_get()) == 0))
84536ac495dSmrg 	  _S_last_request.operator++();
84636ac495dSmrg 
84736ac495dSmrg 	if (__builtin_expect(_S_last_request._M_finished() == true, false))
84836ac495dSmrg 	  {
84936ac495dSmrg 	    // Fall Back to First Fit algorithm.
85036ac495dSmrg 	    typedef typename __detail::_Ffit_finder<_Alloc_block*> _FFF;
85136ac495dSmrg 	    _FFF __fff;
85236ac495dSmrg 	    _BPiter __bpi = _S_find(__detail::_Functor_Ref<_FFF>(__fff));
85336ac495dSmrg 
85436ac495dSmrg 	    if (__bpi != _S_mem_blocks.end())
85536ac495dSmrg 	      {
85636ac495dSmrg 		// Search was successful. Ok, now mark the first bit from
85736ac495dSmrg 		// the right as 0, meaning Allocated. This bit is obtained
85836ac495dSmrg 		// by calling _M_get() on __fff.
85936ac495dSmrg 		size_t __nz_bit = _Bit_scan_forward(*__fff._M_get());
86036ac495dSmrg 		__detail::__bit_allocate(__fff._M_get(), __nz_bit);
86136ac495dSmrg 
86236ac495dSmrg 		_S_last_request._M_reset(__bpi - _S_mem_blocks.begin());
86336ac495dSmrg 
86436ac495dSmrg 		// Now, get the address of the bit we marked as allocated.
86536ac495dSmrg 		pointer __ret = reinterpret_cast<pointer>
86636ac495dSmrg 		  (__bpi->first + __fff._M_offset() + __nz_bit);
86736ac495dSmrg 		size_t* __puse_count =
86836ac495dSmrg 		  reinterpret_cast<size_t*>
86936ac495dSmrg 		  (__bpi->first) - (__detail::__num_bitmaps(*__bpi) + 1);
87036ac495dSmrg 
87136ac495dSmrg 		++(*__puse_count);
87236ac495dSmrg 		return __ret;
87336ac495dSmrg 	      }
87436ac495dSmrg 	    else
87536ac495dSmrg 	      {
87636ac495dSmrg 		// Search was unsuccessful. We Add more memory to the
87736ac495dSmrg 		// pool by calling _S_refill_pool().
87836ac495dSmrg 		_S_refill_pool();
87936ac495dSmrg 
88036ac495dSmrg 		// _M_Reset the _S_last_request structure to the first
88136ac495dSmrg 		// free block's bit map.
88236ac495dSmrg 		_S_last_request._M_reset(_S_mem_blocks.size() - 1);
88336ac495dSmrg 
88436ac495dSmrg 		// Now, mark that bit as allocated.
88536ac495dSmrg 	      }
88636ac495dSmrg 	  }
88736ac495dSmrg 
88836ac495dSmrg 	// _S_last_request holds a pointer to a valid bit map, that
88936ac495dSmrg 	// points to a free block in memory.
89036ac495dSmrg 	size_t __nz_bit = _Bit_scan_forward(*_S_last_request._M_get());
89136ac495dSmrg 	__detail::__bit_allocate(_S_last_request._M_get(), __nz_bit);
89236ac495dSmrg 
89336ac495dSmrg 	pointer __ret = reinterpret_cast<pointer>
89436ac495dSmrg 	  (_S_last_request._M_base() + _S_last_request._M_offset() + __nz_bit);
89536ac495dSmrg 
89636ac495dSmrg 	size_t* __puse_count = reinterpret_cast<size_t*>
89736ac495dSmrg 	  (_S_mem_blocks[_S_last_request._M_where()].first)
89836ac495dSmrg 	  - (__detail::
89936ac495dSmrg 	     __num_bitmaps(_S_mem_blocks[_S_last_request._M_where()]) + 1);
90036ac495dSmrg 
90136ac495dSmrg 	++(*__puse_count);
90236ac495dSmrg 	return __ret;
90336ac495dSmrg       }
90436ac495dSmrg 
90536ac495dSmrg       /** @brief  Deallocates memory that belongs to a single object of
90636ac495dSmrg        *  size sizeof(_Tp).
90736ac495dSmrg        *
90836ac495dSmrg        *  Complexity: O(lg(N)), but the worst case is not hit
90936ac495dSmrg        *  often!  This is because containers usually deallocate memory
91036ac495dSmrg        *  close to each other and this case is handled in O(1) time by
91136ac495dSmrg        *  the deallocate function.
91236ac495dSmrg        */
91336ac495dSmrg       void
91436ac495dSmrg       _M_deallocate_single_object(pointer __p) throw()
91536ac495dSmrg       {
916*8feb0f0bSmrg 	using std::size_t;
91736ac495dSmrg #if defined __GTHREADS
91836ac495dSmrg 	__scoped_lock __bit_lock(_S_mut);
91936ac495dSmrg #endif
92036ac495dSmrg 	_Alloc_block* __real_p = reinterpret_cast<_Alloc_block*>(__p);
92136ac495dSmrg 
92236ac495dSmrg 	typedef typename _BPVector::iterator _Iterator;
92336ac495dSmrg 	typedef typename _BPVector::difference_type _Difference_type;
92436ac495dSmrg 
92536ac495dSmrg 	_Difference_type __diff;
92636ac495dSmrg 	long __displacement;
92736ac495dSmrg 
92836ac495dSmrg 	_GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0);
92936ac495dSmrg 
93036ac495dSmrg 	__detail::_Inclusive_between<_Alloc_block*> __ibt(__real_p);
93136ac495dSmrg 	if (__ibt(_S_mem_blocks[_S_last_dealloc_index]))
93236ac495dSmrg 	  {
93336ac495dSmrg 	    _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index
93436ac495dSmrg 				  <= _S_mem_blocks.size() - 1);
93536ac495dSmrg 
93636ac495dSmrg 	    // Initial Assumption was correct!
93736ac495dSmrg 	    __diff = _S_last_dealloc_index;
93836ac495dSmrg 	    __displacement = __real_p - _S_mem_blocks[__diff].first;
93936ac495dSmrg 	  }
94036ac495dSmrg 	else
94136ac495dSmrg 	  {
94236ac495dSmrg 	    _Iterator _iter = _S_find(__ibt);
94336ac495dSmrg 
94436ac495dSmrg 	    _GLIBCXX_DEBUG_ASSERT(_iter != _S_mem_blocks.end());
94536ac495dSmrg 
94636ac495dSmrg 	    __diff = _iter - _S_mem_blocks.begin();
94736ac495dSmrg 	    __displacement = __real_p - _S_mem_blocks[__diff].first;
94836ac495dSmrg 	    _S_last_dealloc_index = __diff;
94936ac495dSmrg 	  }
95036ac495dSmrg 
95136ac495dSmrg 	// Get the position of the iterator that has been found.
95236ac495dSmrg 	const size_t __rotate = (__displacement
95336ac495dSmrg 				 % size_t(__detail::bits_per_block));
95436ac495dSmrg 	size_t* __bitmapC =
95536ac495dSmrg 	  reinterpret_cast<size_t*>
95636ac495dSmrg 	  (_S_mem_blocks[__diff].first) - 1;
95736ac495dSmrg 	__bitmapC -= (__displacement / size_t(__detail::bits_per_block));
95836ac495dSmrg 
95936ac495dSmrg 	__detail::__bit_free(__bitmapC, __rotate);
96036ac495dSmrg 	size_t* __puse_count = reinterpret_cast<size_t*>
96136ac495dSmrg 	  (_S_mem_blocks[__diff].first)
96236ac495dSmrg 	  - (__detail::__num_bitmaps(_S_mem_blocks[__diff]) + 1);
96336ac495dSmrg 
96436ac495dSmrg 	_GLIBCXX_DEBUG_ASSERT(*__puse_count != 0);
96536ac495dSmrg 
96636ac495dSmrg 	--(*__puse_count);
96736ac495dSmrg 
96836ac495dSmrg 	if (__builtin_expect(*__puse_count == 0, false))
96936ac495dSmrg 	  {
97036ac495dSmrg 	    _S_block_size /= 2;
97136ac495dSmrg 
97236ac495dSmrg 	    // We can safely remove this block.
97336ac495dSmrg 	    // _Block_pair __bp = _S_mem_blocks[__diff];
97436ac495dSmrg 	    this->_M_insert(__puse_count);
97536ac495dSmrg 	    _S_mem_blocks.erase(_S_mem_blocks.begin() + __diff);
97636ac495dSmrg 
97736ac495dSmrg 	    // Reset the _S_last_request variable to reflect the
97836ac495dSmrg 	    // erased block. We do this to protect future requests
97936ac495dSmrg 	    // after the last block has been removed from a particular
98036ac495dSmrg 	    // memory Chunk, which in turn has been returned to the
98136ac495dSmrg 	    // free list, and hence had been erased from the vector,
98236ac495dSmrg 	    // so the size of the vector gets reduced by 1.
98336ac495dSmrg 	    if ((_Difference_type)_S_last_request._M_where() >= __diff--)
98436ac495dSmrg 	      _S_last_request._M_reset(__diff);
98536ac495dSmrg 
98636ac495dSmrg 	    // If the Index into the vector of the region of memory
98736ac495dSmrg 	    // that might hold the next address that will be passed to
98836ac495dSmrg 	    // deallocated may have been invalidated due to the above
98936ac495dSmrg 	    // erase procedure being called on the vector, hence we
99036ac495dSmrg 	    // try to restore this invariant too.
99136ac495dSmrg 	    if (_S_last_dealloc_index >= _S_mem_blocks.size())
99236ac495dSmrg 	      {
99336ac495dSmrg 		_S_last_dealloc_index =(__diff != -1 ? __diff : 0);
99436ac495dSmrg 		_GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0);
99536ac495dSmrg 	      }
99636ac495dSmrg 	  }
99736ac495dSmrg       }
99836ac495dSmrg 
99936ac495dSmrg     public:
100036ac495dSmrg       bitmap_allocator() _GLIBCXX_USE_NOEXCEPT
100136ac495dSmrg       { }
100236ac495dSmrg 
100336ac495dSmrg       bitmap_allocator(const bitmap_allocator&) _GLIBCXX_USE_NOEXCEPT
100436ac495dSmrg       { }
100536ac495dSmrg 
100636ac495dSmrg       template<typename _Tp1>
100736ac495dSmrg         bitmap_allocator(const bitmap_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT
100836ac495dSmrg         { }
100936ac495dSmrg 
101036ac495dSmrg       ~bitmap_allocator() _GLIBCXX_USE_NOEXCEPT
101136ac495dSmrg       { }
101236ac495dSmrg 
1013c0a68be4Smrg       _GLIBCXX_NODISCARD pointer
101436ac495dSmrg       allocate(size_type __n)
101536ac495dSmrg       {
101636ac495dSmrg 	if (__n > this->max_size())
101736ac495dSmrg 	  std::__throw_bad_alloc();
101836ac495dSmrg 
101936ac495dSmrg #if __cpp_aligned_new
102036ac495dSmrg 	if (alignof(value_type) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
102136ac495dSmrg 	  {
102236ac495dSmrg 	    const size_type __b = __n * sizeof(value_type);
102336ac495dSmrg 	    std::align_val_t __al = std::align_val_t(alignof(value_type));
102436ac495dSmrg 	    return static_cast<pointer>(::operator new(__b, __al));
102536ac495dSmrg 	  }
102636ac495dSmrg #endif
102736ac495dSmrg 
102836ac495dSmrg 	if (__builtin_expect(__n == 1, true))
102936ac495dSmrg 	  return this->_M_allocate_single_object();
103036ac495dSmrg 	else
103136ac495dSmrg 	  {
103236ac495dSmrg 	    const size_type __b = __n * sizeof(value_type);
103336ac495dSmrg 	    return reinterpret_cast<pointer>(::operator new(__b));
103436ac495dSmrg 	  }
103536ac495dSmrg       }
103636ac495dSmrg 
1037c0a68be4Smrg       _GLIBCXX_NODISCARD pointer
103836ac495dSmrg       allocate(size_type __n, typename bitmap_allocator<void>::const_pointer)
103936ac495dSmrg       { return allocate(__n); }
104036ac495dSmrg 
104136ac495dSmrg       void
104236ac495dSmrg       deallocate(pointer __p, size_type __n) throw()
104336ac495dSmrg       {
104436ac495dSmrg 	if (__builtin_expect(__p != 0, true))
104536ac495dSmrg 	  {
104636ac495dSmrg #if __cpp_aligned_new
104736ac495dSmrg 	    // Types with extended alignment are handled by operator delete.
104836ac495dSmrg 	    if (alignof(value_type) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
104936ac495dSmrg 	      {
105036ac495dSmrg 		::operator delete(__p, std::align_val_t(alignof(value_type)));
105136ac495dSmrg 		return;
105236ac495dSmrg 	      }
105336ac495dSmrg #endif
105436ac495dSmrg 
105536ac495dSmrg 	    if (__builtin_expect(__n == 1, true))
105636ac495dSmrg 	      this->_M_deallocate_single_object(__p);
105736ac495dSmrg 	    else
105836ac495dSmrg 	      ::operator delete(__p);
105936ac495dSmrg 	  }
106036ac495dSmrg       }
106136ac495dSmrg 
106236ac495dSmrg       pointer
106336ac495dSmrg       address(reference __r) const _GLIBCXX_NOEXCEPT
106436ac495dSmrg       { return std::__addressof(__r); }
106536ac495dSmrg 
106636ac495dSmrg       const_pointer
106736ac495dSmrg       address(const_reference __r) const _GLIBCXX_NOEXCEPT
106836ac495dSmrg       { return std::__addressof(__r); }
106936ac495dSmrg 
107036ac495dSmrg       size_type
107136ac495dSmrg       max_size() const _GLIBCXX_USE_NOEXCEPT
107236ac495dSmrg       { return size_type(-1) / sizeof(value_type); }
107336ac495dSmrg 
107436ac495dSmrg #if __cplusplus >= 201103L
107536ac495dSmrg       template<typename _Up, typename... _Args>
107636ac495dSmrg         void
107736ac495dSmrg         construct(_Up* __p, _Args&&... __args)
107836ac495dSmrg 	{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
107936ac495dSmrg 
108036ac495dSmrg       template<typename _Up>
108136ac495dSmrg         void
108236ac495dSmrg         destroy(_Up* __p)
108336ac495dSmrg         { __p->~_Up(); }
108436ac495dSmrg #else
108536ac495dSmrg       void
108636ac495dSmrg       construct(pointer __p, const_reference __data)
108736ac495dSmrg       { ::new((void *)__p) value_type(__data); }
108836ac495dSmrg 
108936ac495dSmrg       void
109036ac495dSmrg       destroy(pointer __p)
109136ac495dSmrg       { __p->~value_type(); }
109236ac495dSmrg #endif
109336ac495dSmrg     };
109436ac495dSmrg 
109536ac495dSmrg   template<typename _Tp1, typename _Tp2>
109636ac495dSmrg     bool
109736ac495dSmrg     operator==(const bitmap_allocator<_Tp1>&,
109836ac495dSmrg 	       const bitmap_allocator<_Tp2>&) throw()
109936ac495dSmrg     { return true; }
110036ac495dSmrg 
1101*8feb0f0bSmrg #if __cpp_impl_three_way_comparison < 201907L
110236ac495dSmrg   template<typename _Tp1, typename _Tp2>
110336ac495dSmrg     bool
110436ac495dSmrg     operator!=(const bitmap_allocator<_Tp1>&,
110536ac495dSmrg 	       const bitmap_allocator<_Tp2>&) throw()
110636ac495dSmrg     { return false; }
1107*8feb0f0bSmrg #endif
110836ac495dSmrg 
110936ac495dSmrg   // Static member definitions.
111036ac495dSmrg   template<typename _Tp>
111136ac495dSmrg     typename bitmap_allocator<_Tp>::_BPVector
111236ac495dSmrg     bitmap_allocator<_Tp>::_S_mem_blocks;
111336ac495dSmrg 
111436ac495dSmrg   template<typename _Tp>
1115*8feb0f0bSmrg     std::size_t bitmap_allocator<_Tp>::_S_block_size
1116*8feb0f0bSmrg       = 2 * std::size_t(__detail::bits_per_block);
111736ac495dSmrg 
111836ac495dSmrg   template<typename _Tp>
111936ac495dSmrg     typename bitmap_allocator<_Tp>::_BPVector::size_type
112036ac495dSmrg     bitmap_allocator<_Tp>::_S_last_dealloc_index = 0;
112136ac495dSmrg 
112236ac495dSmrg   template<typename _Tp>
112336ac495dSmrg     __detail::_Bitmap_counter
112436ac495dSmrg       <typename bitmap_allocator<_Tp>::_Alloc_block*>
112536ac495dSmrg     bitmap_allocator<_Tp>::_S_last_request(_S_mem_blocks);
112636ac495dSmrg 
112736ac495dSmrg #if defined __GTHREADS
112836ac495dSmrg   template<typename _Tp>
112936ac495dSmrg     typename bitmap_allocator<_Tp>::__mutex_type
113036ac495dSmrg     bitmap_allocator<_Tp>::_S_mut;
113136ac495dSmrg #endif
113236ac495dSmrg 
113336ac495dSmrg _GLIBCXX_END_NAMESPACE_VERSION
113436ac495dSmrg } // namespace __gnu_cxx
113536ac495dSmrg 
113636ac495dSmrg #endif
1137