re PR libstdc++/18654 (Shrink-to-fit std::string::reserve() calls can reallocate & copy string contents unnecessarily)

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
This commit is contained in:
Paolo Carlini 2004-11-29 12:59:53 +00:00 committed by Paolo Carlini
parent 1cb1fa517f
commit d1a7222cce
4 changed files with 121 additions and 1 deletions

View 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

View 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);

View File

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

View File

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