Commit d38d4e5d by Benjamin Kosnik Committed by Benjamin Kosnik

new_allocator.h (new_allocator): Proper allocator class.


2003-12-23  Benjamin Kosnik  <bkoz@redhat.com>

	* include/ext/new_allocator.h (new_allocator): Proper allocator class.
	(__new_alloc): Delete.
	* include/ext/malloc_allocator.h (malloc_allocator): Same.
	(__malloc_alloc): Delete.
 	* include/ext/mt_allocator.h: Same, but weakly.
 	* include/ext/debug_allocator.h: Convert to the new style.
	* include/ext/pool_allocator.h: Use global new and delete directly.
	* include/backward/alloc.h: Don't inject malloc_allocator, or
	debug_allocator.
	* testsuite/ext/allocators.cc: Minimal fixups for usage of new
	classes.  Comment out tests with __pool_alloc for now.
	* testsuite/performance/allocator.cc: Same.

From-SVN: r74965
parent cbf6e52a
2003-12-23 Benjamin Kosnik <bkoz@redhat.com>
* include/ext/new_allocator.h (new_allocator): Proper allocator class.
(__new_alloc): Delete.
* include/ext/malloc_allocator.h (malloc_allocator): Same.
(__malloc_alloc): Delete.
* include/ext/mt_allocator.h: Same, but weakly.
* include/ext/debug_allocator.h: Convert to the new style.
* include/ext/pool_allocator.h: Use global new and delete directly.
* include/backward/alloc.h: Don't inject malloc_allocator, or
debug_allocator.
* testsuite/ext/allocators.cc: Minimal fixups for usage of new
classes. Comment out tests with __pool_alloc for now.
* testsuite/performance/allocator.cc: Same.
2003-12-22 Matt Austern <austern@apple.com> 2003-12-22 Matt Austern <austern@apple.com>
* include/bits/stl_bvector.h (_Bvector_alloc_base): Eliminate. * include/bits/stl_bvector.h (_Bvector_alloc_base): Eliminate.
......
...@@ -46,11 +46,7 @@ ...@@ -46,11 +46,7 @@
#include "backward_warning.h" #include "backward_warning.h"
#include <bits/c++config.h> #include <bits/c++config.h>
#include <bits/allocator.h> #include <bits/allocator.h>
#include <ext/debug_allocator.h>
#include <ext/malloc_allocator.h>
using __gnu_cxx::__malloc_alloc;
using __gnu_cxx::__debug_alloc;
using __gnu_cxx::__pool_alloc; using __gnu_cxx::__pool_alloc;
using std::__alloc; using std::__alloc;
using std::__simple_alloc; using std::__simple_alloc;
......
...@@ -48,92 +48,58 @@ ...@@ -48,92 +48,58 @@
#ifndef _DEBUG_ALLOCATOR_H #ifndef _DEBUG_ALLOCATOR_H
#define _DEBUG_ALLOCATOR_H 1 #define _DEBUG_ALLOCATOR_H 1
#include <bits/allocator_traits.h> #include <memory>
namespace __gnu_cxx namespace __gnu_cxx
{ {
/** /**
* @if maint * @brief A meta-allocator with debugging bits, as per [20.4].
* An adaptor for an underlying allocator (_Alloc) to check the size
* arguments for debugging.
* *
* "There is some evidence that this can confuse Purify." - SGI comment * This is precisely the allocator defined in the C++ Standard.
* - all allocation calls operator new
* - all deallocation calls operator delete
* *
* This adaptor is "SGI" style. The _Alloc parameter must also be "SGI".
* @endif
* (See @link Allocators allocators info @endlink for more.) * (See @link Allocators allocators info @endlink for more.)
*/ */
template<typename _Alloc> template<typename _Alloc>
class __debug_alloc class debug_allocator
{ {
public:
typedef typename _Alloc::size_type size_type;
typedef typename _Alloc::difference_type difference_type;
typedef typename _Alloc::pointer pointer;
typedef typename _Alloc::const_pointer const_pointer;
typedef typename _Alloc::reference reference;
typedef typename _Alloc::const_reference const_reference;
typedef typename _Alloc::value_type value_type;
private: private:
// Size of space used to store size. Note that this must be // Size of space used to store size. Note that this must be
// large enough to preserve alignment. // large enough to preserve alignment.
enum {_S_extra = 8}; const size_t _M_extra;
_Alloc _M_allocator;
public: public:
static void* debug_allocator() : _M_extra(8) { }
allocate(size_t __n)
pointer
allocate(size_type __n, std::allocator<void>::const_pointer = 0)
{ {
char* __result = (char*)_Alloc::allocate(__n + (int) _S_extra); pointer __result = _M_allocator.allocate(__n + _M_extra);
*(size_t*)__result = __n; *__result = __n;
return __result + (int) _S_extra; return __result + _M_extra;
} }
static void void
deallocate(void* __p, size_t __n) deallocate(pointer __p, size_type __n)
{ {
char* __real_p = (char*)__p - (int) _S_extra; pointer __real_p = __p - _M_extra;
if (*(size_t*)__real_p != __n) if (*__real_p != __n)
abort(); abort();
_Alloc::deallocate(__real_p, __n + (int) _S_extra); _M_allocator.deallocate(__real_p, __n + _M_extra);
} }
}; };
//@{
/** Comparison operators for all of the predifined SGI-style allocators.
* This ensures that __allocator<malloc_alloc> (for example) will work
* correctly. As required, all allocators compare equal.
*/
template<typename _Alloc>
inline bool
operator==(const __debug_alloc<_Alloc>&, const __debug_alloc<_Alloc>&)
{ return true; }
template<typename _Alloc>
inline bool
operator!=(const __debug_alloc<_Alloc>&, const __debug_alloc<_Alloc>&)
{ return false; }
//@}
} // namespace __gnu_cxx } // namespace __gnu_cxx
namespace std
{
//@{
/// Versions for the predefined "SGI" style allocators.
template<typename _Tp, typename _Alloc>
struct _Alloc_traits<_Tp, __gnu_cxx::__debug_alloc<_Alloc> >
{
static const bool _S_instanceless = true;
typedef __gnu_cxx::__debug_alloc<_Alloc> base_alloc_type;
typedef __simple_alloc<_Tp, base_alloc_type> _Alloc_type;
typedef __allocator<_Tp, base_alloc_type> allocator_type;
};
//@}
//@{
/// Versions for the __allocator adaptor used with the predefined
/// "SGI" style allocators.
template<typename _Tp, typename _Tp1, typename _Alloc>
struct _Alloc_traits<_Tp, __allocator<_Tp1,
__gnu_cxx::__debug_alloc<_Alloc> > >
{
static const bool _S_instanceless = true;
typedef __gnu_cxx::__debug_alloc<_Alloc> base_alloc_type;
typedef __simple_alloc<_Tp, base_alloc_type> _Alloc_type;
typedef __allocator<_Tp, base_alloc_type> allocator_type;
};
//@}
} // namespace std
#endif #endif
// Allocators -*- C++ -*- // Allocator that wraps "C" malloc -*- C++ -*-
// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
// //
...@@ -27,137 +27,78 @@ ...@@ -27,137 +27,78 @@
// invalidate any other reasons why the executable file might be covered by // invalidate any other reasons why the executable file might be covered by
// the GNU General Public License. // the GNU General Public License.
/*
* Copyright (c) 1996-1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/** @file ext/debug_allocator.h
* This file is a GNU extension to the Standard C++ Library.
* You should only include this header if you are using GCC 3 or later.
*/
#ifndef _MALLOC_ALLOCATOR_H #ifndef _MALLOC_ALLOCATOR_H
#define _MALLOC_ALLOCATOR_H 1 #define _MALLOC_ALLOCATOR_H 1
#include <bits/allocator_traits.h> #include <new>
#include <memory>
namespace __gnu_cxx namespace __gnu_cxx
{ {
/** /**
* @if maint * @brief An allocator that uses malloc
* A malloc-based allocator. Typically slower than the *
* __pool_alloc (below). Typically thread-safe and more * This is precisely the allocator defined in the C++ Standard.
* storage efficient. The template argument is unused and is only present * - all allocation calls malloc
* to permit multiple instantiations (but see __pool_alloc * - all deallocation calls free
* for caveats). "SGI" style, plus __set_malloc_handler for OOM conditions. *
* @endif
* (See @link Allocators allocators info @endlink for more.) * (See @link Allocators allocators info @endlink for more.)
*/ */
template<int __inst> template<typename _Tp>
class __malloc_alloc class malloc_allocator
{ {
private:
static void* _S_oom_malloc(size_t);
static void (* __malloc_alloc_oom_handler)();
public: public:
static void* typedef size_t size_type;
allocate(size_t __n) typedef ptrdiff_t difference_type;
{ typedef _Tp* pointer;
void* __result = malloc(__n); typedef const _Tp* const_pointer;
if (__builtin_expect(__result == 0, 0)) typedef _Tp& reference;
__result = _S_oom_malloc(__n); typedef const _Tp& const_reference;
return __result; typedef _Tp value_type;
}
static void
deallocate(void* __p, size_t /* __n */)
{ free(__p); }
static void (* __set_malloc_handler(void (*__f)()))()
{
void (* __old)() = __malloc_alloc_oom_handler;
__malloc_alloc_oom_handler = __f;
return __old;
}
};
// malloc_alloc out-of-memory handling template<typename _Tp1>
template<int __inst> struct rebind
void (* __malloc_alloc<__inst>::__malloc_alloc_oom_handler)() = 0; { typedef malloc_allocator<_Tp1> other; };
template<int __inst> malloc_allocator() throw() { }
void*
__malloc_alloc<__inst>::
_S_oom_malloc(size_t __n)
{
void (* __my_malloc_handler)();
void* __result;
for (;;)
{
__my_malloc_handler = __malloc_alloc_oom_handler;
if (__builtin_expect(__my_malloc_handler == 0, 0))
__throw_bad_alloc();
(*__my_malloc_handler)();
__result = malloc(__n);
if (__result)
return __result;
}
}
//@{
/** Comparison operators for all of the predifined SGI-style allocators.
* This ensures that __allocator<malloc_alloc> (for example) will work
* correctly. As required, all allocators compare equal.
*/
template<int inst>
inline bool
operator==(const __malloc_alloc<inst>&, const __malloc_alloc<inst>&)
{ return true; }
template<int __inst>
inline bool
operator!=(const __malloc_alloc<__inst>&, const __malloc_alloc<__inst>&)
{ return false; }
//@}
} // namespace __gnu_cxx
namespace std malloc_allocator(const malloc_allocator&) throw() { }
{
//@{ template<typename _Tp1>
/// Versions for the predefined "SGI" style allocators. malloc_allocator(const malloc_allocator<_Tp1>&) throw() { }
template<typename _Tp, int __inst>
struct _Alloc_traits<_Tp, __gnu_cxx::__malloc_alloc<__inst> > ~malloc_allocator() throw() { }
{
static const bool _S_instanceless = true; pointer
typedef __gnu_cxx:: __malloc_alloc<__inst> base_alloc_type; address(reference __x) const { return &__x; }
typedef __simple_alloc<_Tp, base_alloc_type> _Alloc_type;
typedef __allocator<_Tp, base_alloc_type> allocator_type; const_pointer
}; address(const_reference __x) const { return &__x; }
//@}
// NB: __n is permitted to be 0. The C++ standard says nothing
//@{ // about what the return value is when __n == 0.
/// Versions for the __allocator adaptor used with the predefined pointer
/// "SGI" style allocators. allocate(size_type __n, std::allocator<void>::const_pointer __h = 0)
template<typename _Tp, typename _Tp1, int __inst> { return static_cast<_Tp*>(malloc(__n * sizeof(_Tp))); }
struct _Alloc_traits<_Tp, __allocator<_Tp1,
__gnu_cxx::__malloc_alloc<__inst> > > // __p is not permitted to be a null pointer.
{ void
static const bool _S_instanceless = true; deallocate(pointer __p, size_type __n)
typedef __gnu_cxx:: __malloc_alloc<__inst> base_alloc_type; { free(static_cast<void*>(__p)); }
typedef __simple_alloc<_Tp, base_alloc_type> _Alloc_type;
typedef __allocator<_Tp, base_alloc_type> allocator_type; size_type
max_size() const throw()
{ return size_t(-1) / sizeof(_Tp); }
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 402. wrong new expression in [some_] allocator::construct
void
construct(pointer __p, const _Tp& __val)
{ *__p = __val; }
void
destroy(pointer __p) { __p->~_Tp(); }
}; };
//@} } // namespace __gnu_cxx
} // namespace std
#endif #endif
// Allocators -*- C++ -*- // Allocator that wraps operator new -*- C++ -*-
// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
// //
...@@ -35,24 +35,69 @@ ...@@ -35,24 +35,69 @@
namespace __gnu_cxx namespace __gnu_cxx
{ {
/** /**
* @if maint * @brief An allocator that uses global new, as per [20.4].
* A new-based allocator, as required by the standard. Allocation and *
* deallocation forward to global new and delete. "SGI" style, minus * This is precisely the allocator defined in the C++ Standard.
* reallocate(). * - all allocation calls operator new
* @endif * - all deallocation calls operator delete
*
* (See @link Allocators allocators info @endlink for more.) * (See @link Allocators allocators info @endlink for more.)
*/ */
class __new_alloc template<typename _Tp>
{ class new_allocator
public: {
static void* public:
allocate(size_t __n) typedef size_t size_type;
{ return ::operator new(__n); } typedef ptrdiff_t difference_type;
typedef _Tp* pointer;
static void typedef const _Tp* const_pointer;
deallocate(void* __p, size_t) typedef _Tp& reference;
{ ::operator delete(__p); } typedef const _Tp& const_reference;
}; typedef _Tp value_type;
template<typename _Tp1>
struct rebind
{ typedef new_allocator<_Tp1> other; };
new_allocator() throw() { }
new_allocator(const new_allocator&) throw() { }
template<typename _Tp1>
new_allocator(const new_allocator<_Tp1>&) throw() { }
~new_allocator() throw() { }
pointer
address(reference __x) const { return &__x; }
const_pointer
address(const_reference __x) const { return &__x; }
// NB: __n is permitted to be 0. The C++ standard says nothing
// about what the return value is when __n == 0.
pointer
allocate(size_type __n, allocator<void>::const_pointer __h = 0)
{ return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); }
// __p is not permitted to be a null pointer.
void
deallocate(pointer __p, size_type __n)
{ ::operator delete(__p); }
size_type
max_size() const throw()
{ return size_t(-1) / sizeof(_Tp); }
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 402. wrong new expression in [some_] allocator::construct
void
construct(pointer __p, const _Tp& __val)
{ ::new(__p) _Tp(__val); }
void
destroy(pointer __p) { __p->~_Tp(); }
};
} // namespace __gnu_cxx } // namespace __gnu_cxx
#endif #endif
...@@ -44,15 +44,14 @@ ...@@ -44,15 +44,14 @@
* This file is a GNU extension to the Standard C++ Library. * This file is a GNU extension to the Standard C++ Library.
* You should only include this header if you are using GCC 3 or later. * You should only include this header if you are using GCC 3 or later.
*/ */
#ifndef _POOL_ALLOCATOR_H #ifndef _POOL_ALLOCATOR_H
#define _POOL_ALLOCATOR_H 1 #define _POOL_ALLOCATOR_H 1
#include <new>
#include <bits/functexcept.h> #include <bits/functexcept.h>
#include <bits/stl_threads.h> #include <bits/stl_threads.h>
#include <bits/atomicity.h> #include <bits/atomicity.h>
#include <bits/allocator_traits.h> #include <bits/allocator_traits.h>
#include <ext/new_allocator.h>
namespace __gnu_cxx namespace __gnu_cxx
{ {
...@@ -65,9 +64,9 @@ namespace __gnu_cxx ...@@ -65,9 +64,9 @@ namespace __gnu_cxx
* when in default high-speed pool mode). * when in default high-speed pool mode).
* *
* Important implementation properties: * Important implementation properties:
* 0. If globally mandated, then allocate objects from __new_alloc * 0. If globally mandated, then allocate objects from new
* 1. If the clients request an object of size > _S_max_bytes, the resulting * 1. If the clients request an object of size > _S_max_bytes, the resulting
* object will be obtained directly from __new_alloc * object will be obtained directly from new
* 2. In all other cases, we allocate an object of size exactly * 2. In all other cases, we allocate an object of size exactly
* _S_round_up(requested_size). Thus the client has enough size * _S_round_up(requested_size). Thus the client has enough size
* information that we can return the object to the proper free list * information that we can return the object to the proper free list
...@@ -201,7 +200,7 @@ namespace __gnu_cxx ...@@ -201,7 +200,7 @@ namespace __gnu_cxx
((_Obj*)(void*)_S_start_free)->_M_free_list_link = *__free_list; ((_Obj*)(void*)_S_start_free)->_M_free_list_link = *__free_list;
*__free_list = (_Obj*)(void*)_S_start_free; *__free_list = (_Obj*)(void*)_S_start_free;
} }
_S_start_free = (char*) __new_alloc::allocate(__bytes_to_get); _S_start_free = new char[__bytes_to_get];
if (_S_start_free == 0) if (_S_start_free == 0)
{ {
size_t __i; size_t __i;
...@@ -226,7 +225,7 @@ namespace __gnu_cxx ...@@ -226,7 +225,7 @@ namespace __gnu_cxx
} }
} }
_S_end_free = 0; // In case of exception. _S_end_free = 0; // In case of exception.
_S_start_free = (char*)__new_alloc::allocate(__bytes_to_get); _S_start_free = new char[__bytes_to_get];
// This should either throw an exception or remedy the situation. // This should either throw an exception or remedy the situation.
// Thus we assume it succeeded. // Thus we assume it succeeded.
} }
...@@ -291,7 +290,7 @@ namespace __gnu_cxx ...@@ -291,7 +290,7 @@ namespace __gnu_cxx
} }
if ((__n > (size_t) _S_max_bytes) || (_S_force_new > 0)) if ((__n > (size_t) _S_max_bytes) || (_S_force_new > 0))
__ret = __new_alloc::allocate(__n); __ret = new char[__n];
else else
{ {
_Obj* volatile* __free_list = _S_free_list + _S_freelist_index(__n); _Obj* volatile* __free_list = _S_free_list + _S_freelist_index(__n);
...@@ -318,7 +317,7 @@ namespace __gnu_cxx ...@@ -318,7 +317,7 @@ namespace __gnu_cxx
__pool_alloc<__threads, __inst>::deallocate(void* __p, size_t __n) __pool_alloc<__threads, __inst>::deallocate(void* __p, size_t __n)
{ {
if ((__n > (size_t) _S_max_bytes) || (_S_force_new > 0)) if ((__n > (size_t) _S_max_bytes) || (_S_force_new > 0))
__new_alloc::deallocate(__p, __n); delete [] __p;
else else
{ {
_Obj* volatile* __free_list = _S_free_list + _S_freelist_index(__n); _Obj* volatile* __free_list = _S_free_list + _S_freelist_index(__n);
......
...@@ -22,25 +22,23 @@ ...@@ -22,25 +22,23 @@
#include <cstdlib> #include <cstdlib>
#include <memory> #include <memory>
#include <ext/pool_allocator.h> //#include <ext/pool_allocator.h>
#include <ext/debug_allocator.h> #include <ext/debug_allocator.h>
#include <ext/malloc_allocator.h> #include <ext/malloc_allocator.h>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
using __gnu_cxx::__malloc_alloc; using __gnu_cxx::malloc_allocator;
using __gnu_cxx::__debug_alloc; using __gnu_cxx::debug_allocator;
using __gnu_cxx::__pool_alloc;
template class __malloc_alloc<3>;
template class __debug_alloc<__malloc_alloc<3> >;
template class __pool_alloc<true, 3>;
template class __pool_alloc<false, 3>;
struct big template class malloc_allocator<int>;
{ template class debug_allocator<malloc_allocator<int> >;
long f[15];
};
#if 0
using __gnu_cxx::__pool_alloc;
template class __pool_alloc<true, 3>;
template class __pool_alloc<false, 3>;
#endif
bool new_called; bool new_called;
bool delete_called; bool delete_called;
...@@ -69,31 +67,39 @@ void check_allocator() ...@@ -69,31 +67,39 @@ void check_allocator()
delete_called = false; delete_called = false;
requested = 0; requested = 0;
std::__allocator<big, Alloc> a; Alloc a;
big *p = a.allocate(10); typename Alloc::pointer p = a.allocate(10);
if (uses_global_new_and_delete) if (uses_global_new_and_delete)
VERIFY( requested >= (10 * 15 * sizeof(long)) ); VERIFY( requested >= (10 * 15 * sizeof(long)) );
// Touch the far end of supposedly-allocated memory to check that we got
// all of it. Why "3"? Because it's my favorite integer between e and pi.
p[9].f[14] = 3;
VERIFY( new_called == uses_global_new_and_delete ); VERIFY( new_called == uses_global_new_and_delete );
a.deallocate(p,10); a.deallocate(p, 10);
VERIFY( delete_called == uses_global_new_and_delete ); VERIFY( delete_called == uses_global_new_and_delete );
} }
// These just help tracking down error messages. // These just help tracking down error messages.
void test01() { check_allocator<__malloc_alloc<3>, false>(); } void test01()
void test02() { check_allocator<__debug_alloc<__malloc_alloc<3> >, false>(); } { check_allocator<malloc_allocator<int>, false>(); }
void test03() { check_allocator<__pool_alloc<true, 3>, true>(); }
void test04() { check_allocator<__pool_alloc<false, 3>, true>(); } 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() int main()
{ {
test01(); test01();
test02(); test02();
#if 0
test03(); test03();
test04(); test04();
#endif
return 0; return 0;
} }
...@@ -61,7 +61,7 @@ ...@@ -61,7 +61,7 @@
#include <testsuite_performance.h> #include <testsuite_performance.h>
using namespace std; using namespace std;
using __gnu_cxx::__malloc_alloc; using __gnu_cxx::malloc_allocator;
using __gnu_cxx::__mt_alloc; using __gnu_cxx::__mt_alloc;
/* /*
...@@ -155,7 +155,7 @@ test_ints_malloc_alloc(int iterations) ...@@ -155,7 +155,7 @@ test_ints_malloc_alloc(int iterations)
tstart(); tstart();
for(int i = 0; i < iterations; i++) for(int i = 0; i < iterations; i++)
{ {
vector<int, __malloc_alloc<0> > v1; vector<int, malloc_allocator<int> > v1;
for(int j = 0; j < insert_values; j++) for(int j = 0; j < insert_values; j++)
{ {
...@@ -173,7 +173,7 @@ test_ints_mt_alloc(int iterations) ...@@ -173,7 +173,7 @@ test_ints_mt_alloc(int iterations)
tstart(); tstart();
for(int i = 0; i < iterations; i++) for(int i = 0; i < iterations; i++)
{ {
vector<int, __mt_alloc<0> > v1; vector<int, __mt_alloc<int> > v1;
for(int j = 0; j < insert_values; j++) for(int j = 0; j < insert_values; j++)
{ {
......
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