Commit 210d7a8f by Benjamin Kosnik Committed by Benjamin Kosnik

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

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
parent f1a66265
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> 2004-10-25 Geoffrey Keating <geoffk@apple.com>
* libsupc++/new_op.cc (new): Make weak. * libsupc++/new_op.cc (new): Make weak.
......
...@@ -297,7 +297,7 @@ ...@@ -297,7 +297,7 @@
<td>&lt;memory&gt;</td> <td>&lt;memory&gt;</td>
</tr> </tr>
<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>&lt;ext/pool_allocator.h&gt;</td>
<td>std::__default_alloc_template&lt;bool,int&gt;</td> <td>std::__default_alloc_template&lt;bool,int&gt;</td>
<td>&lt;memory&gt;</td> <td>&lt;memory&gt;</td>
...@@ -316,7 +316,26 @@ ...@@ -316,7 +316,26 @@
</tr> </tr>
</table> </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> <ul>
<li><code>new_allocator</code> <li><code>new_allocator</code>
<p>Simply wraps <code>::operator new</code> <p>Simply wraps <code>::operator new</code>
...@@ -330,6 +349,18 @@ ...@@ -330,6 +349,18 @@
elsewhere). elsewhere).
</p> </p>
</li> </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> <li><code>debug_allocator</code>
<p> A wrapper around an <p> A wrapper around an
arbitrary allocator A. It passes on slightly increased size arbitrary allocator A. It passes on slightly increased size
...@@ -347,10 +378,15 @@ ...@@ -347,10 +378,15 @@
and the allocate/deallocate request is passed to and the allocate/deallocate request is passed to
<code>::operator new</code> directly. </p> <code>::operator new</code> directly. </p>
<p> This class take a boolean template parameter, called <p> For versions of <code>__pool_alloc</code> after 3.4.0, there is
<code>thr</code>, and an integer template parameter, called only one template parameter, as per the standard.
<code>inst</code>. </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>
<p>The <code>inst</code> number is used to track additional memory <p>The <code>inst</code> number is used to track additional memory
pools. The point of the number is to allow multiple pools. The point of the number is to allow multiple
instantiations of the classes without changing the semantics at instantiations of the classes without changing the semantics at
...@@ -374,6 +410,12 @@ ...@@ -374,6 +410,12 @@
is is threadsafe, while thr=false, and is slightly faster but is is threadsafe, while thr=false, and is slightly faster but
unsafe for multiple threads. unsafe for multiple threads.
</p> </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 <p>(Note that the GCC thread abstraction layer allows us to provide safe
zero-overhead stubs for the threading routines, if threads were zero-overhead stubs for the threading routines, if threads were
disabled at configuration time.) disabled at configuration time.)
......
...@@ -118,7 +118,7 @@ namespace __gnu_cxx ...@@ -118,7 +118,7 @@ namespace __gnu_cxx
allocate(size_type __n, const void* = 0) allocate(size_type __n, const void* = 0)
{ {
static size_type __used; 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(); std::__throw_bad_alloc();
pointer __ret = _M_array->begin() + __used; pointer __ret = _M_array->begin() + __used;
__used += __n; __used += __n;
......
// 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;
}
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