1 // Reference-counted versatile string base -*- C++ -*-
3 // Copyright (C) 2005-2024 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file ext/rc_string_base.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{ext/vstring.h}
30 #ifndef _RC_STRING_BASE_H
31 #define _RC_STRING_BASE_H 1
33 #include <bits/requires_hosted.h> // GNU extensions are currently omitted
35 #include <ext/atomicity.h>
36 #include <ext/alloc_traits.h>
37 #include <bits/stl_iterator_base_funcs.h>
39 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 * Documentation? What's that?
45 * Nathan Myers <ncm@cantrip.org>.
47 * A string looks like this:
52 * [__rc_string_base<char_type>] _M_capacity
53 * _M_dataplus _M_refcount
54 * _M_p ----------------> unnamed array of char_type
57 * Where the _M_p points to the first character in the string, and
58 * you cast it to a pointer-to-_Rep and subtract 1 to get a
59 * pointer to the header.
61 * This approach has the enormous advantage that a string object
62 * requires only one allocation. All the ugliness is confined
63 * within a single pair of inline functions, which each compile to
64 * a single @a add instruction: _Rep::_M_refdata(), and
65 * __rc_string_base::_M_rep(); and the allocation function which gets a
66 * block of raw bytes and with room enough and constructs a _Rep
67 * object at the front.
69 * The reason you want _M_data pointing to the character array and
70 * not the _Rep is so that the debugger can see the string
71 * contents. (Probably we should add a non-inline member to get
72 * the _Rep for the debugger to use, so users can check the actual
75 * Note that the _Rep object is a POD so that you can have a
76 * static <em>empty string</em> _Rep object already @a constructed before
77 * static constructors have run. The reference-count encoding is
78 * chosen so that a 0 indicates one reference, so you never try to
79 * destroy the empty-string _Rep object.
81 * All but the last paragraph is considered pretty conventional
82 * for a C++ string implementation.
84 template<typename _CharT
, typename _Traits
, typename _Alloc
>
85 class __rc_string_base
86 : protected __vstring_utility
<_CharT
, _Traits
, _Alloc
>
89 typedef _Traits traits_type
;
90 typedef typename
_Traits::char_type value_type
;
91 typedef _Alloc allocator_type
;
93 typedef __vstring_utility
<_CharT
, _Traits
, _Alloc
> _Util_Base
;
94 typedef typename
_Util_Base::_CharT_alloc_type _CharT_alloc_type
;
95 typedef typename
_CharT_alloc_type::size_type size_type
;
98 // _Rep: string representation
100 // 1. String really contains _M_length + 1 characters: due to 21.3.4
101 // must be kept null-terminated.
102 // 2. _M_capacity >= _M_length
103 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
104 // 3. _M_refcount has three states:
105 // -1: leaked, one reference, no ref-copies allowed, non-const.
106 // 0: one reference, non-const.
107 // n>0: n + 1 references, operations require a lock, const.
108 // 4. All fields == 0 is an empty string, given the extra storage
109 // beyond-the-end for a null terminator; thus, the shared
110 // empty string representation needs no constructor.
118 size_type _M_capacity
;
119 _Atomic_word _M_refcount
;
122 // Only for alignment purposes.
126 typedef typename __alloc_traits
<_Alloc
>::template rebind
<_Rep
>::other
131 { return reinterpret_cast<_CharT
*>(this + 1); }
136 __atomic_add_dispatch(&_M_info
._M_refcount
, 1);
141 _M_set_length(size_type __n
)
143 _M_info
._M_refcount
= 0; // One reference.
144 _M_info
._M_length
= __n
;
145 // grrr. (per 21.3.4)
146 // You cannot leave those LWG people alone for a second.
147 traits_type::assign(_M_refdata()[__n
], _CharT());
152 _S_create(size_type
, size_type
, const _Alloc
&);
155 _M_destroy(const _Alloc
&) throw();
158 _M_clone(const _Alloc
&, size_type __res
= 0);
167 static _Rep_empty _S_empty_rep
;
169 // The maximum number of individual char_type elements of an
170 // individual string is determined by _S_max_size. This is the
171 // value that will be returned by max_size(). (Whereas npos
172 // is the maximum number of bytes the allocator can allocate.)
173 // If one was to divvy up the theoretical largest size string,
174 // with a terminating character and m _CharT elements, it'd
176 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
177 // + sizeof(_Rep) - 1
178 // (NB: last two terms for rounding reasons, see _M_create below)
180 // m = ((npos - 2 * sizeof(_Rep) + 1) / sizeof(_CharT)) - 1
181 // In addition, this implementation halves this amount.
182 enum { _S_max_size
= (((static_cast<size_type
>(-1) - 2 * sizeof(_Rep
)
183 + 1) / sizeof(_CharT
)) - 1) / 2 };
185 // Data Member (private):
186 mutable typename
_Util_Base::template _Alloc_hider
<_Alloc
> _M_dataplus
;
190 { _M_dataplus
._M_p
= __p
; }
194 { return &((reinterpret_cast<_Rep
*>(_M_data()))[-1]); }
197 _M_grab(const _Alloc
& __alloc
) const
199 return (!_M_is_leaked() && _M_get_allocator() == __alloc
)
200 ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc
);
206 // Be race-detector-friendly. For more info see bits/c++config.
207 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_rep()->_M_info
.
209 if (__exchange_and_add_dispatch(&_M_rep()->_M_info
._M_refcount
,
212 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_rep()->_M_info
.
214 _M_rep()->_M_destroy(_M_get_allocator());
220 { return _M_rep()->_M_info
._M_refcount
< 0; }
224 { _M_rep()->_M_info
._M_refcount
= 0; }
229 // _S_construct_aux is used to implement the 21.3.1 para 15 which
230 // requires special behaviour if _InIterator is an integral type
231 template<typename _InIterator
>
233 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
234 const _Alloc
& __a
, std::__false_type
)
236 typedef typename
std::iterator_traits
<_InIterator
>::iterator_category
238 return _S_construct(__beg
, __end
, __a
, _Tag());
241 // _GLIBCXX_RESOLVE_LIB_DEFECTS
242 // 438. Ambiguity in the "do the right thing" clause
243 template<typename _Integer
>
245 _S_construct_aux(_Integer __beg
, _Integer __end
,
246 const _Alloc
& __a
, std::__true_type
)
247 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
251 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
252 { return _S_construct(__req
, __c
, __a
); }
254 template<typename _InIterator
>
256 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
258 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
259 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
262 // For Input Iterators, used in istreambuf_iterators, etc.
263 template<typename _InIterator
>
265 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
266 std::input_iterator_tag
);
268 // For forward_iterators up to random_access_iterators, used for
269 // string::iterator, _CharT*, etc.
270 template<typename _FwdIterator
>
272 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
273 std::forward_iterator_tag
);
276 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
281 { return size_type(_S_max_size
); }
285 { return _M_dataplus
._M_p
; }
289 { return _M_rep()->_M_info
._M_length
; }
293 { return _M_rep()->_M_info
._M_capacity
; }
297 { return _M_rep()->_M_info
._M_refcount
> 0; }
301 { _M_rep()->_M_info
._M_refcount
= -1; }
304 _M_leak() // for use in begin() & non-const op[]
311 _M_set_length(size_type __n
)
312 { _M_rep()->_M_set_length(__n
); }
315 : _M_dataplus(_S_empty_rep
._M_refcopy()) { }
317 __rc_string_base(const _Alloc
& __a
);
319 __rc_string_base(const __rc_string_base
& __rcs
);
321 #if __cplusplus >= 201103L
322 __rc_string_base(__rc_string_base
&& __rcs
)
323 : _M_dataplus(__rcs
._M_dataplus
)
324 { __rcs
._M_data(_S_empty_rep
._M_refcopy()); }
327 __rc_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
);
329 template<typename _InputIterator
>
330 __rc_string_base(_InputIterator __beg
, _InputIterator __end
,
338 { return _M_dataplus
; }
340 const allocator_type
&
341 _M_get_allocator() const
342 { return _M_dataplus
; }
345 _M_swap(__rc_string_base
& __rcs
);
348 _M_assign(const __rc_string_base
& __rcs
);
351 _M_reserve(size_type __res
);
354 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
358 _M_erase(size_type __pos
, size_type __n
);
364 _M_data(_S_empty_rep
._M_refcopy());
368 _M_compare(const __rc_string_base
&) const
372 template<typename _CharT
, typename _Traits
, typename _Alloc
>
373 typename __rc_string_base
<_CharT
, _Traits
, _Alloc
>::_Rep_empty
374 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::_S_empty_rep
;
376 template<typename _CharT
, typename _Traits
, typename _Alloc
>
377 typename __rc_string_base
<_CharT
, _Traits
, _Alloc
>::_Rep
*
378 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::_Rep::
379 _S_create(size_type __capacity
, size_type __old_capacity
,
380 const _Alloc
& __alloc
)
382 // _GLIBCXX_RESOLVE_LIB_DEFECTS
383 // 83. String::npos vs. string::max_size()
384 if (__capacity
> size_type(_S_max_size
))
385 std::__throw_length_error(__N("__rc_string_base::_Rep::_S_create"));
387 // The standard places no restriction on allocating more memory
388 // than is strictly needed within this layer at the moment or as
389 // requested by an explicit application call to reserve().
391 // Many malloc implementations perform quite poorly when an
392 // application attempts to allocate memory in a stepwise fashion
393 // growing each allocation size by only 1 char. Additionally,
394 // it makes little sense to allocate less linear memory than the
395 // natural blocking size of the malloc implementation.
396 // Unfortunately, we would need a somewhat low-level calculation
397 // with tuned parameters to get this perfect for any particular
398 // malloc implementation. Fortunately, generalizations about
399 // common features seen among implementations seems to suffice.
401 // __pagesize need not match the actual VM page size for good
402 // results in practice, thus we pick a common value on the low
403 // side. __malloc_header_size is an estimate of the amount of
404 // overhead per memory allocation (in practice seen N * sizeof
405 // (void*) where N is 0, 2 or 4). According to folklore,
406 // picking this value on the high side is better than
407 // low-balling it (especially when this algorithm is used with
408 // malloc implementations that allocate memory blocks rounded up
409 // to a size which is a power of 2).
410 const size_type __pagesize
= 4096;
411 const size_type __malloc_header_size
= 4 * sizeof(void*);
413 // The below implements an exponential growth policy, necessary to
414 // meet amortized linear time requirements of the library: see
415 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
416 if (__capacity
> __old_capacity
&& __capacity
< 2 * __old_capacity
)
418 __capacity
= 2 * __old_capacity
;
419 // Never allocate a string bigger than _S_max_size.
420 if (__capacity
> size_type(_S_max_size
))
421 __capacity
= size_type(_S_max_size
);
424 // NB: Need an array of char_type[__capacity], plus a terminating
425 // null char_type() element, plus enough for the _Rep data structure,
426 // plus sizeof(_Rep) - 1 to upper round to a size multiple of
428 // Whew. Seemingly so needy, yet so elemental.
429 size_type __size
= ((__capacity
+ 1) * sizeof(_CharT
)
430 + 2 * sizeof(_Rep
) - 1);
432 const size_type __adj_size
= __size
+ __malloc_header_size
;
433 if (__adj_size
> __pagesize
&& __capacity
> __old_capacity
)
435 const size_type __extra
= __pagesize
- __adj_size
% __pagesize
;
436 __capacity
+= __extra
/ sizeof(_CharT
);
437 if (__capacity
> size_type(_S_max_size
))
438 __capacity
= size_type(_S_max_size
);
439 __size
= (__capacity
+ 1) * sizeof(_CharT
) + 2 * sizeof(_Rep
) - 1;
442 // NB: Might throw, but no worries about a leak, mate: _Rep()
444 _Rep
* __place
= _Rep_alloc_type(__alloc
).allocate(__size
/ sizeof(_Rep
));
445 _Rep
* __p
= new (__place
) _Rep
;
446 __p
->_M_info
._M_capacity
= __capacity
;
450 template<typename _CharT
, typename _Traits
, typename _Alloc
>
452 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::_Rep::
453 _M_destroy(const _Alloc
& __a
) throw ()
455 const size_type __size
= ((_M_info
._M_capacity
+ 1) * sizeof(_CharT
)
456 + 2 * sizeof(_Rep
) - 1);
457 _Rep_alloc_type(__a
).deallocate(this, __size
/ sizeof(_Rep
));
460 template<typename _CharT
, typename _Traits
, typename _Alloc
>
462 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::_Rep::
463 _M_clone(const _Alloc
& __alloc
, size_type __res
)
465 // Requested capacity of the clone.
466 const size_type __requested_cap
= _M_info
._M_length
+ __res
;
467 _Rep
* __r
= _Rep::_S_create(__requested_cap
, _M_info
._M_capacity
,
470 if (_M_info
._M_length
)
471 __rc_string_base::_S_copy(__r
->_M_refdata(), _M_refdata(), _M_info
._M_length
);
473 __r
->_M_set_length(_M_info
._M_length
);
474 return __r
->_M_refdata();
477 template<typename _CharT
, typename _Traits
, typename _Alloc
>
478 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
479 __rc_string_base(const _Alloc
& __a
)
480 : _M_dataplus(__a
, _S_construct(size_type(), _CharT(), __a
)) { }
482 template<typename _CharT
, typename _Traits
, typename _Alloc
>
483 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
484 __rc_string_base(const __rc_string_base
& __rcs
)
485 : _M_dataplus(__rcs
._M_get_allocator(),
486 __rcs
._M_grab(__rcs
._M_get_allocator())) { }
488 template<typename _CharT
, typename _Traits
, typename _Alloc
>
489 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
490 __rc_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
)
491 : _M_dataplus(__a
, _S_construct(__n
, __c
, __a
)) { }
493 template<typename _CharT
, typename _Traits
, typename _Alloc
>
494 template<typename _InputIterator
>
495 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
496 __rc_string_base(_InputIterator __beg
, _InputIterator __end
,
498 : _M_dataplus(__a
, _S_construct(__beg
, __end
, __a
)) { }
500 template<typename _CharT
, typename _Traits
, typename _Alloc
>
502 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
510 // NB: This is the special case for Input Iterators, used in
511 // istreambuf_iterators, etc.
512 // Input Iterators have a cost structure very different from
513 // pointers, calling for a different coding style.
514 template<typename _CharT
, typename _Traits
, typename _Alloc
>
515 template<typename _InIterator
>
517 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
518 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
519 std::input_iterator_tag
)
521 if (__beg
== __end
&& __a
== _Alloc())
522 return _S_empty_rep
._M_refcopy();
524 // Avoid reallocation for common case.
527 while (__beg
!= __end
&& __len
< sizeof(__buf
) / sizeof(_CharT
))
529 __buf
[__len
++] = *__beg
;
532 _Rep
* __r
= _Rep::_S_create(__len
, size_type(0), __a
);
533 _S_copy(__r
->_M_refdata(), __buf
, __len
);
536 while (__beg
!= __end
)
538 if (__len
== __r
->_M_info
._M_capacity
)
540 // Allocate more space.
541 _Rep
* __another
= _Rep::_S_create(__len
+ 1, __len
, __a
);
542 _S_copy(__another
->_M_refdata(), __r
->_M_refdata(), __len
);
543 __r
->_M_destroy(__a
);
546 __r
->_M_refdata()[__len
++] = *__beg
;
552 __r
->_M_destroy(__a
);
553 __throw_exception_again
;
555 __r
->_M_set_length(__len
);
556 return __r
->_M_refdata();
559 template<typename _CharT
, typename _Traits
, typename _Alloc
>
560 template<typename _InIterator
>
562 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
563 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
564 std::forward_iterator_tag
)
566 if (__beg
== __end
&& __a
== _Alloc())
567 return _S_empty_rep
._M_refcopy();
569 // NB: Not required, but considered best practice.
570 if (__is_null_pointer(__beg
) && __beg
!= __end
)
571 std::__throw_logic_error(__N("__rc_string_base::"
572 "_S_construct null not valid"));
574 const size_type __dnew
= static_cast<size_type
>(std::distance(__beg
,
576 // Check for out_of_range and length_error exceptions.
577 _Rep
* __r
= _Rep::_S_create(__dnew
, size_type(0), __a
);
579 { __rc_string_base::_S_copy_chars(__r
->_M_refdata(), __beg
, __end
); }
582 __r
->_M_destroy(__a
);
583 __throw_exception_again
;
585 __r
->_M_set_length(__dnew
);
586 return __r
->_M_refdata();
589 template<typename _CharT
, typename _Traits
, typename _Alloc
>
591 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
592 _S_construct(size_type __n
, _CharT __c
, const _Alloc
& __a
)
594 if (__n
== 0 && __a
== _Alloc())
595 return _S_empty_rep
._M_refcopy();
597 // Check for out_of_range and length_error exceptions.
598 _Rep
* __r
= _Rep::_S_create(__n
, size_type(0), __a
);
600 __rc_string_base::_S_assign(__r
->_M_refdata(), __n
, __c
);
602 __r
->_M_set_length(__n
);
603 return __r
->_M_refdata();
606 template<typename _CharT
, typename _Traits
, typename _Alloc
>
608 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
609 _M_swap(__rc_string_base
& __rcs
)
613 if (__rcs
._M_is_leaked())
614 __rcs
._M_set_sharable();
616 _CharT
* __tmp
= _M_data();
617 _M_data(__rcs
._M_data());
618 __rcs
._M_data(__tmp
);
620 // _GLIBCXX_RESOLVE_LIB_DEFECTS
621 // 431. Swapping containers with unequal allocators.
622 std::__alloc_swap
<allocator_type
>::_S_do_it(_M_get_allocator(),
623 __rcs
._M_get_allocator());
626 template<typename _CharT
, typename _Traits
, typename _Alloc
>
628 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
629 _M_assign(const __rc_string_base
& __rcs
)
631 if (_M_rep() != __rcs
._M_rep())
633 _CharT
* __tmp
= __rcs
._M_grab(_M_get_allocator());
639 template<typename _CharT
, typename _Traits
, typename _Alloc
>
641 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
642 _M_reserve(size_type __res
)
644 // Make sure we don't shrink below the current size.
645 if (__res
< _M_length())
648 if (__res
!= _M_capacity() || _M_is_shared())
650 _CharT
* __tmp
= _M_rep()->_M_clone(_M_get_allocator(),
651 __res
- _M_length());
657 template<typename _CharT
, typename _Traits
, typename _Alloc
>
659 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
660 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
663 const size_type __how_much
= _M_length() - __pos
- __len1
;
665 _Rep
* __r
= _Rep::_S_create(_M_length() + __len2
- __len1
,
666 _M_capacity(), _M_get_allocator());
669 this->_S_copy(__r
->_M_refdata(), _M_data(), __pos
);
671 this->_S_copy(__r
->_M_refdata() + __pos
, __s
, __len2
);
673 this->_S_copy(__r
->_M_refdata() + __pos
+ __len2
,
674 _M_data() + __pos
+ __len1
, __how_much
);
677 _M_data(__r
->_M_refdata());
680 template<typename _CharT
, typename _Traits
, typename _Alloc
>
682 __rc_string_base
<_CharT
, _Traits
, _Alloc
>::
683 _M_erase(size_type __pos
, size_type __n
)
685 const size_type __new_size
= _M_length() - __n
;
686 const size_type __how_much
= _M_length() - __pos
- __n
;
691 _Rep
* __r
= _Rep::_S_create(__new_size
, _M_capacity(),
695 this->_S_copy(__r
->_M_refdata(), _M_data(), __pos
);
697 this->_S_copy(__r
->_M_refdata() + __pos
,
698 _M_data() + __pos
+ __n
, __how_much
);
701 _M_data(__r
->_M_refdata());
703 else if (__how_much
&& __n
)
706 this->_S_move(_M_data() + __pos
,
707 _M_data() + __pos
+ __n
, __how_much
);
710 _M_rep()->_M_set_length(__new_size
);
715 __rc_string_base
<char, std::char_traits
<char>,
716 std::allocator
<char> >::
717 _M_compare(const __rc_string_base
& __rcs
) const
719 if (_M_rep() == __rcs
._M_rep())
726 __rc_string_base
<wchar_t, std::char_traits
<wchar_t>,
727 std::allocator
<wchar_t> >::
728 _M_compare(const __rc_string_base
& __rcs
) const
730 if (_M_rep() == __rcs
._M_rep())
735 _GLIBCXX_END_NAMESPACE_VERSION
738 #endif /* _RC_STRING_BASE_H */