OSDN Git Service

Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / bitmap_allocator.h
index 793f4dc..e5bfa18 100644 (file)
@@ -1,11 +1,12 @@
 // Bitmap Allocator. -*- C++ -*-
 
-// Copyright (C) 2004 Free Software Foundation, Inc.
+// Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
+// Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
 // terms of the GNU General Public License as published by the
-// Free Software Foundation; either version 2, or (at your option)
+// Free Software Foundation; either version 3, or (at your option)
 // any later version.
 
 // This library is distributed in the hope that it will be useful,
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 // GNU General Public License for more details.
 
-// You should have received a copy of the GNU General Public License along
-// with this library; see the file COPYING.  If not, write to the Free
-// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
-// USA.
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
 
-// As a special exception, you may use this file as part of a free software
-// library without restriction.  Specifically, if other files instantiate
-// templates or use macros or inline functions from this file, or you compile
-// this file and link it with other files to produce an executable, this
-// file does not by itself cause the resulting executable to be covered by
-// the GNU General Public License.  This exception does not however
-// invalidate any other reasons why the executable file might be covered by
-// the GNU General Public License.
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
 
 /** @file ext/bitmap_allocator.h
  *  This file is a GNU extension to the Standard C++ Library.
- *  You should only include this header if you are using GCC 3 or later.
  */
 
 #ifndef _BITMAP_ALLOCATOR_H
 #define _BITMAP_ALLOCATOR_H 1
 
-// For std::size_t, and ptrdiff_t.
-#include <cstddef>
-
-// For std::pair.
-#include <utility>
-
-// For greater_equal, and less_equal.
-#include <functional>
-
-// For operator new.
-#include <new>
-
-// For __gthread_mutex_t, __gthread_mutex_lock and __gthread_mutex_unlock.
-#include <bits/gthr.h>
-
-// Define this to enable error checking withing the allocator
-// itself(to debug the allocator itself).
-//#define _BALLOC_SANITY_CHECK
-
-// The constant in the expression below is the alignment required in
-// bytes.
+#include <cstddef> // For std::size_t, and ptrdiff_t.
+#include <bits/functexcept.h> // For __throw_bad_alloc().
+#include <utility> // For std::pair.
+#include <functional> // For greater_equal, and less_equal.
+#include <new> // For operator new.
+#include <debug/debug.h> // _GLIBCXX_DEBUG_ASSERT
+#include <ext/concurrence.h>
+#include <bits/move.h>
+
+/** @brief The constant in the expression below is the alignment
+ * required in bytes.
+ */
 #define _BALLOC_ALIGN_BYTES 8
 
-#if defined _BALLOC_SANITY_CHECK
-#include <cassert>
-#define _BALLOC_ASSERT(_EXPR) assert(_EXPR)
-#else
-#define _BALLOC_ASSERT(_EXPR)
-#endif
-
-
-namespace __gnu_cxx
-{
-#if defined __GTHREADS
-  namespace
-  {
-    // If true, then the application being compiled will be using
-    // threads, so use mutexes as a synchronization primitive, else do
-    // no use any synchronization primitives.
-    bool const __threads_enabled = __gthread_active_p();
-  }
-#endif
-
-#if defined __GTHREADS
-  // _Mutex is an OO-Wrapper for __gthread_mutex_t. It does not allow
-  // you to copy or assign an already initialized mutex. This is used
-  // merely as a convenience for the locking classes.
-  class _Mutex 
-  {
-    __gthread_mutex_t _M_mut;
-
-    // Prevent Copying and assignment.
-    _Mutex(_Mutex const&);
-    _Mutex& operator=(_Mutex const&);
-
-  public:
-    _Mutex()
-    {
-      if (__threads_enabled)
-       {
-#if !defined __GTHREAD_MUTEX_INIT
-         __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mut);
-#else
-         __gthread_mutex_t __mtemp = __GTHREAD_MUTEX_INIT;
-         _M_mut = __mtemp;
-#endif
-       }
-    }
-
-    ~_Mutex()
-    {
-      // Gthreads does not define a Mutex Destruction Function.
-    }
+_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
 
-    __gthread_mutex_t*
-    _M_get() { return &_M_mut; }
-  };
+  using std::size_t;
+  using std::ptrdiff_t;
 
