Commit 8bfd0a46 by Benjamin Kosnik Committed by Benjamin Kosnik

re PR libstdc++/16614 (Excessive resource usage in __mt_alloc)


2004-09-01  Benjamin Kosnik  <bkoz@redhat.com>

	PR libstdc++/16614
	* include/ext/mt_allocator.h (__mt_base): Not type dependent,
	split into..
	(__pool): New, specialize.
	(__common_pool): New, static bits here.
	(__per_type_pool): New, and here.
	(__mt_alloc_base): New.
	(__mt_alloc): Add template parameter, inherit from it.
	* src/allocator.cc: Split this...
	* src/allocator-inst.cc: And this...
	* src/pool_allocator.cc: ...into this.
	* src/mt_allocator.cc: ... and this. Add definitions for
	__mt_base.
	* src/Makefile.am (sources): Split allocator.cc to
	pool_allocator.cc and mt_allocator.cc.
	* src/Makefile.in: Regenerate.
	* config/linker-map.gnu: Add symbols.
	* docs/html/ext/mt_allocator.html: Document new design.
	* testsuite/ext/mt_allocator/tune-1.cc: New.
	* testsuite/ext/mt_allocator/tune-2.cc: New.
	* testsuite/ext/mt_allocator/tune-3.cc: New.
	* testsuite/ext/mt_allocator/tune-4.cc: New.

	* testsuite/testsuite_allocator.h (__gnu_test::check_new): New.
	* testsuite/ext/allocators.cc: Use check_new, split into...
	* testsuite/ext/mt_allocator/check_new.cc: this.
	* testsuite/ext/pool_allocator/check_new.cc: this.
	* testsuite/ext/malloc_allocator/check_new.cc: this.
	* testsuite/ext/debug_allocator/check_new.cc: this.
	* testsuite/ext/mt_allocator/instantiate.cc: this.
	* testsuite/ext/pool_allocator/instantiate.cc: this.
	* testsuite/ext/malloc_allocator/instantiate.cc: this.
	* testsuite/ext/debug_allocator/instantiate.cc: this.

