OSDN Git Service

2009-10-08 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / bitmap_allocator.h
index 7f5466a..d91d9aa 100644 (file)
@@ -1,11 +1,12 @@
 // Bitmap Allocator. -*- C++ -*-
 
-// Copyright (C) 2004, 2005, 2006, 2007, 2008 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
-// 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.
@@ -108,20 +104,8 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
        // insert(iterator, const_reference), erase(iterator),
        // begin(), end(), back(), operator[].
 
-       __mini_vector() : _M_start(0), _M_finish(0), 
-                         _M_end_of_storage(0)
-       { }
-
-#if 0
-       ~__mini_vector()
-       {
-         if (this->_M_start)
-           {
-             this->deallocate(this->_M_start, this->_M_end_of_storage 
-                              - this->_M_start);
-           }
-       }
-#endif
+       __mini_vector()
+        : _M_start(0), _M_finish(0), _M_end_of_storage(0) { }
 
        size_type
        size() const throw()
@@ -280,15 +264,6 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
        return __first;
       }
 
-    template<typename _InputIterator, typename _Predicate>
-      inline _InputIterator
-      __find_if(_InputIterator __first, _InputIterator __last, _Predicate __p)
-      {
-       while (__first != __last && !__p(*__first))
-         ++__first;
-       return __first;
-      }
-
     /** @brief The number of Blocks pointed to by the address pair
      *  passed to the function.
      */
@@ -384,12 +359,10 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
          // is equal to the number of Objects that the current Block can
          // store, then there is definitely no space for another single
          // object, so just return false.
-         _Counter_type __diff = 
-           __gnu_cxx::__detail::__num_bitmaps(__bp);
+         _Counter_type __diff = __detail::__num_bitmaps(__bp);
 
          if (*(reinterpret_cast<size_t*>
-               (__bp.first) - (__diff + 1))
-             == __gnu_cxx::__detail::__num_blocks(__bp))
+               (__bp.first) - (__diff + 1)) == __detail::__num_blocks(__bp))
            return false;
 
          size_t* __rover = reinterpret_cast<size_t*>(__bp.first) - 1;
@@ -406,7 +379,6 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
            }
          return false;
        }
-
     
        size_t*
        _M_get() const throw()