-  // _Lock is a simple manual lokcing class which allows you to
-  // manually lock and unlock a mutex associated with the lock. There
-  // is not automatic locking or unlocking happening without the
-  // programmer's explicit instructions. This class unlocks the mutex
-  // ONLY if it has not been locked. However, this check does not
-  // apply for lokcing, and wayward use may cause dead-locks.
-  class _Lock 
+  namespace __detail
   {
-    _Mutex* _M_pmt;
-    bool _M_locked;
-
-    // Prevent Copying and assignment.
-    _Lock(_Lock const&);
-    _Lock& operator=(_Lock const&);
-
-  public:
-    _Lock(_Mutex* __mptr)
-    : _M_pmt(__mptr), _M_locked(false)
-    { }
-
-    void
-    _M_lock()
-    {
-      if (__threads_enabled)
-       {
-         _M_locked = true;
-         __gthread_mutex_lock(_M_pmt->_M_get());
-       }
-    }
-
-    void
-    _M_unlock()
-    {
-      if (__threads_enabled)
-       {
-         if (__builtin_expect(_M_locked, true))
-           {
-             __gthread_mutex_unlock(_M_pmt->_M_get());
-             _M_locked = false;
-           }
-       }
-    }
-    
-    ~_Lock() { }
-  };
-
-  // _Auto_Lock locks the associated mutex on construction, and
-  // unlocks on it's destruction. There are no checks performed, and
-  // this calss follows the RAII principle.
-  class _Auto_Lock 
-  {
-    _Mutex* _M_pmt;
-    // Prevent Copying and assignment.
-    _Auto_Lock(_Auto_Lock const&);
-    _Auto_Lock& operator=(_Auto_Lock const&);
-
-    void
-    _M_lock()
-    {
-      if (__threads_enabled)
-       __gthread_mutex_lock(_M_pmt->_M_get());
-    }
-
-    void
-    _M_unlock()
-    {
-      if (__threads_enabled)
-       __gthread_mutex_unlock(_M_pmt->_M_get());
-    }
-
-  public:
-    _Auto_Lock(_Mutex* __mptr) : _M_pmt(__mptr)
-    { this->_M_lock(); }
-
-    ~_Auto_Lock() { this->_M_unlock(); }
-  };
-#endif 
-
-  namespace balloc
-  {
-    // __mini_vector<> is to be used only for built-in types or
-    // PODs. It is a stripped down version of the full-fledged
-    // std::vector<>. Noteable differences are: 
-    // 
-    // 1. Not all accessor functions are present.
-    // 2. Used ONLY for PODs.
-    // 3. No Allocator template argument. Uses ::operator new() to get
-    // memory, and ::operator delete() to free it.
+    /** @class  __mini_vector bitmap_allocator.h bitmap_allocator.h
+     *
+     *  @brief  __mini_vector<> is a stripped down version of the
+     *  full-fledged std::vector<>.
+     *
+     *  It is to be used only for built-in types or PODs. Notable
+     *  differences are:
+     * 
+     *  @detail
+     *  1. Not all accessor functions are present.
+     *  2. Used ONLY for PODs.
+     *  3. No Allocator template argument. Uses ::operator new() to get
+     *  memory, and ::operator delete() to free it.
+     *  Caveat: The dtor does NOT free the memory allocated, so this a
+     *  memory-leaking vector!
+     */
     template<typename _Tp>
       class __mini_vector
       {
@@ -212,8 +78,8 @@ namespace __gnu_cxx
        typedef _Tp* pointer;
        typedef _Tp& reference;
        typedef const _Tp& const_reference;
-       typedef std::size_t size_type;
-       typedef std::ptrdiff_t difference_type;
+       typedef size_t size_type;
+       typedef ptrdiff_t difference_type;
        typedef pointer iterator;
 
       private:
@@ -370,13 +236,13 @@ namespace __gnu_cxx
       struct __mv_iter_traits<_Tp*>
       {
        typedef _Tp value_type;
-       typedef std::ptrdiff_t difference_type;
+       typedef ptrdiff_t difference_type;
       };
 
     enum 
       { 
-       bits_per_byte = 8, 
-       bits_per_block = sizeof(size_t) * bits_per_byte 
+       bits_per_byte = 8,
+       bits_per_block = sizeof(size_t) * size_t(bits_per_byte) 
       };
 
     template<typename _ForwardIterator, typename _Tp, typename _Compare>
@@ -419,15 +285,21 @@ namespace __gnu_cxx
        return __first;
       }
 
+    /** @brief The number of Blocks pointed to by the address pair
+     *  passed to the function.
+     */
     template<typename _AddrPair>
       inline size_t
       __num_blocks(_AddrPair __ap)
       { return (__ap.second - __ap.first) + 1; }
 
+    /** @brief The number of Bit-maps pointed to by the address pair
+     *  passed to the function.
+     */
     template<typename _AddrPair>
       inline size_t
       __num_bitmaps(_AddrPair __ap)
-      { return __num_blocks(__ap) / bits_per_block; }
+      { return __num_blocks(__ap) / size_t(bits_per_block); }
 
     // _Tp should be a pointer type.
     template<typename _Tp>
@@ -473,6 +345,11 @@ namespace __gnu_cxx
        { return _M_fref(__arg); }
       };
 
