This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[v3] Fix libstdc++/18654
- From: Paolo Carlini <pcarlini at suse dot de>
- To: "'gcc-patches at gcc dot gnu dot org'" <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 29 Nov 2004 14:03:55 +0100
- Subject: [v3] Fix libstdc++/18654
Hi,
tested x86-linux, committed to mainline.
Paolo.
//////////////
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.
diff -urN libstdc++-v3-orig/include/bits/basic_string.tcc libstdc++-v3/include/bits/basic_string.tcc
--- libstdc++-v3-orig/include/bits/basic_string.tcc 2004-11-24 05:11:09.000000000 +0100
+++ libstdc++-v3/include/bits/basic_string.tcc 2004-11-29 12:29:56.000000000 +0100
@@ -577,7 +577,7 @@
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 -urN libstdc++-v3-orig/testsuite/21_strings/basic_string/capacity/char/18654.cc libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/18654.cc
--- libstdc++-v3-orig/testsuite/21_strings/basic_string/capacity/char/18654.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/18654.cc 2004-11-29 12:49:23.000000000 +0100
@@ -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 -urN libstdc++-v3-orig/testsuite/21_strings/basic_string/capacity/wchar_t/18654.cc libstdc++-v3/testsuite/21_strings/basic_string/capacity/wchar_t/18654.cc
--- libstdc++-v3-orig/testsuite/21_strings/basic_string/capacity/wchar_t/18654.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/21_strings/basic_string/capacity/wchar_t/18654.cc 2004-11-29 12:49:59.000000000 +0100
@@ -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;
+}