⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rc_string_base.h

📁 linux下编程用 编译软件
💻 H
📖 第 1 页 / 共 2 页
字号:
      // _GLIBCXX_RESOLVE_LIB_DEFECTS      // 83.  String::npos vs. string::max_size()      if (__capacity > size_type(_S_max_size))	std::__throw_length_error(__N("__rc_string_base::_Rep::_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().      // 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.      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,      // plus sizeof(_Rep) - 1 to upper round to a size multiple of      // sizeof(_Rep).      // Whew. Seemingly so needy, yet so elemental.      size_type __size = ((__capacity + 1) * sizeof(_CharT)			  + 2 * sizeof(_Rep) - 1);      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 > size_type(_S_max_size))	    __capacity = size_type(_S_max_size);	  __size = (__capacity + 1) * sizeof(_CharT) + 2 * sizeof(_Rep) - 1;	}      // NB: Might throw, but no worries about a leak, mate: _Rep()      // does not throw.      _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size / sizeof(_Rep));      _Rep* __p = new (__place) _Rep;      __p->_M_info._M_capacity = __capacity;      return __p;    }  template<typename _CharT, typename _Traits, typename _Alloc>    void    __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::    _M_destroy(const _Alloc& __a) throw ()    {      const size_type __size = ((_M_info._M_capacity + 1) * sizeof(_CharT)				+ 2 * sizeof(_Rep) - 1);      _Rep_alloc_type(__a).deallocate(this, __size / sizeof(_Rep));    }  template<typename _CharT, typename _Traits, typename _Alloc>    _CharT*    __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::    _M_clone(const _Alloc& __alloc, size_type __res)    {      // Requested capacity of the clone.      const size_type __requested_cap = _M_info._M_length + __res;      _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,				  __alloc);      if (_M_info._M_length)	_S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);      __r->_M_set_length(_M_info._M_length);      return __r->_M_refdata();    }  template<typename _CharT, typename _Traits, typename _Alloc>    __rc_string_base<_CharT, _Traits, _Alloc>::    __rc_string_base(const _Alloc& __a)    : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }  template<typename _CharT, typename _Traits, typename _Alloc>    __rc_string_base<_CharT, _Traits, _Alloc>::    __rc_string_base(const __rc_string_base& __rcs)    : _M_dataplus(__rcs._M_get_allocator(),		  __rcs._M_grab(__rcs._M_get_allocator())) { }  template<typename _CharT, typename _Traits, typename _Alloc>    __rc_string_base<_CharT, _Traits, _Alloc>::    __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a)    : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }  template<typename _CharT, typename _Traits, typename _Alloc>    template<typename _InputIterator>    __rc_string_base<_CharT, _Traits, _Alloc>::    __rc_string_base(_InputIterator __beg, _InputIterator __end,		     const _Alloc& __a)    : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }  template<typename _CharT, typename _Traits, typename _Alloc>    void    __rc_string_base<_CharT, _Traits, _Alloc>::    _M_leak_hard()    {      if (_M_is_shared())	_M_erase(0, 0);      _M_set_leaked();    }  // 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*      __rc_string_base<_CharT, _Traits, _Alloc>::      _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,		   std::input_iterator_tag)      {	if (__beg == __end && __a == _Alloc())	  return _S_empty_rep._M_refcopy();	// 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);	_S_copy(__r->_M_refdata(), __buf, __len);	try	  {	    while (__beg != __end)	      {		if (__len == __r->_M_info._M_capacity)		  {		    // Allocate more space.		    _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);		    _S_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(__len);	return __r->_M_refdata();      }  template<typename _CharT, typename _Traits, typename _Alloc>    template<typename _InIterator>      _CharT*      __rc_string_base<_CharT, _Traits, _Alloc>::      _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,		   std::forward_iterator_tag)      {	if (__beg == __end && __a == _Alloc())	  return _S_empty_rep._M_refcopy();	// NB: Not required, but considered best practice.	if (__builtin_expect(_S_is_null_pointer(__beg) && __beg != __end, 0))	  std::__throw_logic_error(__N("__rc_string_base::"				       "_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(__dnew);	return __r->_M_refdata();      }  template<typename _CharT, typename _Traits, typename _Alloc>    _CharT*    __rc_string_base<_CharT, _Traits, _Alloc>::    _S_construct(size_type __n, _CharT __c, const _Alloc& __a)    {      if (__n == 0 && __a == _Alloc())	return _S_empty_rep._M_refcopy();      // Check for out_of_range and length_error exceptions.      _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);      if (__n)	_S_assign(__r->_M_refdata(), __n, __c);      __r->_M_set_length(__n);      return __r->_M_refdata();    }  template<typename _CharT, typename _Traits, typename _Alloc>    void    __rc_string_base<_CharT, _Traits, _Alloc>::    _M_swap(__rc_string_base& __rcs)    {      if (_M_is_leaked())	_M_set_sharable();      if (__rcs._M_is_leaked())	__rcs._M_set_sharable();            _CharT* __tmp = _M_data();      _M_data(__rcs._M_data());      __rcs._M_data(__tmp);            // NB: Implement Option 3 of DR 431 (see N1599).      std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),						  __rcs._M_get_allocator());    }   template<typename _CharT, typename _Traits, typename _Alloc>    void    __rc_string_base<_CharT, _Traits, _Alloc>::    _M_assign(const __rc_string_base& __rcs)    {      if (_M_rep() != __rcs._M_rep())	{	  _CharT* __tmp = __rcs._M_grab(_M_get_allocator());	  _M_dispose();	  _M_data(__tmp);	}    }  template<typename _CharT, typename _Traits, typename _Alloc>    void    __rc_string_base<_CharT, _Traits, _Alloc>::    _M_reserve(size_type __res)    {      // Make sure we don't shrink below the current size.      if (__res < _M_length())	__res = _M_length();            if (__res != _M_capacity() || _M_is_shared())	{	  _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),					     __res - _M_length());	  _M_dispose();	  _M_data(__tmp);	}    }  template<typename _CharT, typename _Traits, typename _Alloc>    void    __rc_string_base<_CharT, _Traits, _Alloc>::    _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,	      size_type __len2)    {      const size_type __how_much = _M_length() - __pos - __len1;            _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,				  _M_capacity(), _M_get_allocator());            if (__pos)	_S_copy(__r->_M_refdata(), _M_data(), __pos);      if (__s && __len2)	_S_copy(__r->_M_refdata() + __pos, __s, __len2);      if (__how_much)	_S_copy(__r->_M_refdata() + __pos + __len2,		_M_data() + __pos + __len1, __how_much);            _M_dispose();      _M_data(__r->_M_refdata());    }  template<typename _CharT, typename _Traits, typename _Alloc>    void    __rc_string_base<_CharT, _Traits, _Alloc>::    _M_erase(size_type __pos, size_type __n)    {      const size_type __new_size = _M_length() - __n;      const size_type __how_much = _M_length() - __pos - __n;            if (_M_is_shared())	{	  // Must reallocate.	  _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),				      _M_get_allocator());	  if (__pos)	    _S_copy(__r->_M_refdata(), _M_data(), __pos);	  if (__how_much)	    _S_copy(__r->_M_refdata() + __pos,		    _M_data() + __pos + __n, __how_much);	  _M_dispose();	  _M_data(__r->_M_refdata());	}      else if (__how_much && __n)	{	  // Work in-place.	  _S_move(_M_data() + __pos,		  _M_data() + __pos + __n, __how_much);	}      _M_rep()->_M_set_length(__new_size);          }  template<>    inline bool    __rc_string_base<char, std::char_traits<char>,		     std::allocator<char> >::    _M_compare(const __rc_string_base& __rcs) const    {      if (_M_rep() == __rcs._M_rep())	return true;      return false;    }  template<>    inline bool    __rc_string_base<wchar_t, std::char_traits<wchar_t>,		     std::allocator<wchar_t> >::    _M_compare(const __rc_string_base& __rcs) const    {      if (_M_rep() == __rcs._M_rep())	return true;      return false;    }} // namespace __gnu_cxx#endif /* _RC_STRING_BASE_H */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -