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