From-SVN: r86936
parent 0705d602
2004-09-01 Benjamin Kosnik <bkoz@redhat.com>
PR libstdc++/16614
* include/ext/mt_allocator.h (__mt_base): Not type dependent,
split into..
(__pool): New, specialize.
(__common_pool): New, static bits here.
(__per_type_pool): New, and here.
(__mt_alloc_base): New.
(__mt_alloc): Add template parameter, inherit from it.
* src/allocator.cc: Split this...
* src/allocator-inst.cc: And this...
* src/pool_allocator.cc: ...into this.
* src/mt_allocator.cc: ... and this. Add definitions for
__mt_base.
* src/Makefile.am (sources): Split allocator.cc to
pool_allocator.cc and mt_allocator.cc.
* src/Makefile.in: Regenerate.
* config/linker-map.gnu: Add symbols.
* docs/html/ext/mt_allocator.html: Document new design.
* testsuite/ext/mt_allocator/tune-1.cc: New.
* testsuite/ext/mt_allocator/tune-2.cc: New.
* testsuite/ext/mt_allocator/tune-3.cc: New.
* testsuite/ext/mt_allocator/tune-4.cc: New.
* testsuite/testsuite_allocator.h (__gnu_test::check_new): New.
* testsuite/ext/allocators.cc: Use check_new, split into...
* testsuite/ext/mt_allocator/check_new.cc: this.
* testsuite/ext/pool_allocator/check_new.cc: this.
* testsuite/ext/malloc_allocator/check_new.cc: this.
* testsuite/ext/debug_allocator/check_new.cc: this.
* testsuite/ext/mt_allocator/instantiate.cc: this.
* testsuite/ext/pool_allocator/instantiate.cc: this.
* testsuite/ext/malloc_allocator/instantiate.cc: this.
* testsuite/ext/debug_allocator/instantiate.cc: this.
2004-08-30 Phil Edwards <phil@codesourcery.com>
* docs/html/install.html: Update locales list (from Paolo).
......
......@@ -255,10 +255,20 @@ GLIBCXX_3.4.2 {
_ZN9__gnu_cxx18stdio_sync_filebufI[cw]St11char_traitsI[cw]EE4fileEv;
# pool_alloc
_ZN9__gnu_cxx17__pool_alloc_base9_M_refillE[jm];
_ZN9__gnu_cxx17__pool_alloc_base16_M_get_free_listE[jm];
_ZN9__gnu_cxx17__pool_alloc_base12_M_get_mutexEv;
# mt_alloc
_ZN9__gnu_cxx6__poolILb0EE13_M_initializeEv;
_ZN9__gnu_cxx6__poolILb1EE13_M_initializeEPFvPvE;
_ZN9__gnu_cxx6__poolILb1EE21_M_destroy_thread_keyEPv;
_ZN9__gnu_cxx6__poolILb1EE16_M_get_thread_idEv;
_ZN9__gnu_cxx6__poolILb[01]EE17_M_reserve_memoryE[jm][jm];
_ZN9__gnu_cxx6__poolILb[01]EE17_M_reclaim_memoryEPc[jm];
_ZN9__gnu_cxx20__common_pool_policyILb[01]EE11_S_get_poolEv;
} GLIBCXX_3.4.1;
# Symbols in the support library (libsupc++) have their own tag.
......
......@@ -42,10 +42,12 @@ initially developed specifically to suit the needs of multi threaded
applications [hereinafter referred to as an MT application]. Over time
the allocator has evolved and been improved in many ways, one of the
being that it now also does a good job in single threaded applications
[hereinafter referred to as a ST application]. (Note: In this
[hereinafter referred to as a ST application]. (Note: In this
document, when referring to single threaded applications this also
includes applications that are compiled with gcc without thread
support enabled. This is accomplished using ifdef's on __GTHREADS)
support enabled. This is accomplished using ifdef's on
__GTHREADS). This allocator is tunable, very flexible, and capable of
high-performance.
</p>
<p>
......@@ -54,11 +56,66 @@ view - the "inner workings" of the allocator.
</p>
<h3 class="left">
<a name="design">Design Overview</a>
</h3>
<p> There are three general components to the allocator: a datum
describing the characteristics of the memory pool, a policy class
containing this pool that links instantiation types to common or
individual pools, and a class inheriting from the policy class that is
the actual allocator.
</p>
<p>The datum describing pools characteristics is
<pre>
template&lt;bool _Thread&gt;
class __pool
</pre>
This class is parametrized on thread support, and is explicitly
specialized for both multiple threads (with <code>bool==true</code>)
and single threads (via <code>bool==false</code>.)
</p>
<p> There are two distinct policy classes, each of which can be used
with either type of underlying pool datum.
</p>
<pre>
template&lt;bool _Thread&gt;
struct __common_pool_policy
template&lt;typename _Tp, bool _Thread&gt;
struct __per_type_pool_policy
</pre>
<p> The first policy, <code>__common_pool_policy</code>, implements a
common pool. This means that allocators that are instantiated with
different types, say <code>char</code> and <code>long</code> will both
use the same pool. This is the default policy.
</p>
<p> The second policy, <code>__per_type_pool_policy</code>, implements
a separate pool for each instantiating type. Thus, <code>char</code>
and <code>long</code> will use separate pools. This allows per-type
tuning, for instance.
</p>
<p> Putting this all together, the actual allocator class is
<pre>
template&lt;typename _Tp, typename _Poolp = __default_policy&gt;
class __mt_alloc : public __mt_alloc_base&lt;_Tp&gt;, _Poolp
</pre>
This class has the interface required for standard library allocator
classes, namely member functions <code>allocate</code> and
<code>deallocate</code>, plus others.
</p>
<h3 class="left">
<a name="init">Tunable parameters</a>
</h3>
<p>Certain allocation parameters can be modified on a per-type
basis. There exists a nested <pre>struct _Tune</pre> that contains all
<p>Certain allocation parameters can be modified, or tuned. There
exists a nested <pre>struct __pool_base::_Tune</pre> that contains all
these parameters, which include settings for
</p>
<ul>
......@@ -87,16 +144,16 @@ int main()
{
typedef pod value_type;
typedef __gnu_cxx::__mt_alloc&lt;value_type&gt; allocator_type;
typedef allocator_type::_Tune tune_type;
typedef __gnu_cxx::__pool_base::_Tune tune_type;
tune_type t_default;
tune_type t_opt(16, 5120, 32, 5120, 20, 10, false);
tune_type t_single(16, 5120, 32, 5120, 1, 10, false);
tune_type t;
t = allocator_type::_S_get_options();
allocator_type::_S_set_options(t_opt);
t = allocator_type::_S_get_options();
t = allocator_type::_M_get_options();
allocator_type::_M_set_options(t_opt);
t = allocator_type::_M_get_options();
allocator_type a;
allocator_type::pointer p1 = a.allocate(128);
......@@ -119,14 +176,15 @@ are initialized as above, or are set to the global defaults.
</p>
<p>
The very first allocate() call will always call the _S_init() function.
In order to make sure that this function is called exactly once we make use
of a __gthread_once (with _S_once_mt and _S_init as arguments) call in MT
applications and check a static bool (_S_initialized) in ST applications.
The very first allocate() call will always call the
_S_initialize_once() function. In order to make sure that this
function is called exactly once we make use of a __gthread_once call
in MT applications and check a static bool (_S_init) in ST
applications.
</p>
<p>
The _S_init() function:
The _S_initialize() function:
- If the GLIBCXX_FORCE_NEW environment variable is set, it sets the bool
_S_force_new to true and then returns. This will cause subsequent calls to
allocate() to return memory directly from a new() call, and deallocate will
......
......@@ -96,7 +96,8 @@ basic_file.cc: ${glibcxx_srcdir}/$(BASIC_FILE_CC)
# Sources present in the src directory.
sources = \
allocator.cc \
pool_allocator.cc \
mt_allocator.cc \
codecvt.cc \
complex_io.cc \
ctype.cc \
......
......@@ -64,14 +64,15 @@ am__objects_1 = atomicity.lo codecvt_members.lo collate_members.lo \
ctype_members.lo messages_members.lo monetary_members.lo \
numeric_members.lo time_members.lo
am__objects_2 = basic_file.lo c++locale.lo
am__objects_3 = allocator.lo codecvt.lo complex_io.lo ctype.lo \
debug.lo debug_list.lo functexcept.lo globals_locale.lo \
globals_io.lo ios.lo ios_failure.lo ios_init.lo ios_locale.lo \
limits.lo list.lo locale.lo locale_init.lo locale_facets.lo \
localename.lo stdexcept.lo strstream.lo tree.lo \
allocator-inst.lo concept-inst.lo fstream-inst.lo ext-inst.lo \
io-inst.lo istream-inst.lo locale-inst.lo locale-misc-inst.lo \
misc-inst.lo ostream-inst.lo sstream-inst.lo streambuf-inst.lo \
am__objects_3 = pool_allocator.lo mt_allocator.lo codecvt.lo \
complex_io.lo ctype.lo debug.lo debug_list.lo functexcept.lo \
globals_locale.lo globals_io.lo ios.lo ios_failure.lo \
ios_init.lo ios_locale.lo limits.lo list.lo locale.lo \
locale_init.lo locale_facets.lo localename.lo stdexcept.lo \
strstream.lo tree.lo allocator-inst.lo concept-inst.lo \
fstream-inst.lo ext-inst.lo io-inst.lo istream-inst.lo \
locale-inst.lo locale-misc-inst.lo misc-inst.lo \
ostream-inst.lo sstream-inst.lo streambuf-inst.lo \
string-inst.lo valarray-inst.lo wlocale-inst.lo \
wstring-inst.lo $(am__objects_1) $(am__objects_2)
am_libstdc___la_OBJECTS = $(am__objects_3)
......@@ -303,7 +304,8 @@ host_sources_extra = \
# Sources present in the src directory.
sources = \
allocator.cc \
pool_allocator.cc \
mt_allocator.cc \
codecvt.cc \
complex_io.cc \
ctype.cc \
......
......@@ -32,20 +32,9 @@
//
#include <memory>
#include <ext/mt_allocator.h>
#include <ext/pool_allocator.h>
namespace std
{
template class allocator<char>;
template class allocator<wchar_t>;
} // namespace std
namespace __gnu_cxx
{
template class __mt_alloc<char>;
template class __mt_alloc<wchar_t>;
template class __pool_alloc<char>;
template class __pool_alloc<wchar_t>;
} // namespace __gnu_cxx
......@@ -32,8 +32,7 @@
//
#include <bits/c++config.h>
#include <memory>
#include <ext/mt_allocator.h>
#include <cstdlib>
#include <ext/pool_allocator.h>
namespace __gnu_internal
......@@ -166,4 +165,8 @@ namespace __gnu_cxx
char* __pool_alloc_base::_S_end_free = 0;
size_t __pool_alloc_base::_S_heap_size = 0;
// Instantiations.
template class __pool_alloc<char>;
template class __pool_alloc<wchar_t>;
} // namespace __gnu_cxx
// 2001-11-25 Phil Edwards <pme@gcc.gnu.org>
//
// Copyright (C) 2001, 2003 Free Software Foundation, Inc.
// Copyright (C) 2001, 2003, 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
......@@ -21,29 +21,13 @@
// 20.4.1.1 allocator members
#include <cstdlib>
#include <memory>
//#include <ext/pool_allocator.h>
#include <ext/debug_allocator.h>
#include <ext/malloc_allocator.h>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
using __gnu_cxx::malloc_allocator;
using __gnu_cxx::debug_allocator;
template class malloc_allocator<int>;
template class debug_allocator<malloc_allocator<int> >;
#if 0
using __gnu_cxx::__pool_alloc;
template class __pool_alloc<true, 3>;
template class __pool_alloc<false, 3>;
#endif
bool new_called;
bool delete_called;
std::size_t requested;
void*
operator new(std::size_t n) throw(std::bad_alloc)
{
......@@ -59,47 +43,14 @@ operator delete(void *v) throw()
return std::free(v);
}
template<typename Alloc, bool uses_global_new_and_delete>
void check_allocator()
{
bool test __attribute__((unused)) = true;
new_called = false;
delete_called = false;
requested = 0;
Alloc a;
typename Alloc::pointer p = a.allocate(10);
if (uses_global_new_and_delete)
VERIFY( requested >= (10 * 15 * sizeof(long)) );
VERIFY( new_called == uses_global_new_and_delete );
a.deallocate(p, 10);
VERIFY( delete_called == uses_global_new_and_delete );
bool test02()
{
typedef debug_allocator<malloc_allocator<unsigned int> > allocator_type;
return (__gnu_test::check_new<allocator_type, false>() == false);
}
// These just help tracking down error messages.
void test01()
{ check_allocator<malloc_allocator<int>, false>(); }
void test02()
{ check_allocator<debug_allocator<malloc_allocator<int> >, false>(); }
#if 0
void test03()
{ check_allocator<__pool_alloc<true, 3>, true>(); }
void test04()
{ check_allocator<__pool_alloc<false, 3>, true>(); }
#endif
int main()
{
test01();
test02();
#if 0
test03();
test04();
#endif
return 0;
return test02();
}
// { dg-do compile }
// 2001-11-25 Phil Edwards <pme@gcc.gnu.org>
//
// Copyright (C) 2001, 2003, 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.
// 20.4.1.1 allocator members
#include <cstdlib>
#include <ext/debug_allocator.h>
#include <ext/malloc_allocator.h>
template class __gnu_cxx::debug_allocator<__gnu_cxx::malloc_allocator<int> >;
// 2001-11-25 Phil Edwards <pme@gcc.gnu.org>
//
// Copyright (C) 2001, 2003, 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.
// 20.4.1.1 allocator members
#include <cstdlib>
#include <ext/malloc_allocator.h>
#include <testsuite_allocator.h>
using __gnu_cxx::malloc_allocator;
void*
operator new(std::size_t n) throw(std::bad_alloc)
{
new_called = true;
requested = n;
return std::malloc(n);
}
void
operator delete(void *v) throw()
{
delete_called = true;
return std::free(v);
}
// These just help tracking down error messages.
bool test01()
{
typedef malloc_allocator<unsigned int> allocator_type;
return (__gnu_test::check_new<allocator_type, false>() == false);
}
int main()
{
return test01();
}
// { dg-do compile }
// 2001-11-25 Phil Edwards <pme@gcc.gnu.org>
//
// Copyright (C) 2001, 2003, 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.
// 20.4.1.1 allocator members
#include <cstdlib>
#include <ext/malloc_allocator.h>
template class __gnu_cxx::malloc_allocator<int>;
// 2001-11-25 Phil Edwards <pme@gcc.gnu.org>
//
// Copyright (C) 2001, 2003, 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.
// 20.4.1.1 allocator members
#include <cstdlib>
#include <ext/mt_allocator.h>
#include <testsuite_allocator.h>
using __gnu_cxx::__mt_alloc;
void*
operator new(std::size_t n) throw(std::bad_alloc)
{
new_called = true;
requested = n;
return std::malloc(n);
}
void
operator delete(void *v) throw()
{
delete_called = true;
return std::free(v);
}
bool test03()
{
typedef __mt_alloc<unsigned int> allocator_type;
return (__gnu_test::check_new<allocator_type, true>() == true);
}
int main()
{
return test03();
}
// { dg-do compile }
// 2001-11-25 Phil Edwards <pme@gcc.gnu.org>
//
// Copyright (C) 2001, 2003, 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.
// 20.4.1.1 allocator members
#include <cstdlib>
#include <ext/mt_allocator.h>
template class __gnu_cxx::__mt_alloc<int>;
// 2004-08-25 Benjamin Kosnik <bkoz@redhat.com>
//
// 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.
#include <cassert>
#include <memory>
#include <ext/mt_allocator.h>
struct pod
{
int i;
};
// Tune characteristics.
// __common_pool_policy
void test01()
{
typedef pod value_type;
#ifdef __GTHREADS
typedef __gnu_cxx::__common_pool_policy<true> policy_type;
#else
typedef __gnu_cxx::__common_pool_policy<false> policy_type;
#endif
typedef __gnu_cxx::__mt_alloc<value_type, policy_type> allocator_type;
typedef __gnu_cxx::__pool_base::_Tune tune_type;
tune_type t_default;
tune_type t_opt(16, 5120, 32, 5120, 20, 10, false);
tune_type t_single(16, 5120, 32, 5120, 1, 10, false);
allocator_type a;
tune_type t1 = a._M_get_options();
assert(t1._M_align == t_default._M_align);
a._M_set_options(t_opt);
tune_type t2 = a._M_get_options();
assert(t1._M_align != t2._M_align);
allocator_type::pointer p1 = a.allocate(128);
allocator_type::pointer p2 = a.allocate(5128);
a._M_set_options(t_single);
t1 = a._M_get_options();
assert(t1._M_max_threads != t_single._M_max_threads);
assert(t1._M_max_threads == t_opt._M_max_threads);
a.deallocate(p1, 128);
a.deallocate(p2, 5128);
}
int main()
{
test01();
return 0;
}
// 2004-08-25 Benjamin Kosnik <bkoz@redhat.com>
//
// 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.
#include <cassert>
#include <memory>
#include <ext/mt_allocator.h>
struct pod
{
int i;
};
// Tune characteristics.
// __per_type_pool_policy
void test02()
{
typedef pod value_type;
#ifdef __GTHREADS
typedef __gnu_cxx::__per_type_pool_policy<value_type, true> policy_type;
#else
typedef __gnu_cxx::__per_type_pool_policy<value_type, false> policy_type;
#endif
typedef __gnu_cxx::__mt_alloc<value_type, policy_type> allocator_type;
typedef __gnu_cxx::__pool_base::_Tune tune_type;
tune_type t_default;
tune_type t_opt(16, 5120, 32, 5120, 20, 10, false);
tune_type t_single(16, 5120, 32, 5120, 1, 10, false);
allocator_type a;
tune_type t1 = a._M_get_options();
assert(t1._M_align == t_default._M_align);
a._M_set_options(t_opt);
tune_type t2 = a._M_get_options();
assert(t1._M_align != t2._M_align);
allocator_type::pointer p1 = a.allocate(128);
allocator_type::pointer p2 = a.allocate(5128);
a._M_set_options(t_single);
t1 = a._M_get_options();
assert(t1._M_max_threads != t_single._M_max_threads);
assert(t1._M_max_threads == t_opt._M_max_threads);
a.deallocate(p1, 128);
a.deallocate(p2, 5128);
}
int main()
{
test02();
return 0;
}
// 2004-08-25 Benjamin Kosnik <bkoz@redhat.com>
//
// 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.
#include <cassert>
#include <memory>
#include <ext/mt_allocator.h>
struct pod
{
int i;
};
// Tune characteristics, two of same type
template<typename _Tp>
struct test_policy
{ static bool per_type() { return true; } };
template<bool _Thread>
struct test_policy<__gnu_cxx::__common_pool_policy<_Thread> >
{
typedef __gnu_cxx::__common_pool_policy<_Thread> pool_type;
static bool per_type() { return false; }
};
// Tune characteristics, two of different types
template<typename _Tp, typename _Cp>
void test03()
{
typedef __gnu_cxx::__pool_base::_Tune tune_type;
typedef _Tp value_type;
typedef _Cp policy_type;
typedef __gnu_cxx::__mt_alloc<value_type, policy_type> allocator_type;
tune_type t_default;
tune_type t_opt(16, 5120, 32, 5120, 20, 10, false);
tune_type t_single(16, 5120, 32, 5120, 1, 10, false);
// First instances assured.
allocator_type a;
tune_type t1 = a._M_get_options();
tune_type t2;
if (test_policy<policy_type>::per_type())
{
assert(t1._M_align == t_default._M_align);
a._M_set_options(t_opt);
t2 = a._M_get_options();
assert(t1._M_align != t2._M_align);
}
else
t2 = t1;
// Lock tune settings.
typename allocator_type::pointer p1 = a.allocate(128);
allocator_type a2;
tune_type t3 = a2._M_get_options();
tune_type t4;
assert(t3._M_max_threads == t2._M_max_threads);
typename allocator_type::pointer p2 = a2.allocate(5128);
a2._M_set_options(t_single);
t4 = a2._M_get_options();
assert(t4._M_max_threads != t_single._M_max_threads);
assert(t4._M_max_threads == t3._M_max_threads);
a.deallocate(p1, 128);
a2.deallocate(p2, 5128);
}
int main()
{
#ifdef __GTHREADS
test03<int, __gnu_cxx::__per_type_pool_policy<int, true> >();
test03<int, __gnu_cxx::__common_pool_policy<true> >();
#endif
test03<int, __gnu_cxx::__common_pool_policy<false> >();
test03<int, __gnu_cxx::__per_type_pool_policy<int, false> >();
return 0;
}
// 2004-08-25 Benjamin Kosnik <bkoz@redhat.com>
//
// 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.
#include <cassert>
#include <memory>
#include <ext/mt_allocator.h>
struct pod
{
int i;
};
// Tune characteristics, two of same type
template<typename _Tp>
struct test_policy
{ static bool per_type() { return true; } };
template<bool _Thread>
struct test_policy<__gnu_cxx::__common_pool_policy<_Thread> >
{
typedef __gnu_cxx::__common_pool_policy<_Thread> pool_type;
static bool per_type() { return false; }
};
struct pod2
{
int i;
int j;
};
// Tune characteristics, two of different instantiations
template<typename _Tp, typename _Cp>
void test04()
{
typedef __gnu_cxx::__pool_base::_Tune tune_type;
typedef _Tp value_type;
typedef _Cp policy_type;
typedef __gnu_cxx::__mt_alloc<value_type, policy_type> allocator_type;
tune_type t_default;
tune_type t_opt(16, 5120, 32, 5120, 20, 10, false);
tune_type t_single(16, 5120, 32, 5120, 1, 10, false);
allocator_type a;
tune_type t1 = a._M_get_options();
tune_type t2;
if (test_policy<policy_type>::per_type())
{
assert(t1._M_align == t_default._M_align);
a._M_set_options(t_opt);
t2 = a._M_get_options();
assert(t1._M_align != t2._M_align);
}
else
t2 = t1;
// Lock tune settings.
typename allocator_type::pointer p1 = a.allocate(128);
// First instance of local type assured.
typedef pod2 value2_type;
typedef typename allocator_type::template rebind<value2_type>::other rebind_type;
rebind_type a2;
tune_type t3 = a2._M_get_options();
tune_type t4;
// Both policy_type and rebind_type::policy_type have same characteristics.
if (test_policy<policy_type>::per_type())
{
assert(t3._M_align == t_default._M_align);
a2._M_set_options(t_opt);
t4 = a2._M_get_options();
assert(t3._M_align != t4._M_align);
t3 = t4;
}
else
assert(t3._M_max_threads == t2._M_max_threads);
typename rebind_type::pointer p2 = a2.allocate(5128);
a2._M_set_options(t_single);
t4 = a2._M_get_options();
assert(t4._M_max_threads != t_single._M_max_threads);
assert(t4._M_max_threads == t3._M_max_threads);
a.deallocate(p1, 128);
a2.deallocate(p2, 5128);
}
int main()
{
#ifdef __GTHREADS
test04<float, __gnu_cxx::__common_pool_policy<true> >();
test04<double, __gnu_cxx::__per_type_pool_policy<double, true> >();
#endif
test04<float, __gnu_cxx::__common_pool_policy<false> >();
test04<double, __gnu_cxx::__per_type_pool_policy<double, false> >();
return 0;
}
// 2001-11-25 Phil Edwards <pme@gcc.gnu.org>
//
// Copyright (C) 2001, 2003, 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.
// 20.4.1.1 allocator members
#include <cstdlib>
#include <ext/pool_allocator.h>
#include <testsuite_allocator.h>
using __gnu_cxx::__pool_alloc;
void*
operator new(std::size_t n) throw(std::bad_alloc)
{
new_called = true;
requested = n;
return std::malloc(n);
}
void
operator delete(void *v) throw()
{
delete_called = true;
return std::free(v);
}
bool test03()
{
typedef __pool_alloc<unsigned int> allocator_type;
return (__gnu_test::check_new<allocator_type, true>() == true);
}
int main()
{
return test03();
}
// { dg-do compile }
// 2001-11-25 Phil Edwards <pme@gcc.gnu.org>
//
// Copyright (C) 2001, 2003, 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.
// 20.4.1.1 allocator members
#include <cstdlib>
#include <ext/pool_allocator.h>
template class __gnu_cxx::__pool_alloc<int>;
......@@ -38,6 +38,13 @@
#include <cstddef>
#include <limits>
namespace
{
bool new_called = false;
bool delete_called = false;
std::size_t requested = 0;
};
namespace __gnu_test
{
class allocation_tracker
......@@ -170,9 +177,24 @@ namespace __gnu_test
operator!=(const tracker_alloc<T1>&, const tracker_alloc<T2>&) throw()
{ return false; }
bool
check_construct_destroy(const char* tag, int expected_c, int expected_d);
bool
check_construct_destroy(const char* tag, int expected_c, int expected_d);
template<typename Alloc, bool uses_global_new_and_delete>
bool check_new()
{
bool test __attribute__((unused)) = true;
Alloc a;
typename Alloc::pointer p = a.allocate(10);
if (uses_global_new_and_delete)
test &= ( requested >= (10 * 15 * sizeof(long)) );
test &= ( new_called == uses_global_new_and_delete );
a.deallocate(p, 10);
test &= ( delete_called == uses_global_new_and_delete );
return test;
}
}; // namespace __gnu_test
#endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment