basic_string.tcc (_M_mutate): Do not reallocate unnecessarily when _M_rep() == &_S_empty_rep() and __new_size...

2004-10-18  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/basic_string.tcc (_M_mutate): Do not reallocate
	unnecessarily when _M_rep() == &_S_empty_rep() and __new_size
	== capacity() (== 0): is ok to just leave everything unchanged.

	* include/bits/basic_string.h: Minor formatting fixes.
	* include/bits/basic_string.tcc: Likewise.

From-SVN: r89199
This commit is contained in:
Paolo Carlini 2004-10-18 08:43:17 +00:00 committed by Paolo Carlini
parent 7e698b2c89
commit cc6e67d442
3 changed files with 50 additions and 26 deletions

View File

@ -1,3 +1,12 @@
2004-10-18 Paolo Carlini <pcarlini@suse.de>
* include/bits/basic_string.tcc (_M_mutate): Do not reallocate
unnecessarily when _M_rep() == &_S_empty_rep() and __new_size
== capacity() (== 0): is ok to just leave everything unchanged.
* include/bits/basic_string.h: Minor formatting fixes.
* include/bits/basic_string.tcc: Likewise.
2004-10-17 Benjamin Kosnik <bkoz@redhat.com> 2004-10-17 Benjamin Kosnik <bkoz@redhat.com>
* include/ext/mt_allocator.h (__pool::_M_get_align): New. * include/ext/mt_allocator.h (__pool::_M_get_align): New.

View File

