]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR libstdc++/18654 (Shrink-to-fit std::string::reserve() calls can reallocate...
authorPaolo Carlini <pcarlini@suse.de>
Mon, 29 Nov 2004 12:59:53 +0000 (12:59 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Mon, 29 Nov 2004 12:59:53 +0000 (12:59 +0000)
2004-11-29  Paolo Carlini  <pcarlini@suse.de>

PR libstdc++/18654
* include/bits/basic_string.tcc (_Rep::_S_create): When shrinking,
do not round to pagesize.
* testsuite/21_strings/basic_string/capacity/char/18654.cc: New.
* testsuite/21_strings/basic_string/capacity/wchar_t/18654.cc: New.

From-SVN: r91466

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/basic_string.tcc
libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/18654.cc [new file with mode: 0644]
libstdc++-v3/testsuite/21_strings/basic_string/capacity/wchar_t/18654.cc [new file with mode: 0644]

index 0dee3c5cef7eb126245e057cedf9febac5c8e6ec..b0d82f84ab69141e375b54f873adc8a71ca77b16 100644 (file)
@@ -1,3 +1,11 @@
+2004-11-29  Paolo Carlini  <pcarlini@suse.de>
+
+       PR libstdc++/18654
+       * include/bits/basic_string.tcc (_Rep::_S_create): When shrinking,
+       do not round to pagesize.
+       * testsuite/21_strings/basic_string/capacity/char/18654.cc: New.
+       * testsuite/21_strings/basic_string/capacity/wchar_t/18654.cc: New.
+
 2004-11-27  Paolo Carlini  <pcarlini@suse.de>
 
        PR libstdc++/18690
index 872156f12aba730b051863a15a855879049304e7..3bdce0f090abcbe59d3e178407ec4e92c3878760 100644 (file)
@@ -577,7 +577,7 @@ namespace std
       size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
 
       const size_type __adj_size = __size + __malloc_header_size;
-      if (__adj_size > __pagesize)
+      if (__adj_size > __pagesize && __capacity > __old_capacity)
        {
          const size_type __extra = __pagesize - __adj_size % __pagesize;
          __capacity += __extra / sizeof(_CharT);
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/18654.cc b/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/18654.cc
new file mode 100644 (file)
index 0000000..16dd7ea
--- /dev/null
@@ -0,0 +1,56 @@
+// 2004-11-29  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2004 Free Software Foundation
+//
+// 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.
+
+// 21.3.3 string capacity
+
+#include <string>
+#include <testsuite_hooks.h>
+
+// libstdc++/18654
+void test01()
+{
+  using namespace std;
+  bool test __attribute__((unused)) = true;
+
+  typedef string::size_type size_type;
+
+  // Our current implementation provides exact shrink-to-size
+  // and shrink-to-fit (in the future, maybe this will change
+  // for short strings).
+  size_type min = 2 << 0;
+  size_type max = 2 << 20;
+  for (size_type i = min; i <= max; i *= 2)
+    {
+      string str(i, 'x');
+      str.reserve(3 * i);
+
+      str.reserve(2 * i);
+      VERIFY( str.capacity() == 2 * i );
+
+      str.reserve();
+      VERIFY( str.capacity() == i );
+    }
+}
+
+int main() 
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/capacity/wchar_t/18654.cc b/libstdc++-v3/testsuite/21_strings/basic_string/capacity/wchar_t/18654.cc
new file mode 100644 (file)
index 0000000..1b683bb
--- /dev/null
@@ -0,0 +1,56 @@
+// 2004-11-29  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2004 Free Software Foundation
+//
+// 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.
+
+// 21.3.3 string capacity
+
+#include <string>
+#include <testsuite_hooks.h>
+
+// libstdc++/18654
+void test01()
+{
+  using namespace std;
+  bool test __attribute__((unused)) = true;
+
+  typedef wstring::size_type size_type;
+
+  // Our current implementation provides exact shrink-to-size
+  // and shrink-to-fit (in the future, maybe this will change
+  // for short strings).
+  size_type min = 2 << 0;
+  size_type max = 2 << 20;
+  for (size_type i = min; i <= max; i *= 2)
+    {
+      wstring str(i, L'x');
+      str.reserve(3 * i);
+
+      str.reserve(2 * i);
+      VERIFY( str.capacity() == 2 * i );
+
+      str.reserve();
+      VERIFY( str.capacity() == i );
+    }
+}
+
+int main() 
+{
+  test01();
+  return 0;
+}