gcc/libstdc++-v3/include/ext/throw_allocator.h
Benjamin Kosnik 3441f1062c typelist.h (type_to_type): Remove.
2006-09-27  Benjamin Kosnik  <bkoz@redhat.com>

	* include/ext/typelist.h (type_to_type): Remove.
	* include/ext/throw_allocator.h (assert_allocatod): Remove.
	(check_allocated(label)): New.
	(print_to_string): Print numerical output correctly.

	* testsuite/ext/pb_ds/example/hash_resize_neg.cc: Adjust line numbers.

	* testsuite/util/regression/rand/priority_queue/detail/
	operator_fn_imps.hpp: Set and clear label, throw
        probability. Check only allocations of the marked label.
	* testsuite/util/regression/rand/assoc/detail/
	operator_fn_imps.hpp: Same.

	* testsuite/performance/ext/pb_ds/text_find_timing.cc: Adjustments for
        removal of type_to_type.
	* testsuite/performance/ext/pb_ds/
	hash_random_int_erase_mem_usage.cc: Same.
	* testsuite/performance/ext/pb_ds/tree_text_insert_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/
	priority_queue_text_push_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/
	random_int_subscript_find_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/tree_split_join_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/random_int_find_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/
	multimap_text_insert_mem_usage.hpp: Same.
	* testsuite/performance/ext/pb_ds/
	priority_queue_random_int_push_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/multimap_text_find_timing.hpp: Same.
	* testsuite/performance/ext/pb_ds/
	priority_queue_text_modify_timing.hpp: Same.
	* testsuite/performance/ext/pb_ds/
	tree_order_statistics_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/
	priority_queue_text_push_pop_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/
	priority_queue_text_join_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/
	priority_queue_random_int_push_pop_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/
	random_int_subscript_insert_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/tree_text_lor_find_timing.cc: Same.
	* testsuite/performance/ext/pb_ds/
	multimap_text_insert_timing.hpp: Same.
	* testsuite/performance/ext/pb_ds/
	priority_queue_text_pop_mem_usage.cc: Same.
	* testsuite/performance/ext/pb_ds/
	hash_zlob_random_int_find_timing.cc: Same. 
	* testsuite/util/regression/trait/assoc/type_trait.hpp: Same.
	* testsuite/util/regression/rand/priority_queue/
	rand_regression_test.hpp: Same.
	* testsuite/util/regression/rand/assoc/detail/
	constructor_destructor_fn_imps.hpp: Same.
	* testsuite/util/regression/rand/assoc/detail/insert_fn_imps.hpp: Same.
	* testsuite/util/regression/rand/assoc/rand_regression_test.hpp: Same.
	* testsuite/util/regression/rand/assoc/
	container_rand_regression_test.hpp: Same.
	* testsuite/util/regression/assoc/common_type.hpp: Same.
	* testsuite/util/regression/basic_type.hpp: Same.
	* testsuite/util/common_type/assoc/common_type.hpp: Same.
	* testsuite/util/common_type/assoc/string_form.hpp: Same.
	* testsuite/util/common_type/assoc/template_policy.hpp: Same.
	* testsuite/util/common_type/assoc/native_set.hpp: Same.
	* testsuite/util/performance/priority_queue/mem_usage/
	pop_test.hpp: Same.
	* testsuite/util/performance/priority_queue/timing/
	push_pop_test.hpp: Same.
	* testsuite/util/performance/priority_queue/timing/join_test.hpp: Same.
	* testsuite/util/performance/priority_queue/timing/push_test.hpp: Same.
	* testsuite/util/performance/priority_queue/timing/
	modify_test.hpp: Same.
	* testsuite/util/performance/assoc/mem_usage/
	multimap_insert_test.hpp: Same.
	* testsuite/util/performance/assoc/mem_usage/erase_test.hpp: Same.
	* testsuite/util/performance/assoc/timing/
	multimap_insert_test.hpp: Same.
	* testsuite/util/performance/assoc/timing/
	subscript_find_test.hpp: Same.
	* testsuite/util/performance/assoc/timing/find_test.hpp: Same.
	* testsuite/util/performance/assoc/timing/
	subscript_insert_test.hpp: Same.
	* testsuite/util/performance/assoc/timing/insert_test.hpp: Same.
	* testsuite/util/performance/assoc/timing/
	tree_order_statistics_test.hpp: Same.
	* testsuite/util/performance/assoc/timing/multimap_find_test.hpp: Same.
	* testsuite/util/performance/assoc/timing/
	tree_split_join_test.hpp: Same.
	* testsuite/util/performance/assoc/multimap_common_type.hpp: Same.
	* testsuite/util/testsuite_common_types.h: Same.

	* include/ext/pb_ds/detail/cc_hash_table_map_/
	constructor_destructor_no_store_hash_fn_imps.hpp: Typedef
        normalization, consistency in member names between cc and gp hash
        tables, formatting fixes.
	* include/ext/pb_ds/detail/cc_hash_table_map_/
	erase_store_hash_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/cc_hash_table_map_/
	insert_no_store_hash_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/cc_hash_table_map_/
	constructor_destructor_store_hash_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/cc_hash_table_map_/
	insert_store_hash_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/cc_hash_table_map_/debug_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/cc_hash_table_map_/trace_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/cc_hash_table_map_/erase_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/cc_hash_table_map_/cc_ht_map_.hpp: Same.
	* include/ext/pb_ds/detail/cc_hash_table_map_/resize_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/cc_hash_table_map_/
	constructor_destructor_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/cc_hash_table_map_/
	erase_no_store_hash_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/
	constructor_destructor_no_store_hash_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/
	erase_store_hash_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/
	insert_no_store_hash_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/find_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/
	constructor_destructor_store_hash_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/gp_ht_map_.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/
	insert_store_hash_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/debug_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/trace_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/erase_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/resize_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/
	constructor_destructor_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/gp_hash_table_map_/
	erase_no_store_hash_fn_imps.hpp: Same.

	* include/ext/pb_ds/assoc_container.hpp: Formatting fixes.
	* include/ext/pb_ds/detail/resize_policy/
	hash_prime_size_policy_imp.hpp: Same.
	* include/ext/pb_ds/detail/resize_policy/
	hash_standard_resize_policy_imp.hpp: Same.
	* include/ext/pb_ds/detail/types_traits.hpp: Same.
	* include/ext/pb_ds/detail/constructors_destructor_fn_imps.hpp: Same.
	* include/ext/pb_ds/detail/ov_tree_map_/ov_tree_map_.hpp: Same.
	* include/ext/pb_ds/detail/hash_fn/ranged_probe_fn.hpp: Same.
	* include/ext/pb_ds/detail/hash_fn/
	direct_mod_range_hashing_imp.hpp: Same.
	* include/ext/pb_ds/detail/hash_fn/mask_based_range_hashing.hpp: Same.
	* include/ext/pb_ds/detail/hash_fn/
	direct_mask_range_hashing_imp.hpp: Same.

