2009-04-07 Andrew Stubbs <ams@codesourcery.com>
[official-gcc.git] / libstdc++-v3 / src / bitmap_allocator.cc
blob1d30b96a701a41ca68f6c9c84a17cc1d060d0fb6
1 // Bitmap Allocator. Out of line function definitions. -*- C++ -*-
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.
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.
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.
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.
31 #include <ext/bitmap_allocator.h>
33 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
35 namespace __detail
37 template class __mini_vector<
38 std::pair<bitmap_allocator<char>::_Alloc_block*,
39 bitmap_allocator<char>::_Alloc_block*> >;
41 template class __mini_vector<
42 std::pair<bitmap_allocator<wchar_t>::_Alloc_block*,
43 bitmap_allocator<wchar_t>::_Alloc_block*> >;
45 template class __mini_vector<size_t*>;
47 template size_t** __lower_bound(size_t**, size_t**, size_t const&,
48 free_list::_LT_pointer_compare);
51 size_t*
52 free_list::
53 _M_get(size_t __sz) throw(std::bad_alloc)
55 #if defined __GTHREADS
56 __mutex_type& __bfl_mutex = _M_get_mutex();
57 #endif
58 const vector_type& __free_list = _M_get_free_list();
59 using __gnu_cxx::__detail::__lower_bound;
60 iterator __tmp = __lower_bound(__free_list.begin(), __free_list.end(),
61 __sz, _LT_pointer_compare());
63 if (__tmp == __free_list.end() || !_M_should_i_give(**__tmp, __sz))
65 // We release the lock here, because operator new is
66 // guaranteed to be thread-safe by the underlying
67 // implementation.
68 #if defined __GTHREADS
69 __bfl_mutex.unlock();
70 #endif
71 // Try twice to get the memory: once directly, and the 2nd
72 // time after clearing the free list. If both fail, then throw
73 // std::bad_alloc().
74 int __ctr = 2;
75 while (__ctr)
77 size_t* __ret = 0;
78 --__ctr;
79 __try
81 __ret = reinterpret_cast<size_t*>
82 (::operator new(__sz + sizeof(size_t)));
84 __catch(...)
86 this->_M_clear();
88 if (!__ret)
89 continue;
90 *__ret = __sz;
91 return __ret + 1;
93 std::__throw_bad_alloc();
95 else
97 size_t* __ret = *__tmp;
98 _M_get_free_list().erase(__tmp);
99 #if defined __GTHREADS
100 __bfl_mutex.unlock();
101 #endif
102 return __ret + 1;
106 void
107 free_list::
108 _M_clear()
110 #if defined __GTHREADS
111 __gnu_cxx::__scoped_lock __bfl_lock(_M_get_mutex());
112 #endif
113 vector_type& __free_list = _M_get_free_list();
114 iterator __iter = __free_list.begin();
115 while (__iter != __free_list.end())
117 ::operator delete((void*)*__iter);
118 ++__iter;
120 __free_list.clear();
123 // Instantiations.
124 template class bitmap_allocator<char>;
125 template class bitmap_allocator<wchar_t>;
127 _GLIBCXX_END_NAMESPACE