1169691Skan// Reference-counted versatile string base -*- C++ -*- 2169691Skan 3169691Skan// Copyright (C) 2005, 2006 Free Software Foundation, Inc. 4169691Skan// 5169691Skan// This file is part of the GNU ISO C++ Library. This library is free 6169691Skan// software; you can redistribute it and/or modify it under the 7169691Skan// terms of the GNU General Public License as published by the 8169691Skan// Free Software Foundation; either version 2, or (at your option) 9169691Skan// any later version. 10169691Skan 11169691Skan// This library is distributed in the hope that it will be useful, 12169691Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of 13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14169691Skan// GNU General Public License for more details. 15169691Skan 16169691Skan// You should have received a copy of the GNU General Public License along 17169691Skan// with this library; see the file COPYING. If not, write to the Free 18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 19169691Skan// USA. 20169691Skan 21169691Skan// As a special exception, you may use this file as part of a free software 22169691Skan// library without restriction. Specifically, if other files instantiate 23169691Skan// templates or use macros or inline functions from this file, or you compile 24169691Skan// this file and link it with other files to produce an executable, this 25169691Skan// file does not by itself cause the resulting executable to be covered by 26169691Skan// the GNU General Public License. This exception does not however 27169691Skan// invalidate any other reasons why the executable file might be covered by 28169691Skan// the GNU General Public License. 29169691Skan 30169691Skan/** @file ext/rc_string_base.h 31169691Skan * This file is a GNU extension to the Standard C++ Library. 32169691Skan * This is an internal header file, included by other library headers. 33169691Skan * You should not attempt to use it directly. 34169691Skan */ 35169691Skan 36169691Skan#ifndef _RC_STRING_BASE_H 37169691Skan#define _RC_STRING_BASE_H 1 38169691Skan 39169691Skan#include <ext/atomicity.h> 40169691Skan 41169691Skan_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 42169691Skan 43169691Skan /** 44169691Skan * @if maint 45169691Skan * Documentation? What's that? 46169691Skan * Nathan Myers <ncm@cantrip.org>. 47169691Skan * 48169691Skan * A string looks like this: 49169691Skan * 50169691Skan * @code 51169691Skan * [_Rep] 52169691Skan * _M_length 53169691Skan * [__rc_string_base<char_type>] _M_capacity 54169691Skan * _M_dataplus _M_refcount 55169691Skan * _M_p ----------------> unnamed array of char_type 56169691Skan * @endcode 57169691Skan * 58169691Skan * Where the _M_p points to the first character in the string, and 59169691Skan * you cast it to a pointer-to-_Rep and subtract 1 to get a 60169691Skan * pointer to the header. 61169691Skan * 62169691Skan * This approach has the enormous advantage that a string object 63169691Skan * requires only one allocation. All the ugliness is confined 64169691Skan * within a single pair of inline functions, which each compile to 65169691Skan * a single "add" instruction: _Rep::_M_refdata(), and 66169691Skan * __rc_string_base::_M_rep(); and the allocation function which gets a 67169691Skan * block of raw bytes and with room enough and constructs a _Rep 68169691Skan * object at the front. 69169691Skan * 70169691Skan * The reason you want _M_data pointing to the character array and 71169691Skan * not the _Rep is so that the debugger can see the string 72169691Skan * contents. (Probably we should add a non-inline member to get 73169691Skan * the _Rep for the debugger to use, so users can check the actual 74169691Skan * string length.) 75169691Skan * 76169691Skan * Note that the _Rep object is a POD so that you can have a 77169691Skan * static "empty string" _Rep object already "constructed" before 78169691Skan * static constructors have run. The reference-count encoding is 79169691Skan * chosen so that a 0 indicates one reference, so you never try to 80169691Skan * destroy the empty-string _Rep object. 81169691Skan * 82169691Skan * All but the last paragraph is considered pretty conventional 83169691Skan * for a C++ string implementation. 84169691Skan * @endif 85169691Skan */ 86169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 87169691Skan class __rc_string_base 88169691Skan : protected __vstring_utility<_CharT, _Traits, _Alloc> 89169691Skan { 90169691Skan public: 91169691Skan typedef _Traits traits_type; 92169691Skan typedef typename _Traits::char_type value_type; 93169691Skan typedef _Alloc allocator_type; 94169691Skan 95169691Skan typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base; 96169691Skan typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type; 97169691Skan typedef typename _CharT_alloc_type::size_type size_type; 98169691Skan 99169691Skan private: 100169691Skan // _Rep: string representation 101169691Skan // Invariants: 102169691Skan // 1. String really contains _M_length + 1 characters: due to 21.3.4 103169691Skan // must be kept null-terminated. 104169691Skan // 2. _M_capacity >= _M_length 105169691Skan // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT). 106169691Skan // 3. _M_refcount has three states: 107169691Skan // -1: leaked, one reference, no ref-copies allowed, non-const. 108169691Skan // 0: one reference, non-const. 109169691Skan // n>0: n + 1 references, operations require a lock, const. 110169691Skan // 4. All fields == 0 is an empty string, given the extra storage 111169691Skan // beyond-the-end for a null terminator; thus, the shared 112169691Skan // empty string representation needs no constructor. 113169691Skan struct _Rep 114169691Skan { 115169691Skan union 116169691Skan { 117169691Skan struct 118169691Skan { 119169691Skan size_type _M_length; 120169691Skan size_type _M_capacity; 121169691Skan _Atomic_word _M_refcount; 122169691Skan } _M_info; 123169691Skan 124169691Skan // Only for alignment purposes. 125169691Skan _CharT _M_align; 126169691Skan }; 127169691Skan 128169691Skan typedef typename _Alloc::template rebind<_Rep>::other _Rep_alloc_type; 129169691Skan 130169691Skan _CharT* 131169691Skan _M_refdata() throw() 132169691Skan { return reinterpret_cast<_CharT*>(this + 1); } 133169691Skan 134169691Skan _CharT* 135169691Skan _M_refcopy() throw() 136169691Skan { 137169691Skan __atomic_add_dispatch(&_M_info._M_refcount, 1); 138169691Skan return _M_refdata(); 139169691Skan } // XXX MT 140169691Skan 141169691Skan void 142169691Skan _M_set_length(size_type __n) 143169691Skan { 144169691Skan _M_info._M_refcount = 0; // One reference. 145169691Skan _M_info._M_length = __n; 146169691Skan // grrr. (per 21.3.4) 147169691Skan // You cannot leave those LWG people alone for a second. 148169691Skan traits_type::assign(_M_refdata()[__n], _CharT()); 149169691Skan } 150169691Skan 151169691Skan // Create & Destroy 152169691Skan static _Rep* 153169691Skan _S_create(size_type, size_type, const _Alloc&); 154169691Skan 155169691Skan void 156169691Skan _M_destroy(const _Alloc&) throw(); 157169691Skan 158169691Skan _CharT* 159169691Skan _M_clone(const _Alloc&, size_type __res = 0); 160169691Skan }; 161169691Skan 162169691Skan struct _Rep_empty 163169691Skan : public _Rep 164169691Skan { 165169691Skan _CharT _M_terminal; 166169691Skan }; 167169691Skan 168169691Skan static _Rep_empty _S_empty_rep; 169169691Skan 170169691Skan // The maximum number of individual char_type elements of an 171169691Skan // individual string is determined by _S_max_size. This is the 172169691Skan // value that will be returned by max_size(). (Whereas npos 173169691Skan // is the maximum number of bytes the allocator can allocate.) 174169691Skan // If one was to divvy up the theoretical largest size string, 175169691Skan // with a terminating character and m _CharT elements, it'd 176169691Skan // look like this: 177169691Skan // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT) 178169691Skan // + sizeof(_Rep) - 1 179169691Skan // (NB: last two terms for rounding reasons, see _M_create below) 180169691Skan // Solving for m: 181169691Skan // m = ((npos - 2 * sizeof(_Rep) + 1) / sizeof(_CharT)) - 1 182169691Skan // In addition, this implementation halfs this amount. 183169691Skan enum { _S_max_size = (((static_cast<size_type>(-1) - 2 * sizeof(_Rep) 184169691Skan + 1) / sizeof(_CharT)) - 1) / 2 }; 185169691Skan 186169691Skan // Data Member (private): 187169691Skan mutable typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus; 188169691Skan 189169691Skan void 190169691Skan _M_data(_CharT* __p) 191169691Skan { _M_dataplus._M_p = __p; } 192169691Skan 193169691Skan _Rep* 194169691Skan _M_rep() const 195169691Skan { return &((reinterpret_cast<_Rep*>(_M_data()))[-1]); } 196169691Skan 197169691Skan _CharT* 198169691Skan _M_grab(const _Alloc& __alloc) const 199169691Skan { 200169691Skan return (!_M_is_leaked() && _M_get_allocator() == __alloc) 201169691Skan ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc); 202169691Skan } 203169691Skan 204169691Skan void 205169691Skan _M_dispose() 206169691Skan { 207169691Skan if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount, 208169691Skan -1) <= 0) 209169691Skan _M_rep()->_M_destroy(_M_get_allocator()); 210169691Skan } // XXX MT 211169691Skan 212169691Skan bool 213169691Skan _M_is_leaked() const 214169691Skan { return _M_rep()->_M_info._M_refcount < 0; } 215169691Skan 216169691Skan void 217169691Skan _M_set_sharable() 218169691Skan { _M_rep()->_M_info._M_refcount = 0; } 219169691Skan 220169691Skan void 221169691Skan _M_leak_hard(); 222169691Skan 223169691Skan // _S_construct_aux is used to implement the 21.3.1 para 15 which 224169691Skan // requires special behaviour if _InIterator is an integral type 225169691Skan template<typename _InIterator> 226169691Skan static _CharT* 227169691Skan _S_construct_aux(_InIterator __beg, _InIterator __end, 228169691Skan const _Alloc& __a, std::__false_type) 229169691Skan { 230169691Skan typedef typename iterator_traits<_InIterator>::iterator_category _Tag; 231169691Skan return _S_construct(__beg, __end, __a, _Tag()); 232169691Skan } 233169691Skan 234169691Skan template<typename _InIterator> 235169691Skan static _CharT* 236169691Skan _S_construct_aux(_InIterator __beg, _InIterator __end, 237169691Skan const _Alloc& __a, std::__true_type) 238169691Skan { return _S_construct(static_cast<size_type>(__beg), 239169691Skan static_cast<value_type>(__end), __a); } 240169691Skan 241169691Skan template<typename _InIterator> 242169691Skan static _CharT* 243169691Skan _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a) 244169691Skan { 245169691Skan typedef typename std::__is_integer<_InIterator>::__type _Integral; 246169691Skan return _S_construct_aux(__beg, __end, __a, _Integral()); 247169691Skan } 248169691Skan 249169691Skan // For Input Iterators, used in istreambuf_iterators, etc. 250169691Skan template<typename _InIterator> 251169691Skan static _CharT* 252169691Skan _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, 253169691Skan std::input_iterator_tag); 254169691Skan 255169691Skan // For forward_iterators up to random_access_iterators, used for 256169691Skan // string::iterator, _CharT*, etc. 257169691Skan template<typename _FwdIterator> 258169691Skan static _CharT* 259169691Skan _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a, 260169691Skan std::forward_iterator_tag); 261169691Skan 262169691Skan static _CharT* 263169691Skan _S_construct(size_type __req, _CharT __c, const _Alloc& __a); 264169691Skan 265169691Skan public: 266169691Skan size_type 267169691Skan _M_max_size() const 268169691Skan { return size_type(_S_max_size); } 269169691Skan 270169691Skan _CharT* 271169691Skan _M_data() const 272169691Skan { return _M_dataplus._M_p; } 273169691Skan 274169691Skan size_type 275169691Skan _M_length() const 276169691Skan { return _M_rep()->_M_info._M_length; } 277169691Skan 278169691Skan size_type 279169691Skan _M_capacity() const 280169691Skan { return _M_rep()->_M_info._M_capacity; } 281169691Skan 282169691Skan bool 283169691Skan _M_is_shared() const 284169691Skan { return _M_rep()->_M_info._M_refcount > 0; } 285169691Skan 286169691Skan void 287169691Skan _M_set_leaked() 288169691Skan { _M_rep()->_M_info._M_refcount = -1; } 289169691Skan 290169691Skan void 291169691Skan _M_leak() // for use in begin() & non-const op[] 292169691Skan { 293169691Skan if (!_M_is_leaked()) 294169691Skan _M_leak_hard(); 295169691Skan } 296169691Skan 297169691Skan void 298169691Skan _M_set_length(size_type __n) 299169691Skan { _M_rep()->_M_set_length(__n); } 300169691Skan 301169691Skan __rc_string_base() 302169691Skan : _M_dataplus(_Alloc(), _S_empty_rep._M_refcopy()) { } 303169691Skan 304169691Skan __rc_string_base(const _Alloc& __a); 305169691Skan 306169691Skan __rc_string_base(const __rc_string_base& __rcs); 307169691Skan 308169691Skan __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a); 309169691Skan 310169691Skan template<typename _InputIterator> 311169691Skan __rc_string_base(_InputIterator __beg, _InputIterator __end, 312169691Skan const _Alloc& __a); 313169691Skan 314169691Skan ~__rc_string_base() 315169691Skan { _M_dispose(); } 316169691Skan 317169691Skan allocator_type& 318169691Skan _M_get_allocator() 319169691Skan { return _M_dataplus; } 320169691Skan 321169691Skan const allocator_type& 322169691Skan _M_get_allocator() const 323169691Skan { return _M_dataplus; } 324169691Skan 325169691Skan void 326169691Skan _M_swap(__rc_string_base& __rcs); 327169691Skan 328169691Skan void 329169691Skan _M_assign(const __rc_string_base& __rcs); 330169691Skan 331169691Skan void 332169691Skan _M_reserve(size_type __res); 333169691Skan 334169691Skan void 335169691Skan _M_mutate(size_type __pos, size_type __len1, const _CharT* __s, 336169691Skan size_type __len2); 337169691Skan 338169691Skan void 339169691Skan _M_erase(size_type __pos, size_type __n); 340169691Skan 341169691Skan void 342169691Skan _M_clear() 343169691Skan { _M_erase(size_type(0), _M_length()); } 344169691Skan 345169691Skan bool 346169691Skan _M_compare(const __rc_string_base&) const 347169691Skan { return false; } 348169691Skan }; 349169691Skan 350169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 351169691Skan typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep_empty 352169691Skan __rc_string_base<_CharT, _Traits, _Alloc>::_S_empty_rep; 353169691Skan 354169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 355169691Skan typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep* 356169691Skan __rc_string_base<_CharT, _Traits, _Alloc>::_Rep:: 357169691Skan _S_create(size_type __capacity, size_type __old_capacity, 358169691Skan const _Alloc& __alloc) 359169691Skan { 360169691Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 361169691Skan // 83. String::npos vs. string::max_size() 362169691Skan if (__capacity > size_type(_S_max_size)) 363169691Skan std::__throw_length_error(__N("__rc_string_base::_Rep::_S_create")); 364169691Skan 365169691Skan // The standard places no restriction on allocating more memory 366169691Skan // than is strictly needed within this layer at the moment or as 367169691Skan // requested by an explicit application call to reserve(). 368169691Skan 369169691Skan // Many malloc implementations perform quite poorly when an 370169691Skan // application attempts to allocate memory in a stepwise fashion 371169691Skan // growing each allocation size by only 1 char. Additionally, 372169691Skan // it makes little sense to allocate less linear memory than the 373169691Skan // natural blocking size of the malloc implementation. 374169691Skan // Unfortunately, we would need a somewhat low-level calculation 375169691Skan // with tuned parameters to get this perfect for any particular 376169691Skan // malloc implementation. Fortunately, generalizations about 377169691Skan // common features seen among implementations seems to suffice. 378169691Skan 379169691Skan // __pagesize need not match the actual VM page size for good 380169691Skan // results in practice, thus we pick a common value on the low 381169691Skan // side. __malloc_header_size is an estimate of the amount of 382169691Skan // overhead per memory allocation (in practice seen N * sizeof 383169691Skan // (void*) where N is 0, 2 or 4). According to folklore, 384169691Skan // picking this value on the high side is better than 385169691Skan // low-balling it (especially when this algorithm is used with 386169691Skan // malloc implementations that allocate memory blocks rounded up 387169691Skan // to a size which is a power of 2). 388169691Skan const size_type __pagesize = 4096; 389169691Skan const size_type __malloc_header_size = 4 * sizeof(void*); 390169691Skan 391169691Skan // The below implements an exponential growth policy, necessary to 392169691Skan // meet amortized linear time requirements of the library: see 393169691Skan // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html. 394169691Skan if (__capacity > __old_capacity && __capacity < 2 * __old_capacity) 395169691Skan { 396169691Skan __capacity = 2 * __old_capacity; 397169691Skan // Never allocate a string bigger than _S_max_size. 398169691Skan if (__capacity > size_type(_S_max_size)) 399169691Skan __capacity = size_type(_S_max_size); 400169691Skan } 401169691Skan 402169691Skan // NB: Need an array of char_type[__capacity], plus a terminating 403169691Skan // null char_type() element, plus enough for the _Rep data structure, 404169691Skan // plus sizeof(_Rep) - 1 to upper round to a size multiple of 405169691Skan // sizeof(_Rep). 406169691Skan // Whew. Seemingly so needy, yet so elemental. 407169691Skan size_type __size = ((__capacity + 1) * sizeof(_CharT) 408169691Skan + 2 * sizeof(_Rep) - 1); 409169691Skan 410169691Skan const size_type __adj_size = __size + __malloc_header_size; 411169691Skan if (__adj_size > __pagesize && __capacity > __old_capacity) 412169691Skan { 413169691Skan const size_type __extra = __pagesize - __adj_size % __pagesize; 414169691Skan __capacity += __extra / sizeof(_CharT); 415169691Skan if (__capacity > size_type(_S_max_size)) 416169691Skan __capacity = size_type(_S_max_size); 417169691Skan __size = (__capacity + 1) * sizeof(_CharT) + 2 * sizeof(_Rep) - 1; 418169691Skan } 419169691Skan 420169691Skan // NB: Might throw, but no worries about a leak, mate: _Rep() 421169691Skan // does not throw. 422169691Skan _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size / sizeof(_Rep)); 423169691Skan _Rep* __p = new (__place) _Rep; 424169691Skan __p->_M_info._M_capacity = __capacity; 425169691Skan return __p; 426169691Skan } 427169691Skan 428169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 429169691Skan void 430169691Skan __rc_string_base<_CharT, _Traits, _Alloc>::_Rep:: 431169691Skan _M_destroy(const _Alloc& __a) throw () 432169691Skan { 433169691Skan const size_type __size = ((_M_info._M_capacity + 1) * sizeof(_CharT) 434169691Skan + 2 * sizeof(_Rep) - 1); 435169691Skan _Rep_alloc_type(__a).deallocate(this, __size / sizeof(_Rep)); 436169691Skan } 437169691Skan 438169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 439169691Skan _CharT* 440169691Skan __rc_string_base<_CharT, _Traits, _Alloc>::_Rep:: 441169691Skan _M_clone(const _Alloc& __alloc, size_type __res) 442169691Skan { 443169691Skan // Requested capacity of the clone. 444169691Skan const size_type __requested_cap = _M_info._M_length + __res; 445169691Skan _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity, 446169691Skan __alloc); 447169691Skan 448169691Skan if (_M_info._M_length) 449169691Skan _S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length); 450169691Skan 451169691Skan __r->_M_set_length(_M_info._M_length); 452169691Skan return __r->_M_refdata(); 453169691Skan } 454169691Skan 455169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 456169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 457169691Skan __rc_string_base(const _Alloc& __a) 458169691Skan : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { } 459169691Skan 460169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 461169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 462169691Skan __rc_string_base(const __rc_string_base& __rcs) 463169691Skan : _M_dataplus(__rcs._M_get_allocator(), 464169691Skan __rcs._M_grab(__rcs._M_get_allocator())) { } 465169691Skan 466169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 467169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 468169691Skan __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a) 469169691Skan : _M_dataplus(__a, _S_construct(__n, __c, __a)) { } 470169691Skan 471169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 472169691Skan template<typename _InputIterator> 473169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 474169691Skan __rc_string_base(_InputIterator __beg, _InputIterator __end, 475169691Skan const _Alloc& __a) 476169691Skan : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { } 477169691Skan 478169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 479169691Skan void 480169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 481169691Skan _M_leak_hard() 482169691Skan { 483169691Skan if (_M_is_shared()) 484169691Skan _M_erase(0, 0); 485169691Skan _M_set_leaked(); 486169691Skan } 487169691Skan 488169691Skan // NB: This is the special case for Input Iterators, used in 489169691Skan // istreambuf_iterators, etc. 490169691Skan // Input Iterators have a cost structure very different from 491169691Skan // pointers, calling for a different coding style. 492169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 493169691Skan template<typename _InIterator> 494169691Skan _CharT* 495169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 496169691Skan _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, 497169691Skan std::input_iterator_tag) 498169691Skan { 499169691Skan if (__beg == __end && __a == _Alloc()) 500169691Skan return _S_empty_rep._M_refcopy(); 501169691Skan 502169691Skan // Avoid reallocation for common case. 503169691Skan _CharT __buf[128]; 504169691Skan size_type __len = 0; 505169691Skan while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT)) 506169691Skan { 507169691Skan __buf[__len++] = *__beg; 508169691Skan ++__beg; 509169691Skan } 510169691Skan _Rep* __r = _Rep::_S_create(__len, size_type(0), __a); 511169691Skan _S_copy(__r->_M_refdata(), __buf, __len); 512169691Skan try 513169691Skan { 514169691Skan while (__beg != __end) 515169691Skan { 516169691Skan if (__len == __r->_M_info._M_capacity) 517169691Skan { 518169691Skan // Allocate more space. 519169691Skan _Rep* __another = _Rep::_S_create(__len + 1, __len, __a); 520169691Skan _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len); 521169691Skan __r->_M_destroy(__a); 522169691Skan __r = __another; 523169691Skan } 524169691Skan __r->_M_refdata()[__len++] = *__beg; 525169691Skan ++__beg; 526169691Skan } 527169691Skan } 528169691Skan catch(...) 529169691Skan { 530169691Skan __r->_M_destroy(__a); 531169691Skan __throw_exception_again; 532169691Skan } 533169691Skan __r->_M_set_length(__len); 534169691Skan return __r->_M_refdata(); 535169691Skan } 536169691Skan 537169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 538169691Skan template<typename _InIterator> 539169691Skan _CharT* 540169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 541169691Skan _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, 542169691Skan std::forward_iterator_tag) 543169691Skan { 544169691Skan if (__beg == __end && __a == _Alloc()) 545169691Skan return _S_empty_rep._M_refcopy(); 546169691Skan 547169691Skan // NB: Not required, but considered best practice. 548169691Skan if (__builtin_expect(_S_is_null_pointer(__beg) && __beg != __end, 0)) 549169691Skan std::__throw_logic_error(__N("__rc_string_base::" 550169691Skan "_S_construct NULL not valid")); 551169691Skan 552169691Skan const size_type __dnew = static_cast<size_type>(std::distance(__beg, 553169691Skan __end)); 554169691Skan // Check for out_of_range and length_error exceptions. 555169691Skan _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a); 556169691Skan try 557169691Skan { _S_copy_chars(__r->_M_refdata(), __beg, __end); } 558169691Skan catch(...) 559169691Skan { 560169691Skan __r->_M_destroy(__a); 561169691Skan __throw_exception_again; 562169691Skan } 563169691Skan __r->_M_set_length(__dnew); 564169691Skan return __r->_M_refdata(); 565169691Skan } 566169691Skan 567169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 568169691Skan _CharT* 569169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 570169691Skan _S_construct(size_type __n, _CharT __c, const _Alloc& __a) 571169691Skan { 572169691Skan if (__n == 0 && __a == _Alloc()) 573169691Skan return _S_empty_rep._M_refcopy(); 574169691Skan 575169691Skan // Check for out_of_range and length_error exceptions. 576169691Skan _Rep* __r = _Rep::_S_create(__n, size_type(0), __a); 577169691Skan if (__n) 578169691Skan _S_assign(__r->_M_refdata(), __n, __c); 579169691Skan 580169691Skan __r->_M_set_length(__n); 581169691Skan return __r->_M_refdata(); 582169691Skan } 583169691Skan 584169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 585169691Skan void 586169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 587169691Skan _M_swap(__rc_string_base& __rcs) 588169691Skan { 589169691Skan if (_M_is_leaked()) 590169691Skan _M_set_sharable(); 591169691Skan if (__rcs._M_is_leaked()) 592169691Skan __rcs._M_set_sharable(); 593169691Skan 594169691Skan _CharT* __tmp = _M_data(); 595169691Skan _M_data(__rcs._M_data()); 596169691Skan __rcs._M_data(__tmp); 597169691Skan 598169691Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 599169691Skan // 431. Swapping containers with unequal allocators. 600169691Skan std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(), 601169691Skan __rcs._M_get_allocator()); 602169691Skan } 603169691Skan 604169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 605169691Skan void 606169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 607169691Skan _M_assign(const __rc_string_base& __rcs) 608169691Skan { 609169691Skan if (_M_rep() != __rcs._M_rep()) 610169691Skan { 611169691Skan _CharT* __tmp = __rcs._M_grab(_M_get_allocator()); 612169691Skan _M_dispose(); 613169691Skan _M_data(__tmp); 614169691Skan } 615169691Skan } 616169691Skan 617169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 618169691Skan void 619169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 620169691Skan _M_reserve(size_type __res) 621169691Skan { 622169691Skan // Make sure we don't shrink below the current size. 623169691Skan if (__res < _M_length()) 624169691Skan __res = _M_length(); 625169691Skan 626169691Skan if (__res != _M_capacity() || _M_is_shared()) 627169691Skan { 628169691Skan _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(), 629169691Skan __res - _M_length()); 630169691Skan _M_dispose(); 631169691Skan _M_data(__tmp); 632169691Skan } 633169691Skan } 634169691Skan 635169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 636169691Skan void 637169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 638169691Skan _M_mutate(size_type __pos, size_type __len1, const _CharT* __s, 639169691Skan size_type __len2) 640169691Skan { 641169691Skan const size_type __how_much = _M_length() - __pos - __len1; 642169691Skan 643169691Skan _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1, 644169691Skan _M_capacity(), _M_get_allocator()); 645169691Skan 646169691Skan if (__pos) 647169691Skan _S_copy(__r->_M_refdata(), _M_data(), __pos); 648169691Skan if (__s && __len2) 649169691Skan _S_copy(__r->_M_refdata() + __pos, __s, __len2); 650169691Skan if (__how_much) 651169691Skan _S_copy(__r->_M_refdata() + __pos + __len2, 652169691Skan _M_data() + __pos + __len1, __how_much); 653169691Skan 654169691Skan _M_dispose(); 655169691Skan _M_data(__r->_M_refdata()); 656169691Skan } 657169691Skan 658169691Skan template<typename _CharT, typename _Traits, typename _Alloc> 659169691Skan void 660169691Skan __rc_string_base<_CharT, _Traits, _Alloc>:: 661169691Skan _M_erase(size_type __pos, size_type __n) 662169691Skan { 663169691Skan const size_type __new_size = _M_length() - __n; 664169691Skan const size_type __how_much = _M_length() - __pos - __n; 665169691Skan 666169691Skan if (_M_is_shared()) 667169691Skan { 668169691Skan // Must reallocate. 669169691Skan _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(), 670169691Skan _M_get_allocator()); 671169691Skan 672169691Skan if (__pos) 673169691Skan _S_copy(__r->_M_refdata(), _M_data(), __pos); 674169691Skan if (__how_much) 675169691Skan _S_copy(__r->_M_refdata() + __pos, 676169691Skan _M_data() + __pos + __n, __how_much); 677169691Skan 678169691Skan _M_dispose(); 679169691Skan _M_data(__r->_M_refdata()); 680169691Skan } 681169691Skan else if (__how_much && __n) 682169691Skan { 683169691Skan // Work in-place. 684169691Skan _S_move(_M_data() + __pos, 685169691Skan _M_data() + __pos + __n, __how_much); 686169691Skan } 687169691Skan 688169691Skan _M_rep()->_M_set_length(__new_size); 689169691Skan } 690169691Skan 691169691Skan template<> 692169691Skan inline bool 693169691Skan __rc_string_base<char, std::char_traits<char>, 694169691Skan std::allocator<char> >:: 695169691Skan _M_compare(const __rc_string_base& __rcs) const 696169691Skan { 697169691Skan if (_M_rep() == __rcs._M_rep()) 698169691Skan return true; 699169691Skan return false; 700169691Skan } 701169691Skan 702169691Skan#ifdef _GLIBCXX_USE_WCHAR_T 703169691Skan template<> 704169691Skan inline bool 705169691Skan __rc_string_base<wchar_t, std::char_traits<wchar_t>, 706169691Skan std::allocator<wchar_t> >:: 707169691Skan _M_compare(const __rc_string_base& __rcs) const 708169691Skan { 709169691Skan if (_M_rep() == __rcs._M_rep()) 710169691Skan return true; 711169691Skan return false; 712169691Skan } 713169691Skan#endif 714169691Skan 715169691Skan_GLIBCXX_END_NAMESPACE 716169691Skan 717169691Skan#endif /* _RC_STRING_BASE_H */ 718