Import gcc-4.4.1
[dragonfly.git] / contrib / gcc-4.4 / libstdc++-v3 / src / bitmap_allocator.cc
1 // Bitmap Allocator. Out of line function definitions. -*- 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 #include <ext/bitmap_allocator.h>
27
28 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
29
30   namespace __detail
31   {
32     template class __mini_vector<
33       std::pair<bitmap_allocator<char>::_Alloc_block*,
34                 bitmap_allocator<char>::_Alloc_block*> >;
35
36     template class __mini_vector<
37       std::pair<bitmap_allocator<wchar_t>::_Alloc_block*,
38                 bitmap_allocator<wchar_t>::_Alloc_block*> >;
39
40     template class __mini_vector<size_t*>;
41
42     template size_t** __lower_bound(size_t**, size_t**, size_t const&, 
43                                     free_list::_LT_pointer_compare);
44   }
45
46   size_t*
47   free_list::
48   _M_get(size_t __sz) throw(std::bad_alloc)
49   {
50 #if defined __GTHREADS
51     __mutex_type& __bfl_mutex = _M_get_mutex();
52 #endif
53     const vector_type& __free_list = _M_get_free_list();
54     using __gnu_cxx::__detail::__lower_bound;
55     iterator __tmp = __lower_bound(__free_list.begin(), __free_list.end(), 
56                                    __sz, _LT_pointer_compare());
57
58     if (__tmp == __free_list.end() || !_M_should_i_give(**__tmp, __sz))
59       {
60         // We release the lock here, because operator new is
61         // guaranteed to be thread-safe by the underlying
62         // implementation.
63 #if defined __GTHREADS
64         __bfl_mutex.unlock();
65 #endif
66         // Try twice to get the memory: once directly, and the 2nd
67         // time after clearing the free list. If both fail, then throw
68         // std::bad_alloc().
69         int __ctr = 2;
70         while (__ctr)
71           {
72             size_t* __ret = 0;
73             --__ctr;
74             __try
75               {
76                 __ret = reinterpret_cast<size_t*>
77                   (::operator new(__sz + sizeof(size_t)));
78               }
79             __catch(...)
80               {
81                 this->_M_clear();
82               }
83             if (!__ret)
84               continue;
85             *__ret = __sz;
86             return __ret + 1;
87           }
88         std::__throw_bad_alloc();
89       }
90     else
91       {
92         size_t* __ret = *__tmp;
93         _M_get_free_list().erase(__tmp);
94 #if defined __GTHREADS
95         __bfl_mutex.unlock();
96 #endif
97         return __ret + 1;
98       }
99   }
100
101   void
102   free_list::
103   _M_clear()
104   {
105 #if defined __GTHREADS
106     __gnu_cxx::__scoped_lock __bfl_lock(_M_get_mutex());
107 #endif
108     vector_type& __free_list = _M_get_free_list();
109     iterator __iter = __free_list.begin();
110     while (__iter != __free_list.end())
111       {
112         ::operator delete((void*)*__iter);
113         ++__iter;
114       }
115     __free_list.clear();
116   }
117
118   // Instantiations.
119   template class bitmap_allocator<char>;
120   template class bitmap_allocator<wchar_t>;
121
122 _GLIBCXX_END_NAMESPACE