libstdc++
GNU C++ library
Loading...
Searching...
No Matches

◆ _S_create()

template<typename _CharT, typename _Traits, typename _Alloc>
basic_string< _CharT, _Traits, _Alloc >::_Rep * std::basic_string< _CharT, _Traits, _Alloc >::_Rep::_S_create ( size_type __capacity,
size_type __old_capacity,
const _Alloc & __alloc )
static

Definition at line 3723 of file cow_string.h.

3726 {
3727 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3728 // 83. String::npos vs. string::max_size()
3729 if (__capacity > _S_max_size)
3730 __throw_length_error(__N("basic_string::_S_create"));
3731
3732 // The standard places no restriction on allocating more memory
3733 // than is strictly needed within this layer at the moment or as
3734 // requested by an explicit application call to reserve(n).
3735
3736 // Many malloc implementations perform quite poorly when an
3737 // application attempts to allocate memory in a stepwise fashion
3738 // growing each allocation size by only 1 char. Additionally,
3739 // it makes little sense to allocate less linear memory than the
3740 // natural blocking size of the malloc implementation.
3741 // Unfortunately, we would need a somewhat low-level calculation
3742 // with tuned parameters to get this perfect for any particular
3743 // malloc implementation. Fortunately, generalizations about
3744 // common features seen among implementations seems to suffice.
3745
3746 // __pagesize need not match the actual VM page size for good
3747 // results in practice, thus we pick a common value on the low
3748 // side. __malloc_header_size is an estimate of the amount of
3749 // overhead per memory allocation (in practice seen N * sizeof
3750 // (void*) where N is 0, 2 or 4). According to folklore,
3751 // picking this value on the high side is better than
3752 // low-balling it (especially when this algorithm is used with
3753 // malloc implementations that allocate memory blocks rounded up
3754 // to a size which is a power of 2).
3755 const size_type __pagesize = 4096;
3756 const size_type __malloc_header_size = 4 * sizeof(void*);
3757
3758 // The below implements an exponential growth policy, necessary to
3759 // meet amortized linear time requirements of the library: see
3760 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
3761 // It's active for allocations requiring an amount of memory above
3762 // system pagesize. This is consistent with the requirements of the
3763 // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
3766
3767 // NB: Need an array of char_type[__capacity], plus a terminating
3768 // null char_type() element, plus enough for the _Rep data structure.
3769 // Whew. Seemingly so needy, yet so elemental.
3770 size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
3771
3774 {
3776 __capacity += __extra / sizeof(_CharT);
3777 // Never allocate a string bigger than _S_max_size.
3778 if (__capacity > _S_max_size)
3780 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
3781 }
3782
3783 // NB: Might throw, but no worries about a leak, mate: _Rep()
3784 // does not throw.
3785 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
3786 _Rep *__p = new (__place) _Rep;
3788 // ABI compatibility - 3.4.x set in _S_create both
3789 // _M_refcount and _M_length. All callers of _S_create
3790 // in basic_string.tcc then set just _M_length.
3791 // In 4.0.x and later both _M_refcount and _M_length
3792 // are initialized in the callers, unfortunately we can
3793 // have 3.4.x compiled code with _S_create callers inlined
3794 // calling 4.0.x+ _S_create.
3795 __p->_M_set_sharable();
3796 return __p;
3797 }
#define __N(msgid)
void __throw_length_error(const char *)
constexpr void _M_capacity(size_type __capacity)
Managing sequences of characters and character-like objects.
static const size_type _S_max_size
Definition cow_string.h:181
__gnu_cxx::__alloc_traits< _Alloc >::template rebind< char >::other _Raw_bytes_alloc
Definition cow_string.h:166

References std::basic_string< _CharT, _Traits, _Alloc >::basic_string(), __N, std::__throw_length_error(), std::basic_string< _CharT, _Traits, _Alloc >::_Rep_base::_M_capacity, _M_set_sharable(), and _S_max_size.

Referenced by _M_clone(), std::basic_string< _CharT, _Traits, _Alloc >::_M_mutate(), std::basic_string< _CharT, _Traits, _Alloc >::_S_construct(), and std::basic_string< _CharT, _Traits, _Alloc >::_S_construct().

Here is the call graph for this function:
Here is the caller graph for this function: