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