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