+    /** @class  _Ffit_finder bitmap_allocator.h bitmap_allocator.h
+     *
+     *  @brief  The class which acts as a predicate for applying the
+     *  first-fit memory allocation policy for the bitmap allocator.
+     */
     // _Tp should be a pointer type, and _Alloc is the Allocator for
     // the vector.
     template<typename _Tp>
@@ -480,7 +357,7 @@ namespace __gnu_cxx
       : public std::unary_function<typename std::pair<_Tp, _Tp>, bool>
       {
        typedef typename std::pair<_Tp, _Tp> _Block_pair;
-       typedef typename balloc::__mini_vector<_Block_pair> _BPVector;
+       typedef typename __detail::__mini_vector<_Block_pair> _BPVector;
        typedef typename _BPVector::difference_type _Counter_type;
 
        size_t* _M_pbitmap;
@@ -496,7 +373,7 @@ namespace __gnu_cxx
          // Set the _rover to the last physical location bitmap,
          // which is the bitmap which belongs to the first free
          // block. Thus, the bitmaps are in exact reverse order of
-         // the actual memory layout. So, we count down the bimaps,
+         // the actual memory layout. So, we count down the bitmaps,
          // which is the same as moving up the memory.
 
          // If the used count stored at the start of the Bit Map headers
@@ -504,11 +381,11 @@ namespace __gnu_cxx
          // store, then there is definitely no space for another single
          // object, so just return false.
          _Counter_type __diff = 
-           __gnu_cxx::balloc::__num_bitmaps(__bp);
+           __gnu_cxx::__detail::__num_bitmaps(__bp);
 
          if (*(reinterpret_cast<size_t*>
                (__bp.first) - (__diff + 1))
-             == __gnu_cxx::balloc::__num_blocks(__bp))
+             == __gnu_cxx::__detail::__num_blocks(__bp))
            return false;
 
          size_t* __rover = reinterpret_cast<size_t*>(__bp.first) - 1;
@@ -533,16 +410,21 @@ namespace __gnu_cxx
 
        _Counter_type
        _M_offset() const throw()
-       { return _M_data_offset * bits_per_block; }
+       { return _M_data_offset * size_t(bits_per_block); }
       };
 
 
-  
+    /** @class  _Bitmap_counter bitmap_allocator.h bitmap_allocator.h
+     *
+     *  @brief  The bitmap counter which acts as the bitmap
+     *  manipulator, and manages the bit-manipulation functions and
+     *  the searching and identification functions on the bit-map.
+     */
     // _Tp should be a pointer type.
     template<typename _Tp>
       class _Bitmap_counter
       {
-       typedef typename balloc::__mini_vector<typename std::pair<_Tp, _Tp> > 
+       typedef typename __detail::__mini_vector<typename std::pair<_Tp, _Tp> >
        _BPVector;
        typedef typename _BPVector::size_type _Index_type;
        typedef _Tp pointer;
@@ -573,12 +455,12 @@ namespace __gnu_cxx
          _M_curr_bmap = reinterpret_cast<size_t*>
            (_M_vbp[_M_curr_index].first) - 1;
          
-         _BALLOC_ASSERT(__index <= (long)_M_vbp.size() - 1);
+         _GLIBCXX_DEBUG_ASSERT(__index <= (long)_M_vbp.size() - 1);
        
          _M_last_bmap_in_block = _M_curr_bmap
            - ((_M_vbp[_M_curr_index].second 
                - _M_vbp[_M_curr_index].first + 1) 
-              / bits_per_block - 1);
+              / size_t(bits_per_block) - 1);
        }
     
        // Dangerous Function! Use with extreme care. Pass to this
