array_allocator.h (array::allocate): Check for valid array object, use its size membe...
authorBenjamin Kosnik <bkoz@redhat.com>
Tue, 26 Oct 2004 06:37:10 +0000 (06:37 +0000)
committerBenjamin Kosnik <bkoz@gcc.gnu.org>
Tue, 26 Oct 2004 06:37:10 +0000 (06:37 +0000)
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

libstdc++-v3/ChangeLog
libstdc++-v3/docs/html/20_util/allocator.html
libstdc++-v3/include/ext/array_allocator.h
libstdc++-v3/testsuite/ext/array_allocator/3.cc [new file with mode: 0644]

index 18b574b1b723cc68fe6ace22020d363b7087c7a0..6404896d3903198ea7a45266d8c0effb4435e905 100644 (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.
index 791649a00260c8029006f93babea4aa3aedb963e..c853dd4cf8fdbd5895f5531d7c5e7faa50649a5f 100644 (file)
     <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>
   </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>
          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
       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
       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.)
index 294ee68198f4abe0e70c3097bf718b96e9688989..1bba58a34299fe2dc9f65d9997bdcd6fc85fbfc4 100644 (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;
diff --git a/libstdc++-v3/testsuite/ext/array_allocator/3.cc b/libstdc++-v3/testsuite/ext/array_allocator/3.cc
new file mode 100644 (file)
index 0000000..297b902
--- /dev/null
@@ -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;
+}