libstdc++
bitmap_allocator.h
Go to the documentation of this file.
00001 // Bitmap Allocator. -*- C++ -*-
00002 
00003 // Copyright (C) 2004-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file ext/bitmap_allocator.h
00026  *  This file is a GNU extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _BITMAP_ALLOCATOR_H
00030 #define _BITMAP_ALLOCATOR_H 1
00031 
00032 #include <utility> // For std::pair.
00033 #include <bits/functexcept.h> // For __throw_bad_alloc().
00034 #include <functional> // For greater_equal, and less_equal.
00035 #include <new> // For operator new.
00036 #include <debug/debug.h> // _GLIBCXX_DEBUG_ASSERT
00037 #include <ext/concurrence.h>
00038 #include <bits/move.h>
00039 
00040 /** @brief The constant in the expression below is the alignment
00041  * required in bytes.
00042  */
00043 #define _BALLOC_ALIGN_BYTES 8
00044 
00045 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00046 {
00047   using std::size_t;
00048   using std::ptrdiff_t;
00049 
00050   namespace __detail
00051   {
00052   _GLIBCXX_BEGIN_NAMESPACE_VERSION
00053     /** @class  __mini_vector bitmap_allocator.h bitmap_allocator.h
00054      *
00055      *  @brief  __mini_vector<> is a stripped down version of the
00056      *  full-fledged std::vector<>.
00057      *
00058      *  It is to be used only for built-in types or PODs. Notable
00059      *  differences are:
00060      * 
00061      *  1. Not all accessor functions are present.
00062      *  2. Used ONLY for PODs.
00063      *  3. No Allocator template argument. Uses ::operator new() to get
00064      *  memory, and ::operator delete() to free it.
00065      *  Caveat: The dtor does NOT free the memory allocated, so this a
00066      *  memory-leaking vector!
00067      */
00068     template<typename _Tp>
00069       class __mini_vector
00070       {
00071         __mini_vector(const __mini_vector&);
00072         __mini_vector& operator=(const __mini_vector&);
00073 
00074       public:
00075         typedef _Tp value_type;
00076         typedef _Tp* pointer;
00077         typedef _Tp& reference;
00078         typedef const _Tp& const_reference;
00079         typedef size_t size_type;
00080         typedef ptrdiff_t difference_type;
00081         typedef pointer iterator;
00082 
00083       private:
00084         pointer _M_start;
00085         pointer _M_finish;
00086         pointer _M_end_of_storage;
00087 
00088         size_type
00089         _M_space_left() const throw()
00090         { return _M_end_of_storage - _M_finish; }
00091 
00092         pointer
00093         allocate(size_type __n)
00094         { return static_cast<pointer>(::operator new(__n * sizeof(_Tp))); }
00095 
00096         void
00097         deallocate(pointer __p, size_type)
00098         { ::operator delete(__p); }
00099 
00100       public:
00101         // Members used: size(), push_back(), pop_back(),
00102         // insert(iterator, const_reference), erase(iterator),
00103         // begin(), end(), back(), operator[].
00104 
00105         __mini_vector()
00106         : _M_start(0), _M_finish(0), _M_end_of_storage(0) { }
00107 
00108         size_type
00109         size() const throw()
00110         { return _M_finish - _M_start; }
00111 
00112         iterator
00113         begin() const throw()
00114         { return this->_M_start; }
00115 
00116         iterator
00117         end() const throw()
00118         { return this->_M_finish; }
00119 
00120         reference
00121         back() const throw()
00122         { return *(this->end() - 1); }
00123 
00124         reference
00125         operator[](const size_type __pos) const throw()
00126         { return this->_M_start[__pos]; }
00127 
00128         void
00129         insert(iterator __pos, const_reference __x);
00130 
00131         void
00132         push_back(const_reference __x)
00133         {
00134           if (this->_M_space_left())
00135             {
00136               *this->end() = __x;
00137               ++this->_M_finish;
00138             }
00139           else
00140             this->insert(this->end(), __x);
00141         }
00142 
00143         void
00144         pop_back() throw()
00145         { --this->_M_finish; }
00146 
00147         void
00148         erase(iterator __pos) throw();
00149 
00150         void
00151         clear() throw()
00152         { this->_M_finish = this->_M_start; }
00153       };
00154 
00155     // Out of line function definitions.
00156     template<typename _Tp>
00157       void __mini_vector<_Tp>::
00158       insert(iterator __pos, const_reference __x)
00159       {
00160         if (this->_M_space_left())
00161           {
00162             size_type __to_move = this->_M_finish - __pos;
00163             iterator __dest = this->end();
00164             iterator __src = this->end() - 1;
00165 
00166             ++this->_M_finish;
00167             while (__to_move)
00168               {
00169                 *__dest = *__src;
00170                 --__dest; --__src; --__to_move;
00171               }
00172             *__pos = __x;
00173           }
00174         else
00175           {
00176             size_type __new_size = this->size() ? this->size() * 2 : 1;
00177             iterator __new_start = this->allocate(__new_size);
00178             iterator __first = this->begin();
00179             iterator __start = __new_start;
00180             while (__first != __pos)
00181               {
00182                 *__start = *__first;
00183                 ++__start; ++__first;
00184               }
00185             *__start = __x;
00186             ++__start;
00187             while (__first != this->end())
00188               {
00189                 *__start = *__first;
00190                 ++__start; ++__first;
00191               }
00192             if (this->_M_start)
00193               this->deallocate(this->_M_start, this->size());
00194 
00195             this->_M_start = __new_start;
00196             this->_M_finish = __start;
00197             this->_M_end_of_storage = this->_M_start + __new_size;
00198           }
00199       }
00200 
00201     template<typename _Tp>
00202       void __mini_vector<_Tp>::
00203       erase(iterator __pos) throw()
00204       {
00205         while (__pos + 1 != this->end())
00206           {
00207             *__pos = __pos[1];
00208             ++__pos;
00209           }
00210         --this->_M_finish;
00211       }
00212 
00213 
00214     template<typename _Tp>
00215       struct __mv_iter_traits
00216       {
00217         typedef typename _Tp::value_type value_type;
00218         typedef typename _Tp::difference_type difference_type;
00219       };
00220 
00221     template<typename _Tp>
00222       struct __mv_iter_traits<_Tp*>
00223       {
00224         typedef _Tp value_type;
00225         typedef ptrdiff_t difference_type;
00226       };
00227 
00228     enum 
00229       { 
00230         bits_per_byte = 8,
00231         bits_per_block = sizeof(size_t) * size_t(bits_per_byte) 
00232       };
00233 
00234     template<typename _ForwardIterator, typename _Tp, typename _Compare>
00235       _ForwardIterator
00236       __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
00237                     const _Tp& __val, _Compare __comp)
00238       {
00239         typedef typename __mv_iter_traits<_ForwardIterator>::difference_type
00240           _DistanceType;
00241 
00242         _DistanceType __len = __last - __first;
00243         _DistanceType __half;
00244         _ForwardIterator __middle;
00245 
00246         while (__len > 0)
00247           {
00248             __half = __len >> 1;
00249             __middle = __first;
00250             __middle += __half;
00251             if (__comp(*__middle, __val))
00252               {
00253                 __first = __middle;
00254                 ++__first;
00255                 __len = __len - __half - 1;
00256               }
00257             else
00258               __len = __half;
00259           }
00260         return __first;
00261       }
00262 
00263     /** @brief The number of Blocks pointed to by the address pair
00264      *  passed to the function.
00265      */
00266     template<typename _AddrPair>
00267       inline size_t
00268       __num_blocks(_AddrPair __ap)
00269       { return (__ap.second - __ap.first) + 1; }
00270 
00271     /** @brief The number of Bit-maps pointed to by the address pair
00272      *  passed to the function.
00273      */
00274     template<typename _AddrPair>
00275       inline size_t
00276       __num_bitmaps(_AddrPair __ap)
00277       { return __num_blocks(__ap) / size_t(bits_per_block); }
00278 
00279     // _Tp should be a pointer type.
00280     template<typename _Tp>
00281       class _Inclusive_between 
00282       : public std::unary_function<typename std::pair<_Tp, _Tp>, bool>
00283       {
00284         typedef _Tp pointer;
00285         pointer _M_ptr_value;
00286         typedef typename std::pair<_Tp, _Tp> _Block_pair;
00287         
00288       public:
00289         _Inclusive_between(pointer __ptr) : _M_ptr_value(__ptr) 
00290         { }
00291         
00292         bool 
00293         operator()(_Block_pair __bp) const throw()
00294         {
00295           if (std::less_equal<pointer>()(_M_ptr_value, __bp.second) 
00296               && std::greater_equal<pointer>()(_M_ptr_value, __bp.first))
00297             return true;
00298           else
00299             return false;
00300         }
00301       };
00302   
00303     // Used to pass a Functor to functions by reference.
00304     template<typename _Functor>
00305       class _Functor_Ref 
00306       : public std::unary_function<typename _Functor::argument_type, 
00307                                    typename _Functor::result_type>
00308       {
00309         _Functor& _M_fref;
00310         
00311       public:
00312         typedef typename _Functor::argument_type argument_type;
00313         typedef typename _Functor::result_type result_type;
00314 
00315         _Functor_Ref(_Functor& __fref) : _M_fref(__fref) 
00316         { }
00317 
00318         result_type 
00319         operator()(argument_type __arg) 
00320         { return _M_fref(__arg); }
00321       };
00322 
00323     /** @class  _Ffit_finder bitmap_allocator.h bitmap_allocator.h
00324      *
00325      *  @brief  The class which acts as a predicate for applying the
00326      *  first-fit memory allocation policy for the bitmap allocator.
00327      */
00328     // _Tp should be a pointer type, and _Alloc is the Allocator for
00329     // the vector.
00330     template<typename _Tp>
00331       class _Ffit_finder 
00332       : public std::unary_function<typename std::pair<_Tp, _Tp>, bool>
00333       {
00334         typedef typename std::pair<_Tp, _Tp> _Block_pair;
00335         typedef typename __detail::__mini_vector<_Block_pair> _BPVector;
00336         typedef typename _BPVector::difference_type _Counter_type;
00337 
00338         size_t* _M_pbitmap;
00339         _Counter_type _M_data_offset;
00340 
00341       public:
00342         _Ffit_finder() : _M_pbitmap(0), _M_data_offset(0)
00343         { }
00344 
00345         bool 
00346         operator()(_Block_pair __bp) throw()
00347         {
00348           // Set the _rover to the last physical location bitmap,
00349           // which is the bitmap which belongs to the first free
00350           // block. Thus, the bitmaps are in exact reverse order of
00351           // the actual memory layout. So, we count down the bitmaps,
00352           // which is the same as moving up the memory.
00353 
00354           // If the used count stored at the start of the Bit Map headers
00355           // is equal to the number of Objects that the current Block can
00356           // store, then there is definitely no space for another single
00357           // object, so just return false.
00358           _Counter_type __diff = __detail::__num_bitmaps(__bp);
00359 
00360           if (*(reinterpret_cast<size_t*>
00361                 (__bp.first) - (__diff + 1)) == __detail::__num_blocks(__bp))
00362             return false;
00363 
00364           size_t* __rover = reinterpret_cast<size_t*>(__bp.first) - 1;
00365 
00366           for (_Counter_type __i = 0; __i < __diff; ++__i)
00367             {
00368               _M_data_offset = __i;
00369               if (*__rover)
00370                 {
00371                   _M_pbitmap = __rover;
00372                   return true;
00373                 }
00374               --__rover;
00375             }
00376           return false;
00377         }
00378     
00379         size_t*
00380         _M_get() const throw()
00381         { return _M_pbitmap; }
00382 
00383         _Counter_type
00384         _M_offset() const throw()
00385         { return _M_data_offset * size_t(bits_per_block); }
00386       };
00387 
00388     /** @class  _Bitmap_counter bitmap_allocator.h bitmap_allocator.h
00389      *
00390      *  @brief  The bitmap counter which acts as the bitmap
00391      *  manipulator, and manages the bit-manipulation functions and
00392      *  the searching and identification functions on the bit-map.
00393      */
00394     // _Tp should be a pointer type.
00395     template<typename _Tp>
00396       class _Bitmap_counter
00397       {
00398         typedef typename
00399         __detail::__mini_vector<typename std::pair<_Tp, _Tp> > _BPVector;
00400         typedef typename _BPVector::size_type _Index_type;
00401         typedef _Tp pointer;
00402 
00403         _BPVector& _M_vbp;
00404         size_t* _M_curr_bmap;
00405         size_t* _M_last_bmap_in_block;
00406         _Index_type _M_curr_index;
00407     
00408       public:
00409         // Use the 2nd parameter with care. Make sure that such an
00410         // entry exists in the vector before passing that particular
00411         // index to this ctor.
00412         _Bitmap_counter(_BPVector& Rvbp, long __index = -1) : _M_vbp(Rvbp)
00413         { this->_M_reset(__index); }
00414     
00415         void 
00416         _M_reset(long __index = -1) throw()
00417         {
00418           if (__index == -1)
00419             {
00420               _M_curr_bmap = 0;
00421               _M_curr_index = static_cast<_Index_type>(-1);
00422               return;
00423             }
00424 
00425           _M_curr_index = __index;
00426           _M_curr_bmap = reinterpret_cast<size_t*>
00427             (_M_vbp[_M_curr_index].first) - 1;
00428           
00429           _GLIBCXX_DEBUG_ASSERT(__index <= (long)_M_vbp.size() - 1);
00430         
00431           _M_last_bmap_in_block = _M_curr_bmap
00432             - ((_M_vbp[_M_curr_index].second 
00433                 - _M_vbp[_M_curr_index].first + 1) 
00434                / size_t(bits_per_block) - 1);
00435         }
00436     
00437         // Dangerous Function! Use with extreme care. Pass to this
00438         // function ONLY those values that are known to be correct,
00439         // otherwise this will mess up big time.
00440         void
00441         _M_set_internal_bitmap(size_t* __new_internal_marker) throw()
00442         { _M_curr_bmap = __new_internal_marker; }
00443     
00444         bool
00445         _M_finished() const throw()
00446         { return(_M_curr_bmap == 0); }
00447     
00448         _Bitmap_counter&
00449         operator++() throw()
00450         {
00451           if (_M_curr_bmap == _M_last_bmap_in_block)
00452             {
00453               if (++_M_curr_index == _M_vbp.size())
00454                 _M_curr_bmap = 0;
00455               else
00456                 this->_M_reset(_M_curr_index);
00457             }
00458           else
00459             --_M_curr_bmap;
00460           return *this;
00461         }
00462     
00463         size_t*
00464         _M_get() const throw()
00465         { return _M_curr_bmap; }
00466     
00467         pointer 
00468         _M_base() const throw()
00469         { return _M_vbp[_M_curr_index].first; }
00470 
00471         _Index_type
00472         _M_offset() const throw()
00473         {
00474           return size_t(bits_per_block)
00475             * ((reinterpret_cast<size_t*>(this->_M_base()) 
00476                 - _M_curr_bmap) - 1);
00477         }
00478     
00479         _Index_type
00480         _M_where() const throw()
00481         { return _M_curr_index; }
00482       };
00483 
00484     /** @brief  Mark a memory address as allocated by re-setting the
00485      *  corresponding bit in the bit-map.
00486      */
00487     inline void 
00488     __bit_allocate(size_t* __pbmap, size_t __pos) throw()
00489     {
00490       size_t __mask = 1 << __pos;
00491       __mask = ~__mask;
00492       *__pbmap &= __mask;
00493     }
00494   
00495     /** @brief  Mark a memory address as free by setting the
00496      *  corresponding bit in the bit-map.
00497      */
00498     inline void 
00499     __bit_free(size_t* __pbmap, size_t __pos) throw()
00500     {
00501       size_t __mask = 1 << __pos;
00502       *__pbmap |= __mask;
00503     }
00504 
00505   _GLIBCXX_END_NAMESPACE_VERSION
00506   } // namespace __detail
00507 
00508 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00509 
00510   /** @brief  Generic Version of the bsf instruction.
00511    */
00512   inline size_t 
00513   _Bit_scan_forward(size_t __num)
00514   { return static_cast<size_t>(__builtin_ctzl(__num)); }
00515 
00516   /** @class  free_list bitmap_allocator.h bitmap_allocator.h
00517    *
00518    *  @brief  The free list class for managing chunks of memory to be
00519    *  given to and returned by the bitmap_allocator.
00520    */
00521   class free_list
00522   {
00523   public:
00524     typedef size_t*                             value_type;
00525     typedef __detail::__mini_vector<value_type> vector_type;
00526     typedef vector_type::iterator               iterator;
00527     typedef __mutex                             __mutex_type;
00528 
00529   private:
00530     struct _LT_pointer_compare
00531     {
00532       bool
00533       operator()(const size_t* __pui, 
00534                  const size_t __cui) const throw()
00535       { return *__pui < __cui; }
00536     };
00537 
00538 #if defined __GTHREADS
00539     __mutex_type&
00540     _M_get_mutex()
00541     {
00542       static __mutex_type _S_mutex;
00543       return _S_mutex;
00544     }
00545 #endif
00546 
00547     vector_type&
00548     _M_get_free_list()
00549     {
00550       static vector_type _S_free_list;
00551       return _S_free_list;
00552     }
00553 
00554     /** @brief  Performs validation of memory based on their size.
00555      *
00556      *  @param  __addr The pointer to the memory block to be
00557      *  validated.
00558      *
00559      *  Validates the memory block passed to this function and
00560      *  appropriately performs the action of managing the free list of
00561      *  blocks by adding this block to the free list or deleting this
00562      *  or larger blocks from the free list.
00563      */
00564     void
00565     _M_validate(size_t* __addr) throw()
00566     {
00567       vector_type& __free_list = _M_get_free_list();
00568       const vector_type::size_type __max_size = 64;
00569       if (__free_list.size() >= __max_size)
00570         {
00571           // Ok, the threshold value has been reached.  We determine
00572           // which block to remove from the list of free blocks.
00573           if (*__addr >= *__free_list.back())
00574             {
00575               // Ok, the new block is greater than or equal to the
00576               // last block in the list of free blocks. We just free
00577               // the new block.
00578               ::operator delete(static_cast<void*>(__addr));
00579               return;
00580             }
00581           else
00582             {
00583               // Deallocate the last block in the list of free lists,
00584               // and insert the new one in its correct position.
00585               ::operator delete(static_cast<void*>(__free_list.back()));
00586               __free_list.pop_back();
00587             }
00588         }
00589           
00590       // Just add the block to the list of free lists unconditionally.
00591       iterator __temp = __detail::__lower_bound
00592         (__free_list.begin(), __free_list.end(), 
00593          *__addr, _LT_pointer_compare());
00594 
00595       // We may insert the new free list before _temp;
00596       __free_list.insert(__temp, __addr);
00597     }
00598 
00599     /** @brief  Decides whether the wastage of memory is acceptable for
00600      *  the current memory request and returns accordingly.
00601      *
00602      *  @param __block_size The size of the block available in the free
00603      *  list.
00604      *
00605      *  @param __required_size The required size of the memory block.
00606      *
00607      *  @return true if the wastage incurred is acceptable, else returns
00608      *  false.
00609      */
00610     bool 
00611     _M_should_i_give(size_t __block_size, 
00612                      size_t __required_size) throw()
00613     {
00614       const size_t __max_wastage_percentage = 36;
00615       if (__block_size >= __required_size && 
00616           (((__block_size - __required_size) * 100 / __block_size)
00617            < __max_wastage_percentage))
00618         return true;
00619       else
00620         return false;
00621     }
00622 
00623   public:
00624     /** @brief This function returns the block of memory to the
00625      *  internal free list.
00626      *
00627      *  @param  __addr The pointer to the memory block that was given
00628      *  by a call to the _M_get function.
00629      */
00630     inline void 
00631     _M_insert(size_t* __addr) throw()
00632     {
00633 #if defined __GTHREADS
00634       __scoped_lock __bfl_lock(_M_get_mutex());
00635 #endif
00636       // Call _M_validate to decide what should be done with
00637       // this particular free list.
00638       this->_M_validate(reinterpret_cast<size_t*>(__addr) - 1);
00639       // See discussion as to why this is 1!
00640     }
00641     
00642     /** @brief  This function gets a block of memory of the specified
00643      *  size from the free list.
00644      *
00645      *  @param  __sz The size in bytes of the memory required.
00646      *
00647      *  @return  A pointer to the new memory block of size at least
00648      *  equal to that requested.
00649      */
00650     size_t*
00651     _M_get(size_t __sz) throw(std::bad_alloc);
00652 
00653     /** @brief  This function just clears the internal Free List, and
00654      *  gives back all the memory to the OS.
00655      */
00656     void 
00657     _M_clear();
00658   };
00659 
00660 
00661   // Forward declare the class.
00662   template<typename _Tp> 
00663     class bitmap_allocator;
00664 
00665   // Specialize for void:
00666   template<>
00667     class bitmap_allocator<void>
00668     {
00669     public:
00670       typedef void*       pointer;
00671       typedef const void* const_pointer;
00672 
00673       // Reference-to-void members are impossible.
00674       typedef void  value_type;
00675       template<typename _Tp1>
00676         struct rebind
00677         {
00678           typedef bitmap_allocator<_Tp1> other;
00679         };
00680     };
00681 
00682   /**
00683    * @brief Bitmap Allocator, primary template.
00684    * @ingroup allocators
00685    */
00686   template<typename _Tp>
00687     class bitmap_allocator : private free_list
00688     {
00689     public:
00690       typedef size_t                    size_type;
00691       typedef ptrdiff_t                 difference_type;
00692       typedef _Tp*                      pointer;
00693       typedef const _Tp*                const_pointer;
00694       typedef _Tp&                      reference;
00695       typedef const _Tp&                const_reference;
00696       typedef _Tp                       value_type;
00697       typedef free_list::__mutex_type   __mutex_type;
00698 
00699       template<typename _Tp1>
00700         struct rebind
00701         {
00702           typedef bitmap_allocator<_Tp1> other;
00703         };
00704 
00705 #if __cplusplus >= 201103L
00706       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00707       // 2103. propagate_on_container_move_assignment
00708       typedef std::true_type propagate_on_container_move_assignment;
00709 #endif
00710 
00711     private:
00712       template<size_t _BSize, size_t _AlignSize>
00713         struct aligned_size
00714         {
00715           enum
00716             { 
00717               modulus = _BSize % _AlignSize,
00718               value = _BSize + (modulus ? _AlignSize - (modulus) : 0)
00719             };
00720         };
00721 
00722       struct _Alloc_block
00723       {
00724         char __M_unused[aligned_size<sizeof(value_type),
00725                         _BALLOC_ALIGN_BYTES>::value];
00726       };
00727 
00728 
00729       typedef typename std::pair<_Alloc_block*, _Alloc_block*> _Block_pair;
00730 
00731       typedef typename __detail::__mini_vector<_Block_pair> _BPVector;
00732       typedef typename _BPVector::iterator _BPiter;
00733 
00734       template<typename _Predicate>
00735         static _BPiter
00736         _S_find(_Predicate __p)
00737         {
00738           _BPiter __first = _S_mem_blocks.begin();
00739           while (__first != _S_mem_blocks.end() && !__p(*__first))
00740             ++__first;
00741           return __first;
00742         }
00743 
00744 #if defined _GLIBCXX_DEBUG
00745       // Complexity: O(lg(N)). Where, N is the number of block of size
00746       // sizeof(value_type).
00747       void 
00748       _S_check_for_free_blocks() throw()
00749       {
00750         typedef typename __detail::_Ffit_finder<_Alloc_block*> _FFF;
00751         _BPiter __bpi = _S_find(_FFF());
00752 
00753         _GLIBCXX_DEBUG_ASSERT(__bpi == _S_mem_blocks.end());
00754       }
00755 #endif
00756 
00757       /** @brief  Responsible for exponentially growing the internal
00758        *  memory pool.
00759        *
00760        *  @throw  std::bad_alloc. If memory can not be allocated.
00761        *
00762        *  Complexity: O(1), but internally depends upon the
00763        *  complexity of the function free_list::_M_get. The part where
00764        *  the bitmap headers are written has complexity: O(X),where X
00765        *  is the number of blocks of size sizeof(value_type) within
00766        *  the newly acquired block. Having a tight bound.
00767        */
00768       void 
00769       _S_refill_pool() throw(std::bad_alloc)
00770       {
00771 #if defined _GLIBCXX_DEBUG
00772         _S_check_for_free_blocks();
00773 #endif
00774 
00775         const size_t __num_bitmaps = (_S_block_size
00776                                       / size_t(__detail::bits_per_block));
00777         const size_t __size_to_allocate = sizeof(size_t) 
00778           + _S_block_size * sizeof(_Alloc_block) 
00779           + __num_bitmaps * sizeof(size_t);
00780 
00781         size_t* __temp =
00782           reinterpret_cast<size_t*>(this->_M_get(__size_to_allocate));
00783         *__temp = 0;
00784         ++__temp;
00785 
00786         // The Header information goes at the Beginning of the Block.
00787         _Block_pair __bp = 
00788           std::make_pair(reinterpret_cast<_Alloc_block*>
00789                          (__temp + __num_bitmaps), 
00790                          reinterpret_cast<_Alloc_block*>
00791                          (__temp + __num_bitmaps) 
00792                          + _S_block_size - 1);
00793         
00794         // Fill the Vector with this information.
00795         _S_mem_blocks.push_back(__bp);
00796 
00797         for (size_t __i = 0; __i < __num_bitmaps; ++__i)
00798           __temp[__i] = ~static_cast<size_t>(0); // 1 Indicates all Free.
00799 
00800         _S_block_size *= 2;
00801       }
00802 
00803       static _BPVector _S_mem_blocks;
00804       static size_t _S_block_size;
00805       static __detail::_Bitmap_counter<_Alloc_block*> _S_last_request;
00806       static typename _BPVector::size_type _S_last_dealloc_index;
00807 #if defined __GTHREADS
00808       static __mutex_type _S_mut;
00809 #endif
00810 
00811     public:
00812 
00813       /** @brief  Allocates memory for a single object of size
00814        *  sizeof(_Tp).
00815        *
00816        *  @throw  std::bad_alloc. If memory can not be allocated.
00817        *
00818        *  Complexity: Worst case complexity is O(N), but that
00819        *  is hardly ever hit. If and when this particular case is
00820        *  encountered, the next few cases are guaranteed to have a
00821        *  worst case complexity of O(1)!  That's why this function
00822        *  performs very well on average. You can consider this
00823        *  function to have a complexity referred to commonly as:
00824        *  Amortized Constant time.
00825        */
00826       pointer 
00827       _M_allocate_single_object() throw(std::bad_alloc)
00828       {
00829 #if defined __GTHREADS
00830         __scoped_lock __bit_lock(_S_mut);
00831 #endif
00832 
00833         // The algorithm is something like this: The last_request
00834         // variable points to the last accessed Bit Map. When such a
00835         // condition occurs, we try to find a free block in the
00836         // current bitmap, or succeeding bitmaps until the last bitmap
00837         // is reached. If no free block turns up, we resort to First
00838         // Fit method.
00839 
00840         // WARNING: Do not re-order the condition in the while
00841         // statement below, because it relies on C++'s short-circuit
00842         // evaluation. The return from _S_last_request->_M_get() will
00843         // NOT be dereference able if _S_last_request->_M_finished()
00844         // returns true. This would inevitably lead to a NULL pointer
00845         // dereference if tinkered with.
00846         while (_S_last_request._M_finished() == false
00847                && (*(_S_last_request._M_get()) == 0))
00848           _S_last_request.operator++();
00849 
00850         if (__builtin_expect(_S_last_request._M_finished() == true, false))
00851           {
00852             // Fall Back to First Fit algorithm.
00853             typedef typename __detail::_Ffit_finder<_Alloc_block*> _FFF;
00854             _FFF __fff;
00855             _BPiter __bpi = _S_find(__detail::_Functor_Ref<_FFF>(__fff));
00856 
00857             if (__bpi != _S_mem_blocks.end())
00858               {
00859                 // Search was successful. Ok, now mark the first bit from
00860                 // the right as 0, meaning Allocated. This bit is obtained
00861                 // by calling _M_get() on __fff.
00862                 size_t __nz_bit = _Bit_scan_forward(*__fff._M_get());
00863                 __detail::__bit_allocate(__fff._M_get(), __nz_bit);
00864 
00865                 _S_last_request._M_reset(__bpi - _S_mem_blocks.begin());
00866 
00867                 // Now, get the address of the bit we marked as allocated.
00868                 pointer __ret = reinterpret_cast<pointer>
00869                   (__bpi->first + __fff._M_offset() + __nz_bit);
00870                 size_t* __puse_count = 
00871                   reinterpret_cast<size_t*>
00872                   (__bpi->first) - (__detail::__num_bitmaps(*__bpi) + 1);
00873                 
00874                 ++(*__puse_count);
00875                 return __ret;
00876               }
00877             else
00878               {
00879                 // Search was unsuccessful. We Add more memory to the
00880                 // pool by calling _S_refill_pool().
00881                 _S_refill_pool();
00882 
00883                 // _M_Reset the _S_last_request structure to the first
00884                 // free block's bit map.
00885                 _S_last_request._M_reset(_S_mem_blocks.size() - 1);
00886 
00887                 // Now, mark that bit as allocated.
00888               }
00889           }
00890 
00891         // _S_last_request holds a pointer to a valid bit map, that
00892         // points to a free block in memory.
00893         size_t __nz_bit = _Bit_scan_forward(*_S_last_request._M_get());
00894         __detail::__bit_allocate(_S_last_request._M_get(), __nz_bit);
00895 
00896         pointer __ret = reinterpret_cast<pointer>
00897           (_S_last_request._M_base() + _S_last_request._M_offset() + __nz_bit);
00898 
00899         size_t* __puse_count = reinterpret_cast<size_t*>
00900           (_S_mem_blocks[_S_last_request._M_where()].first)
00901           - (__detail::
00902              __num_bitmaps(_S_mem_blocks[_S_last_request._M_where()]) + 1);
00903 
00904         ++(*__puse_count);
00905         return __ret;
00906       }
00907 
00908       /** @brief  Deallocates memory that belongs to a single object of
00909        *  size sizeof(_Tp).
00910        *
00911        *  Complexity: O(lg(N)), but the worst case is not hit
00912        *  often!  This is because containers usually deallocate memory
00913        *  close to each other and this case is handled in O(1) time by
00914        *  the deallocate function.
00915        */
00916       void 
00917       _M_deallocate_single_object(pointer __p) throw()
00918       {
00919 #if defined __GTHREADS
00920         __scoped_lock __bit_lock(_S_mut);
00921 #endif
00922         _Alloc_block* __real_p = reinterpret_cast<_Alloc_block*>(__p);
00923 
00924         typedef typename _BPVector::iterator _Iterator;
00925         typedef typename _BPVector::difference_type _Difference_type;
00926 
00927         _Difference_type __diff;
00928         long __displacement;
00929 
00930         _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0);
00931 
00932         __detail::_Inclusive_between<_Alloc_block*> __ibt(__real_p);
00933         if (__ibt(_S_mem_blocks[_S_last_dealloc_index]))
00934           {
00935             _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index
00936                                   <= _S_mem_blocks.size() - 1);
00937 
00938             // Initial Assumption was correct!
00939             __diff = _S_last_dealloc_index;
00940             __displacement = __real_p - _S_mem_blocks[__diff].first;
00941           }
00942         else
00943           {
00944             _Iterator _iter = _S_find(__ibt);
00945 
00946             _GLIBCXX_DEBUG_ASSERT(_iter != _S_mem_blocks.end());
00947 
00948             __diff = _iter - _S_mem_blocks.begin();
00949             __displacement = __real_p - _S_mem_blocks[__diff].first;
00950             _S_last_dealloc_index = __diff;
00951           }
00952 
00953         // Get the position of the iterator that has been found.
00954         const size_t __rotate = (__displacement
00955                                  % size_t(__detail::bits_per_block));
00956         size_t* __bitmapC = 
00957           reinterpret_cast<size_t*>
00958           (_S_mem_blocks[__diff].first) - 1;
00959         __bitmapC -= (__displacement / size_t(__detail::bits_per_block));
00960       
00961         __detail::__bit_free(__bitmapC, __rotate);
00962         size_t* __puse_count = reinterpret_cast<size_t*>
00963           (_S_mem_blocks[__diff].first)
00964           - (__detail::__num_bitmaps(_S_mem_blocks[__diff]) + 1);
00965         
00966         _GLIBCXX_DEBUG_ASSERT(*__puse_count != 0);
00967 
00968         --(*__puse_count);
00969 
00970         if (__builtin_expect(*__puse_count == 0, false))
00971           {
00972             _S_block_size /= 2;
00973           
00974             // We can safely remove this block.
00975             // _Block_pair __bp = _S_mem_blocks[__diff];
00976             this->_M_insert(__puse_count);
00977             _S_mem_blocks.erase(_S_mem_blocks.begin() + __diff);
00978 
00979             // Reset the _S_last_request variable to reflect the
00980             // erased block. We do this to protect future requests
00981             // after the last block has been removed from a particular
00982             // memory Chunk, which in turn has been returned to the
00983             // free list, and hence had been erased from the vector,
00984             // so the size of the vector gets reduced by 1.
00985             if ((_Difference_type)_S_last_request._M_where() >= __diff--)
00986               _S_last_request._M_reset(__diff); 
00987 
00988             // If the Index into the vector of the region of memory
00989             // that might hold the next address that will be passed to
00990             // deallocated may have been invalidated due to the above
00991             // erase procedure being called on the vector, hence we
00992             // try to restore this invariant too.
00993             if (_S_last_dealloc_index >= _S_mem_blocks.size())
00994               {
00995                 _S_last_dealloc_index =(__diff != -1 ? __diff : 0);
00996                 _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0);
00997               }
00998           }
00999       }
01000 
01001     public:
01002       bitmap_allocator() _GLIBCXX_USE_NOEXCEPT
01003       { }
01004 
01005       bitmap_allocator(const bitmap_allocator&) _GLIBCXX_USE_NOEXCEPT
01006       { }
01007 
01008       template<typename _Tp1>
01009         bitmap_allocator(const bitmap_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT
01010         { }
01011 
01012       ~bitmap_allocator() _GLIBCXX_USE_NOEXCEPT
01013       { }
01014 
01015       pointer 
01016       allocate(size_type __n)
01017       {
01018         if (__n > this->max_size())
01019           std::__throw_bad_alloc();
01020 
01021         if (__builtin_expect(__n == 1, true))
01022           return this->_M_allocate_single_object();
01023         else
01024           { 
01025             const size_type __b = __n * sizeof(value_type);
01026             return reinterpret_cast<pointer>(::operator new(__b));
01027           }
01028       }
01029 
01030       pointer 
01031       allocate(size_type __n, typename bitmap_allocator<void>::const_pointer)
01032       { return allocate(__n); }
01033 
01034       void 
01035       deallocate(pointer __p, size_type __n) throw()
01036       {
01037         if (__builtin_expect(__p != 0, true))
01038           {
01039             if (__builtin_expect(__n == 1, true))
01040               this->_M_deallocate_single_object(__p);
01041             else
01042 	      ::operator delete(__p);
01043           }
01044       }
01045 
01046       pointer 
01047       address(reference __r) const _GLIBCXX_NOEXCEPT
01048       { return std::__addressof(__r); }
01049 
01050       const_pointer 
01051       address(const_reference __r) const _GLIBCXX_NOEXCEPT
01052       { return std::__addressof(__r); }
01053 
01054       size_type 
01055       max_size() const _GLIBCXX_USE_NOEXCEPT
01056       { return size_type(-1) / sizeof(value_type); }
01057 
01058 #if __cplusplus >= 201103L
01059       template<typename _Up, typename... _Args>
01060         void
01061         construct(_Up* __p, _Args&&... __args)
01062         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
01063 
01064       template<typename _Up>
01065         void 
01066         destroy(_Up* __p)
01067         { __p->~_Up(); }
01068 #else
01069       void 
01070       construct(pointer __p, const_reference __data)
01071       { ::new((void *)__p) value_type(__data); }
01072 
01073       void 
01074       destroy(pointer __p)
01075       { __p->~value_type(); }
01076 #endif
01077     };
01078 
01079   template<typename _Tp1, typename _Tp2>
01080     bool 
01081     operator==(const bitmap_allocator<_Tp1>&, 
01082                const bitmap_allocator<_Tp2>&) throw()
01083     { return true; }
01084   
01085   template<typename _Tp1, typename _Tp2>
01086     bool 
01087     operator!=(const bitmap_allocator<_Tp1>&, 
01088                const bitmap_allocator<_Tp2>&) throw() 
01089   { return false; }
01090 
01091   // Static member definitions.
01092   template<typename _Tp>
01093     typename bitmap_allocator<_Tp>::_BPVector
01094     bitmap_allocator<_Tp>::_S_mem_blocks;
01095 
01096   template<typename _Tp>
01097     size_t bitmap_allocator<_Tp>::_S_block_size = 
01098     2 * size_t(__detail::bits_per_block);
01099 
01100   template<typename _Tp>
01101     typename bitmap_allocator<_Tp>::_BPVector::size_type 
01102     bitmap_allocator<_Tp>::_S_last_dealloc_index = 0;
01103 
01104   template<typename _Tp>
01105     __detail::_Bitmap_counter
01106       <typename bitmap_allocator<_Tp>::_Alloc_block*>
01107     bitmap_allocator<_Tp>::_S_last_request(_S_mem_blocks);
01108 
01109 #if defined __GTHREADS
01110   template<typename _Tp>
01111     typename bitmap_allocator<_Tp>::__mutex_type
01112     bitmap_allocator<_Tp>::_S_mut;
01113 #endif
01114 
01115 _GLIBCXX_END_NAMESPACE_VERSION
01116 } // namespace __gnu_cxx
01117 
01118 #endif 
01119