From-SVN: r117268
2006-09-28 01:23:58 +00:00

421 lines
10 KiB
C++

// -*- C++ -*-
// Copyright (C) 2005, 2006 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.
// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.
/**
* @file throw_allocator.h Contains an exception-throwing allocator
* useful for testing exception safety. In addition, allocation
* addresses are stored and sanity checked.
*/
#ifndef _THROW_ALLOCATOR_H
#define _THROW_ALLOCATOR_H 1
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <ostream>
#include <stdexcept>
#include <utility>
#include <tr1/random>
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
class twister_rand_gen
{
public:
twister_rand_gen(unsigned int seed =
static_cast<unsigned int>(std::time(0)));
void
init(unsigned int);
double
get_prob();
private:
std::tr1::mt19937 _M_generator;
};
struct forced_exception_error : public std::exception
{ };
class throw_allocator_base
{
public:
void
init(unsigned long seed);
static void
set_throw_prob(double throw_prob);
static double
get_throw_prob();
static void
set_label(size_t l);
static bool
empty();
struct group_throw_prob_adjustor
{
group_throw_prob_adjustor(size_t size)
: _M_throw_prob_orig(_S_throw_prob)
{
_S_throw_prob =
1 - ::pow(double(1 - _S_throw_prob), double(0.5 / (size + 1)));
}
~group_throw_prob_adjustor()
{ _S_throw_prob = _M_throw_prob_orig; }
private:
const double _M_throw_prob_orig;
};
struct zero_throw_prob_adjustor
{
zero_throw_prob_adjustor() : _M_throw_prob_orig(_S_throw_prob)
{ _S_throw_prob = 0; }
~zero_throw_prob_adjustor()
{ _S_throw_prob = _M_throw_prob_orig; }
private:
const double _M_throw_prob_orig;
};
protected:
static void
insert(void*, size_t);
static void
erase(void*, size_t);
static void
throw_conditionally();
// See if a particular address and size has been allocated by this
// allocator.
static void
check_allocated(void*, size_t);
// See if a given label has been allocated by this allocator.
static void
check_allocated(size_t);
private:
typedef std::pair<size_t, size_t> alloc_data_type;
typedef std::map<void*, alloc_data_type> map_type;
typedef map_type::value_type entry_type;
typedef map_type::const_iterator const_iterator;
typedef map_type::const_reference const_reference;
friend std::ostream&
operator<<(std::ostream&, const throw_allocator_base&);
static entry_type
make_entry(void*, size_t);
static void
print_to_string(std::string&);
static void
print_to_string(std::string&, const_reference);
static twister_rand_gen _S_g;
static map_type _S_map;
static double _S_throw_prob;
static size_t _S_label;
};
template<typename T>
class throw_allocator : public throw_allocator_base
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
template<typename U>
struct rebind
{
typedef throw_allocator<U> other;
};
throw_allocator() throw() { }
throw_allocator(const throw_allocator&) throw() { }
template<typename U>
throw_allocator(const throw_allocator<U>&) throw() { }
~throw_allocator() throw() { }
size_type
max_size() const throw()
{ return std::allocator<value_type>().max_size(); }
pointer
allocate(size_type num, std::allocator<void>::const_pointer hint = 0)
{
throw_conditionally();
value_type* const a = std::allocator<value_type>().allocate(num, hint);
insert(a, sizeof(value_type) * num);
return a;
}
void
construct(pointer p, const T& val)
{ return std::allocator<value_type>().construct(p, val); }
void
destroy(pointer p)
{ std::allocator<value_type>().destroy(p); }
void
deallocate(pointer p, size_type num)
{
erase(p, sizeof(value_type) * num);
std::allocator<value_type>().deallocate(p, num);
}
void
check_allocated(pointer p, size_type num)
{ throw_allocator_base::check_allocated(p, sizeof(value_type) * num); }
void
check_allocated(size_type label)
{ throw_allocator_base::check_allocated(label); }
};
template<typename T>
inline bool
operator==(const throw_allocator<T>&, const throw_allocator<T>&)
{ return true; }
template<typename T>
inline bool
operator!=(const throw_allocator<T>&, const throw_allocator<T>&)
{ return false; }
std::ostream&
operator<<(std::ostream& os, const throw_allocator_base& alloc)
{
std::string error;
throw_allocator_base::print_to_string(error);
os << error;
return os;
}
// XXX Should be in .cc.
twister_rand_gen::
twister_rand_gen(unsigned int seed) : _M_generator(seed) { }
void
twister_rand_gen::
init(unsigned int seed)
{ _M_generator.seed(seed); }
double
twister_rand_gen::
get_prob()
{
const double eng_min = _M_generator.min();
const double eng_range =
static_cast<const double>(_M_generator.max() - eng_min);
const double eng_res =
static_cast<const double>(_M_generator() - eng_min);
const double ret = eng_res / eng_range;
_GLIBCXX_DEBUG_ASSERT(ret >= 0 && ret <= 1);
return ret;
}
twister_rand_gen throw_allocator_base::_S_g;
throw_allocator_base::map_type
throw_allocator_base::_S_map;
double throw_allocator_base::_S_throw_prob;
size_t throw_allocator_base::_S_label = 0;
throw_allocator_base::entry_type
throw_allocator_base::make_entry(void* p, size_t size)
{ return std::make_pair(p, alloc_data_type(_S_label, size)); }
void
throw_allocator_base::init(unsigned long seed)
{ _S_g.init(seed); }
void
throw_allocator_base::set_throw_prob(double throw_prob)
{ _S_throw_prob = throw_prob; }
double
throw_allocator_base::get_throw_prob()
{ return _S_throw_prob; }
void
throw_allocator_base::set_label(size_t l)
{ _S_label = l; }
void
throw_allocator_base::insert(void* p, size_t size)
{
const_iterator found_it = _S_map.find(p);
if (found_it != _S_map.end())
{
std::string error("throw_allocator_base::insert");
error += "double insert!";
error += '\n';
print_to_string(error, make_entry(p, size));
print_to_string(error, *found_it);
throw std::logic_error(error);
}
_S_map.insert(make_entry(p, size));
}
bool
throw_allocator_base::empty()
{ return _S_map.empty(); }
void
throw_allocator_base::erase(void* p, size_t size)
{
check_allocated(p, size);
_S_map.erase(p);
}
void
throw_allocator_base::check_allocated(void* p, size_t size)
{
const_iterator found_it = _S_map.find(p);
if (found_it == _S_map.end())
{
std::string error("throw_allocator_base::check_allocated by value ");
error += "null erase!";
error += '\n';
print_to_string(error, make_entry(p, size));
throw std::logic_error(error);
}
if (found_it->second.second != size)
{
std::string error("throw_allocator_base::check_allocated by value ");
error += "wrong-size erase!";
error += '\n';
print_to_string(error, make_entry(p, size));
print_to_string(error, *found_it);
throw std::logic_error(error);
}
}
void
throw_allocator_base::check_allocated(size_t label)
{
std::string found;
const_iterator it = _S_map.begin();
while (it != _S_map.end())
{
if (it->second.first == label)
print_to_string(found, *it);
++it;
}
if (!found.empty())
{
std::string error("throw_allocator_base::check_allocated by label ");
error += '\n';
error += found;
throw std::logic_error(error);
}
}
void
throw_allocator_base::throw_conditionally()
{
if (_S_g.get_prob() < _S_throw_prob)
throw forced_exception_error();
}
void
throw_allocator_base::print_to_string(std::string& s)
{
const_iterator begin = throw_allocator_base::_S_map.begin();
const_iterator end = throw_allocator_base::_S_map.end();
for (; begin != end; ++begin)
print_to_string(s, *begin);
}
void
throw_allocator_base::print_to_string(std::string& s, const_reference ref)
{
char buf[40];
const char tab('\t');
s += "address: ";
sprintf(buf, "%p", ref.first);
s += buf;
s += tab;
s += "label: ";
sprintf(buf, "%u", ref.second.first);
s += buf;
s += tab;
s += "size: ";
sprintf(buf, "%u", ref.second.second);
s += buf;
s += '\n';
}
_GLIBCXX_END_NAMESPACE
#endif