PR c++/27270
[official-gcc.git] / libstdc++-v3 / src / bitmap_allocator.cc
blob63208df60f97b2cb3744b0ae2974e35369b038d7
1 // Bitmap Allocator. Out of line function definitions. -*- C++ -*-
3 // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 #include <ext/bitmap_allocator.h>
32 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
34 namespace balloc
36 template class __mini_vector<std::pair<bitmap_allocator<char>::_Alloc_block*, bitmap_allocator<char>::_Alloc_block*> >;
37 template class __mini_vector<std::pair<bitmap_allocator<wchar_t>::_Alloc_block*, bitmap_allocator<wchar_t>::_Alloc_block*> >;
38 template class __mini_vector<size_t*>;
40 template size_t** __lower_bound(size_t**, size_t**, size_t const&,
41 free_list::_LT_pointer_compare);
44 size_t*
45 free_list::
46 _M_get(size_t __sz) throw(std::bad_alloc)
48 #if defined __GTHREADS
49 mutex_type& __bfl_mutex = _M_get_mutex();
50 #endif
51 const vector_type& __free_list = _M_get_free_list();
52 using __gnu_cxx::balloc::__lower_bound;
53 iterator __tmp = __lower_bound(__free_list.begin(), __free_list.end(),
54 __sz, _LT_pointer_compare());
56 if (__tmp == __free_list.end() || !_M_should_i_give(**__tmp, __sz))
58 // We release the lock here, because operator new is
59 // guaranteed to be thread-safe by the underlying
60 // implementation.
61 #if defined __GTHREADS
62 __bfl_mutex.unlock();
63 #endif
64 // Try twice to get the memory: once directly, and the 2nd
65 // time after clearing the free list. If both fail, then throw
66 // std::bad_alloc().
67 int __ctr = 2;
68 while (__ctr)
70 size_t* __ret = 0;
71 --__ctr;
72 try
74 __ret = reinterpret_cast<size_t*>(::operator new(__sz + sizeof(size_t)));
76 catch(...)
78 this->_M_clear();
80 if (!__ret)
81 continue;
82 *__ret = __sz;
83 return __ret + 1;
85 std::__throw_bad_alloc();
87 else
89 size_t* __ret = *__tmp;
90 _M_get_free_list().erase(__tmp);
91 #if defined __GTHREADS
92 __bfl_mutex.unlock();
93 #endif
94 return __ret + 1;
98 void
99 free_list::
100 _M_clear()
102 #if defined __GTHREADS
103 __gnu_cxx::__scoped_lock __bfl_lock(_M_get_mutex());
104 #endif
105 vector_type& __free_list = _M_get_free_list();
106 iterator __iter = __free_list.begin();
107 while (__iter != __free_list.end())
109 ::operator delete((void*)*__iter);
110 ++__iter;
112 __free_list.clear();
115 // Instantiations.
116 template class bitmap_allocator<char>;
117 template class bitmap_allocator<wchar_t>;
119 _GLIBCXX_END_NAMESPACE