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