libstdc++: Move COW string definitions to separate header
This moves the definitions of the COW string to a separate file, so that they don't need to be preprocessed for the common case. We could also move the SSO string definitions to a new file, so that they don't need to be preprocessed for the old ABI case, but that would require more shovel work because there are some parts of <bits/basic_string.h> and <bits/basic_string.tcc> that are common to both definitions. libstdc++-v3/ChangeLog: * include/Makefile.am: Add new header. * include/Makefile.in: Regenerate. * include/bits/basic_string.h [!_GLIBCXX_USE_CXX11_ABI] (basic_string): Move definition of Copy-on-Write string to new file. * include/bits/basic_string.tcc: Likewise. * include/bits/cow_string.h: New file.
This commit is contained in:
parent
16158c9649
commit
7b527614dd
|
@ -119,6 +119,7 @@ bits_headers = \
|
|||
${bits_srcdir}/charconv.h \
|
||||
${bits_srcdir}/codecvt.h \
|
||||
${bits_srcdir}/concept_check.h \
|
||||
${bits_srcdir}/cow_string.h \
|
||||
${bits_srcdir}/cpp_type_traits.h \
|
||||
${bits_srcdir}/deque.tcc \
|
||||
${bits_srcdir}/enable_special_members.h \
|
||||
|
|
|
@ -469,6 +469,7 @@ bits_headers = \
|
|||
${bits_srcdir}/charconv.h \
|
||||
${bits_srcdir}/codecvt.h \
|
||||
${bits_srcdir}/concept_check.h \
|
||||
${bits_srcdir}/cow_string.h \
|
||||
${bits_srcdir}/cpp_type_traits.h \
|
||||
${bits_srcdir}/deque.tcc \
|
||||
${bits_srcdir}/enable_special_members.h \
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -515,654 +515,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|||
return __n;
|
||||
}
|
||||
|
||||
#else // !_GLIBCXX_USE_CXX11_ABI
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
const typename basic_string<_CharT, _Traits, _Alloc>::size_type
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
_Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
const _CharT
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
_Rep::_S_terminal = _CharT();
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
const typename basic_string<_CharT, _Traits, _Alloc>::size_type
|
||||
basic_string<_CharT, _Traits, _Alloc>::npos;
|
||||
|
||||
// Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
|
||||
// at static init time (before static ctors are run).
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
typename basic_string<_CharT, _Traits, _Alloc>::size_type
|
||||
basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
|
||||
(sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
|
||||
sizeof(size_type)];
|
||||
|
||||
// NB: This is the special case for Input Iterators, used in
|
||||
// istreambuf_iterators, etc.
|
||||
// Input Iterators have a cost structure very different from
|
||||
// pointers, calling for a different coding style.
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
template<typename _InIterator>
|
||||
_CharT*
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
_S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
|
||||
input_iterator_tag)
|
||||
{
|
||||
#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
|
||||
if (__beg == __end && __a == _Alloc())
|
||||
return _S_empty_rep()._M_refdata();
|
||||
#endif
|
||||
// Avoid reallocation for common case.
|
||||
_CharT __buf[128];
|
||||
size_type __len = 0;
|
||||
while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
|
||||
{
|
||||
__buf[__len++] = *__beg;
|
||||
++__beg;
|
||||
}
|
||||
_Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
|
||||
_M_copy(__r->_M_refdata(), __buf, __len);
|
||||
__try
|
||||
{
|
||||
while (__beg != __end)
|
||||
{
|
||||
if (__len == __r->_M_capacity)
|
||||
{
|
||||
// Allocate more space.
|
||||
_Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
|
||||
_M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
|
||||
__r->_M_destroy(__a);
|
||||
__r = __another;
|
||||
}
|
||||
__r->_M_refdata()[__len++] = *__beg;
|
||||
++__beg;
|
||||
}
|
||||
}
|
||||
__catch(...)
|
||||
{
|
||||
__r->_M_destroy(__a);
|
||||
__throw_exception_again;
|
||||
}
|
||||
__r->_M_set_length_and_sharable(__len);
|
||||
return __r->_M_refdata();
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
template <typename _InIterator>
|
||||
_CharT*
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
_S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
|
||||
forward_iterator_tag)
|
||||
{
|
||||
#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
|
||||
if (__beg == __end && __a == _Alloc())
|
||||
return _S_empty_rep()._M_refdata();
|
||||
#endif
|
||||
// NB: Not required, but considered best practice.
|
||||
if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
|
||||
__throw_logic_error(__N("basic_string::_S_construct null not valid"));
|
||||
|
||||
const size_type __dnew = static_cast<size_type>(std::distance(__beg,
|
||||
__end));
|
||||
// Check for out_of_range and length_error exceptions.
|
||||
_Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
|
||||
__try
|
||||
{ _S_copy_chars(__r->_M_refdata(), __beg, __end); }
|
||||
__catch(...)
|
||||
{
|
||||
__r->_M_destroy(__a);
|
||||
__throw_exception_again;
|
||||
}
|
||||
__r->_M_set_length_and_sharable(__dnew);
|
||||
return __r->_M_refdata();
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
_CharT*
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
_S_construct(size_type __n, _CharT __c, const _Alloc& __a)
|
||||
{
|
||||
#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
|
||||
if (__n == 0 && __a == _Alloc())
|
||||
return _S_empty_rep()._M_refdata();
|
||||
#endif
|
||||
// Check for out_of_range and length_error exceptions.
|
||||
_Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
|
||||
if (__n)
|
||||
_M_assign(__r->_M_refdata(), __n, __c);
|
||||
|
||||
__r->_M_set_length_and_sharable(__n);
|
||||
return __r->_M_refdata();
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a)
|
||||
: _M_dataplus(_S_construct(__str._M_data()
|
||||
+ __str._M_check(__pos,
|
||||
"basic_string::basic_string"),
|
||||
__str._M_data() + __str._M_limit(__pos, npos)
|
||||
+ __pos, __a), __a)
|
||||
{ }
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
basic_string(const basic_string& __str, size_type __pos, size_type __n)
|
||||
: _M_dataplus(_S_construct(__str._M_data()
|
||||
+ __str._M_check(__pos,
|
||||
"basic_string::basic_string"),
|
||||
__str._M_data() + __str._M_limit(__pos, __n)
|
||||
+ __pos, _Alloc()), _Alloc())
|
||||
{ }
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
basic_string(const basic_string& __str, size_type __pos,
|
||||
size_type __n, const _Alloc& __a)
|
||||
: _M_dataplus(_S_construct(__str._M_data()
|
||||
+ __str._M_check(__pos,
|
||||
"basic_string::basic_string"),
|
||||
__str._M_data() + __str._M_limit(__pos, __n)
|
||||
+ __pos, __a), __a)
|
||||
{ }
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
assign(const basic_string& __str)
|
||||
{
|
||||
if (_M_rep() != __str._M_rep())
|
||||
{
|
||||
// XXX MT
|
||||
const allocator_type __a = this->get_allocator();
|
||||
_CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
|
||||
_M_rep()->_M_dispose(__a);
|
||||
_M_data(__tmp);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
assign(const _CharT* __s, size_type __n)
|
||||
{
|
||||
__glibcxx_requires_string_len(__s, __n);
|
||||
_M_check_length(this->size(), __n, "basic_string::assign");
|
||||
if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
|
||||
return _M_replace_safe(size_type(0), this->size(), __s, __n);
|
||||
else
|
||||
{
|
||||
// Work in-place.
|
||||
const size_type __pos = __s - _M_data();
|
||||
if (__pos >= __n)
|
||||
_M_copy(_M_data(), __s, __n);
|
||||
else if (__pos)
|
||||
_M_move(_M_data(), __s, __n);
|
||||
_M_rep()->_M_set_length_and_sharable(__n);
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
append(size_type __n, _CharT __c)
|
||||
{
|
||||
if (__n)
|
||||
{
|
||||
_M_check_length(size_type(0), __n, "basic_string::append");
|
||||
const size_type __len = __n + this->size();
|
||||
if (__len > this->capacity() || _M_rep()->_M_is_shared())
|
||||
this->reserve(__len);
|
||||
_M_assign(_M_data() + this->size(), __n, __c);
|
||||
_M_rep()->_M_set_length_and_sharable(__len);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
append(const _CharT* __s, size_type __n)
|
||||
{
|
||||
__glibcxx_requires_string_len(__s, __n);
|
||||
if (__n)
|
||||
{
|
||||
_M_check_length(size_type(0), __n, "basic_string::append");
|
||||
const size_type __len = __n + this->size();
|
||||
if (__len > this->capacity() || _M_rep()->_M_is_shared())
|
||||
{
|
||||
if (_M_disjunct(__s))
|
||||
this->reserve(__len);
|
||||
else
|
||||
{
|
||||
const size_type __off = __s - _M_data();
|
||||
this->reserve(__len);
|
||||
__s = _M_data() + __off;
|
||||
}
|
||||
}
|
||||
_M_copy(_M_data() + this->size(), __s, __n);
|
||||
_M_rep()->_M_set_length_and_sharable(__len);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
append(const basic_string& __str)
|
||||
{
|
||||
const size_type __size = __str.size();
|
||||
if (__size)
|
||||
{
|
||||
const size_type __len = __size + this->size();
|
||||
if (__len > this->capacity() || _M_rep()->_M_is_shared())
|
||||
this->reserve(__len);
|
||||
_M_copy(_M_data() + this->size(), __str._M_data(), __size);
|
||||
_M_rep()->_M_set_length_and_sharable(__len);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
append(const basic_string& __str, size_type __pos, size_type __n)
|
||||
{
|
||||
__str._M_check(__pos, "basic_string::append");
|
||||
__n = __str._M_limit(__pos, __n);
|
||||
if (__n)
|
||||
{
|
||||
const size_type __len = __n + this->size();
|
||||
if (__len > this->capacity() || _M_rep()->_M_is_shared())
|
||||
this->reserve(__len);
|
||||
_M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
|
||||
_M_rep()->_M_set_length_and_sharable(__len);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
insert(size_type __pos, const _CharT* __s, size_type __n)
|
||||
{
|
||||
__glibcxx_requires_string_len(__s, __n);
|
||||
_M_check(__pos, "basic_string::insert");
|
||||
_M_check_length(size_type(0), __n, "basic_string::insert");
|
||||
if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
|
||||
return _M_replace_safe(__pos, size_type(0), __s, __n);
|
||||
else
|
||||
{
|
||||
// Work in-place.
|
||||
const size_type __off = __s - _M_data();
|
||||
_M_mutate(__pos, 0, __n);
|
||||
__s = _M_data() + __off;
|
||||
_CharT* __p = _M_data() + __pos;
|
||||
if (__s + __n <= __p)
|
||||
_M_copy(__p, __s, __n);
|
||||
else if (__s >= __p)
|
||||
_M_copy(__p, __s + __n, __n);
|
||||
else
|
||||
{
|
||||
const size_type __nleft = __p - __s;
|
||||
_M_copy(__p, __s, __nleft);
|
||||
_M_copy(__p + __nleft, __p + __n, __n - __nleft);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
typename basic_string<_CharT, _Traits, _Alloc>::iterator
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
erase(iterator __first, iterator __last)
|
||||
{
|
||||
_GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
|
||||
&& __last <= _M_iend());
|
||||
|
||||
// NB: This isn't just an optimization (bail out early when
|
||||
// there is nothing to do, really), it's also a correctness
|
||||
// issue vs MT, see libstdc++/40518.
|
||||
const size_type __size = __last - __first;
|
||||
if (__size)
|
||||
{
|
||||
const size_type __pos = __first - _M_ibegin();
|
||||
_M_mutate(__pos, __size, size_type(0));
|
||||
_M_rep()->_M_set_leaked();
|
||||
return iterator(_M_data() + __pos);
|
||||
}
|
||||
else
|
||||
return __first;
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
replace(size_type __pos, size_type __n1, const _CharT* __s,
|
||||
size_type __n2)
|
||||
{
|
||||
__glibcxx_requires_string_len(__s, __n2);
|
||||
_M_check(__pos, "basic_string::replace");
|
||||
__n1 = _M_limit(__pos, __n1);
|
||||
_M_check_length(__n1, __n2, "basic_string::replace");
|
||||
bool __left;
|
||||
if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
|
||||
return _M_replace_safe(__pos, __n1, __s, __n2);
|
||||
else if ((__left = __s + __n2 <= _M_data() + __pos)
|
||||
|| _M_data() + __pos + __n1 <= __s)
|
||||
{
|
||||
// Work in-place: non-overlapping case.
|
||||
size_type __off = __s - _M_data();
|
||||
__left ? __off : (__off += __n2 - __n1);
|
||||
_M_mutate(__pos, __n1, __n2);
|
||||
_M_copy(_M_data() + __pos, _M_data() + __off, __n2);
|
||||
return *this;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Todo: overlapping case.
|
||||
const basic_string __tmp(__s, __n2);
|
||||
return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
void
|
||||
basic_string<_CharT, _Traits, _Alloc>::_Rep::
|
||||
_M_destroy(const _Alloc& __a) throw ()
|
||||
{
|
||||
const size_type __size = sizeof(_Rep_base) +
|
||||
(this->_M_capacity + 1) * sizeof(_CharT);
|
||||
_Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
void
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
_M_leak_hard()
|
||||
{
|
||||
#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
|
||||
if (_M_rep() == &_S_empty_rep())
|
||||
return;
|
||||
#endif
|
||||
if (_M_rep()->_M_is_shared())
|
||||
_M_mutate(0, 0, 0);
|
||||
_M_rep()->_M_set_leaked();
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
void
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
_M_mutate(size_type __pos, size_type __len1, size_type __len2)
|
||||
{
|
||||
const size_type __old_size = this->size();
|
||||
const size_type __new_size = __old_size + __len2 - __len1;
|
||||
const size_type __how_much = __old_size - __pos - __len1;
|
||||
|
||||
if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
|
||||
{
|
||||
// Must reallocate.
|
||||
const allocator_type __a = get_allocator();
|
||||
_Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
|
||||
|
||||
if (__pos)
|
||||
_M_copy(__r->_M_refdata(), _M_data(), __pos);
|
||||
if (__how_much)
|
||||
_M_copy(__r->_M_refdata() + __pos + __len2,
|
||||
_M_data() + __pos + __len1, __how_much);
|
||||
|
||||
_M_rep()->_M_dispose(__a);
|
||||
_M_data(__r->_M_refdata());
|
||||
}
|
||||
else if (__how_much && __len1 != __len2)
|
||||
{
|
||||
// Work in-place.
|
||||
_M_move(_M_data() + __pos + __len2,
|
||||
_M_data() + __pos + __len1, __how_much);
|
||||
}
|
||||
_M_rep()->_M_set_length_and_sharable(__new_size);
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
void
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
reserve(size_type __res)
|
||||
{
|
||||
const size_type __capacity = capacity();
|
||||
|
||||
// _GLIBCXX_RESOLVE_LIB_DEFECTS
|
||||
// 2968. Inconsistencies between basic_string reserve and
|
||||
// vector/unordered_map/unordered_set reserve functions
|
||||
// P0966 reserve should not shrink
|
||||
if (__res <= __capacity)
|
||||
{
|
||||
if (!_M_rep()->_M_is_shared())
|
||||
return;
|
||||
|
||||
// unshare, but keep same capacity
|
||||
__res = __capacity;
|
||||
}
|
||||
|
||||
const allocator_type __a = get_allocator();
|
||||
_CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
|
||||
_M_rep()->_M_dispose(__a);
|
||||
_M_data(__tmp);
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
void
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
swap(basic_string& __s)
|
||||
_GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
|
||||
{
|
||||
if (_M_rep()->_M_is_leaked())
|
||||
_M_rep()->_M_set_sharable();
|
||||
if (__s._M_rep()->_M_is_leaked())
|
||||
__s._M_rep()->_M_set_sharable();
|
||||
if (this->get_allocator() == __s.get_allocator())
|
||||
{
|
||||
_CharT* __tmp = _M_data();
|
||||
_M_data(__s._M_data());
|
||||
__s._M_data(__tmp);
|
||||
}
|
||||
// The code below can usually be optimized away.
|
||||
else
|
||||
{
|
||||
const basic_string __tmp1(_M_ibegin(), _M_iend(),
|
||||
__s.get_allocator());
|
||||
const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
|
||||
this->get_allocator());
|
||||
*this = __tmp2;
|
||||
__s = __tmp1;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
|
||||
basic_string<_CharT, _Traits, _Alloc>::_Rep::
|
||||
_S_create(size_type __capacity, size_type __old_capacity,
|
||||
const _Alloc& __alloc)
|
||||
{
|
||||
// _GLIBCXX_RESOLVE_LIB_DEFECTS
|
||||
// 83. String::npos vs. string::max_size()
|
||||
if (__capacity > _S_max_size)
|
||||
__throw_length_error(__N("basic_string::_S_create"));
|
||||
|
||||
// The standard places no restriction on allocating more memory
|
||||
// than is strictly needed within this layer at the moment or as
|
||||
// requested by an explicit application call to reserve(n).
|
||||
|
||||
// Many malloc implementations perform quite poorly when an
|
||||
// application attempts to allocate memory in a stepwise fashion
|
||||
// growing each allocation size by only 1 char. Additionally,
|
||||
// it makes little sense to allocate less linear memory than the
|
||||
// natural blocking size of the malloc implementation.
|
||||
// Unfortunately, we would need a somewhat low-level calculation
|
||||
// with tuned parameters to get this perfect for any particular
|
||||
// malloc implementation. Fortunately, generalizations about
|
||||
// common features seen among implementations seems to suffice.
|
||||
|
||||
// __pagesize need not match the actual VM page size for good
|
||||
// results in practice, thus we pick a common value on the low
|
||||
// side. __malloc_header_size is an estimate of the amount of
|
||||
// overhead per memory allocation (in practice seen N * sizeof
|
||||
// (void*) where N is 0, 2 or 4). According to folklore,
|
||||
// picking this value on the high side is better than
|
||||
// low-balling it (especially when this algorithm is used with
|
||||
// malloc implementations that allocate memory blocks rounded up
|
||||
// to a size which is a power of 2).
|
||||
const size_type __pagesize = 4096;
|
||||
const size_type __malloc_header_size = 4 * sizeof(void*);
|
||||
|
||||
// The below implements an exponential growth policy, necessary to
|
||||
// meet amortized linear time requirements of the library: see
|
||||
// http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
|
||||
// It's active for allocations requiring an amount of memory above
|
||||
// system pagesize. This is consistent with the requirements of the
|
||||
// standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
|
||||
if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
|
||||
__capacity = 2 * __old_capacity;
|
||||
|
||||
// NB: Need an array of char_type[__capacity], plus a terminating
|
||||
// null char_type() element, plus enough for the _Rep data structure.
|
||||
// Whew. Seemingly so needy, yet so elemental.
|
||||
size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
|
||||
|
||||
const size_type __adj_size = __size + __malloc_header_size;
|
||||
if (__adj_size > __pagesize && __capacity > __old_capacity)
|
||||
{
|
||||
const size_type __extra = __pagesize - __adj_size % __pagesize;
|
||||
__capacity += __extra / sizeof(_CharT);
|
||||
// Never allocate a string bigger than _S_max_size.
|
||||
if (__capacity > _S_max_size)
|
||||
__capacity = _S_max_size;
|
||||
__size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
|
||||
}
|
||||
|
||||
// NB: Might throw, but no worries about a leak, mate: _Rep()
|
||||
// does not throw.
|
||||
void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
|
||||
_Rep *__p = new (__place) _Rep;
|
||||
__p->_M_capacity = __capacity;
|
||||
// ABI compatibility - 3.4.x set in _S_create both
|
||||
// _M_refcount and _M_length. All callers of _S_create
|
||||
// in basic_string.tcc then set just _M_length.
|
||||
// In 4.0.x and later both _M_refcount and _M_length
|
||||
// are initialized in the callers, unfortunately we can
|
||||
// have 3.4.x compiled code with _S_create callers inlined
|
||||
// calling 4.0.x+ _S_create.
|
||||
__p->_M_set_sharable();
|
||||
return __p;
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
_CharT*
|
||||
basic_string<_CharT, _Traits, _Alloc>::_Rep::
|
||||
_M_clone(const _Alloc& __alloc, size_type __res)
|
||||
{
|
||||
// Requested capacity of the clone.
|
||||
const size_type __requested_cap = this->_M_length + __res;
|
||||
_Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
|
||||
__alloc);
|
||||
if (this->_M_length)
|
||||
_M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
|
||||
|
||||
__r->_M_set_length_and_sharable(this->_M_length);
|
||||
return __r->_M_refdata();
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
void
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
resize(size_type __n, _CharT __c)
|
||||
{
|
||||
const size_type __size = this->size();
|
||||
_M_check_length(__size, __n, "basic_string::resize");
|
||||
if (__size < __n)
|
||||
this->append(__n - __size, __c);
|
||||
else if (__n < __size)
|
||||
this->erase(__n);
|
||||
// else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
template<typename _InputIterator>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
_M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
|
||||
_InputIterator __k2, __false_type)
|
||||
{
|
||||
const basic_string __s(__k1, __k2);
|
||||
const size_type __n1 = __i2 - __i1;
|
||||
_M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
|
||||
return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
|
||||
__s.size());
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
_M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
|
||||
_CharT __c)
|
||||
{
|
||||
_M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
|
||||
_M_mutate(__pos1, __n1, __n2);
|
||||
if (__n2)
|
||||
_M_assign(_M_data() + __pos1, __n2, __c);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>&
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
_M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
|
||||
size_type __n2)
|
||||
{
|
||||
_M_mutate(__pos1, __n1, __n2);
|
||||
if (__n2)
|
||||
_M_copy(_M_data() + __pos1, __s, __n2);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
void
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
reserve()
|
||||
{
|
||||
#if __cpp_exceptions
|
||||
if (length() < capacity() || _M_rep()->_M_is_shared())
|
||||
try
|
||||
{
|
||||
const allocator_type __a = get_allocator();
|
||||
_CharT* __tmp = _M_rep()->_M_clone(__a);
|
||||
_M_rep()->_M_dispose(__a);
|
||||
_M_data(__tmp);
|
||||
}
|
||||
catch (const __cxxabiv1::__forced_unwind&)
|
||||
{ throw; }
|
||||
catch (...)
|
||||
{ /* swallow the exception */ }
|
||||
#endif
|
||||
}
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
typename basic_string<_CharT, _Traits, _Alloc>::size_type
|
||||
basic_string<_CharT, _Traits, _Alloc>::
|
||||
copy(_CharT* __s, size_type __n, size_type __pos) const
|
||||
{
|
||||
_M_check(__pos, "basic_string::copy");
|
||||
__n = _M_limit(__pos, __n);
|
||||
__glibcxx_requires_string_len(__s, __n);
|
||||
if (__n)
|
||||
_M_copy(__s, _M_data() + __pos, __n);
|
||||
// 21.3.5.7 par 3: do not append null. (good.)
|
||||
return __n;
|
||||
}
|
||||
#endif // !_GLIBCXX_USE_CXX11_ABI
|
||||
#endif // _GLIBCXX_USE_CXX11_ABI
|
||||
|
||||
template<typename _CharT, typename _Traits, typename _Alloc>
|
||||
basic_string<_CharT, _Traits, _Alloc>
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue