1// Bitmap Allocator. Out of line function definitions. -*- C++ -*-
2
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.
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
18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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
32namespace __gnu_cxx
33{
34  namespace balloc
35  {
36    template class __mini_vector<std::pair
37    <bitmap_allocator<char>::_Alloc_block*,
38     bitmap_allocator<char>::_Alloc_block*> >;
39
40    template class __mini_vector<std::pair
41    <bitmap_allocator<wchar_t>::_Alloc_block*,
42     bitmap_allocator<wchar_t>::_Alloc_block*> >;
43
44    template class __mini_vector<size_t*>;
45
46    template size_t** __lower_bound
47    (size_t**, size_t**,
48     size_t const&, free_list::_LT_pointer_compare);
49  }
50
51  size_t*
52  free_list::
53  _M_get(size_t __sz) throw(std::bad_alloc)
54  {
55#if defined __GTHREADS
56    _Lock __bfl_lock(_M_get_mutex());
57    __bfl_lock._M_lock();
58#endif
59    iterator __temp =
60      __gnu_cxx::balloc::__lower_bound
61      (_M_get_free_list().begin(), _M_get_free_list().end(),
62       __sz, _LT_pointer_compare());
63
64    if (__temp == _M_get_free_list().end() || !_M_should_i_give(**__temp, __sz))
65      {
66	// We release the lock here, because operator new is
67	// guaranteed to be thread-safe by the underlying
68	// implementation.
69#if defined __GTHREADS
70	__bfl_lock._M_unlock();
71#endif
72	// Try twice to get the memory: once directly, and the 2nd
73	// time after clearing the free list. If both fail, then
74	// throw std::bad_alloc().
75	int __ctr = 2;
76	while (__ctr)
77	  {
78	    size_t* __ret = 0;
79	    --__ctr;
80	    try
81	      {
82		__ret = reinterpret_cast<size_t*>
83		  (::operator new(__sz + sizeof(size_t)));
84	      }
85	    catch(...)
86	      {
87		this->_M_clear();
88	      }
89	    if (!__ret)
90	      continue;
91	    *__ret = __sz;
92	    return __ret + 1;
93	  }
94	std::__throw_bad_alloc();
95      }
96    else
97      {
98	size_t* __ret = *__temp;
99	_M_get_free_list().erase(__temp);
100#if defined __GTHREADS
101	__bfl_lock._M_unlock();
102#endif
103	return __ret + 1;
104      }
105  }
106
107  void
108  free_list::
109  _M_clear()
110  {
111#if defined __GTHREADS
112    _Auto_Lock __bfl_lock(_M_get_mutex());
113#endif
114    vector_type& __free_list = _M_get_free_list();
115    iterator __iter = __free_list.begin();
116    while (__iter != __free_list.end())
117      {
118	::operator delete((void*)*__iter);
119	++__iter;
120      }
121    __free_list.clear();
122  }
123
124  // Instantiations.
125  template class bitmap_allocator<char>;
126  template class bitmap_allocator<wchar_t>;
127} // namespace __gnu_cxx
128