array_allocator.h (array::allocate): Check for valid array object, use its size member function directly.

2004-10-26  Benjamin Kosnik  <bkoz@redhat.com>

	* 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
This commit is contained in:
Benjamin Kosnik 2004-10-26 06:37:10 +00:00 committed by Benjamin Kosnik
parent f1a6626519
commit 210d7a8f70
4 changed files with 121 additions and 6 deletions

View File

@ -1,3 +1,10 @@
2004-10-26 Benjamin Kosnik <bkoz@redhat.com>
* 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 <geoffk@apple.com>
* libsupc++/new_op.cc (new): Make weak.

View File

@ -297,7 +297,7 @@
<td>&lt;memory&gt;</td>
</tr>
<tr>
<td>__gnu_cxx::__pool_alloc&lt;bool, int&gt;</td>
<td>__gnu_cxx::__pool_alloc&lt;T&gt;</td>
<td>&lt;ext/pool_allocator.h&gt;</td>
<td>std::__default_alloc_template&lt;bool,int&gt;</td>
<td>&lt;memory&gt;</td>
@ -316,7 +316,26 @@
</tr>
</table>
<p>More details on each of these allocators follows. </p>
<p> 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.
</p>
<table title="more extension allocators" border="1">
<tr>
<th>Allocator</th>
<th>Include</th>
<th>Version</th>
</tr>
<tr>
<td>__gnu_cxx::array_allocator&lt;T&gt;</td>
<td>&lt;ext/array_allocator.h&gt;</td>
<td>4.0.0</td>
</tr>
</table>
<p>More details on each of these extension allocators follows. </p>
<ul>
<li><code>new_allocator</code>
<p>Simply wraps <code>::operator new</code>
@ -330,6 +349,18 @@
elsewhere).
</p>
</li>
<li><code>array_allocator</code>
<p>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 <code>::operator new</code> and
<code>::operator delete</code>. 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.
</p>
</li>
<li><code>debug_allocator</code>
<p> 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
<code>::operator new</code> directly. </p>
<p> This class take a boolean template parameter, called
<code>thr</code>, and an integer template parameter, called
<code>inst</code>.
<p> For versions of <code>__pool_alloc</code> after 3.4.0, there is
only one template parameter, as per the standard.
</p>
<p> Older versions of this class take a boolean template parameter,
called <code>thr</code>, and an integer template parameter,
called <code>inst</code>.
</p>
<p>The <code>inst</code> 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.
</p>
<p>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.
</p>
<p>(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.)

View File

@ -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;

View File

@ -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 <cassert>
#include <string>
#include <ext/array_allocator.h>
typedef char char_type;
typedef std::char_traits<char_type> traits_type;
typedef std::tr1::array<char_type, 4> array_type;
array_type extern_array;
void test01()
{
using std::basic_string;
typedef __gnu_cxx::array_allocator<char_type, array_type> allocator_type;
typedef basic_string<char_type, traits_type, allocator_type> 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;
}