@@ -618,7 +500,7 @@ namespace __gnu_cxx
        _Index_type
        _M_offset() const throw()
        {
-         return bits_per_block
+         return size_t(bits_per_block)
            * ((reinterpret_cast<size_t*>(this->_M_base()) 
                - _M_curr_bmap) - 1);
        }
@@ -628,6 +510,9 @@ namespace __gnu_cxx
        { return _M_curr_index; }
       };
 
+    /** @brief  Mark a memory address as allocated by re-setting the
+     *  corresponding bit in the bit-map.
+     */
     inline void 
     __bit_allocate(size_t* __pbmap, size_t __pos) throw()
     {
@@ -636,25 +521,37 @@ namespace __gnu_cxx
       *__pbmap &= __mask;
     }
   
+    /** @brief  Mark a memory address as free by setting the
+     *  corresponding bit in the bit-map.
+     */
     inline void 
     __bit_free(size_t* __pbmap, size_t __pos) throw()
     {
       size_t __mask = 1 << __pos;
       *__pbmap |= __mask;
     }
-  } // namespace balloc
+  } // namespace __detail
 
-  // Generic Version of the bsf instruction.
+  /** @brief  Generic Version of the bsf instruction.
+   */
   inline size_t 
   _Bit_scan_forward(size_t __num)
   { return static_cast<size_t>(__builtin_ctzl(__num)); }
 
+  /** @class  free_list bitmap_allocator.h bitmap_allocator.h
+   *
+   *  @brief  The free list class for managing chunks of memory to be
+   *  given to and returned by the bitmap_allocator.
+   */
   class free_list
   {
-    typedef size_t* value_type;
-    typedef balloc::__mini_vector<value_type> vector_type;
-    typedef vector_type::iterator iterator;
+  public:
+    typedef size_t*                            value_type;
+    typedef __detail::__mini_vector<value_type> vector_type;
+    typedef vector_type::iterator              iterator;
+    typedef __mutex                            __mutex_type;
 
+  private:
     struct _LT_pointer_compare
     {
       bool
@@ -663,20 +560,42 @@ namespace __gnu_cxx
       { return *__pui < __cui; }
     };
 
-#if defined __GTHREADS 
-    static _Mutex _S_bfl_mutex;
+#if defined __GTHREADS
+    __mutex_type&
+    _M_get_mutex()
+    {
+      static __mutex_type _S_mutex;
+      return _S_mutex;
+    }
 #endif
-    static vector_type _S_free_list;
-    
+
+    vector_type&
+    _M_get_free_list()
+    {
+      static vector_type _S_free_list;
+      return _S_free_list;
+    }
+
+    /** @brief  Performs validation of memory based on their size.
+     *
+     *  @param  __addr The pointer to the memory block to be
+     *  validated.
+     *
+     *  @detail  Validates the memory block passed to this function and
+     *  appropriately performs the action of managing the free list of
+     *  blocks by adding this block to the free list or deleting this
+     *  or larger blocks from the free list.
+     */
     void
     _M_validate(size_t* __addr) throw()
     {
+      vector_type& __free_list = _M_get_free_list();
       const vector_type::size_type __max_size = 64;
-      if (_S_free_list.size() >= __max_size)
+      if (__free_list.size() >= __max_size)
        {
          // Ok, the threshold value has been reached.  We determine
          // which block to remove from the list of free blocks.
-         if (*__addr >= *_S_free_list.back())
+         if (*__addr >= *__free_list.back())
            {
              // Ok, the new block is greater than or equal to the
              // last block in the list of free blocks. We just free
@@ -687,21 +606,32 @@ namespace __gnu_cxx
          else
            {
              // Deallocate the last block in the list of free lists,
-             // and insert the new one in it's correct position.
-             ::operator delete(static_cast<void*>(_S_free_list.back()));
-             _S_free_list.pop_back();
+             // and insert the new one in its correct position.
+             ::operator delete(static_cast<void*>(__free_list.back()));
+             __free_list.pop_back();
            }
        }
          
       // Just add the block to the list of free lists unconditionally.
-      iterator __temp = __gnu_cxx::balloc::__lower_bound
-       (_S_free_list.begin(), _S_free_list.end(), 
+      iterator __temp = __gnu_cxx::__detail::__lower_bound
+       (__free_list.begin(), __free_list.end(), 
         *__addr, _LT_pointer_compare());
 
       // We may insert the new free list before _temp;
-      _S_free_list.insert(__temp, __addr);
+      __free_list.insert(__temp, __addr);
     }
 
+    /** @brief  Decides whether the wastage of memory is acceptable for
+     *  the current memory request and returns accordingly.
+     *
+     *  @param __block_size The size of the block available in the free
+     *  list.
+     *
+     *  @param __required_size The required size of the memory block.
+     *
+     *  @return true if the wastage incurred is acceptable, else returns
+     *  false.
+     */
     bool 
     _M_should_i_give(size_t __block_size, 
                     size_t __required_size) throw()
@@ -716,11 +646,17 @@ namespace __gnu_cxx
     }
 
   public:
+    /** @brief This function returns the block of memory to the
+     *  internal free list.
+     *
+     *  @param  __addr The pointer to the memory block that was given
+     *  by a call to the _M_get function.
+     */
     inline void 
     _M_insert(size_t* __addr) throw()
     {
 #if defined __GTHREADS
-      _Auto_Lock __bfl_lock(&_S_bfl_mutex);
+      __gnu_cxx::__scoped_lock __bfl_lock(_M_get_mutex());
 #endif
       // Call _M_validate to decide what should be done with
       // this particular free list.
@@ -728,11 +664,20 @@ namespace __gnu_cxx
       // See discussion as to why this is 1!
     }
     
+    /** @brief  This function gets a block of memory of the specified
+     *  size from the free list.
+     *
+     *  @param  __sz The size in bytes of the memory required.
+     *
+     *  @return  A pointer to the new memory block of size at least
+     *  equal to that requested.
+     */
     size_t*
     _M_get(size_t __sz) throw(std::bad_alloc);
 
-    // This function just clears the internal Free List, and gives back
-    // all the memory to the OS.
+    /** @brief  This function just clears the internal Free List, and
+     *  gives back all the memory to the OS.
+     */
     void 
     _M_clear();
   };
@@ -759,17 +704,23 @@ namespace __gnu_cxx
        };
     };
 
