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