@ -271,10 +271,12 @@ namespace std
// For the internal use we have functions similar to `begin'/`end' // For the internal use we have functions similar to `begin'/`end'
// but they do not call _M_leak. // but they do not call _M_leak.
iterator iterator
_M_ibegin() const { return iterator(_M_data()); } _M_ibegin() const
{ return iterator(_M_data()); }
iterator iterator
_M_iend() const { return iterator(_M_data() + this->size()); } _M_iend() const
{ return iterator(_M_data() + this->size()); }
void void
_M_leak() // for use in begin() & non-const op[] _M_leak() // for use in begin() & non-const op[]
@ -527,16 +529,19 @@ namespace std
/// Returns the number of characters in the string, not including any /// Returns the number of characters in the string, not including any
/// null-termination. /// null-termination.
size_type size_type
size() const { return _M_rep()->_M_length; } size() const
{ return _M_rep()->_M_length; }
/// Returns the number of characters in the string, not including any /// Returns the number of characters in the string, not including any
/// null-termination. /// null-termination.
size_type size_type
length() const { return _M_rep()->_M_length; } length() const
{ return _M_rep()->_M_length; }
/// Returns the size() of the largest possible %string. /// Returns the size() of the largest possible %string.
size_type size_type
max_size() const { return _Rep::_S_max_size; } max_size() const
{ return _Rep::_S_max_size; }
/** /**
* @brief Resizes the %string to the specified number of characters. * @brief Resizes the %string to the specified number of characters.
@ -562,14 +567,16 @@ namespace std
* setting them to 0. * setting them to 0.
*/ */
void void
resize(size_type __n) { this->resize(__n, _CharT()); } resize(size_type __n)
{ this->resize(__n, _CharT()); }
/** /**
* Returns the total number of characters that the %string can hold * Returns the total number of characters that the %string can hold
* before needing to allocate more memory. * before needing to allocate more memory.
*/ */
size_type size_type
capacity() const { return _M_rep()->_M_capacity; } capacity() const
{ return _M_rep()->_M_capacity; }
/** /**
* @brief Attempt to preallocate enough memory for specified number of * @brief Attempt to preallocate enough memory for specified number of
@ -595,13 +602,15 @@ namespace std
* Erases the string, making it empty. * Erases the string, making it empty.
*/ */
void void
clear() { _M_mutate(0, this->size(), 0); } clear()
{ _M_mutate(0, this->size(), 0); }
/** /**
* Returns true if the %string is empty. Equivalent to *this == "". * Returns true if the %string is empty. Equivalent to *this == "".
*/ */
bool bool
empty() const { return this->size() == 0; } empty() const
{ return this->size() == 0; }
// Element access: // Element access:
/** /**
@ -684,7 +693,8 @@ namespace std
* @return Reference to this string. * @return Reference to this string.
*/ */
basic_string& basic_string&
operator+=(const basic_string& __str) { return this->append(__str); } operator+=(const basic_string& __str)
{ return this->append(__str); }
/** /**
* @brief Append a C string. * @brief Append a C string.
@ -692,7 +702,8 @@ namespace std
* @return Reference to this string. * @return Reference to this string.
*/ */
basic_string& basic_string&
operator+=(const _CharT* __s) { return this->append(__s); } operator+=(const _CharT* __s)
{ return this->append(__s); }
/** /**
* @brief Append a character. * @brief Append a character.
@ -700,7 +711,8 @@ namespace std
* @return Reference to this string. * @return Reference to this string.
*/ */
basic_string& basic_string&
operator+=(_CharT __c) { return this->append(size_type(1), __c); } operator+=(_CharT __c)
{ return this->append(size_type(1), __c); }
/** /**
* @brief Append a string to this string. * @brief Append a string to this string.
@ -1447,7 +1459,8 @@ namespace std
* happen. * happen.
*/ */
const _CharT* const _CharT*
c_str() const { return _M_data(); } c_str() const
{ return _M_data(); }
/** /**
* @brief Return const pointer to contents. * @brief Return const pointer to contents.
@ -1456,13 +1469,15 @@ namespace std
* happen. * happen.
*/ */
const _CharT* const _CharT*
data() const { return _M_data(); } data() const
{ return _M_data(); }
/** /**
* @brief Return copy of allocator used to construct this string. * @brief Return copy of allocator used to construct this string.
*/ */
allocator_type allocator_type
get_allocator() const { return _M_dataplus; } get_allocator() const
{ return _M_dataplus; }
/** /**
* @brief Find position of a C substring. * @brief Find position of a C substring.

View File

@ -373,7 +373,8 @@ namespace std
template<typename _CharT, typename _Traits, typename _Alloc> template<typename _CharT, typename _Traits, typename _Alloc>
void void
basic_string<_CharT, _Traits, _Alloc>::_M_leak_hard() basic_string<_CharT, _Traits, _Alloc>::
_M_leak_hard()
{ {
#ifndef _GLIBCXX_FULLY_DYNAMIC_STRING #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
if (_M_rep() == &_S_empty_rep()) if (_M_rep() == &_S_empty_rep())
@ -393,16 +394,11 @@ namespace std
const size_type __new_size = __old_size + __len2 - __len1; const size_type __new_size = __old_size + __len2 - __len1;
const size_type __how_much = __old_size - __pos - __len1; const size_type __how_much = __old_size - __pos - __len1;
#ifndef _GLIBCXX_FULLY_DYNAMIC_STRING if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
if (_M_rep() == &_S_empty_rep()
|| _M_rep()->_M_is_shared() || __new_size > capacity())
#else
if (_M_rep()->_M_is_shared() || __new_size > capacity())
#endif
{ {
// Must reallocate. // Must reallocate.
const allocator_type __a = get_allocator(); const allocator_type __a = get_allocator();
_Rep* __r = _Rep::_S_create(__new_size, capacity(), __a); _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
if (__pos) if (__pos)
traits_type::copy(__r->_M_refdata(), _M_data(), __pos); traits_type::copy(__r->_M_refdata(), _M_data(), __pos);
@ -427,7 +423,8 @@ namespace std
template<typename _CharT, typename _Traits, typename _Alloc> template<typename _CharT, typename _Traits, typename _Alloc>
void void
basic_string<_CharT, _Traits, _Alloc>::reserve(size_type __res) basic_string<_CharT, _Traits, _Alloc>::
reserve(size_type __res)
{ {
if (__res != this->capacity() || _M_rep()->_M_is_shared()) if (__res != this->capacity() || _M_rep()->_M_is_shared())
{ {
@ -444,7 +441,9 @@ namespace std
} }
template<typename _CharT, typename _Traits, typename _Alloc> template<typename _CharT, typename _Traits, typename _Alloc>
void basic_string<_CharT, _Traits, _Alloc>::swap(basic_string& __s) void
basic_string<_CharT, _Traits, _Alloc>::
swap(basic_string& __s)
{ {
if (_M_rep()->_M_is_leaked()) if (_M_rep()->_M_is_leaked())
_M_rep()->_M_set_sharable(); _M_rep()->_M_set_sharable();
@ -561,7 +560,8 @@ namespace std
template<typename _CharT, typename _Traits, typename _Alloc> template<typename _CharT, typename _Traits, typename _Alloc>
void void
basic_string<_CharT, _Traits, _Alloc>::resize(size_type __n, _CharT __c) basic_string<_CharT, _Traits, _Alloc>::
resize(size_type __n, _CharT __c)
{ {
if (__n > max_size()) if (__n > max_size())
__throw_length_error(__N("basic_string::resize")); __throw_length_error(__N("basic_string::resize"));