* Makefile.in (WERROR_FLAGS): Renamed from WERROR.
[official-gcc.git] / libstdc++-v3 / src / bitmap_allocator.cc
blobb953ffdd876ba6835ec1be3278101e8af63ae4cb
1 // Bitmap Allocator. Out of line function definitions. -*- C++ -*-
3 // Copyright (C) 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 namespace __gnu_cxx
34 namespace balloc
36 template class __mini_vector<std::pair
37 <bitmap_allocator<char>::_Alloc_block*,
38 bitmap_allocator<char>::_Alloc_block*> >;
40 template class __mini_vector<std::pair
41 <bitmap_allocator<wchar_t>::_Alloc_block*,
42 bitmap_allocator<wchar_t>::_Alloc_block*> >;
44 template class __mini_vector<size_t*>;
46 template size_t** __lower_bound
47 (size_t**, size_t**,
48 size_t const&, free_list::_LT_pointer_compare);
51 #if defined __GTHREADS
52 _Mutex free_list::_S_bfl_mutex;
53 #endif
54 free_list::vector_type free_list::_S_free_list;
56 size_t*
57 free_list::
58 _M_get(size_t __sz) throw(std::bad_alloc)
60 #if defined __GTHREADS
61 _Lock __bfl_lock(&_S_bfl_mutex);
62 __bfl_lock._M_lock();
63 #endif
64 iterator __temp =
65 __gnu_cxx::balloc::__lower_bound
66 (_S_free_list.begin(), _S_free_list.end(),
67 __sz, _LT_pointer_compare());
69 if (__temp == _S_free_list.end() || !_M_should_i_give(**__temp, __sz))
71 // We release the lock here, because operator new is
72 // guaranteed to be thread-safe by the underlying
73 // implementation.
74 #if defined __GTHREADS
75 __bfl_lock._M_unlock();
76 #endif
77 // Try twice to get the memory: once directly, and the 2nd
78 // time after clearing the free list. If both fail, then
79 // throw std::bad_alloc().
80 int __ctr = 2;
81 while (__ctr)
83 size_t* __ret = 0;
84 --__ctr;
85 try
87 __ret = reinterpret_cast<size_t*>
88 (::operator new(__sz + sizeof(size_t)));
90 catch(...)
92 this->_M_clear();
94 if (!__ret)
95 continue;
96 *__ret = __sz;
97 return __ret + 1;
99 throw std::bad_alloc();
101 else
103 size_t* __ret = *__temp;
104 _S_free_list.erase(__temp);
105 #if defined __GTHREADS
106 __bfl_lock._M_unlock();
107 #endif
108 return __ret + 1;
112 void
113 free_list::
114 _M_clear()
116 #if defined __GTHREADS
117 _Auto_Lock __bfl_lock(&_S_bfl_mutex);
118 #endif
119 iterator __iter = _S_free_list.begin();
120 while (__iter != _S_free_list.end())
122 ::operator delete((void*)*__iter);
123 ++__iter;
125 _S_free_list.clear();
128 // Instantiations.
129 template class bitmap_allocator<char>;
130 template class bitmap_allocator<wchar_t>;
131 } // namespace __gnu_cxx