From 210d7a8f70203c5220d9f8b5545f21623f2d7634 Mon Sep 17 00:00:00 2001 From: Benjamin Kosnik Date: Tue, 26 Oct 2004 06:37:10 +0000 Subject: [PATCH] array_allocator.h (array::allocate): Check for valid array object, use its size member function directly. 2004-10-26 Benjamin Kosnik * include/ext/array_allocator.h (array::allocate): Check for valid array object, use its size member function directly. * testsuite/ext/array_allocator/3.cc: New. * docs/html/20_util/allocator.html: Add docs. From-SVN: r89573 --- libstdc++-v3/ChangeLog | 7 ++ libstdc++-v3/docs/html/20_util/allocator.html | 52 +++++++++++++-- libstdc++-v3/include/ext/array_allocator.h | 2 +- .../testsuite/ext/array_allocator/3.cc | 66 +++++++++++++++++++ 4 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 libstdc++-v3/testsuite/ext/array_allocator/3.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 18b574b1b72..6404896d390 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2004-10-26 Benjamin Kosnik + + * include/ext/array_allocator.h (array::allocate): Check for valid + array object, use its size member function directly. + * testsuite/ext/array_allocator/3.cc: New. + * docs/html/20_util/allocator.html: Add docs. + 2004-10-25 Geoffrey Keating * libsupc++/new_op.cc (new): Make weak. diff --git a/libstdc++-v3/docs/html/20_util/allocator.html b/libstdc++-v3/docs/html/20_util/allocator.html index 791649a0026..c853dd4cf8f 100644 --- a/libstdc++-v3/docs/html/20_util/allocator.html +++ b/libstdc++-v3/docs/html/20_util/allocator.html @@ -297,7 +297,7 @@ <memory> - __gnu_cxx::__pool_alloc<bool, int> + __gnu_cxx::__pool_alloc<T> <ext/pool_allocator.h> std::__default_alloc_template<bool,int> <memory> @@ -316,7 +316,26 @@ -

More details on each of these allocators follows.

+

Releases after gcc-3.4 have continued to add to the collection + of available allocators. All of these new allocators are + standard-style. The following table includes details, along with + the first released version of GCC that included the extension allocator. +

+ + + + + + + + + + + + +
AllocatorIncludeVersion
__gnu_cxx::array_allocator<T><ext/array_allocator.h>4.0.0
+ +

More details on each of these extension allocators follows.

  • new_allocator

    Simply wraps ::operator new @@ -330,6 +349,18 @@ elsewhere).

  • +
  • array_allocator +

    Allows allocations of known and fixed sizes using existing + global or external storage allocated via construction of + std::tr1::array objects. By using this allocator, fixed size + containers (including std::string) can be used without + instances calling ::operator new and + ::operator delete. This capability allows the + use of STL abstractions without runtime complications or + overhead, even in situations such as program startup. For + usage examples, please consult the libstdc++ testsuite. +

    +
  • debug_allocator

    A wrapper around an arbitrary allocator A. It passes on slightly increased size @@ -347,10 +378,15 @@ and the allocate/deallocate request is passed to ::operator new directly.

    -

    This class take a boolean template parameter, called - thr, and an integer template parameter, called - inst. +

    For versions of __pool_alloc after 3.4.0, there is + only one template parameter, as per the standard.

    + +

    Older versions of this class take a boolean template parameter, + called thr, and an integer template parameter, + called inst. +

    +

    The inst number is used to track additional memory pools. The point of the number is to allow multiple instantiations of the classes without changing the semantics at @@ -374,6 +410,12 @@ is is threadsafe, while thr=false, and is slightly faster but unsafe for multiple threads.

    + +

    For thread-enabled configurations, the pool is locked with a + single big lock. In some situations, this implementation detail may + result in severe performance degredation. +

    +

    (Note that the GCC thread abstraction layer allows us to provide safe zero-overhead stubs for the threading routines, if threads were disabled at configuration time.) diff --git a/libstdc++-v3/include/ext/array_allocator.h b/libstdc++-v3/include/ext/array_allocator.h index 294ee68198f..1bba58a3429 100644 --- a/libstdc++-v3/include/ext/array_allocator.h +++ b/libstdc++-v3/include/ext/array_allocator.h @@ -118,7 +118,7 @@ namespace __gnu_cxx allocate(size_type __n, const void* = 0) { static size_type __used; - if (__builtin_expect(__used + __n > array_type::_S_index, false)) + if (_M_array == 0 || __used + __n > _M_array->size()) std::__throw_bad_alloc(); pointer __ret = _M_array->begin() + __used; __used += __n; diff --git a/libstdc++-v3/testsuite/ext/array_allocator/3.cc b/libstdc++-v3/testsuite/ext/array_allocator/3.cc new file mode 100644 index 00000000000..297b902c883 --- /dev/null +++ b/libstdc++-v3/testsuite/ext/array_allocator/3.cc @@ -0,0 +1,66 @@ +// 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 +#include +#include + +typedef char char_type; +typedef std::char_traits traits_type; +typedef std::tr1::array array_type; + +array_type extern_array; + +void test01() +{ + using std::basic_string; + typedef __gnu_cxx::array_allocator allocator_type; + typedef basic_string string_type; + + // Construct array_allocator without underlying array. + allocator_type a; + string_type s(a); + + try + { + s.reserve(4); // Actually need 4 + 1 + sizeof(std::string::_Rep). + } + catch(std::bad_alloc& obj) + { + assert(true); + } + catch(...) + { + assert(false); + } +} + +int main() +{ + test01(); + return 0; +}