+  /**
+   * @brief Bitmap Allocator, primary template.
+   * @ingroup allocators
+   */
   template<typename _Tp>
     class bitmap_allocator : private free_list
     {
     public:
-      typedef std::size_t    size_type;
-      typedef std::ptrdiff_t difference_type;
-      typedef _Tp*        pointer;
-      typedef const _Tp*  const_pointer;
-      typedef _Tp&        reference;
-      typedef const _Tp&  const_reference;
-      typedef _Tp         value_type;
+      typedef size_t                   size_type;
+      typedef ptrdiff_t                difference_type;
+      typedef _Tp*                     pointer;
+      typedef const _Tp*               const_pointer;
+      typedef _Tp&                     reference;
+      typedef const _Tp&               const_reference;
+      typedef _Tp                      value_type;
+      typedef free_list::__mutex_type  __mutex_type;
+
       template<typename _Tp1>
         struct rebind
        {
@@ -797,41 +748,47 @@ namespace __gnu_cxx
       typedef typename std::pair<_Alloc_block*, _Alloc_block*> _Block_pair;
 
       typedef typename 
-      balloc::__mini_vector<_Block_pair> _BPVector;
+      __detail::__mini_vector<_Block_pair> _BPVector;
 
-#if defined _BALLOC_SANITY_CHECK
+#if defined _GLIBCXX_DEBUG
       // Complexity: O(lg(N)). Where, N is the number of block of size
       // sizeof(value_type).
       void 
       _S_check_for_free_blocks() throw()
       {
        typedef typename 
-         __gnu_cxx::balloc::_Ffit_finder<_Alloc_block*> _FFF;
+         __gnu_cxx::__detail::_Ffit_finder<_Alloc_block*> _FFF;
        _FFF __fff;
        typedef typename _BPVector::iterator _BPiter;
        _BPiter __bpi = 
-         __gnu_cxx::balloc::__find_if
+         __gnu_cxx::__detail::__find_if
          (_S_mem_blocks.begin(), _S_mem_blocks.end(), 
-          __gnu_cxx::balloc::_Functor_Ref<_FFF>(__fff));
+          __gnu_cxx::__detail::_Functor_Ref<_FFF>(__fff));
 
-       _BALLOC_ASSERT(__bpi == _S_mem_blocks.end());
+       _GLIBCXX_DEBUG_ASSERT(__bpi == _S_mem_blocks.end());
       }
 #endif
 
-      // Complexity: O(1), but internally depends upon the complexity
-      // of the function free_list::_M_get. The
-      // part where the bitmap headers are written is of worst case
-      // complexity: O(X),where X is the number of blocks of size
-      // sizeof(value_type) within the newly acquired block. Having a
-      // tight bound.
+      /** @brief  Responsible for exponentially growing the internal
+       *  memory pool.
+       *
+       *  @throw  std::bad_alloc. If memory can not be allocated.
+       *
+       *  @detail  Complexity: O(1), but internally depends upon the
+       *  complexity of the function free_list::_M_get. The part where
+       *  the bitmap headers are written has complexity: O(X),where X
+       *  is the number of blocks of size sizeof(value_type) within
+       *  the newly acquired block. Having a tight bound.
+       */
       void 
       _S_refill_pool() throw(std::bad_alloc)
       {
-#if defined _BALLOC_SANITY_CHECK
+#if defined _GLIBCXX_DEBUG
        _S_check_for_free_blocks();
 #endif
 
-       const size_t __num_bitmaps = _S_block_size / balloc::bits_per_block;
+       const size_t __num_bitmaps = (_S_block_size
+                                     / size_t(__detail::bits_per_block));
        const size_t __size_to_allocate = sizeof(size_t) 
          + _S_block_size * sizeof(_Alloc_block) 
          + __num_bitmaps * sizeof(size_t);
@@ -865,27 +822,33 @@ namespace __gnu_cxx
 
       static _BPVector _S_mem_blocks;
       static size_t _S_block_size;
-      static __gnu_cxx::balloc::
+      static __gnu_cxx::__detail::
       _Bitmap_counter<_Alloc_block*> _S_last_request;
       static typename _BPVector::size_type _S_last_dealloc_index;
 #if defined __GTHREADS
-      static _Mutex _S_mut;
+      static __mutex_type _S_mut;
 #endif
 
     public:
 
-      // Complexity: Worst case complexity is O(N), but that is hardly
-      // ever hit. if and when this particular case is encountered,
-      // the next few cases are guaranteed to have a worst case
-      // complexity of O(1)!  That's why this function performs very
-      // well on the average. you can consider this function to be
-      // having a complexity referred to commonly as: Amortized
-      // Constant time.
+      /** @brief  Allocates memory for a single object of size
+       *  sizeof(_Tp).
+       *
+       *  @throw  std::bad_alloc. If memory can not be allocated.
+       *
+       *  @detail  Complexity: Worst case complexity is O(N), but that
+       *  is hardly ever hit. If and when this particular case is
+       *  encountered, the next few cases are guaranteed to have a
+       *  worst case complexity of O(1)!  That's why this function
+       *  performs very well on average. You can consider this
+       *  function to have a complexity referred to commonly as:
+       *  Amortized Constant time.
+       */
       pointer 
       _M_allocate_single_object() throw(std::bad_alloc)
       {
 #if defined __GTHREADS
-       _Auto_Lock __bit_lock(&_S_mut);
+       __gnu_cxx::__scoped_lock __bit_lock(_S_mut);
 #endif
 
        // The algorithm is something like this: The last_request
@@ -911,13 +874,13 @@ namespace __gnu_cxx
          {
            // Fall Back to First Fit algorithm.
            typedef typename 
-             __gnu_cxx::balloc::_Ffit_finder<_Alloc_block*> _FFF;
+             __gnu_cxx::__detail::_Ffit_finder<_Alloc_block*> _FFF;
            _FFF __fff;
            typedef typename _BPVector::iterator _BPiter;
            _BPiter __bpi = 
-             __gnu_cxx::balloc::__find_if
+             __gnu_cxx::__detail::__find_if
              (_S_mem_blocks.begin(), _S_mem_blocks.end(), 
-              __gnu_cxx::balloc::_Functor_Ref<_FFF>(__fff));
+              __gnu_cxx::__detail::_Functor_Ref<_FFF>(__fff));
 
            if (__bpi != _S_mem_blocks.end())
              {
@@ -925,7 +888,7 @@ namespace __gnu_cxx
                // the right as 0, meaning Allocated. This bit is obtained
                // by calling _M_get() on __fff.
                size_t __nz_bit = _Bit_scan_forward(*__fff._M_get());
-               balloc::__bit_allocate(__fff._M_get(), __nz_bit);
+               __detail::__bit_allocate(__fff._M_get(), __nz_bit);
 
                _S_last_request._M_reset(__bpi - _S_mem_blocks.begin());
 
@@ -935,7 +898,7 @@ namespace __gnu_cxx
                size_t* __puse_count = 
                  reinterpret_cast<size_t*>
                  (__bpi->first) 
-                 - (__gnu_cxx::balloc::__num_bitmaps(*__bpi) + 1);
+                 - (__gnu_cxx::__detail::__num_bitmaps(*__bpi) + 1);
                
                ++(*__puse_count);
                return __ret;
@@ -957,28 +920,33 @@ namespace __gnu_cxx
        // _S_last_request holds a pointer to a valid bit map, that
        // points to a free block in memory.
        size_t __nz_bit = _Bit_scan_forward(*_S_last_request._M_get());
-       balloc::__bit_allocate(_S_last_request._M_get(), __nz_bit);
+       __detail::__bit_allocate(_S_last_request._M_get(), __nz_bit);
 
        pointer __ret = reinterpret_cast<pointer>
          (_S_last_request._M_base() + _S_last_request._M_offset() + __nz_bit);
 
        size_t* __puse_count = reinterpret_cast<size_t*>
          (_S_mem_blocks[_S_last_request._M_where()].first)
-         - (__gnu_cxx::balloc::
+         - (__gnu_cxx::__detail::
             __num_bitmaps(_S_mem_blocks[_S_last_request._M_where()]) + 1);
 
        ++(*__puse_count);
        return __ret;
       }
 
-      // Complexity: O(lg(N)), but the worst case is hit quite often!
-      // I need to do something about this. I'll be able to work on
-      // it, only when I have some solid figures from a few real apps.
+      /** @brief  Deallocates memory that belongs to a single object of
+       *  size sizeof(_Tp).
+       *
+       *  @detail  Complexity: O(lg(N)), but the worst case is not hit
+       *  often!  This is because containers usually deallocate memory
+       *  close to each other and this case is handled in O(1) time by
+       *  the deallocate function.
+       */
       void 
       _M_deallocate_single_object(pointer __p) throw()
       {
 #if defined __GTHREADS
-       _Auto_Lock __bit_lock(&_S_mut);
+       __gnu_cxx::__scoped_lock __bit_lock(_S_mut);
 #endif
        _Alloc_block* __real_p = reinterpret_cast<_Alloc_block*>(__p);
 
@@ -988,14 +956,14 @@ namespace __gnu_cxx
        _Difference_type __diff;
        long __displacement;
 
-       _BALLOC_ASSERT(_S_last_dealloc_index >= 0);
+       _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0);
 
        
-       if (__gnu_cxx::balloc::_Inclusive_between<_Alloc_block*>
-           (__real_p)
-           (_S_mem_blocks[_S_last_dealloc_index]))
+       if (__gnu_cxx::__detail::_Inclusive_between<_Alloc_block*>
+           (__real_p) (_S_mem_blocks[_S_last_dealloc_index]))
          {
-           _BALLOC_ASSERT(_S_last_dealloc_index <= _S_mem_blocks.size() - 1);
+           _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index
+                                 <= _S_mem_blocks.size() - 1);
 
            // Initial Assumption was correct!
            __diff = _S_last_dealloc_index;
@@ -1003,14 +971,13 @@ namespace __gnu_cxx
          }
        else
          {
-           _Iterator _iter = 
-             __gnu_cxx::balloc::
+           _Iterator _iter = __gnu_cxx::__detail::
              __find_if(_S_mem_blocks.begin(), 
                        _S_mem_blocks.end(), 
-                       __gnu_cxx::balloc::
+                       __gnu_cxx::__detail::
                        _Inclusive_between<_Alloc_block*>(__real_p));
 
-           _BALLOC_ASSERT(_iter != _S_mem_blocks.end());
+           _GLIBCXX_DEBUG_ASSERT(_iter != _S_mem_blocks.end());
 
            __diff = _iter - _S_mem_blocks.begin();
            __displacement = __real_p - _S_mem_blocks[__diff].first;
@@ -1018,18 +985,19 @@ namespace __gnu_cxx
          }
 
        // Get the position of the iterator that has been found.
-       const size_t __rotate = __displacement % balloc::bits_per_block;
+       const size_t __rotate = (__displacement
+                                % size_t(__detail::bits_per_block));
        size_t* __bitmapC = 
          reinterpret_cast<size_t*>
          (_S_mem_blocks[__diff].first) - 1;
-       __bitmapC -= (__displacement / balloc::bits_per_block);
+       __bitmapC -= (__displacement / size_t(__detail::bits_per_block));
       
-       balloc::__bit_free(__bitmapC, __rotate);
+       __detail::__bit_free(__bitmapC, __rotate);
        size_t* __puse_count = reinterpret_cast<size_t*>
          (_S_mem_blocks[__diff].first)
-         - (__gnu_cxx::balloc::__num_bitmaps(_S_mem_blocks[__diff]) + 1);
+         - (__gnu_cxx::__detail::__num_bitmaps(_S_mem_blocks[__diff]) + 1);
        
-       _BALLOC_ASSERT(*__puse_count != 0);
+       _GLIBCXX_DEBUG_ASSERT(*__puse_count != 0);
 
        --(*__puse_count);
 
@@ -1059,7 +1027,7 @@ namespace __gnu_cxx
            if (_S_last_dealloc_index >= _S_mem_blocks.size())
              {
                _S_last_dealloc_index =(__diff != -1 ? __diff : 0);
-               _BALLOC_ASSERT(_S_last_dealloc_index >= 0);
+               _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0);
              }
          }
       }
