OSDN Git Service

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