@@ -417,7 +389,6 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
        { 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
@@ -428,11 +399,11 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
     template<typename _Tp>
       class _Bitmap_counter
       {
-       typedef typename __detail::__mini_vector<typename std::pair<_Tp, _Tp> >
-       _BPVector;
+       typedef typename
+       __detail::__mini_vector<typename std::pair<_Tp, _Tp> > _BPVector;
        typedef typename _BPVector::size_type _Index_type;
        typedef _Tp pointer;
-    
+
        _BPVector& _M_vbp;
        size_t* _M_curr_bmap;
        size_t* _M_last_bmap_in_block;
@@ -549,11 +520,13 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    */
   class free_list
   {
+  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
@@ -615,7 +588,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
        }
          
       // Just add the block to the list of free lists unconditionally.
-      iterator __temp = __gnu_cxx::__detail::__lower_bound
+      iterator __temp = __detail::__lower_bound
        (__free_list.begin(), __free_list.end(), 
         *__addr, _LT_pointer_compare());
 
@@ -658,7 +631,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
     _M_insert(size_t* __addr) throw()
     {
 #if defined __GTHREADS
-      __gnu_cxx::__scoped_lock __bfl_lock(_M_get_mutex());
+      __scoped_lock __bfl_lock(_M_get_mutex());
 #endif
       // Call _M_validate to decide what should be done with
       // this particular free list.
@@ -706,7 +679,10 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
        };
     };
 
-  /// Primary template
+  /**
+   * @brief Bitmap Allocator, primary template.
+   * @ingroup allocators
+   */
   template<typename _Tp>
     class bitmap_allocator : private free_list
     {
@@ -746,8 +722,18 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
 
       typedef typename std::pair<_Alloc_block*, _Alloc_block*> _Block_pair;
 
-      typedef typename 
-      __detail::__mini_vector<_Block_pair> _BPVector;
+      typedef typename __detail::__mini_vector<_Block_pair> _BPVector;
+      typedef typename _BPVector::iterator _BPiter;
+
+      template<typename _Predicate>
+        static _BPiter
+        _S_find(_Predicate __p)
+        {
+         _BPiter __first = _S_mem_blocks.begin();
+         while (__first != _S_mem_blocks.end() && !__p(*__first))
+           ++__first;
+         return __first;
+       }
 
 #if defined _GLIBCXX_DEBUG
       // Complexity: O(lg(N)). Where, N is the number of block of size
@@ -755,14 +741,8 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
       void 
       _S_check_for_free_blocks() throw()
       {
-       typedef typename 
-         __gnu_cxx::__detail::_Ffit_finder<_Alloc_block*> _FFF;
-       _FFF __fff;
-       typedef typename _BPVector::iterator _BPiter;
-       _BPiter __bpi = 
-         __gnu_cxx::__detail::__find_if
-         (_S_mem_blocks.begin(), _S_mem_blocks.end(), 
-          __gnu_cxx::__detail::_Functor_Ref<_FFF>(__fff));
+       typedef typename __detail::_Ffit_finder<_Alloc_block*> _FFF;
+       _BPiter __bpi = _S_find(_FFF());
 
        _GLIBCXX_DEBUG_ASSERT(__bpi == _S_mem_blocks.end());
       }
@@ -792,9 +772,8 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
          + _S_block_size * sizeof(_Alloc_block) 
          + __num_bitmaps * sizeof(size_t);
 
-       size_t* __temp = 
-         reinterpret_cast<size_t*>
-         (this->_M_get(__size_to_allocate));
+       size_t* __temp =
+         reinterpret_cast<size_t*>(this->_M_get(__size_to_allocate));
        *__temp = 0;
        ++__temp;
 
@@ -809,20 +788,15 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
        // Fill the Vector with this information.
        _S_mem_blocks.push_back(__bp);
 
-       size_t __bit_mask = 0; // 0 Indicates all Allocated.
-       __bit_mask = ~__bit_mask; // 1 Indicates all Free.
-
        for (size_t __i = 0; __i < __num_bitmaps; ++__i)
-         __temp[__i] = __bit_mask;
+         __temp[__i] = ~static_cast<size_t>(0); // 1 Indicates all Free.
 
        _S_block_size *= 2;
       }
 
-
       static _BPVector _S_mem_blocks;
       static size_t _S_block_size;
-      static __gnu_cxx::__detail::
-      _Bitmap_counter<_Alloc_block*> _S_last_request;
+      static __detail::_Bitmap_counter<_Alloc_block*> _S_last_request;
       static typename _BPVector::size_type _S_last_dealloc_index;
 #if defined __GTHREADS
       static __mutex_type _S_mut;
@@ -847,7 +821,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
       _M_allocate_single_object() throw(std::bad_alloc)
       {
 #if defined __GTHREADS
-       __gnu_cxx::__scoped_lock __bit_lock(_S_mut);
+       __scoped_lock __bit_lock(_S_mut);
 #endif
 
        // The algorithm is something like this: The last_request
@@ -865,21 +839,14 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
        // dereference if tinkered with.
        while (_S_last_request._M_finished() == false
               && (*(_S_last_request._M_get()) == 0))
-         {
-           _S_last_request.operator++();
-         }
+         _S_last_request.operator++();
 
        if (__builtin_expect(_S_last_request._M_finished() == true, false))
          {
            // Fall Back to First Fit algorithm.
-           typedef typename 
-             __gnu_cxx::__detail::_Ffit_finder<_Alloc_block*> _FFF;
+           typedef typename __detail::_Ffit_finder<_Alloc_block*> _FFF;
            _FFF __fff;
-           typedef typename _BPVector::iterator _BPiter;
-           _BPiter __bpi = 
-             __gnu_cxx::__detail::__find_if
-             (_S_mem_blocks.begin(), _S_mem_blocks.end(), 
-              __gnu_cxx::__detail::_Functor_Ref<_FFF>(__fff));
+           _BPiter __bpi = _S_find(__detail::_Functor_Ref<_FFF>(__fff));
 
            if (__bpi != _S_mem_blocks.end())
              {
@@ -896,8 +863,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
                  (__bpi->first + __fff._M_offset() + __nz_bit);
                size_t* __puse_count = 
                  reinterpret_cast<size_t*>
-                 (__bpi->first) 
-                 - (__gnu_cxx::__detail::__num_bitmaps(*__bpi) + 1);
+                 (__bpi->first) - (__detail::__num_bitmaps(*__bpi) + 1);
                
                ++(*__puse_count);
                return __ret;
@@ -926,7 +892,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
 
        size_t* __puse_count = reinterpret_cast<size_t*>
          (_S_mem_blocks[_S_last_request._M_where()].first)
-         - (__gnu_cxx::__detail::
+         - (__detail::
             __num_bitmaps(_S_mem_blocks[_S_last_request._M_where()]) + 1);
 
        ++(*__puse_count);
@@ -945,7 +911,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
       _M_deallocate_single_object(pointer __p) throw()
       {
 #if defined __GTHREADS
-       __gnu_cxx::__scoped_lock __bit_lock(_S_mut);
+       __scoped_lock __bit_lock(_S_mut);
 #endif
        _Alloc_block* __real_p = reinterpret_cast<_Alloc_block*>(__p);
 
@@ -957,9 +923,8 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
 
        _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0);
 
-       
-       if (__gnu_cxx::__detail::_Inclusive_between<_Alloc_block*>
-           (__real_p) (_S_mem_blocks[_S_last_dealloc_index]))
+       __detail::_Inclusive_between<_Alloc_block*> __ibt(__real_p);
+       if (__ibt(_S_mem_blocks[_S_last_dealloc_index]))
          {
            _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index
                                  <= _S_mem_blocks.size() - 1);
@@ -970,11 +935,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
          }
        else
          {
-           _Iterator _iter = __gnu_cxx::__detail::
-             __find_if(_S_mem_blocks.begin(), 
-                       _S_mem_blocks.end(), 
-                       __gnu_cxx::__detail::
-                       _Inclusive_between<_Alloc_block*>(__real_p));
+           _Iterator _iter = _S_find(__ibt);
 
            _GLIBCXX_DEBUG_ASSERT(_iter != _S_mem_blocks.end());
 
@@ -994,7 +955,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
        __detail::__bit_free(__bitmapC, __rotate);
        size_t* __puse_count = reinterpret_cast<size_t*>
          (_S_mem_blocks[__diff].first)
-         - (__gnu_cxx::__detail::__num_bitmaps(_S_mem_blocks[__diff]) + 1);
+         - (__detail::__num_bitmaps(_S_mem_blocks[__diff]) + 1);
        
        _GLIBCXX_DEBUG_ASSERT(*__puse_count != 0);
 
@@ -1048,7 +1009,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
       pointer 
       allocate(size_type __n)
       {
-       if (__builtin_expect(__n > this->max_size(), false))
+       if (__n > this->max_size())
          std::__throw_bad_alloc();
 
        if (__builtin_expect(__n == 1, true))
@@ -1126,12 +1087,12 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
     2 * size_t(__detail::bits_per_block);
 
   template<typename _Tp>
-    typename __gnu_cxx::bitmap_allocator<_Tp>::_BPVector::size_type 
+    typename bitmap_allocator<_Tp>::_BPVector::size_type 
     bitmap_allocator<_Tp>::_S_last_dealloc_index = 0;
 
   template<typename _Tp>
-    __gnu_cxx::__detail::_Bitmap_counter 
-  <typename bitmap_allocator<_Tp>::_Alloc_block*>
+    __detail::_Bitmap_counter
+      <typename bitmap_allocator<_Tp>::_Alloc_block*>
     bitmap_allocator<_Tp>::_S_last_request(_S_mem_blocks);
 
 #if defined __GTHREADS