]>
Commit | Line | Data |
---|---|---|
1399eca1 DM |
1 | // Bitmap Allocator. Out of line function definitions. -*- C++ -*- |
2 | ||
56acf88c | 3 | // Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. |
1399eca1 DM |
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. | |
10 | ||
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. | |
15 | ||
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 | |
83f51799 | 18 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
1399eca1 DM |
19 | // USA. |
20 | ||
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. | |
29 | ||
30 | #include <ext/bitmap_allocator.h> | |
31 | ||
3cbc7af0 BK |
32 | _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) |
33 | ||
78a53887 | 34 | namespace __detail |
1399eca1 | 35 | { |
56acf88c PC |
36 | template class __mini_vector< |
37 | std::pair<bitmap_allocator<char>::_Alloc_block*, | |
38 | bitmap_allocator<char>::_Alloc_block*> >; | |
39 | ||
40 | template class __mini_vector< | |
41 | std::pair<bitmap_allocator<wchar_t>::_Alloc_block*, | |
42 | bitmap_allocator<wchar_t>::_Alloc_block*> >; | |
43 | ||
a8155711 | 44 | template class __mini_vector<size_t*>; |
1399eca1 | 45 | |
2e362c74 BK |
46 | template size_t** __lower_bound(size_t**, size_t**, size_t const&, |
47 | free_list::_LT_pointer_compare); | |
1399eca1 DM |
48 | } |
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(); |
1399eca1 | 56 | #endif |
2e362c74 | 57 | const vector_type& __free_list = _M_get_free_list(); |
78a53887 | 58 | using __gnu_cxx::__detail::__lower_bound; |
2e362c74 BK |
59 | iterator __tmp = __lower_bound(__free_list.begin(), __free_list.end(), |
60 | __sz, _LT_pointer_compare()); | |
1399eca1 | 61 | |
2e362c74 | 62 | if (__tmp == __free_list.end() || !_M_should_i_give(**__tmp, __sz)) |
1399eca1 DM |
63 | { |
64 | // We release the lock here, because operator new is | |
65 | // guaranteed to be thread-safe by the underlying | |
66 | // implementation. | |
67 | #if defined __GTHREADS | |
2e362c74 | 68 | __bfl_mutex.unlock(); |
1399eca1 DM |
69 | #endif |
70 | // Try twice to get the memory: once directly, and the 2nd | |
2e362c74 BK |
71 | // time after clearing the free list. If both fail, then throw |
72 | // std::bad_alloc(). | |
a8155711 | 73 | int __ctr = 2; |
1399eca1 DM |
74 | while (__ctr) |
75 | { | |
a8155711 | 76 | size_t* __ret = 0; |
1399eca1 DM |
77 | --__ctr; |
78 | try | |
79 | { | |
56acf88c PC |
80 | __ret = reinterpret_cast<size_t*> |
81 | (::operator new(__sz + sizeof(size_t))); | |
1399eca1 DM |
82 | } |
83 | catch(...) | |
84 | { | |
85 | this->_M_clear(); | |
86 | } | |
87 | if (!__ret) | |
88 | continue; | |
89 | *__ret = __sz; | |
a8155711 | 90 | return __ret + 1; |
1399eca1 | 91 | } |
56ffd9b3 | 92 | std::__throw_bad_alloc(); |
1399eca1 DM |
93 | } |
94 | else | |
95 | { | |
2e362c74 BK |
96 | size_t* __ret = *__tmp; |
97 | _M_get_free_list().erase(__tmp); | |
1399eca1 | 98 | #if defined __GTHREADS |
2e362c74 | 99 | __bfl_mutex.unlock(); |
1399eca1 | 100 | #endif |
a8155711 | 101 | return __ret + 1; |
1399eca1 DM |
102 | } |
103 | } | |
104 | ||
56acf88c | 105 | void |
1399eca1 DM |
106 | free_list:: |
107 | _M_clear() | |
108 | { | |
109 | #if defined __GTHREADS | |
2e362c74 | 110 | __gnu_cxx::__scoped_lock __bfl_lock(_M_get_mutex()); |
1399eca1 | 111 | #endif |
57b11c96 BK |
112 | vector_type& __free_list = _M_get_free_list(); |
113 | iterator __iter = __free_list.begin(); | |
114 | while (__iter != __free_list.end()) | |
1399eca1 | 115 | { |
0d6b41f2 | 116 | ::operator delete((void*)*__iter); |
1399eca1 DM |
117 | ++__iter; |
118 | } | |
57b11c96 | 119 | __free_list.clear(); |
1399eca1 DM |
120 | } |
121 | ||
122 | // Instantiations. | |
123 | template class bitmap_allocator<char>; | |
124 | template class bitmap_allocator<wchar_t>; | |
3cbc7af0 BK |
125 | |
126 | _GLIBCXX_END_NAMESPACE |