locale_facets.tcc (collate<>::do_transform( const _CharT*, const _CharT*)): Simplify previous fix for libstdc++/28277, always allocate memory dynamically.

2006-07-11  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/locale_facets.tcc (collate<>::do_transform(
	const _CharT*, const _CharT*)): Simplify previous fix for
	libstdc++/28277, always allocate memory dynamically.

From-SVN: r115356
This commit is contained in:
Paolo Carlini 2006-07-12 00:05:50 +00:00 committed by Paolo Carlini
parent ad084e9d07
commit 420644ea23
2 changed files with 21 additions and 22 deletions

View File

@ -1,3 +1,9 @@
2006-07-11 Paolo Carlini <pcarlini@suse.de>
* include/bits/locale_facets.tcc (collate<>::do_transform(
const _CharT*, const _CharT*)): Simplify previous fix for
libstdc++/28277, always allocate memory dynamically.
2006-07-11 Paolo Carlini <pcarlini@suse.de> 2006-07-11 Paolo Carlini <pcarlini@suse.de>
PR libstdc++/28344 PR libstdc++/28344

View File

@ -2457,28 +2457,18 @@ _GLIBCXX_END_LDBL_NAMESPACE
{ {
string_type __ret; string_type __ret;
// Use alloca for an _M_transform temporary buffer up to an arbitrary, // strxfrm assumes zero-terminated strings so we make a copy
// but limited, asize, to avoid abusing the stack. Otherwise fall back const string_type __str(__lo, __hi);
// to dynamic memory allocation. This means splitting the computation
// itself in hunks: a size <= 8k (thus <= 16k asize) appear sufficient
// for optimal performance.
const size_t __size = std::min(size_t(__hi - __lo), size_t(8192));
const size_t __asize = 2 * __size;
size_t __len = __asize; const _CharT* __p = __str.c_str();
const _CharT* __pend = __str.data() + __str.length();
_CharT* __c = size_t __len = (__hi - __lo) * 2;
static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len));
for (size_t __hunk = __size; __lo < __hi; _CharT* __c = new _CharT[__len];
__lo += __hunk, __hunk = std::min(size_t(__hi - __lo), __hunk))
try
{ {
// strxfrm assumes zero-terminated strings so we make a copy
const string_type __str(__lo, __lo + __hunk);
const _CharT* __p = __str.c_str();
const _CharT* __pend = __str.data() + __hunk;
// strxfrm stops when it sees a nul character so we break // strxfrm stops when it sees a nul character so we break
// the string into zero-terminated substrings and pass those // the string into zero-terminated substrings and pass those
// to strxfrm. // to strxfrm.
@ -2490,9 +2480,8 @@ _GLIBCXX_END_LDBL_NAMESPACE
// correct size. // correct size.
if (__res >= __len) if (__res >= __len)
{ {
if (__len > __asize)
delete [] __c;
__len = __res + 1; __len = __res + 1;
delete [] __c, __c = 0;
__c = new _CharT[__len]; __c = new _CharT[__len];
__res = _M_transform(__c, __p, __len); __res = _M_transform(__c, __p, __len);
} }
@ -2506,9 +2495,13 @@ _GLIBCXX_END_LDBL_NAMESPACE
__ret.push_back(_CharT()); __ret.push_back(_CharT());
} }
} }
catch(...)
{
delete [] __c;
__throw_exception_again;
}
if (__len > __asize) delete [] __c;
delete [] __c;
return __ret; return __ret;
} }