@@ -1078,12 +1046,12 @@ namespace __gnu_cxx
       ~bitmap_allocator() throw()
       { }
 
-      // Complexity: O(1), but internally the complexity depends upon the
-      // complexity of the function(s) _S_allocate_single_object and
-      // operator new.
       pointer 
       allocate(size_type __n)
       {
+       if (__builtin_expect(__n > this->max_size(), false))
+         std::__throw_bad_alloc();
+
        if (__builtin_expect(__n == 1, true))
          return this->_M_allocate_single_object();
        else
@@ -1119,11 +1087,18 @@ namespace __gnu_cxx
 
       size_type 
       max_size() const throw()
-      { return (size_type()-1)/sizeof(value_type); }
+      { return size_type(-1) / sizeof(value_type); }
 
       void 
       construct(pointer __p, const_reference __data)
-      { ::new(__p) value_type(__data); }
+      { ::new((void *)__p) value_type(__data); }
+
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+      template<typename... _Args>
+        void
+        construct(pointer __p, _Args&&... __args)
+       { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
+#endif
 
       void 
       destroy(pointer __p)
@@ -1149,26 +1124,24 @@ namespace __gnu_cxx
 
   template<typename _Tp>
     size_t bitmap_allocator<_Tp>::_S_block_size = 
-    2 * balloc::bits_per_block;
+    2 * size_t(__detail::bits_per_block);
 
   template<typename _Tp>
     typename __gnu_cxx::bitmap_allocator<_Tp>::_BPVector::size_type 
     bitmap_allocator<_Tp>::_S_last_dealloc_index = 0;
 
   template<typename _Tp>
-    __gnu_cxx::balloc::_Bitmap_counter 
+    __gnu_cxx::__detail::_Bitmap_counter 
   <typename bitmap_allocator<_Tp>::_Alloc_block*>
     bitmap_allocator<_Tp>::_S_last_request(_S_mem_blocks);
 
 #if defined __GTHREADS
   template<typename _Tp>
-    __gnu_cxx::_Mutex
+    typename bitmap_allocator<_Tp>::__mutex_type
     bitmap_allocator<_Tp>::_S_mut;
 #endif
 
-
-}
+_GLIBCXX_END_NAMESPACE
 
 #endif 
 
-//  LocalWords:  namespace GTHREADS bool const gthread endif Mutex mutex