]>
Commit | Line | Data |
---|---|---|
1399eca1 DM |
1 | // Bitmap Allocator. Out of line function definitions. -*- C++ -*- |
2 | ||
12ffa228 | 3 | // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011 |
bc2631e0 | 4 | // Free Software Foundation, Inc. |
1399eca1 DM |
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 | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
1399eca1 DM |
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 | ||
748086b7 JJ |
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/>. | |
1399eca1 DM |
25 | |
26 | #include <ext/bitmap_allocator.h> | |
27 | ||
12ffa228 BK |
28 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
29 | { | |
78a53887 | 30 | namespace __detail |
1399eca1 | 31 | { |
12ffa228 | 32 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
56acf88c PC |
33 | template class __mini_vector< |
34 | std::pair<bitmap_allocator<char>::_Alloc_block*, | |
35 | bitmap_allocator<char>::_Alloc_block*> >; | |
36 | ||
37 | template class __mini_vector< | |
38 | std::pair<bitmap_allocator<wchar_t>::_Alloc_block*, | |
39 | bitmap_allocator<wchar_t>::_Alloc_block*> >; | |
40 | ||
a8155711 | 41 | template class __mini_vector<size_t*>; |
1399eca1 | 42 | |
2e362c74 BK |
43 | template size_t** __lower_bound(size_t**, size_t**, size_t const&, |
44 | free_list::_LT_pointer_compare); | |
12ffa228 | 45 | _GLIBCXX_END_NAMESPACE_VERSION |
1399eca1 DM |
46 | } |
47 | ||
12ffa228 BK |
48 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
49 | ||
a8155711 | 50 | size_t* |
1399eca1 | 51 | free_list:: |
a8155711 | 52 | _M_get(size_t __sz) throw(std::bad_alloc) |
1399eca1 DM |
53 | { |
54 | #if defined __GTHREADS | |
56acf88c | 55 | __mutex_type& __bfl_mutex = _M_get_mutex(); |
85220919 | 56 | __bfl_mutex.lock(); |
1399eca1 | 57 | #endif |
2e362c74 | 58 | const vector_type& __free_list = _M_get_free_list(); |
78a53887 | 59 | using __gnu_cxx::__detail::__lower_bound; |
2e362c74 BK |
60 | iterator __tmp = __lower_bound(__free_list.begin(), __free_list.end(), |
61 | __sz, _LT_pointer_compare()); | |
1399eca1 | 62 | |
2e362c74 | 63 | if (__tmp == __free_list.end() || !_M_should_i_give(**__tmp, __sz)) |
1399eca1 DM |
64 | { |
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 | |
2e362c74 | 69 | __bfl_mutex.unlock(); |
1399eca1 DM |
70 | #endif |
71 | // Try twice to get the memory: once directly, and the 2nd | |
2e362c74 BK |
72 | // time after clearing the free list. If both fail, then throw |
73 | // std::bad_alloc(). | |
a8155711 | 74 | int __ctr = 2; |
1399eca1 DM |
75 | while (__ctr) |
76 | { | |
a8155711 | 77 | size_t* __ret = 0; |
1399eca1 | 78 | --__ctr; |
bc2631e0 | 79 | __try |
1399eca1 | 80 | { |
56acf88c PC |
81 | __ret = reinterpret_cast<size_t*> |
82 | (::operator new(__sz + sizeof(size_t))); | |
1399eca1 | 83 | } |
bf4967a1 | 84 | __catch(const std::bad_alloc&) |
1399eca1 DM |
85 | { |
86 | this->_M_clear(); | |
87 | } | |
88 | if (!__ret) | |
89 | continue; | |
90 | *__ret = __sz; | |
a8155711 | 91 | return __ret + 1; |
1399eca1 | 92 | } |
56ffd9b3 | 93 | std::__throw_bad_alloc(); |
1399eca1 DM |
94 | } |
95 | else | |
96 | { | |
2e362c74 BK |
97 | size_t* __ret = *__tmp; |
98 | _M_get_free_list().erase(__tmp); | |
1399eca1 | 99 | #if defined __GTHREADS |
2e362c74 | 100 | __bfl_mutex.unlock(); |
1399eca1 | 101 | #endif |
a8155711 | 102 | return __ret + 1; |
1399eca1 DM |
103 | } |
104 | } | |
105 | ||
56acf88c | 106 | void |
1399eca1 DM |
107 | free_list:: |
108 | _M_clear() | |
109 | { | |
110 | #if defined __GTHREADS | |
2e362c74 | 111 | __gnu_cxx::__scoped_lock __bfl_lock(_M_get_mutex()); |
1399eca1 | 112 | #endif |
57b11c96 BK |
113 | vector_type& __free_list = _M_get_free_list(); |
114 | iterator __iter = __free_list.begin(); | |
115 | while (__iter != __free_list.end()) | |
1399eca1 | 116 | { |
0d6b41f2 | 117 | ::operator delete((void*)*__iter); |
1399eca1 DM |
118 | ++__iter; |
119 | } | |
57b11c96 | 120 | __free_list.clear(); |
1399eca1 DM |
121 | } |
122 | ||
123 | // Instantiations. | |
124 | template class bitmap_allocator<char>; | |
125 | template class bitmap_allocator<wchar_t>; | |
3cbc7af0 | 126 | |
12ffa228 BK |
127 | _GLIBCXX_END_NAMESPACE_VERSION |
128 | } // namespace |