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