diff options
author | Dhruv Matani <dhruvbird@gmx.net> | 2004-10-14 17:52:18 +0000 |
---|---|---|
committer | Paolo Carlini <paolo@gcc.gnu.org> | 2004-10-14 17:52:18 +0000 |
commit | 1399eca1907b6fe3a191c586211bcfcc46f47b4d (patch) | |
tree | 52fc656274dd3ad9a50d564dc1d9d813ec3ff42c /libstdc++-v3/src/bitmap_allocator.cc | |
parent | a023975e421f23fc2799a2c7fcc6e19ed68227f7 (diff) | |
download | gcc-1399eca1907b6fe3a191c586211bcfcc46f47b4d.zip gcc-1399eca1907b6fe3a191c586211bcfcc46f47b4d.tar.gz gcc-1399eca1907b6fe3a191c586211bcfcc46f47b4d.tar.bz2 |
bitmap_allocator.h: Clean-up add/remove functions.
2004-10-14 Dhruv Matani <dhruvbird@gmx.net>
* ext/bitmap_allocator.h: Clean-up add/remove functions.
* src/bitmap_allocator.cc: New file. Contains the out-of-line
function definitions, static initialization of variables, and
explicit instantiations needed for the allocator.
* src/Makefile.am: Add.
* src/Makefile.in: Regenerate.
* config/linker.map.gnu: Add the necessary symbols.
From-SVN: r89042
Diffstat (limited to 'libstdc++-v3/src/bitmap_allocator.cc')
-rw-r--r-- | libstdc++-v3/src/bitmap_allocator.cc | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/libstdc++-v3/src/bitmap_allocator.cc b/libstdc++-v3/src/bitmap_allocator.cc new file mode 100644 index 0000000..f37c5dc --- /dev/null +++ b/libstdc++-v3/src/bitmap_allocator.cc @@ -0,0 +1,133 @@ +// Bitmap Allocator. Out of line function definitions. -*- C++ -*- + +// Copyright (C) 2004 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +#include <ext/bitmap_allocator.h> + +namespace __gnu_cxx +{ + namespace balloc + { + template class __mini_vector<std::pair + <bitmap_allocator<char>::_Alloc_block*, + bitmap_allocator<char>::_Alloc_block*> >; + + template class __mini_vector<std::pair + <bitmap_allocator<wchar_t>::_Alloc_block*, + bitmap_allocator<wchar_t>::_Alloc_block*> >; + + template class __mini_vector<unsigned int*>; + + template unsigned int** __lower_bound + (unsigned int**, unsigned int**, + unsigned int const&, free_list::_LT_pointer_compare); + } + +#if defined __GTHREADS + _Mutex free_list::_S_bfl_mutex; +#endif + free_list::vector_type free_list::_S_free_list; + + unsigned int* + free_list:: + _M_get(unsigned int __sz) throw(std::bad_alloc) + { +#if defined __GTHREADS + _Lock __bfl_lock(&_S_bfl_mutex); + __bfl_lock._M_lock(); +#endif + iterator __temp = + __gnu_cxx::balloc::__lower_bound + (_S_free_list.begin(), _S_free_list.end(), + __sz, _LT_pointer_compare()); + + if (__temp == _S_free_list.end() || !_M_should_i_give(**__temp, __sz)) + { + // We release the lock here, because operator new is + // guaranteed to be thread-safe by the underlying + // implementation. +#if defined __GTHREADS + __bfl_lock._M_unlock(); +#endif + // Try twice to get the memory: once directly, and the 2nd + // time after clearing the free list. If both fail, then + // throw std::bad_alloc(). + unsigned int __ctr = 2; + while (__ctr) + { + unsigned int* __ret = 0; + --__ctr; + try + { + __ret = reinterpret_cast<unsigned int*> + (::operator new(__sz + sizeof(unsigned int))); + } + catch(...) + { + this->_M_clear(); + } + if (!__ret) + continue; + *__ret = __sz; + return reinterpret_cast<unsigned int*> + (reinterpret_cast<char*>(__ret) + sizeof(unsigned int)); + } + throw std::bad_alloc(); + } + else + { + unsigned int* __ret = *__temp; + _S_free_list.erase(__temp); +#if defined __GTHREADS + __bfl_lock._M_unlock(); +#endif + return reinterpret_cast<unsigned int*> + (reinterpret_cast<char*>(__ret) + sizeof(unsigned int)); + } + } + + void + free_list:: + _M_clear() + { +#if defined __GTHREADS + _Auto_Lock __bfl_lock(&_S_bfl_mutex); +#endif + iterator __iter = _S_free_list.begin(); + while (__iter != _S_free_list.end()) + { + operator delete((void*)*__iter); + ++__iter; + } + _S_free_list.clear(); + } + + // Instantiations. + template class bitmap_allocator<char>; + template class bitmap_allocator<wchar_t>; +} // namespace __gnu_cxx |