rc_string_base.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. // Reference-counted versatile string base -*- C++ -*-
  2. // Copyright (C) 2005-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/rc_string_base.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{ext/vstring.h}
  23. */
  24. #ifndef _RC_STRING_BASE_H
  25. #define _RC_STRING_BASE_H 1
  26. #include <ext/atomicity.h>
  27. #include <ext/alloc_traits.h>
  28. #include <bits/stl_iterator_base_funcs.h>
  29. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. /**
  33. * Documentation? What's that?
  34. * Nathan Myers <ncm@cantrip.org>.
  35. *
  36. * A string looks like this:
  37. *
  38. * @code
  39. * [_Rep]
  40. * _M_length
  41. * [__rc_string_base<char_type>] _M_capacity
  42. * _M_dataplus _M_refcount
  43. * _M_p ----------------> unnamed array of char_type
  44. * @endcode
  45. *
  46. * Where the _M_p points to the first character in the string, and
  47. * you cast it to a pointer-to-_Rep and subtract 1 to get a
  48. * pointer to the header.
  49. *
  50. * This approach has the enormous advantage that a string object
  51. * requires only one allocation. All the ugliness is confined
  52. * within a single pair of inline functions, which each compile to
  53. * a single @a add instruction: _Rep::_M_refdata(), and
  54. * __rc_string_base::_M_rep(); and the allocation function which gets a
  55. * block of raw bytes and with room enough and constructs a _Rep
  56. * object at the front.
  57. *
  58. * The reason you want _M_data pointing to the character array and
  59. * not the _Rep is so that the debugger can see the string
  60. * contents. (Probably we should add a non-inline member to get
  61. * the _Rep for the debugger to use, so users can check the actual
  62. * string length.)
  63. *
  64. * Note that the _Rep object is a POD so that you can have a
  65. * static <em>empty string</em> _Rep object already @a constructed before
  66. * static constructors have run. The reference-count encoding is
  67. * chosen so that a 0 indicates one reference, so you never try to
  68. * destroy the empty-string _Rep object.
  69. *
  70. * All but the last paragraph is considered pretty conventional
  71. * for a C++ string implementation.
  72. */
  73. template<typename _CharT, typename _Traits, typename _Alloc>
  74. class __rc_string_base
  75. : protected __vstring_utility<_CharT, _Traits, _Alloc>
  76. {
  77. public:
  78. typedef _Traits traits_type;
  79. typedef typename _Traits::char_type value_type;
  80. typedef _Alloc allocator_type;
  81. typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
  82. typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
  83. typedef typename _CharT_alloc_type::size_type size_type;
  84. private:
  85. // _Rep: string representation
  86. // Invariants:
  87. // 1. String really contains _M_length + 1 characters: due to 21.3.4
  88. // must be kept null-terminated.
  89. // 2. _M_capacity >= _M_length
  90. // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
  91. // 3. _M_refcount has three states:
  92. // -1: leaked, one reference, no ref-copies allowed, non-const.
  93. // 0: one reference, non-const.
  94. // n>0: n + 1 references, operations require a lock, const.
  95. // 4. All fields == 0 is an empty string, given the extra storage
  96. // beyond-the-end for a null terminator; thus, the shared
  97. // empty string representation needs no constructor.
  98. struct _Rep
  99. {
  100. union
  101. {
  102. struct
  103. {
  104. size_type _M_length;
  105. size_type _M_capacity;
  106. _Atomic_word _M_refcount;
  107. } _M_info;
  108. // Only for alignment purposes.
  109. _CharT _M_align;
  110. };
  111. typedef typename __alloc_traits<_Alloc>::template rebind<_Rep>::other
  112. _Rep_alloc_type;
  113. _CharT*
  114. _M_refdata() throw()
  115. { return reinterpret_cast<_CharT*>(this + 1); }
  116. _CharT*
  117. _M_refcopy() throw()
  118. {
  119. __atomic_add_dispatch(&_M_info._M_refcount, 1);
  120. return _M_refdata();
  121. } // XXX MT
  122. void
  123. _M_set_length(size_type __n)
  124. {
  125. _M_info._M_refcount = 0; // One reference.
  126. _M_info._M_length = __n;
  127. // grrr. (per 21.3.4)
  128. // You cannot leave those LWG people alone for a second.
  129. traits_type::assign(_M_refdata()[__n], _CharT());
  130. }
  131. // Create & Destroy
  132. static _Rep*
  133. _S_create(size_type, size_type, const _Alloc&);
  134. void
  135. _M_destroy(const _Alloc&) throw();
  136. _CharT*
  137. _M_clone(const _Alloc&, size_type __res = 0);
  138. };
  139. struct _Rep_empty
  140. : public _Rep
  141. {
  142. _CharT _M_terminal;
  143. };
  144. static _Rep_empty _S_empty_rep;
  145. // The maximum number of individual char_type elements of an
  146. // individual string is determined by _S_max_size. This is the
  147. // value that will be returned by max_size(). (Whereas npos
  148. // is the maximum number of bytes the allocator can allocate.)
  149. // If one was to divvy up the theoretical largest size string,
  150. // with a terminating character and m _CharT elements, it'd
  151. // look like this:
  152. // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
  153. // + sizeof(_Rep) - 1
  154. // (NB: last two terms for rounding reasons, see _M_create below)
  155. // Solving for m:
  156. // m = ((npos - 2 * sizeof(_Rep) + 1) / sizeof(_CharT)) - 1
  157. // In addition, this implementation halves this amount.
  158. enum { _S_max_size = (((static_cast<size_type>(-1) - 2 * sizeof(_Rep)
  159. + 1) / sizeof(_CharT)) - 1) / 2 };
  160. // Data Member (private):
  161. mutable typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus;
  162. void
  163. _M_data(_CharT* __p)
  164. { _M_dataplus._M_p = __p; }
  165. _Rep*
  166. _M_rep() const
  167. { return &((reinterpret_cast<_Rep*>(_M_data()))[-1]); }
  168. _CharT*
  169. _M_grab(const _Alloc& __alloc) const
  170. {
  171. return (!_M_is_leaked() && _M_get_allocator() == __alloc)
  172. ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
  173. }
  174. void
  175. _M_dispose()
  176. {
  177. // Be race-detector-friendly. For more info see bits/c++config.
  178. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_rep()->_M_info.
  179. _M_refcount);
  180. if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount,
  181. -1) <= 0)
  182. {
  183. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_rep()->_M_info.
  184. _M_refcount);
  185. _M_rep()->_M_destroy(_M_get_allocator());
  186. }
  187. } // XXX MT
  188. bool
  189. _M_is_leaked() const
  190. { return _M_rep()->_M_info._M_refcount < 0; }
  191. void
  192. _M_set_sharable()
  193. { _M_rep()->_M_info._M_refcount = 0; }
  194. void
  195. _M_leak_hard();
  196. // _S_construct_aux is used to implement the 21.3.1 para 15 which
  197. // requires special behaviour if _InIterator is an integral type
  198. template<typename _InIterator>
  199. static _CharT*
  200. _S_construct_aux(_InIterator __beg, _InIterator __end,
  201. const _Alloc& __a, std::__false_type)
  202. {
  203. typedef typename std::iterator_traits<_InIterator>::iterator_category
  204. _Tag;
  205. return _S_construct(__beg, __end, __a, _Tag());
  206. }
  207. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  208. // 438. Ambiguity in the "do the right thing" clause
  209. template<typename _Integer>
  210. static _CharT*
  211. _S_construct_aux(_Integer __beg, _Integer __end,
  212. const _Alloc& __a, std::__true_type)
  213. { return _S_construct_aux_2(static_cast<size_type>(__beg),
  214. __end, __a); }
  215. static _CharT*
  216. _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
  217. { return _S_construct(__req, __c, __a); }
  218. template<typename _InIterator>
  219. static _CharT*
  220. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
  221. {
  222. typedef typename std::__is_integer<_InIterator>::__type _Integral;
  223. return _S_construct_aux(__beg, __end, __a, _Integral());
  224. }
  225. // For Input Iterators, used in istreambuf_iterators, etc.
  226. template<typename _InIterator>
  227. static _CharT*
  228. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  229. std::input_iterator_tag);
  230. // For forward_iterators up to random_access_iterators, used for
  231. // string::iterator, _CharT*, etc.
  232. template<typename _FwdIterator>
  233. static _CharT*
  234. _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
  235. std::forward_iterator_tag);
  236. static _CharT*
  237. _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
  238. public:
  239. size_type
  240. _M_max_size() const
  241. { return size_type(_S_max_size); }
  242. _CharT*
  243. _M_data() const
  244. { return _M_dataplus._M_p; }
  245. size_type
  246. _M_length() const
  247. { return _M_rep()->_M_info._M_length; }
  248. size_type
  249. _M_capacity() const
  250. { return _M_rep()->_M_info._M_capacity; }
  251. bool
  252. _M_is_shared() const
  253. { return _M_rep()->_M_info._M_refcount > 0; }
  254. void
  255. _M_set_leaked()
  256. { _M_rep()->_M_info._M_refcount = -1; }
  257. void
  258. _M_leak() // for use in begin() & non-const op[]
  259. {
  260. if (!_M_is_leaked())
  261. _M_leak_hard();
  262. }
  263. void
  264. _M_set_length(size_type __n)
  265. { _M_rep()->_M_set_length(__n); }
  266. __rc_string_base()
  267. : _M_dataplus(_S_empty_rep._M_refcopy()) { }
  268. __rc_string_base(const _Alloc& __a);
  269. __rc_string_base(const __rc_string_base& __rcs);
  270. #if __cplusplus >= 201103L
  271. __rc_string_base(__rc_string_base&& __rcs)
  272. : _M_dataplus(__rcs._M_dataplus)
  273. { __rcs._M_data(_S_empty_rep._M_refcopy()); }
  274. #endif
  275. __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a);
  276. template<typename _InputIterator>
  277. __rc_string_base(_InputIterator __beg, _InputIterator __end,
  278. const _Alloc& __a);
  279. ~__rc_string_base()
  280. { _M_dispose(); }
  281. allocator_type&
  282. _M_get_allocator()
  283. { return _M_dataplus; }
  284. const allocator_type&
  285. _M_get_allocator() const
  286. { return _M_dataplus; }
  287. void
  288. _M_swap(__rc_string_base& __rcs);
  289. void
  290. _M_assign(const __rc_string_base& __rcs);
  291. void
  292. _M_reserve(size_type __res);
  293. void
  294. _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
  295. size_type __len2);
  296. void
  297. _M_erase(size_type __pos, size_type __n);
  298. void
  299. _M_clear()
  300. {
  301. _M_dispose();
  302. _M_data(_S_empty_rep._M_refcopy());
  303. }
  304. bool
  305. _M_compare(const __rc_string_base&) const
  306. { return false; }
  307. };
  308. template<typename _CharT, typename _Traits, typename _Alloc>
  309. typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep_empty
  310. __rc_string_base<_CharT, _Traits, _Alloc>::_S_empty_rep;
  311. template<typename _CharT, typename _Traits, typename _Alloc>
  312. typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep*
  313. __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
  314. _S_create(size_type __capacity, size_type __old_capacity,
  315. const _Alloc& __alloc)
  316. {
  317. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  318. // 83. String::npos vs. string::max_size()
  319. if (__capacity > size_type(_S_max_size))
  320. std::__throw_length_error(__N("__rc_string_base::_Rep::_S_create"));
  321. // The standard places no restriction on allocating more memory
  322. // than is strictly needed within this layer at the moment or as
  323. // requested by an explicit application call to reserve().
  324. // Many malloc implementations perform quite poorly when an
  325. // application attempts to allocate memory in a stepwise fashion
  326. // growing each allocation size by only 1 char. Additionally,
  327. // it makes little sense to allocate less linear memory than the
  328. // natural blocking size of the malloc implementation.
  329. // Unfortunately, we would need a somewhat low-level calculation
  330. // with tuned parameters to get this perfect for any particular
  331. // malloc implementation. Fortunately, generalizations about
  332. // common features seen among implementations seems to suffice.
  333. // __pagesize need not match the actual VM page size for good
  334. // results in practice, thus we pick a common value on the low
  335. // side. __malloc_header_size is an estimate of the amount of
  336. // overhead per memory allocation (in practice seen N * sizeof
  337. // (void*) where N is 0, 2 or 4). According to folklore,
  338. // picking this value on the high side is better than
  339. // low-balling it (especially when this algorithm is used with
  340. // malloc implementations that allocate memory blocks rounded up
  341. // to a size which is a power of 2).
  342. const size_type __pagesize = 4096;
  343. const size_type __malloc_header_size = 4 * sizeof(void*);
  344. // The below implements an exponential growth policy, necessary to
  345. // meet amortized linear time requirements of the library: see
  346. // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
  347. if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
  348. {
  349. __capacity = 2 * __old_capacity;
  350. // Never allocate a string bigger than _S_max_size.
  351. if (__capacity > size_type(_S_max_size))
  352. __capacity = size_type(_S_max_size);
  353. }
  354. // NB: Need an array of char_type[__capacity], plus a terminating
  355. // null char_type() element, plus enough for the _Rep data structure,
  356. // plus sizeof(_Rep) - 1 to upper round to a size multiple of
  357. // sizeof(_Rep).
  358. // Whew. Seemingly so needy, yet so elemental.
  359. size_type __size = ((__capacity + 1) * sizeof(_CharT)
  360. + 2 * sizeof(_Rep) - 1);
  361. const size_type __adj_size = __size + __malloc_header_size;
  362. if (__adj_size > __pagesize && __capacity > __old_capacity)
  363. {
  364. const size_type __extra = __pagesize - __adj_size % __pagesize;
  365. __capacity += __extra / sizeof(_CharT);
  366. if (__capacity > size_type(_S_max_size))
  367. __capacity = size_type(_S_max_size);
  368. __size = (__capacity + 1) * sizeof(_CharT) + 2 * sizeof(_Rep) - 1;
  369. }
  370. // NB: Might throw, but no worries about a leak, mate: _Rep()
  371. // does not throw.
  372. _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size / sizeof(_Rep));
  373. _Rep* __p = new (__place) _Rep;
  374. __p->_M_info._M_capacity = __capacity;
  375. return __p;
  376. }
  377. template<typename _CharT, typename _Traits, typename _Alloc>
  378. void
  379. __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
  380. _M_destroy(const _Alloc& __a) throw ()
  381. {
  382. const size_type __size = ((_M_info._M_capacity + 1) * sizeof(_CharT)
  383. + 2 * sizeof(_Rep) - 1);
  384. _Rep_alloc_type(__a).deallocate(this, __size / sizeof(_Rep));
  385. }
  386. template<typename _CharT, typename _Traits, typename _Alloc>
  387. _CharT*
  388. __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
  389. _M_clone(const _Alloc& __alloc, size_type __res)
  390. {
  391. // Requested capacity of the clone.
  392. const size_type __requested_cap = _M_info._M_length + __res;
  393. _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
  394. __alloc);
  395. if (_M_info._M_length)
  396. __rc_string_base::_S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
  397. __r->_M_set_length(_M_info._M_length);
  398. return __r->_M_refdata();
  399. }
  400. template<typename _CharT, typename _Traits, typename _Alloc>
  401. __rc_string_base<_CharT, _Traits, _Alloc>::
  402. __rc_string_base(const _Alloc& __a)
  403. : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
  404. template<typename _CharT, typename _Traits, typename _Alloc>
  405. __rc_string_base<_CharT, _Traits, _Alloc>::
  406. __rc_string_base(const __rc_string_base& __rcs)
  407. : _M_dataplus(__rcs._M_get_allocator(),
  408. __rcs._M_grab(__rcs._M_get_allocator())) { }
  409. template<typename _CharT, typename _Traits, typename _Alloc>
  410. __rc_string_base<_CharT, _Traits, _Alloc>::
  411. __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a)
  412. : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
  413. template<typename _CharT, typename _Traits, typename _Alloc>
  414. template<typename _InputIterator>
  415. __rc_string_base<_CharT, _Traits, _Alloc>::
  416. __rc_string_base(_InputIterator __beg, _InputIterator __end,
  417. const _Alloc& __a)
  418. : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
  419. template<typename _CharT, typename _Traits, typename _Alloc>
  420. void
  421. __rc_string_base<_CharT, _Traits, _Alloc>::
  422. _M_leak_hard()
  423. {
  424. if (_M_is_shared())
  425. _M_erase(0, 0);
  426. _M_set_leaked();
  427. }
  428. // NB: This is the special case for Input Iterators, used in
  429. // istreambuf_iterators, etc.
  430. // Input Iterators have a cost structure very different from
  431. // pointers, calling for a different coding style.
  432. template<typename _CharT, typename _Traits, typename _Alloc>
  433. template<typename _InIterator>
  434. _CharT*
  435. __rc_string_base<_CharT, _Traits, _Alloc>::
  436. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  437. std::input_iterator_tag)
  438. {
  439. if (__beg == __end && __a == _Alloc())
  440. return _S_empty_rep._M_refcopy();
  441. // Avoid reallocation for common case.
  442. _CharT __buf[128];
  443. size_type __len = 0;
  444. while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
  445. {
  446. __buf[__len++] = *__beg;
  447. ++__beg;
  448. }
  449. _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
  450. _S_copy(__r->_M_refdata(), __buf, __len);
  451. __try
  452. {
  453. while (__beg != __end)
  454. {
  455. if (__len == __r->_M_info._M_capacity)
  456. {
  457. // Allocate more space.
  458. _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
  459. _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
  460. __r->_M_destroy(__a);
  461. __r = __another;
  462. }
  463. __r->_M_refdata()[__len++] = *__beg;
  464. ++__beg;
  465. }
  466. }
  467. __catch(...)
  468. {
  469. __r->_M_destroy(__a);
  470. __throw_exception_again;
  471. }
  472. __r->_M_set_length(__len);
  473. return __r->_M_refdata();
  474. }
  475. template<typename _CharT, typename _Traits, typename _Alloc>
  476. template<typename _InIterator>
  477. _CharT*
  478. __rc_string_base<_CharT, _Traits, _Alloc>::
  479. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  480. std::forward_iterator_tag)
  481. {
  482. if (__beg == __end && __a == _Alloc())
  483. return _S_empty_rep._M_refcopy();
  484. // NB: Not required, but considered best practice.
  485. if (__is_null_pointer(__beg) && __beg != __end)
  486. std::__throw_logic_error(__N("__rc_string_base::"
  487. "_S_construct null not valid"));
  488. const size_type __dnew = static_cast<size_type>(std::distance(__beg,
  489. __end));
  490. // Check for out_of_range and length_error exceptions.
  491. _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
  492. __try
  493. { __rc_string_base::_S_copy_chars(__r->_M_refdata(), __beg, __end); }
  494. __catch(...)
  495. {
  496. __r->_M_destroy(__a);
  497. __throw_exception_again;
  498. }
  499. __r->_M_set_length(__dnew);
  500. return __r->_M_refdata();
  501. }
  502. template<typename _CharT, typename _Traits, typename _Alloc>
  503. _CharT*
  504. __rc_string_base<_CharT, _Traits, _Alloc>::
  505. _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
  506. {
  507. if (__n == 0 && __a == _Alloc())
  508. return _S_empty_rep._M_refcopy();
  509. // Check for out_of_range and length_error exceptions.
  510. _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
  511. if (__n)
  512. __rc_string_base::_S_assign(__r->_M_refdata(), __n, __c);
  513. __r->_M_set_length(__n);
  514. return __r->_M_refdata();
  515. }
  516. template<typename _CharT, typename _Traits, typename _Alloc>
  517. void
  518. __rc_string_base<_CharT, _Traits, _Alloc>::
  519. _M_swap(__rc_string_base& __rcs)
  520. {
  521. if (_M_is_leaked())
  522. _M_set_sharable();
  523. if (__rcs._M_is_leaked())
  524. __rcs._M_set_sharable();
  525. _CharT* __tmp = _M_data();
  526. _M_data(__rcs._M_data());
  527. __rcs._M_data(__tmp);
  528. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  529. // 431. Swapping containers with unequal allocators.
  530. std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
  531. __rcs._M_get_allocator());
  532. }
  533. template<typename _CharT, typename _Traits, typename _Alloc>
  534. void
  535. __rc_string_base<_CharT, _Traits, _Alloc>::
  536. _M_assign(const __rc_string_base& __rcs)
  537. {
  538. if (_M_rep() != __rcs._M_rep())
  539. {
  540. _CharT* __tmp = __rcs._M_grab(_M_get_allocator());
  541. _M_dispose();
  542. _M_data(__tmp);
  543. }
  544. }
  545. template<typename _CharT, typename _Traits, typename _Alloc>
  546. void
  547. __rc_string_base<_CharT, _Traits, _Alloc>::
  548. _M_reserve(size_type __res)
  549. {
  550. // Make sure we don't shrink below the current size.
  551. if (__res < _M_length())
  552. __res = _M_length();
  553. if (__res != _M_capacity() || _M_is_shared())
  554. {
  555. _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
  556. __res - _M_length());
  557. _M_dispose();
  558. _M_data(__tmp);
  559. }
  560. }
  561. template<typename _CharT, typename _Traits, typename _Alloc>
  562. void
  563. __rc_string_base<_CharT, _Traits, _Alloc>::
  564. _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
  565. size_type __len2)
  566. {
  567. const size_type __how_much = _M_length() - __pos - __len1;
  568. _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
  569. _M_capacity(), _M_get_allocator());
  570. if (__pos)
  571. this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
  572. if (__s && __len2)
  573. this->_S_copy(__r->_M_refdata() + __pos, __s, __len2);
  574. if (__how_much)
  575. this->_S_copy(__r->_M_refdata() + __pos + __len2,
  576. _M_data() + __pos + __len1, __how_much);
  577. _M_dispose();
  578. _M_data(__r->_M_refdata());
  579. }
  580. template<typename _CharT, typename _Traits, typename _Alloc>
  581. void
  582. __rc_string_base<_CharT, _Traits, _Alloc>::
  583. _M_erase(size_type __pos, size_type __n)
  584. {
  585. const size_type __new_size = _M_length() - __n;
  586. const size_type __how_much = _M_length() - __pos - __n;
  587. if (_M_is_shared())
  588. {
  589. // Must reallocate.
  590. _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
  591. _M_get_allocator());
  592. if (__pos)
  593. this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
  594. if (__how_much)
  595. this->_S_copy(__r->_M_refdata() + __pos,
  596. _M_data() + __pos + __n, __how_much);
  597. _M_dispose();
  598. _M_data(__r->_M_refdata());
  599. }
  600. else if (__how_much && __n)
  601. {
  602. // Work in-place.
  603. this->_S_move(_M_data() + __pos,
  604. _M_data() + __pos + __n, __how_much);
  605. }
  606. _M_rep()->_M_set_length(__new_size);
  607. }
  608. template<>
  609. inline bool
  610. __rc_string_base<char, std::char_traits<char>,
  611. std::allocator<char> >::
  612. _M_compare(const __rc_string_base& __rcs) const
  613. {
  614. if (_M_rep() == __rcs._M_rep())
  615. return true;
  616. return false;
  617. }
  618. #ifdef _GLIBCXX_USE_WCHAR_T
  619. template<>
  620. inline bool
  621. __rc_string_base<wchar_t, std::char_traits<wchar_t>,
  622. std::allocator<wchar_t> >::
  623. _M_compare(const __rc_string_base& __rcs) const
  624. {
  625. if (_M_rep() == __rcs._M_rep())
  626. return true;
  627. return false;
  628. }
  629. #endif
  630. _GLIBCXX_END_NAMESPACE_VERSION
  631. } // namespace
  632. #endif /* _RC_STRING_BASE_H */