Bug Summary

File:usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_algobase.h
Warning:line 67, column 29
Use of memory after it is freed

Annotated Source Code

[?] Use j/k keys for keyboard navigation

/usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_algobase.h

1// Core algorithmic facilities -*- C++ -*-
2
3// Copyright (C) 2001-2018 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// 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.
19
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/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51/** @file bits/stl_algobase.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{algorithm}
54 */
55
56#ifndef _STL_ALGOBASE_H1
57#define _STL_ALGOBASE_H1 1
58
59#include <bits/c++config.h>
60#include <bits/functexcept.h>
61#include <bits/cpp_type_traits.h>
62#include <ext/type_traits.h>
63#include <ext/numeric_traits.h>
64#include <bits/stl_pair.h>
65#include <bits/stl_iterator_base_types.h>
66#include <bits/stl_iterator_base_funcs.h>
67#include <bits/stl_iterator.h>
68#include <bits/concept_check.h>
69#include <debug/debug.h>
70#include <bits/move.h> // For std::swap
71#include <bits/predefined_ops.h>
72
73namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
74{
75_GLIBCXX_BEGIN_NAMESPACE_VERSION
76
77#if __cplusplus201103L < 201103L
78 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
79 // nutshell, we are partially implementing the resolution of DR 187,
80 // when it's safe, i.e., the value_types are equal.
81 template<bool _BoolType>
82 struct __iter_swap
83 {
84 template<typename _ForwardIterator1, typename _ForwardIterator2>
85 static void
86 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
87 {
88 typedef typename iterator_traits<_ForwardIterator1>::value_type
89 _ValueType1;
90 _ValueType1 __tmp = *__a;
91 *__a = *__b;
92 *__b = __tmp;
93 }
94 };
95
96 template<>
97 struct __iter_swap<true>
98 {
99 template<typename _ForwardIterator1, typename _ForwardIterator2>
100 static void
101 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
102 {
103 swap(*__a, *__b);
104 }
105 };
106#endif
107
108 /**
109 * @brief Swaps the contents of two iterators.
110 * @ingroup mutating_algorithms
111 * @param __a An iterator.
112 * @param __b Another iterator.
113 * @return Nothing.
114 *
115 * This function swaps the values pointed to by two iterators, not the
116 * iterators themselves.
117 */
118 template<typename _ForwardIterator1, typename _ForwardIterator2>
119 inline void
120 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
121 {
122 // concept requirements
123 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
124 _ForwardIterator1>)
125 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
126 _ForwardIterator2>)
127
128#if __cplusplus201103L < 201103L
129 typedef typename iterator_traits<_ForwardIterator1>::value_type
130 _ValueType1;
131 typedef typename iterator_traits<_ForwardIterator2>::value_type
132 _ValueType2;
133
134 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
135 _ValueType2>)
136 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
137 _ValueType1>)
138
139 typedef typename iterator_traits<_ForwardIterator1>::reference
140 _ReferenceType1;
141 typedef typename iterator_traits<_ForwardIterator2>::reference
142 _ReferenceType2;
143 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
144 && __are_same<_ValueType1&, _ReferenceType1>::__value
145 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
146 iter_swap(__a, __b);
147#else
148 swap(*__a, *__b);
149#endif
150 }
151
152 /**
153 * @brief Swap the elements of two sequences.
154 * @ingroup mutating_algorithms
155 * @param __first1 A forward iterator.
156 * @param __last1 A forward iterator.
157 * @param __first2 A forward iterator.
158 * @return An iterator equal to @p first2+(last1-first1).
159 *
160 * Swaps each element in the range @p [first1,last1) with the
161 * corresponding element in the range @p [first2,(last1-first1)).
162 * The ranges must not overlap.
163 */
164 template<typename _ForwardIterator1, typename _ForwardIterator2>
165 _ForwardIterator2
166 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
167 _ForwardIterator2 __first2)
168 {
169 // concept requirements
170 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
171 _ForwardIterator1>)
172 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
173 _ForwardIterator2>)
174 __glibcxx_requires_valid_range(__first1, __last1);
175
176 for (; __first1 != __last1; ++__first1, (void)++__first2)
177 std::iter_swap(__first1, __first2);
178 return __first2;
179 }
180
181 /**
182 * @brief This does what you think it does.
183 * @ingroup sorting_algorithms
184 * @param __a A thing of arbitrary type.
185 * @param __b Another thing of arbitrary type.
186 * @return The lesser of the parameters.
187 *
188 * This is the simple classic generic implementation. It will work on
189 * temporary expressions, since they are only evaluated once, unlike a
190 * preprocessor macro.
191 */
192 template<typename _Tp>
193 _GLIBCXX14_CONSTEXPR
194 inline const _Tp&
195 min(const _Tp& __a, const _Tp& __b)
196 {
197 // concept requirements
198 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
199 //return __b < __a ? __b : __a;
200 if (__b < __a)
201 return __b;
202 return __a;
203 }
204
205 /**
206 * @brief This does what you think it does.
207 * @ingroup sorting_algorithms
208 * @param __a A thing of arbitrary type.
209 * @param __b Another thing of arbitrary type.
210 * @return The greater of the parameters.
211 *
212 * This is the simple classic generic implementation. It will work on
213 * temporary expressions, since they are only evaluated once, unlike a
214 * preprocessor macro.
215 */
216 template<typename _Tp>
217 _GLIBCXX14_CONSTEXPR
218 inline const _Tp&
219 max(const _Tp& __a, const _Tp& __b)
220 {
221 // concept requirements
222 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
223 //return __a < __b ? __b : __a;
224 if (__a < __b)
225 return __b;
226 return __a;
227 }
228
229 /**
230 * @brief This does what you think it does.
231 * @ingroup sorting_algorithms
232 * @param __a A thing of arbitrary type.
233 * @param __b Another thing of arbitrary type.
234 * @param __comp A @link comparison_functors comparison functor@endlink.
235 * @return The lesser of the parameters.
236 *
237 * This will work on temporary expressions, since they are only evaluated
238 * once, unlike a preprocessor macro.
239 */
240 template<typename _Tp, typename _Compare>
241 _GLIBCXX14_CONSTEXPR
242 inline const _Tp&
243 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
244 {
245 //return __comp(__b, __a) ? __b : __a;
246 if (__comp(__b, __a))
247 return __b;
248 return __a;
249 }
250
251 /**
252 * @brief This does what you think it does.
253 * @ingroup sorting_algorithms
254 * @param __a A thing of arbitrary type.
255 * @param __b Another thing of arbitrary type.
256 * @param __comp A @link comparison_functors comparison functor@endlink.
257 * @return The greater of the parameters.
258 *
259 * This will work on temporary expressions, since they are only evaluated
260 * once, unlike a preprocessor macro.
261 */
262 template<typename _Tp, typename _Compare>
263 _GLIBCXX14_CONSTEXPR
264 inline const _Tp&
265 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
266 {
267 //return __comp(__a, __b) ? __b : __a;
268 if (__comp(__a, __b))
269 return __b;
270 return __a;
271 }
272
273 // Fallback implementation of the function in bits/stl_iterator.h used to
274 // remove the __normal_iterator wrapper. See copy, fill, ...
275 template<typename _Iterator>
276 inline _Iterator
277 __niter_base(_Iterator __it)
278 { return __it; }
279
280 // All of these auxiliary structs serve two purposes. (1) Replace
281 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
282 // because the input and output ranges are permitted to overlap.)
283 // (2) If we're using random access iterators, then write the loop as
284 // a for loop with an explicit count.
285
286 template<bool, bool, typename>
287 struct __copy_move
288 {
289 template<typename _II, typename _OI>
290 static _OI
291 __copy_m(_II __first, _II __last, _OI __result)
292 {
293 for (; __first != __last; ++__result, (void)++__first)
294 *__result = *__first;
295 return __result;
296 }
297 };
298
299#if __cplusplus201103L >= 201103L
300 template<typename _Category>
301 struct __copy_move<true, false, _Category>
302 {
303 template<typename _II, typename _OI>
304 static _OI
305 __copy_m(_II __first, _II __last, _OI __result)
306 {
307 for (; __first != __last; ++__result, (void)++__first)
308 *__result = std::move(*__first);
309 return __result;
310 }
311 };
312#endif
313
314 template<>
315 struct __copy_move<false, false, random_access_iterator_tag>
316 {
317 template<typename _II, typename _OI>
318 static _OI
319 __copy_m(_II __first, _II __last, _OI __result)
320 {
321 typedef typename iterator_traits<_II>::difference_type _Distance;
322 for(_Distance __n = __last - __first; __n > 0; --__n)
4
Assuming '__n' is > 0
5
Loop condition is true. Entering loop body
6
Assuming '__n' is > 0
7
Loop condition is true. Entering loop body
26
Assuming '__n' is > 0
27
Loop condition is true. Entering loop body
323 {
324 *__result = *__first;
8
Calling defaulted copy assignment operator for 'shared_ptr'
25
Returning; memory was released
28
Calling defaulted copy assignment operator for 'shared_ptr'
325 ++__first;
326 ++__result;
327 }
328 return __result;
329 }
330 };
331
332#if __cplusplus201103L >= 201103L
333 template<>
334 struct __copy_move<true, false, random_access_iterator_tag>
335 {
336 template<typename _II, typename _OI>
337 static _OI
338 __copy_m(_II __first, _II __last, _OI __result)
339 {
340 typedef typename iterator_traits<_II>::difference_type _Distance;
341 for(_Distance __n = __last - __first; __n > 0; --__n)
342 {
343 *__result = std::move(*__first);
344 ++__first;
345 ++__result;
346 }
347 return __result;
348 }
349 };
350#endif
351
352 template<bool _IsMove>
353 struct __copy_move<_IsMove, true, random_access_iterator_tag>
354 {
355 template<typename _Tp>
356 static _Tp*
357 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
358 {
359#if __cplusplus201103L >= 201103L
360 using __assignable = conditional<_IsMove,
361 is_move_assignable<_Tp>,
362 is_copy_assignable<_Tp>>;
363 // trivial types can have deleted assignment
364 static_assert( __assignable::type::value, "type is not assignable" );
365#endif
366 const ptrdiff_t _Num = __last - __first;
367 if (_Num)
368 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
369 return __result + _Num;
370 }
371 };
372
373 template<bool _IsMove, typename _II, typename _OI>
374 inline _OI
375 __copy_move_a(_II __first, _II __last, _OI __result)
376 {
377 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
378 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
379 typedef typename iterator_traits<_II>::iterator_category _Category;
380 const bool __simple = (__is_trivial(_ValueTypeI)
381 && __is_pointer<_II>::__value
382 && __is_pointer<_OI>::__value
383 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
384
385 return std::__copy_move<_IsMove, __simple,
3
Calling '__copy_move::__copy_m'
386 _Category>::__copy_m(__first, __last, __result);
387 }
388
389 // Helpers for streambuf iterators (either istream or ostream).
390 // NB: avoid including <iosfwd>, relatively large.
391 template<typename _CharT>
392 struct char_traits;
393
394 template<typename _CharT, typename _Traits>
395 class istreambuf_iterator;
396
397 template<typename _CharT, typename _Traits>
398 class ostreambuf_iterator;
399
400 template<bool _IsMove, typename _CharT>
401 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
402 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
403 __copy_move_a2(_CharT*, _CharT*,
404 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
405
406 template<bool _IsMove, typename _CharT>
407 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
408 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
409 __copy_move_a2(const _CharT*, const _CharT*,
410 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
411
412 template<bool _IsMove, typename _CharT>
413 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
414 _CharT*>::__type
415 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
416 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
417
418 template<bool _IsMove, typename _II, typename _OI>
419 inline _OI
420 __copy_move_a2(_II __first, _II __last, _OI __result)
421 {
422 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
2
Calling '__copy_move_a'
423 std::__niter_base(__last),
424 std::__niter_base(__result)));
425 }
426
427 /**
428 * @brief Copies the range [first,last) into result.
429 * @ingroup mutating_algorithms
430 * @param __first An input iterator.
431 * @param __last An input iterator.
432 * @param __result An output iterator.
433 * @return result + (first - last)
434 *
435 * This inline function will boil down to a call to @c memmove whenever
436 * possible. Failing that, if random access iterators are passed, then the
437 * loop count will be known (and therefore a candidate for compiler
438 * optimizations such as unrolling). Result may not be contained within
439 * [first,last); the copy_backward function should be used instead.
440 *
441 * Note that the end of the output range is permitted to be contained
442 * within [first,last).
443 */
444 template<typename _II, typename _OI>
445 inline _OI
446 copy(_II __first, _II __last, _OI __result)
447 {
448 // concept requirements
449 __glibcxx_function_requires(_InputIteratorConcept<_II>)
450 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
451 typename iterator_traits<_II>::value_type>)
452 __glibcxx_requires_valid_range(__first, __last);
453
454 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
1
Calling '__copy_move_a2'
455 (std::__miter_base(__first), std::__miter_base(__last),
456 __result));
457 }
458
459#if __cplusplus201103L >= 201103L
460 /**
461 * @brief Moves the range [first,last) into result.
462 * @ingroup mutating_algorithms
463 * @param __first An input iterator.
464 * @param __last An input iterator.
465 * @param __result An output iterator.
466 * @return result + (first - last)
467 *
468 * This inline function will boil down to a call to @c memmove whenever
469 * possible. Failing that, if random access iterators are passed, then the
470 * loop count will be known (and therefore a candidate for compiler
471 * optimizations such as unrolling). Result may not be contained within
472 * [first,last); the move_backward function should be used instead.
473 *
474 * Note that the end of the output range is permitted to be contained
475 * within [first,last).
476 */
477 template<typename _II, typename _OI>
478 inline _OI
479 move(_II __first, _II __last, _OI __result)
480 {
481 // concept requirements
482 __glibcxx_function_requires(_InputIteratorConcept<_II>)
483 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
484 typename iterator_traits<_II>::value_type>)
485 __glibcxx_requires_valid_range(__first, __last);
486
487 return std::__copy_move_a2<true>(std::__miter_base(__first),
488 std::__miter_base(__last), __result);
489 }
490
491#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp)std::move(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
492#else
493#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp)std::move(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
494#endif
495
496 template<bool, bool, typename>
497 struct __copy_move_backward
498 {
499 template<typename _BI1, typename _BI2>
500 static _BI2
501 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
502 {
503 while (__first != __last)
504 *--__result = *--__last;
505 return __result;
506 }
507 };
508
509#if __cplusplus201103L >= 201103L
510 template<typename _Category>
511 struct __copy_move_backward<true, false, _Category>
512 {
513 template<typename _BI1, typename _BI2>
514 static _BI2
515 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
516 {
517 while (__first != __last)
518 *--__result = std::move(*--__last);
519 return __result;
520 }
521 };
522#endif
523
524 template<>
525 struct __copy_move_backward<false, false, random_access_iterator_tag>
526 {
527 template<typename _BI1, typename _BI2>
528 static _BI2
529 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
530 {
531 typename iterator_traits<_BI1>::difference_type __n;
532 for (__n = __last - __first; __n > 0; --__n)
533 *--__result = *--__last;
534 return __result;
535 }
536 };
537
538#if __cplusplus201103L >= 201103L
539 template<>
540 struct __copy_move_backward<true, false, random_access_iterator_tag>
541 {
542 template<typename _BI1, typename _BI2>
543 static _BI2
544 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
545 {
546 typename iterator_traits<_BI1>::difference_type __n;
547 for (__n = __last - __first; __n > 0; --__n)
548 *--__result = std::move(*--__last);
549 return __result;
550 }
551 };
552#endif
553
554 template<bool _IsMove>
555 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
556 {
557 template<typename _Tp>
558 static _Tp*
559 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
560 {
561#if __cplusplus201103L >= 201103L
562 using __assignable = conditional<_IsMove,
563 is_move_assignable<_Tp>,
564 is_copy_assignable<_Tp>>;
565 // trivial types can have deleted assignment
566 static_assert( __assignable::type::value, "type is not assignable" );
567#endif
568 const ptrdiff_t _Num = __last - __first;
569 if (_Num)
570 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
571 return __result - _Num;
572 }
573 };
574
575 template<bool _IsMove, typename _BI1, typename _BI2>
576 inline _BI2
577 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
578 {
579 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
580 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
581 typedef typename iterator_traits<_BI1>::iterator_category _Category;
582 const bool __simple = (__is_trivial(_ValueType1)
583 && __is_pointer<_BI1>::__value
584 && __is_pointer<_BI2>::__value
585 && __are_same<_ValueType1, _ValueType2>::__value);
586
587 return std::__copy_move_backward<_IsMove, __simple,
588 _Category>::__copy_move_b(__first,
589 __last,
590 __result);
591 }
592
593 template<bool _IsMove, typename _BI1, typename _BI2>
594 inline _BI2
595 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
596 {
597 return _BI2(std::__copy_move_backward_a<_IsMove>
598 (std::__niter_base(__first), std::__niter_base(__last),
599 std::__niter_base(__result)));
600 }
601
602 /**
603 * @brief Copies the range [first,last) into result.
604 * @ingroup mutating_algorithms
605 * @param __first A bidirectional iterator.
606 * @param __last A bidirectional iterator.
607 * @param __result A bidirectional iterator.
608 * @return result - (first - last)
609 *
610 * The function has the same effect as copy, but starts at the end of the
611 * range and works its way to the start, returning the start of the result.
612 * This inline function will boil down to a call to @c memmove whenever
613 * possible. Failing that, if random access iterators are passed, then the
614 * loop count will be known (and therefore a candidate for compiler
615 * optimizations such as unrolling).
616 *
617 * Result may not be in the range (first,last]. Use copy instead. Note
618 * that the start of the output range may overlap [first,last).
619 */
620 template<typename _BI1, typename _BI2>
621 inline _BI2
622 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
623 {
624 // concept requirements
625 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
626 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
627 __glibcxx_function_requires(_ConvertibleConcept<
628 typename iterator_traits<_BI1>::value_type,
629 typename iterator_traits<_BI2>::value_type>)
630 __glibcxx_requires_valid_range(__first, __last);
631
632 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
633 (std::__miter_base(__first), std::__miter_base(__last),
634 __result));
635 }
636
637#if __cplusplus201103L >= 201103L
638 /**
639 * @brief Moves the range [first,last) into result.
640 * @ingroup mutating_algorithms
641 * @param __first A bidirectional iterator.
642 * @param __last A bidirectional iterator.
643 * @param __result A bidirectional iterator.
644 * @return result - (first - last)
645 *
646 * The function has the same effect as move, but starts at the end of the
647 * range and works its way to the start, returning the start of the result.
648 * This inline function will boil down to a call to @c memmove whenever
649 * possible. Failing that, if random access iterators are passed, then the
650 * loop count will be known (and therefore a candidate for compiler
651 * optimizations such as unrolling).
652 *
653 * Result may not be in the range (first,last]. Use move instead. Note
654 * that the start of the output range may overlap [first,last).
655 */
656 template<typename _BI1, typename _BI2>
657 inline _BI2
658 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
659 {
660 // concept requirements
661 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
662 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
663 __glibcxx_function_requires(_ConvertibleConcept<
664 typename iterator_traits<_BI1>::value_type,
665 typename iterator_traits<_BI2>::value_type>)
666 __glibcxx_requires_valid_range(__first, __last);
667
668 return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
669 std::__miter_base(__last),
670 __result);
671 }
672
673#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp)std::move_backward(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
674#else
675#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp)std::move_backward(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
676#endif
677
678 template<typename _ForwardIterator, typename _Tp>
679 inline typename
680 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
681 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
682 const _Tp& __value)
683 {
684 for (; __first != __last; ++__first)
685 *__first = __value;
686 }
687
688 template<typename _ForwardIterator, typename _Tp>
689 inline typename
690 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
691 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
692 const _Tp& __value)
693 {
694 const _Tp __tmp = __value;
695 for (; __first != __last; ++__first)
696 *__first = __tmp;
697 }
698
699 // Specialization: for char types we can use memset.
700 template<typename _Tp>
701 inline typename
702 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
703 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
704 {
705 const _Tp __tmp = __c;
706 if (const size_t __len = __last - __first)
707 __builtin_memset(__first, static_cast<unsigned char>(__tmp), __len);
708 }
709
710 /**
711 * @brief Fills the range [first,last) with copies of value.
712 * @ingroup mutating_algorithms
713 * @param __first A forward iterator.
714 * @param __last A forward iterator.
715 * @param __value A reference-to-const of arbitrary type.
716 * @return Nothing.
717 *
718 * This function fills a range with copies of the same value. For char
719 * types filling contiguous areas of memory, this becomes an inline call
720 * to @c memset or @c wmemset.
721 */
722 template<typename _ForwardIterator, typename _Tp>
723 inline void
724 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
725 {
726 // concept requirements
727 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
728 _ForwardIterator>)
729 __glibcxx_requires_valid_range(__first, __last);
730
731 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
732 __value);
733 }
734
735 template<typename _OutputIterator, typename _Size, typename _Tp>
736 inline typename
737 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
738 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
739 {
740 for (__decltype(__n + 0) __niter = __n;
741 __niter > 0; --__niter, (void) ++__first)
742 *__first = __value;
743 return __first;
744 }
745
746 template<typename _OutputIterator, typename _Size, typename _Tp>
747 inline typename
748 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
749 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
750 {
751 const _Tp __tmp = __value;
752 for (__decltype(__n + 0) __niter = __n;
753 __niter > 0; --__niter, (void) ++__first)
754 *__first = __tmp;
755 return __first;
756 }
757
758 template<typename _Size, typename _Tp>
759 inline typename
760 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
761 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
762 {
763 std::__fill_a(__first, __first + __n, __c);
764 return __first + __n;
765 }
766
767 /**
768 * @brief Fills the range [first,first+n) with copies of value.
769 * @ingroup mutating_algorithms
770 * @param __first An output iterator.
771 * @param __n The count of copies to perform.
772 * @param __value A reference-to-const of arbitrary type.
773 * @return The iterator at first+n.
774 *
775 * This function fills a range with copies of the same value. For char
776 * types filling contiguous areas of memory, this becomes an inline call
777 * to @c memset or @ wmemset.
778 *
779 * _GLIBCXX_RESOLVE_LIB_DEFECTS
780 * DR 865. More algorithms that throw away information
781 */
782 template<typename _OI, typename _Size, typename _Tp>
783 inline _OI
784 fill_n(_OI __first, _Size __n, const _Tp& __value)
785 {
786 // concept requirements
787 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
788
789 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
790 }
791
792 template<bool _BoolType>
793 struct __equal
794 {
795 template<typename _II1, typename _II2>
796 static bool
797 equal(_II1 __first1, _II1 __last1, _II2 __first2)
798 {
799 for (; __first1 != __last1; ++__first1, (void) ++__first2)
800 if (!(*__first1 == *__first2))
801 return false;
802 return true;
803 }
804 };
805
806 template<>
807 struct __equal<true>
808 {
809 template<typename _Tp>
810 static bool
811 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
812 {
813 if (const size_t __len = (__last1 - __first1))
814 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) * __len);
815 return true;
816 }
817 };
818
819 template<typename _II1, typename _II2>
820 inline bool
821 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
822 {
823 typedef typename iterator_traits<_II1>::value_type _ValueType1;
824 typedef typename iterator_traits<_II2>::value_type _ValueType2;
825 const bool __simple = ((__is_integer<_ValueType1>::__value
826 || __is_pointer<_ValueType1>::__value)
827 && __is_pointer<_II1>::__value
828 && __is_pointer<_II2>::__value
829 && __are_same<_ValueType1, _ValueType2>::__value);
830
831 return std::__equal<__simple>::equal(__first1, __last1, __first2);
832 }
833
834 template<typename, typename>
835 struct __lc_rai
836 {
837 template<typename _II1, typename _II2>
838 static _II1
839 __newlast1(_II1, _II1 __last1, _II2, _II2)
840 { return __last1; }
841
842 template<typename _II>
843 static bool
844 __cnd2(_II __first, _II __last)
845 { return __first != __last; }
846 };
847
848 template<>
849 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
850 {
851 template<typename _RAI1, typename _RAI2>
852 static _RAI1
853 __newlast1(_RAI1 __first1, _RAI1 __last1,
854 _RAI2 __first2, _RAI2 __last2)
855 {
856 const typename iterator_traits<_RAI1>::difference_type
857 __diff1 = __last1 - __first1;
858 const typename iterator_traits<_RAI2>::difference_type
859 __diff2 = __last2 - __first2;
860 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
861 }
862
863 template<typename _RAI>
864 static bool
865 __cnd2(_RAI, _RAI)
866 { return true; }
867 };
868
869 template<typename _II1, typename _II2, typename _Compare>
870 bool
871 __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
872 _II2 __first2, _II2 __last2,
873 _Compare __comp)
874 {
875 typedef typename iterator_traits<_II1>::iterator_category _Category1;
876 typedef typename iterator_traits<_II2>::iterator_category _Category2;
877 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
878
879 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
880 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
881 ++__first1, (void)++__first2)
882 {
883 if (__comp(__first1, __first2))
884 return true;
885 if (__comp(__first2, __first1))
886 return false;
887 }
888 return __first1 == __last1 && __first2 != __last2;
889 }
890
891 template<bool _BoolType>
892 struct __lexicographical_compare
893 {
894 template<typename _II1, typename _II2>
895 static bool __lc(_II1, _II1, _II2, _II2);
896 };
897
898 template<bool _BoolType>
899 template<typename _II1, typename _II2>
900 bool
901 __lexicographical_compare<_BoolType>::
902 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
903 {
904 return std::__lexicographical_compare_impl(__first1, __last1,
905 __first2, __last2,
906 __gnu_cxx::__ops::__iter_less_iter());
907 }
908
909 template<>
910 struct __lexicographical_compare<true>
911 {
912 template<typename _Tp, typename _Up>
913 static bool
914 __lc(const _Tp* __first1, const _Tp* __last1,
915 const _Up* __first2, const _Up* __last2)
916 {
917 const size_t __len1 = __last1 - __first1;
918 const size_t __len2 = __last2 - __first2;
919 if (const size_t __len = std::min(__len1, __len2))
920 if (int __result = __builtin_memcmp(__first1, __first2, __len))
921 return __result < 0;
922 return __len1 < __len2;
923 }
924 };
925
926 template<typename _II1, typename _II2>
927 inline bool
928 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
929 _II2 __first2, _II2 __last2)
930 {
931 typedef typename iterator_traits<_II1>::value_type _ValueType1;
932 typedef typename iterator_traits<_II2>::value_type _ValueType2;
933 const bool __simple =
934 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
935 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
936 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
937 && __is_pointer<_II1>::__value
938 && __is_pointer<_II2>::__value);
939
940 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
941 __first2, __last2);
942 }
943
944 template<typename _ForwardIterator, typename _Tp, typename _Compare>
945 _ForwardIterator
946 __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
947 const _Tp& __val, _Compare __comp)
948 {
949 typedef typename iterator_traits<_ForwardIterator>::difference_type
950 _DistanceType;
951
952 _DistanceType __len = std::distance(__first, __last);
953
954 while (__len > 0)
955 {
956 _DistanceType __half = __len >> 1;
957 _ForwardIterator __middle = __first;
958 std::advance(__middle, __half);
959 if (__comp(__middle, __val))
960 {
961 __first = __middle;
962 ++__first;
963 __len = __len - __half - 1;
964 }
965 else
966 __len = __half;
967 }
968 return __first;
969 }
970
971 /**
972 * @brief Finds the first position in which @a val could be inserted
973 * without changing the ordering.
974 * @param __first An iterator.
975 * @param __last Another iterator.
976 * @param __val The search term.
977 * @return An iterator pointing to the first element <em>not less
978 * than</em> @a val, or end() if every element is less than
979 * @a val.
980 * @ingroup binary_search_algorithms
981 */
982 template<typename _ForwardIterator, typename _Tp>
983 inline _ForwardIterator
984 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
985 const _Tp& __val)
986 {
987 // concept requirements
988 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
989 __glibcxx_function_requires(_LessThanOpConcept<
990 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
991 __glibcxx_requires_partitioned_lower(__first, __last, __val);
992
993 return std::__lower_bound(__first, __last, __val,
994 __gnu_cxx::__ops::__iter_less_val());
995 }
996
997 /// This is a helper function for the sort routines and for random.tcc.
998 // Precondition: __n > 0.
999 inline _GLIBCXX_CONSTEXPRconstexpr int
1000 __lg(int __n)
1001 { return sizeof(int) * __CHAR_BIT__8 - 1 - __builtin_clz(__n); }
1002
1003 inline _GLIBCXX_CONSTEXPRconstexpr unsigned
1004 __lg(unsigned __n)
1005 { return sizeof(int) * __CHAR_BIT__8 - 1 - __builtin_clz(__n); }
1006
1007 inline _GLIBCXX_CONSTEXPRconstexpr long
1008 __lg(long __n)
1009 { return sizeof(long) * __CHAR_BIT__8 - 1 - __builtin_clzl(__n); }
1010
1011 inline _GLIBCXX_CONSTEXPRconstexpr unsigned long
1012 __lg(unsigned long __n)
1013 { return sizeof(long) * __CHAR_BIT__8 - 1 - __builtin_clzl(__n); }
1014
1015 inline _GLIBCXX_CONSTEXPRconstexpr long long
1016 __lg(long long __n)
1017 { return sizeof(long long) * __CHAR_BIT__8 - 1 - __builtin_clzll(__n); }
1018
1019 inline _GLIBCXX_CONSTEXPRconstexpr unsigned long long
1020 __lg(unsigned long long __n)
1021 { return sizeof(long long) * __CHAR_BIT__8 - 1 - __builtin_clzll(__n); }
1022
1023_GLIBCXX_BEGIN_NAMESPACE_ALGO
1024
1025 /**
1026 * @brief Tests a range for element-wise equality.
1027 * @ingroup non_mutating_algorithms
1028 * @param __first1 An input iterator.
1029 * @param __last1 An input iterator.
1030 * @param __first2 An input iterator.
1031 * @return A boolean true or false.
1032 *
1033 * This compares the elements of two ranges using @c == and returns true or
1034 * false depending on whether all of the corresponding elements of the
1035 * ranges are equal.
1036 */
1037 template<typename _II1, typename _II2>
1038 inline bool
1039 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1040 {
1041 // concept requirements
1042 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1043 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1044 __glibcxx_function_requires(_EqualOpConcept<
1045 typename iterator_traits<_II1>::value_type,
1046 typename iterator_traits<_II2>::value_type>)
1047 __glibcxx_requires_valid_range(__first1, __last1);
1048
1049 return std::__equal_aux(std::__niter_base(__first1),
1050 std::__niter_base(__last1),
1051 std::__niter_base(__first2));
1052 }
1053
1054 /**
1055 * @brief Tests a range for element-wise equality.
1056 * @ingroup non_mutating_algorithms
1057 * @param __first1 An input iterator.
1058 * @param __last1 An input iterator.
1059 * @param __first2 An input iterator.
1060 * @param __binary_pred A binary predicate @link functors
1061 * functor@endlink.
1062 * @return A boolean true or false.
1063 *
1064 * This compares the elements of two ranges using the binary_pred
1065 * parameter, and returns true or
1066 * false depending on whether all of the corresponding elements of the
1067 * ranges are equal.
1068 */
1069 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1070 inline bool
1071 equal(_IIter1 __first1, _IIter1 __last1,
1072 _IIter2 __first2, _BinaryPredicate __binary_pred)
1073 {
1074 // concept requirements
1075 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1076 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1077 __glibcxx_requires_valid_range(__first1, __last1);
1078
1079 for (; __first1 != __last1; ++__first1, (void)++__first2)
1080 if (!bool(__binary_pred(*__first1, *__first2)))
1081 return false;
1082 return true;
1083 }
1084
1085#if __cplusplus201103L >= 201103L
1086 // 4-iterator version of std::equal<It1, It2> for use in C++11.
1087 template<typename _II1, typename _II2>
1088 inline bool
1089 __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1090 {
1091 using _RATag = random_access_iterator_tag;
1092 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1093 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1094 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1095 if (_RAIters())
1096 {
1097 auto __d1 = std::distance(__first1, __last1);
1098 auto __d2 = std::distance(__first2, __last2);
1099 if (__d1 != __d2)
1100 return false;
1101 return _GLIBCXX_STD_Astd::equal(__first1, __last1, __first2);
1102 }
1103
1104 for (; __first1 != __last1 && __first2 != __last2;
1105 ++__first1, (void)++__first2)
1106 if (!(*__first1 == *__first2))
1107 return false;
1108 return __first1 == __last1 && __first2 == __last2;
1109 }
1110
1111 // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
1112 template<typename _II1, typename _II2, typename _BinaryPredicate>
1113 inline bool
1114 __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2,
1115 _BinaryPredicate __binary_pred)
1116 {
1117 using _RATag = random_access_iterator_tag;
1118 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1119 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1120 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1121 if (_RAIters())
1122 {
1123 auto __d1 = std::distance(__first1, __last1);
1124 auto __d2 = std::distance(__first2, __last2);
1125 if (__d1 != __d2)
1126 return false;
1127 return _GLIBCXX_STD_Astd::equal(__first1, __last1, __first2,
1128 __binary_pred);
1129 }
1130
1131 for (; __first1 != __last1 && __first2 != __last2;
1132 ++__first1, (void)++__first2)
1133 if (!bool(__binary_pred(*__first1, *__first2)))
1134 return false;
1135 return __first1 == __last1 && __first2 == __last2;
1136 }
1137#endif // C++11
1138
1139#if __cplusplus201103L > 201103L
1140
1141#define __cpp_lib_robust_nonmodifying_seq_ops 201304
1142
1143 /**
1144 * @brief Tests a range for element-wise equality.
1145 * @ingroup non_mutating_algorithms
1146 * @param __first1 An input iterator.
1147 * @param __last1 An input iterator.
1148 * @param __first2 An input iterator.
1149 * @param __last2 An input iterator.
1150 * @return A boolean true or false.
1151 *
1152 * This compares the elements of two ranges using @c == and returns true or
1153 * false depending on whether all of the corresponding elements of the
1154 * ranges are equal.
1155 */
1156 template<typename _II1, typename _II2>
1157 inline bool
1158 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1159 {
1160 // concept requirements
1161 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1162 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1163 __glibcxx_function_requires(_EqualOpConcept<
1164 typename iterator_traits<_II1>::value_type,
1165 typename iterator_traits<_II2>::value_type>)
1166 __glibcxx_requires_valid_range(__first1, __last1);
1167 __glibcxx_requires_valid_range(__first2, __last2);
1168
1169 return _GLIBCXX_STD_Astd::__equal4(__first1, __last1, __first2, __last2);
1170 }
1171
1172 /**
1173 * @brief Tests a range for element-wise equality.
1174 * @ingroup non_mutating_algorithms
1175 * @param __first1 An input iterator.
1176 * @param __last1 An input iterator.
1177 * @param __first2 An input iterator.
1178 * @param __last2 An input iterator.
1179 * @param __binary_pred A binary predicate @link functors
1180 * functor@endlink.
1181 * @return A boolean true or false.
1182 *
1183 * This compares the elements of two ranges using the binary_pred
1184 * parameter, and returns true or
1185 * false depending on whether all of the corresponding elements of the
1186 * ranges are equal.
1187 */
1188 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1189 inline bool
1190 equal(_IIter1 __first1, _IIter1 __last1,
1191 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1192 {
1193 // concept requirements
1194 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1195 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1196 __glibcxx_requires_valid_range(__first1, __last1);
1197 __glibcxx_requires_valid_range(__first2, __last2);
1198
1199 return _GLIBCXX_STD_Astd::__equal4(__first1, __last1, __first2, __last2,
1200 __binary_pred);
1201 }
1202#endif // C++14
1203
1204 /**
1205 * @brief Performs @b dictionary comparison on ranges.
1206 * @ingroup sorting_algorithms
1207 * @param __first1 An input iterator.
1208 * @param __last1 An input iterator.
1209 * @param __first2 An input iterator.
1210 * @param __last2 An input iterator.
1211 * @return A boolean true or false.
1212 *
1213 * <em>Returns true if the sequence of elements defined by the range
1214 * [first1,last1) is lexicographically less than the sequence of elements
1215 * defined by the range [first2,last2). Returns false otherwise.</em>
1216 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1217 * then this is an inline call to @c memcmp.
1218 */
1219 template<typename _II1, typename _II2>
1220 inline bool
1221 lexicographical_compare(_II1 __first1, _II1 __last1,
1222 _II2 __first2, _II2 __last2)
1223 {
1224#ifdef _GLIBCXX_CONCEPT_CHECKS
1225 // concept requirements
1226 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1227 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1228#endif
1229 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1230 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1231 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1232 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1233 __glibcxx_requires_valid_range(__first1, __last1);
1234 __glibcxx_requires_valid_range(__first2, __last2);
1235
1236 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1237 std::__niter_base(__last1),
1238 std::__niter_base(__first2),
1239 std::__niter_base(__last2));
1240 }
1241
1242 /**
1243 * @brief Performs @b dictionary comparison on ranges.
1244 * @ingroup sorting_algorithms
1245 * @param __first1 An input iterator.
1246 * @param __last1 An input iterator.
1247 * @param __first2 An input iterator.
1248 * @param __last2 An input iterator.
1249 * @param __comp A @link comparison_functors comparison functor@endlink.
1250 * @return A boolean true or false.
1251 *
1252 * The same as the four-parameter @c lexicographical_compare, but uses the
1253 * comp parameter instead of @c <.
1254 */
1255 template<typename _II1, typename _II2, typename _Compare>
1256 inline bool
1257 lexicographical_compare(_II1 __first1, _II1 __last1,
1258 _II2 __first2, _II2 __last2, _Compare __comp)
1259 {
1260 // concept requirements
1261 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1262 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1263 __glibcxx_requires_valid_range(__first1, __last1);
1264 __glibcxx_requires_valid_range(__first2, __last2);
1265
1266 return std::__lexicographical_compare_impl
1267 (__first1, __last1, __first2, __last2,
1268 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1269 }
1270
1271 template<typename _InputIterator1, typename _InputIterator2,
1272 typename _BinaryPredicate>
1273 pair<_InputIterator1, _InputIterator2>
1274 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1275 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1276 {
1277 while (__first1 != __last1 && __binary_pred(__first1, __first2))
1278 {
1279 ++__first1;
1280 ++__first2;
1281 }
1282 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1283 }
1284
1285 /**
1286 * @brief Finds the places in ranges which don't match.
1287 * @ingroup non_mutating_algorithms
1288 * @param __first1 An input iterator.
1289 * @param __last1 An input iterator.
1290 * @param __first2 An input iterator.
1291 * @return A pair of iterators pointing to the first mismatch.
1292 *
1293 * This compares the elements of two ranges using @c == and returns a pair
1294 * of iterators. The first iterator points into the first range, the
1295 * second iterator points into the second range, and the elements pointed
1296 * to by the iterators are not equal.
1297 */
1298 template<typename _InputIterator1, typename _InputIterator2>
1299 inline pair<_InputIterator1, _InputIterator2>
1300 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1301 _InputIterator2 __first2)
1302 {
1303 // concept requirements
1304 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1305 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1306 __glibcxx_function_requires(_EqualOpConcept<
1307 typename iterator_traits<_InputIterator1>::value_type,
1308 typename iterator_traits<_InputIterator2>::value_type>)
1309 __glibcxx_requires_valid_range(__first1, __last1);
1310
1311 return _GLIBCXX_STD_Astd::__mismatch(__first1, __last1, __first2,
1312 __gnu_cxx::__ops::__iter_equal_to_iter());
1313 }
1314
1315 /**
1316 * @brief Finds the places in ranges which don't match.
1317 * @ingroup non_mutating_algorithms
1318 * @param __first1 An input iterator.
1319 * @param __last1 An input iterator.
1320 * @param __first2 An input iterator.
1321 * @param __binary_pred A binary predicate @link functors
1322 * functor@endlink.
1323 * @return A pair of iterators pointing to the first mismatch.
1324 *
1325 * This compares the elements of two ranges using the binary_pred
1326 * parameter, and returns a pair
1327 * of iterators. The first iterator points into the first range, the
1328 * second iterator points into the second range, and the elements pointed
1329 * to by the iterators are not equal.
1330 */
1331 template<typename _InputIterator1, typename _InputIterator2,
1332 typename _BinaryPredicate>
1333 inline pair<_InputIterator1, _InputIterator2>
1334 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1335 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1336 {
1337 // concept requirements
1338 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1339 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1340 __glibcxx_requires_valid_range(__first1, __last1);
1341
1342 return _GLIBCXX_STD_Astd::__mismatch(__first1, __last1, __first2,
1343 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1344 }
1345
1346#if __cplusplus201103L > 201103L
1347
1348 template<typename _InputIterator1, typename _InputIterator2,
1349 typename _BinaryPredicate>
1350 pair<_InputIterator1, _InputIterator2>
1351 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1352 _InputIterator2 __first2, _InputIterator2 __last2,
1353 _BinaryPredicate __binary_pred)
1354 {
1355 while (__first1 != __last1 && __first2 != __last2
1356 && __binary_pred(__first1, __first2))
1357 {
1358 ++__first1;
1359 ++__first2;
1360 }
1361 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1362 }
1363
1364 /**
1365 * @brief Finds the places in ranges which don't match.
1366 * @ingroup non_mutating_algorithms
1367 * @param __first1 An input iterator.
1368 * @param __last1 An input iterator.
1369 * @param __first2 An input iterator.
1370 * @param __last2 An input iterator.
1371 * @return A pair of iterators pointing to the first mismatch.
1372 *
1373 * This compares the elements of two ranges using @c == and returns a pair
1374 * of iterators. The first iterator points into the first range, the
1375 * second iterator points into the second range, and the elements pointed
1376 * to by the iterators are not equal.
1377 */
1378 template<typename _InputIterator1, typename _InputIterator2>
1379 inline pair<_InputIterator1, _InputIterator2>
1380 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1381 _InputIterator2 __first2, _InputIterator2 __last2)
1382 {
1383 // concept requirements
1384 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1385 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1386 __glibcxx_function_requires(_EqualOpConcept<
1387 typename iterator_traits<_InputIterator1>::value_type,
1388 typename iterator_traits<_InputIterator2>::value_type>)
1389 __glibcxx_requires_valid_range(__first1, __last1);
1390 __glibcxx_requires_valid_range(__first2, __last2);
1391
1392 return _GLIBCXX_STD_Astd::__mismatch(__first1, __last1, __first2, __last2,
1393 __gnu_cxx::__ops::__iter_equal_to_iter());
1394 }
1395
1396 /**
1397 * @brief Finds the places in ranges which don't match.
1398 * @ingroup non_mutating_algorithms
1399 * @param __first1 An input iterator.
1400 * @param __last1 An input iterator.
1401 * @param __first2 An input iterator.
1402 * @param __last2 An input iterator.
1403 * @param __binary_pred A binary predicate @link functors
1404 * functor@endlink.
1405 * @return A pair of iterators pointing to the first mismatch.
1406 *
1407 * This compares the elements of two ranges using the binary_pred
1408 * parameter, and returns a pair
1409 * of iterators. The first iterator points into the first range, the
1410 * second iterator points into the second range, and the elements pointed
1411 * to by the iterators are not equal.
1412 */
1413 template<typename _InputIterator1, typename _InputIterator2,
1414 typename _BinaryPredicate>
1415 inline pair<_InputIterator1, _InputIterator2>
1416 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1417 _InputIterator2 __first2, _InputIterator2 __last2,
1418 _BinaryPredicate __binary_pred)
1419 {
1420 // concept requirements
1421 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1422 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1423 __glibcxx_requires_valid_range(__first1, __last1);
1424 __glibcxx_requires_valid_range(__first2, __last2);
1425
1426 return _GLIBCXX_STD_Astd::__mismatch(__first1, __last1, __first2, __last2,
1427 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1428 }
1429#endif
1430
1431_GLIBCXX_END_NAMESPACE_ALGO
1432_GLIBCXX_END_NAMESPACE_VERSION
1433} // namespace std
1434
1435// NB: This file is included within many other C++ includes, as a way
1436// of getting the base algorithms. So, make sure that parallel bits
1437// come in too if requested.
1438#ifdef _GLIBCXX_PARALLEL
1439# include <parallel/algobase.h>
1440#endif
1441
1442#endif

/usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/shared_ptr.h

1// shared_ptr and weak_ptr implementation -*- C++ -*-
2
3// Copyright (C) 2007-2018 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// 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.
19
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/>.
24
25// GCC Note: Based on files from version 1.32.0 of the Boost library.
26
27// shared_count.hpp
28// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
29
30// shared_ptr.hpp
31// Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32// Copyright (C) 2001, 2002, 2003 Peter Dimov
33
34// weak_ptr.hpp
35// Copyright (C) 2001, 2002, 2003 Peter Dimov
36
37// enable_shared_from_this.hpp
38// Copyright (C) 2002 Peter Dimov
39
40// Distributed under the Boost Software License, Version 1.0. (See
41// accompanying file LICENSE_1_0.txt or copy at
42// http://www.boost.org/LICENSE_1_0.txt)
43
44/** @file
45 * This is an internal header file, included by other library headers.
46 * Do not attempt to use it directly. @headername{memory}
47 */
48
49#ifndef _SHARED_PTR_H1
50#define _SHARED_PTR_H1 1
51
52#include <bits/shared_ptr_base.h>
53
54namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
55{
56_GLIBCXX_BEGIN_NAMESPACE_VERSION
57
58 /**
59 * @addtogroup pointer_abstractions
60 * @{
61 */
62
63 /// 20.7.2.2.11 shared_ptr I/O
64 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
65 inline std::basic_ostream<_Ch, _Tr>&
66 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
67 const __shared_ptr<_Tp, _Lp>& __p)
68 {
69 __os << __p.get();
70 return __os;
71 }
72
73 template<typename _Del, typename _Tp, _Lock_policy _Lp>
74 inline _Del*
75 get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept
76 {
77#if __cpp_rtti199711
78 return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
79#else
80 return 0;
81#endif
82 }
83
84 /// 20.7.2.2.10 shared_ptr get_deleter
85 template<typename _Del, typename _Tp>
86 inline _Del*
87 get_deleter(const shared_ptr<_Tp>& __p) noexcept
88 {
89#if __cpp_rtti199711
90 return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
91#else
92 return 0;
93#endif
94 }
95
96 /**
97 * @brief A smart pointer with reference-counted copy semantics.
98 *
99 * The object pointed to is deleted when the last shared_ptr pointing to
100 * it is destroyed or reset.
101 */
102 template<typename _Tp>
103 class shared_ptr : public __shared_ptr<_Tp>
104 {
105 template<typename... _Args>
106 using _Constructible = typename enable_if<
107 is_constructible<__shared_ptr<_Tp>, _Args...>::value
108 >::type;
109
110 template<typename _Arg>
111 using _Assignable = typename enable_if<
112 is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr&
113 >::type;
114
115 public:
116
117 using element_type = typename __shared_ptr<_Tp>::element_type;
118
119#if __cplusplus201103L > 201402L
120# define __cpp_lib_shared_ptr_weak_type 201606
121 using weak_type = weak_ptr<_Tp>;
122#endif
123 /**
124 * @brief Construct an empty %shared_ptr.
125 * @post use_count()==0 && get()==0
126 */
127 constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { }
128
129 shared_ptr(const shared_ptr&) noexcept = default;
130
131 /**
132 * @brief Construct a %shared_ptr that owns the pointer @a __p.
133 * @param __p A pointer that is convertible to element_type*.
134 * @post use_count() == 1 && get() == __p
135 * @throw std::bad_alloc, in which case @c delete @a __p is called.
136 */
137 template<typename _Yp, typename = _Constructible<_Yp*>>
138 explicit
139 shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { }
140
141 /**
142 * @brief Construct a %shared_ptr that owns the pointer @a __p
143 * and the deleter @a __d.
144 * @param __p A pointer.
145 * @param __d A deleter.
146 * @post use_count() == 1 && get() == __p
147 * @throw std::bad_alloc, in which case @a __d(__p) is called.
148 *
149 * Requirements: _Deleter's copy constructor and destructor must
150 * not throw
151 *
152 * __shared_ptr will release __p by calling __d(__p)
153 */
154 template<typename _Yp, typename _Deleter,
155 typename = _Constructible<_Yp*, _Deleter>>
156 shared_ptr(_Yp* __p, _Deleter __d)
157 : __shared_ptr<_Tp>(__p, std::move(__d)) { }
158
159 /**
160 * @brief Construct a %shared_ptr that owns a null pointer
161 * and the deleter @a __d.
162 * @param __p A null pointer constant.
163 * @param __d A deleter.
164 * @post use_count() == 1 && get() == __p
165 * @throw std::bad_alloc, in which case @a __d(__p) is called.
166 *
167 * Requirements: _Deleter's copy constructor and destructor must
168 * not throw
169 *
170 * The last owner will call __d(__p)
171 */
172 template<typename _Deleter>
173 shared_ptr(nullptr_t __p, _Deleter __d)
174 : __shared_ptr<_Tp>(__p, std::move(__d)) { }
175
176 /**
177 * @brief Construct a %shared_ptr that owns the pointer @a __p
178 * and the deleter @a __d.
179 * @param __p A pointer.
180 * @param __d A deleter.
181 * @param __a An allocator.
182 * @post use_count() == 1 && get() == __p
183 * @throw std::bad_alloc, in which case @a __d(__p) is called.
184 *
185 * Requirements: _Deleter's copy constructor and destructor must
186 * not throw _Alloc's copy constructor and destructor must not
187 * throw.
188 *
189 * __shared_ptr will release __p by calling __d(__p)
190 */
191 template<typename _Yp, typename _Deleter, typename _Alloc,
192 typename = _Constructible<_Yp*, _Deleter, _Alloc>>
193 shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
194 : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
195
196 /**
197 * @brief Construct a %shared_ptr that owns a null pointer
198 * and the deleter @a __d.
199 * @param __p A null pointer constant.
200 * @param __d A deleter.
201 * @param __a An allocator.
202 * @post use_count() == 1 && get() == __p
203 * @throw std::bad_alloc, in which case @a __d(__p) is called.
204 *
205 * Requirements: _Deleter's copy constructor and destructor must
206 * not throw _Alloc's copy constructor and destructor must not
207 * throw.
208 *
209 * The last owner will call __d(__p)
210 */
211 template<typename _Deleter, typename _Alloc>
212 shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
213 : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
214
215 // Aliasing constructor
216
217 /**
218 * @brief Constructs a %shared_ptr instance that stores @a __p
219 * and shares ownership with @a __r.
220 * @param __r A %shared_ptr.
221 * @param __p A pointer that will remain valid while @a *__r is valid.
222 * @post get() == __p && use_count() == __r.use_count()
223 *
224 * This can be used to construct a @c shared_ptr to a sub-object
225 * of an object managed by an existing @c shared_ptr.
226 *
227 * @code
228 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
229 * shared_ptr<int> pi(pii, &pii->first);
230 * assert(pii.use_count() == 2);
231 * @endcode
232 */
233 template<typename _Yp>
234 shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept
235 : __shared_ptr<_Tp>(__r, __p) { }
236
237 /**
238 * @brief If @a __r is empty, constructs an empty %shared_ptr;
239 * otherwise construct a %shared_ptr that shares ownership
240 * with @a __r.
241 * @param __r A %shared_ptr.
242 * @post get() == __r.get() && use_count() == __r.use_count()
243 */
244 template<typename _Yp,
245 typename = _Constructible<const shared_ptr<_Yp>&>>
246 shared_ptr(const shared_ptr<_Yp>& __r) noexcept
247 : __shared_ptr<_Tp>(__r) { }
248
249 /**
250 * @brief Move-constructs a %shared_ptr instance from @a __r.
251 * @param __r A %shared_ptr rvalue.
252 * @post *this contains the old value of @a __r, @a __r is empty.
253 */
254 shared_ptr(shared_ptr&& __r) noexcept
255 : __shared_ptr<_Tp>(std::move(__r)) { }
256
257 /**
258 * @brief Move-constructs a %shared_ptr instance from @a __r.
259 * @param __r A %shared_ptr rvalue.
260 * @post *this contains the old value of @a __r, @a __r is empty.
261 */
262 template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>>
263 shared_ptr(shared_ptr<_Yp>&& __r) noexcept
264 : __shared_ptr<_Tp>(std::move(__r)) { }
265
266 /**
267 * @brief Constructs a %shared_ptr that shares ownership with @a __r
268 * and stores a copy of the pointer stored in @a __r.
269 * @param __r A weak_ptr.
270 * @post use_count() == __r.use_count()
271 * @throw bad_weak_ptr when __r.expired(),
272 * in which case the constructor has no effect.
273 */
274 template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
275 explicit shared_ptr(const weak_ptr<_Yp>& __r)
276 : __shared_ptr<_Tp>(__r) { }
277
278#if _GLIBCXX_USE_DEPRECATED1
279#pragma GCC diagnostic push
280#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
281 template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>>
282 shared_ptr(auto_ptr<_Yp>&& __r);
283#pragma GCC diagnostic pop
284#endif
285
286 // _GLIBCXX_RESOLVE_LIB_DEFECTS
287 // 2399. shared_ptr's constructor from unique_ptr should be constrained
288 template<typename _Yp, typename _Del,
289 typename = _Constructible<unique_ptr<_Yp, _Del>>>
290 shared_ptr(unique_ptr<_Yp, _Del>&& __r)
291 : __shared_ptr<_Tp>(std::move(__r)) { }
292
293#if __cplusplus201103L <= 201402L && _GLIBCXX_USE_DEPRECATED1
294 // This non-standard constructor exists to support conversions that
295 // were possible in C++11 and C++14 but are ill-formed in C++17.
296 // If an exception is thrown this constructor has no effect.
297 template<typename _Yp, typename _Del,
298 _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
299 shared_ptr(unique_ptr<_Yp, _Del>&& __r)
300 : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { }
301#endif
302
303 /**
304 * @brief Construct an empty %shared_ptr.
305 * @post use_count() == 0 && get() == nullptr
306 */
307 constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
308
309 shared_ptr& operator=(const shared_ptr&) noexcept = default;
9
Calling defaulted copy assignment operator for '__shared_ptr'
24
Returning; memory was released
29
Calling defaulted copy assignment operator for '__shared_ptr'
310
311 template<typename _Yp>
312 _Assignable<const shared_ptr<_Yp>&>
313 operator=(const shared_ptr<_Yp>& __r) noexcept
314 {
315 this->__shared_ptr<_Tp>::operator=(__r);
316 return *this;
317 }
318
319#if _GLIBCXX_USE_DEPRECATED1
320#pragma GCC diagnostic push
321#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
322 template<typename _Yp>
323 _Assignable<auto_ptr<_Yp>>
324 operator=(auto_ptr<_Yp>&& __r)
325 {
326 this->__shared_ptr<_Tp>::operator=(std::move(__r));
327 return *this;
328 }
329#pragma GCC diagnostic pop
330#endif
331
332 shared_ptr&
333 operator=(shared_ptr&& __r) noexcept
334 {
335 this->__shared_ptr<_Tp>::operator=(std::move(__r));
336 return *this;
337 }
338
339 template<class _Yp>
340 _Assignable<shared_ptr<_Yp>>
341 operator=(shared_ptr<_Yp>&& __r) noexcept
342 {
343 this->__shared_ptr<_Tp>::operator=(std::move(__r));
344 return *this;
345 }
346
347 template<typename _Yp, typename _Del>
348 _Assignable<unique_ptr<_Yp, _Del>>
349 operator=(unique_ptr<_Yp, _Del>&& __r)
350 {
351 this->__shared_ptr<_Tp>::operator=(std::move(__r));
352 return *this;
353 }
354
355 private:
356 // This constructor is non-standard, it is used by allocate_shared.
357 template<typename _Alloc, typename... _Args>
358 shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
359 : __shared_ptr<_Tp>(__tag, std::forward<_Args>(__args)...)
360 { }
361
362 template<typename _Yp, typename _Alloc, typename... _Args>
363 friend shared_ptr<_Yp>
364 allocate_shared(const _Alloc& __a, _Args&&... __args);
365
366 // This constructor is non-standard, it is used by weak_ptr::lock().
367 shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t)
368 : __shared_ptr<_Tp>(__r, std::nothrow) { }
369
370 friend class weak_ptr<_Tp>;
371 };
372
373#if __cpp_deduction_guides >= 201606
374 template<typename _Tp>
375 shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
376 template<typename _Tp, typename _Del>
377 shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>;
378#endif
379
380 // 20.7.2.2.7 shared_ptr comparisons
381 template<typename _Tp, typename _Up>
382 inline bool
383 operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
384 { return __a.get() == __b.get(); }
385
386 template<typename _Tp>
387 inline bool
388 operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
389 { return !__a; }
390
391 template<typename _Tp>
392 inline bool
393 operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
394 { return !__a; }
395
396 template<typename _Tp, typename _Up>
397 inline bool
398 operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
399 { return __a.get() != __b.get(); }
400
401 template<typename _Tp>
402 inline bool
403 operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
404 { return (bool)__a; }
405
406 template<typename _Tp>
407 inline bool
408 operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
409 { return (bool)__a; }
410
411 template<typename _Tp, typename _Up>
412 inline bool
413 operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
414 {
415 using _Tp_elt = typename shared_ptr<_Tp>::element_type;
416 using _Up_elt = typename shared_ptr<_Up>::element_type;
417 using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
418 return less<_Vp>()(__a.get(), __b.get());
419 }
420
421 template<typename _Tp>
422 inline bool
423 operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
424 {
425 using _Tp_elt = typename shared_ptr<_Tp>::element_type;
426 return less<_Tp_elt*>()(__a.get(), nullptr);
427 }
428
429 template<typename _Tp>
430 inline bool
431 operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
432 {
433 using _Tp_elt = typename shared_ptr<_Tp>::element_type;
434 return less<_Tp_elt*>()(nullptr, __a.get());
435 }
436
437 template<typename _Tp, typename _Up>
438 inline bool
439 operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
440 { return !(__b < __a); }
441
442 template<typename _Tp>
443 inline bool
444 operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
445 { return !(nullptr < __a); }
446
447 template<typename _Tp>
448 inline bool
449 operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
450 { return !(__a < nullptr); }
451
452 template<typename _Tp, typename _Up>
453 inline bool
454 operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
455 { return (__b < __a); }
456
457 template<typename _Tp>
458 inline bool
459 operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
460 { return nullptr < __a; }
461
462 template<typename _Tp>
463 inline bool
464 operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
465 { return __a < nullptr; }
466
467 template<typename _Tp, typename _Up>
468 inline bool
469 operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
470 { return !(__a < __b); }
471
472 template<typename _Tp>
473 inline bool
474 operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
475 { return !(__a < nullptr); }
476
477 template<typename _Tp>
478 inline bool
479 operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
480 { return !(nullptr < __a); }
481
482 template<typename _Tp>
483 struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
484 { };
485
486 // 20.7.2.2.8 shared_ptr specialized algorithms.
487 template<typename _Tp>
488 inline void
489 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
490 { __a.swap(__b); }
491
492 // 20.7.2.2.9 shared_ptr casts.
493 template<typename _Tp, typename _Up>
494 inline shared_ptr<_Tp>
495 static_pointer_cast(const shared_ptr<_Up>& __r) noexcept
496 {
497 using _Sp = shared_ptr<_Tp>;
498 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
499 }
500
501 template<typename _Tp, typename _Up>
502 inline shared_ptr<_Tp>
503 const_pointer_cast(const shared_ptr<_Up>& __r) noexcept
504 {
505 using _Sp = shared_ptr<_Tp>;
506 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
507 }
508
509 template<typename _Tp, typename _Up>
510 inline shared_ptr<_Tp>
511 dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept
512 {
513 using _Sp = shared_ptr<_Tp>;
514 if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
515 return _Sp(__r, __p);
516 return _Sp();
517 }
518
519#if __cplusplus201103L > 201402L
520 template<typename _Tp, typename _Up>
521 inline shared_ptr<_Tp>
522 reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept
523 {
524 using _Sp = shared_ptr<_Tp>;
525 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
526 }
527#endif
528
529 /**
530 * @brief A smart pointer with weak semantics.
531 *
532 * With forwarding constructors and assignment operators.
533 */
534 template<typename _Tp>
535 class weak_ptr : public __weak_ptr<_Tp>
536 {
537 template<typename _Arg>
538 using _Constructible = typename enable_if<
539 is_constructible<__weak_ptr<_Tp>, _Arg>::value
540 >::type;
541
542 template<typename _Arg>
543 using _Assignable = typename enable_if<
544 is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr&
545 >::type;
546
547 public:
548 constexpr weak_ptr() noexcept = default;
549
550 template<typename _Yp,
551 typename = _Constructible<const shared_ptr<_Yp>&>>
552 weak_ptr(const shared_ptr<_Yp>& __r) noexcept
553 : __weak_ptr<_Tp>(__r) { }
554
555 weak_ptr(const weak_ptr&) noexcept = default;
556
557 template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
558 weak_ptr(const weak_ptr<_Yp>& __r) noexcept
559 : __weak_ptr<_Tp>(__r) { }
560
561 weak_ptr(weak_ptr&&) noexcept = default;
562
563 template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>>
564 weak_ptr(weak_ptr<_Yp>&& __r) noexcept
565 : __weak_ptr<_Tp>(std::move(__r)) { }
566
567 weak_ptr&
568 operator=(const weak_ptr& __r) noexcept = default;
569
570 template<typename _Yp>
571 _Assignable<const weak_ptr<_Yp>&>
572 operator=(const weak_ptr<_Yp>& __r) noexcept
573 {
574 this->__weak_ptr<_Tp>::operator=(__r);
575 return *this;
576 }
577
578 template<typename _Yp>
579 _Assignable<const shared_ptr<_Yp>&>
580 operator=(const shared_ptr<_Yp>& __r) noexcept
581 {
582 this->__weak_ptr<_Tp>::operator=(__r);
583 return *this;
584 }
585
586 weak_ptr&
587 operator=(weak_ptr&& __r) noexcept = default;
588
589 template<typename _Yp>
590 _Assignable<weak_ptr<_Yp>>
591 operator=(weak_ptr<_Yp>&& __r) noexcept
592 {
593 this->__weak_ptr<_Tp>::operator=(std::move(__r));
594 return *this;
595 }
596
597 shared_ptr<_Tp>
598 lock() const noexcept
599 { return shared_ptr<_Tp>(*this, std::nothrow); }
600 };
601
602#if __cpp_deduction_guides >= 201606
603 template<typename _Tp>
604 weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
605#endif
606
607 // 20.7.2.3.6 weak_ptr specialized algorithms.
608 template<typename _Tp>
609 inline void
610 swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept
611 { __a.swap(__b); }
612
613
614 /// Primary template owner_less
615 template<typename _Tp = void>
616 struct owner_less;
617
618 /// Void specialization of owner_less
619 template<>
620 struct owner_less<void> : _Sp_owner_less<void, void>
621 { };
622
623 /// Partial specialization of owner_less for shared_ptr.
624 template<typename _Tp>
625 struct owner_less<shared_ptr<_Tp>>
626 : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
627 { };
628
629 /// Partial specialization of owner_less for weak_ptr.
630 template<typename _Tp>
631 struct owner_less<weak_ptr<_Tp>>
632 : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
633 { };
634
635 /**
636 * @brief Base class allowing use of member function shared_from_this.
637 */
638 template<typename _Tp>
639 class enable_shared_from_this
640 {
641 protected:
642 constexpr enable_shared_from_this() noexcept { }
643
644 enable_shared_from_this(const enable_shared_from_this&) noexcept { }
645
646 enable_shared_from_this&
647 operator=(const enable_shared_from_this&) noexcept
648 { return *this; }
649
650 ~enable_shared_from_this() { }
651
652 public:
653 shared_ptr<_Tp>
654 shared_from_this()
655 { return shared_ptr<_Tp>(this->_M_weak_this); }
656
657 shared_ptr<const _Tp>
658 shared_from_this() const
659 { return shared_ptr<const _Tp>(this->_M_weak_this); }
660
661#if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11
662#define __cpp_lib_enable_shared_from_this 201603
663 weak_ptr<_Tp>
664 weak_from_this() noexcept
665 { return this->_M_weak_this; }
666
667 weak_ptr<const _Tp>
668 weak_from_this() const noexcept
669 { return this->_M_weak_this; }
670#endif
671
672 private:
673 template<typename _Tp1>
674 void
675 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
676 { _M_weak_this._M_assign(__p, __n); }
677
678 // Found by ADL when this is an associated class.
679 friend const enable_shared_from_this*
680 __enable_shared_from_this_base(const __shared_count<>&,
681 const enable_shared_from_this* __p)
682 { return __p; }
683
684 template<typename, _Lock_policy>
685 friend class __shared_ptr;
686
687 mutable weak_ptr<_Tp> _M_weak_this;
688 };
689
690 /**
691 * @brief Create an object that is owned by a shared_ptr.
692 * @param __a An allocator.
693 * @param __args Arguments for the @a _Tp object's constructor.
694 * @return A shared_ptr that owns the newly created object.
695 * @throw An exception thrown from @a _Alloc::allocate or from the
696 * constructor of @a _Tp.
697 *
698 * A copy of @a __a will be used to allocate memory for the shared_ptr
699 * and the new object.
700 */
701 template<typename _Tp, typename _Alloc, typename... _Args>
702 inline shared_ptr<_Tp>
703 allocate_shared(const _Alloc& __a, _Args&&... __args)
704 {
705 return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a},
706 std::forward<_Args>(__args)...);
707 }
708
709 /**
710 * @brief Create an object that is owned by a shared_ptr.
711 * @param __args Arguments for the @a _Tp object's constructor.
712 * @return A shared_ptr that owns the newly created object.
713 * @throw std::bad_alloc, or an exception thrown from the
714 * constructor of @a _Tp.
715 */
716 template<typename _Tp, typename... _Args>
717 inline shared_ptr<_Tp>
718 make_shared(_Args&&... __args)
719 {
720 typedef typename std::remove_cv<_Tp>::type _Tp_nc;
721 return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
722 std::forward<_Args>(__args)...);
723 }
724
725 /// std::hash specialization for shared_ptr.
726 template<typename _Tp>
727 struct hash<shared_ptr<_Tp>>
728 : public __hash_base<size_t, shared_ptr<_Tp>>
729 {
730 size_t
731 operator()(const shared_ptr<_Tp>& __s) const noexcept
732 {
733 return std::hash<typename shared_ptr<_Tp>::element_type*>()(__s.get());
734 }
735 };
736
737 // @} group pointer_abstractions
738
739_GLIBCXX_END_NAMESPACE_VERSION
740} // namespace
741
742#endif // _SHARED_PTR_H

/usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/shared_ptr_base.h

1// shared_ptr and weak_ptr implementation details -*- C++ -*-
2
3// Copyright (C) 2007-2018 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// 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.
19
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/>.
24
25// GCC Note: Based on files from version 1.32.0 of the Boost library.
26
27// shared_count.hpp
28// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
29
30// shared_ptr.hpp
31// Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32// Copyright (C) 2001, 2002, 2003 Peter Dimov
33
34// weak_ptr.hpp
35// Copyright (C) 2001, 2002, 2003 Peter Dimov
36
37// enable_shared_from_this.hpp
38// Copyright (C) 2002 Peter Dimov
39
40// Distributed under the Boost Software License, Version 1.0. (See
41// accompanying file LICENSE_1_0.txt or copy at
42// http://www.boost.org/LICENSE_1_0.txt)
43
44/** @file bits/shared_ptr_base.h
45 * This is an internal header file, included by other library headers.
46 * Do not attempt to use it directly. @headername{memory}
47 */
48
49#ifndef _SHARED_PTR_BASE_H1
50#define _SHARED_PTR_BASE_H1 1
51
52#include <typeinfo>
53#include <bits/allocated_ptr.h>
54#include <bits/refwrap.h>
55#include <bits/stl_function.h>
56#include <ext/aligned_buffer.h>
57
58namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
59{
60_GLIBCXX_BEGIN_NAMESPACE_VERSION
61
62#if _GLIBCXX_USE_DEPRECATED1
63#pragma GCC diagnostic push
64#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
65 template<typename> class auto_ptr;
66#pragma GCC diagnostic pop
67#endif
68
69 /**
70 * @brief Exception possibly thrown by @c shared_ptr.
71 * @ingroup exceptions
72 */
73 class bad_weak_ptr : public std::exception
74 {
75 public:
76 virtual char const* what() const noexcept;
77
78 virtual ~bad_weak_ptr() noexcept;
79 };
80
81 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
82 inline void
83 __throw_bad_weak_ptr()
84 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr())(throw (bad_weak_ptr())); }
85
86 using __gnu_cxx::_Lock_policy;
87 using __gnu_cxx::__default_lock_policy;
88 using __gnu_cxx::_S_single;
89 using __gnu_cxx::_S_mutex;
90 using __gnu_cxx::_S_atomic;
91
92 // Empty helper class except when the template argument is _S_mutex.
93 template<_Lock_policy _Lp>
94 class _Mutex_base
95 {
96 protected:
97 // The atomic policy uses fully-fenced builtins, single doesn't care.
98 enum { _S_need_barriers = 0 };
99 };
100
101 template<>
102 class _Mutex_base<_S_mutex>
103 : public __gnu_cxx::__mutex
104 {
105 protected:
106 // This policy is used when atomic builtins are not available.
107 // The replacement atomic operations might not have the necessary
108 // memory barriers.
109 enum { _S_need_barriers = 1 };
110 };
111
112 template<_Lock_policy _Lp = __default_lock_policy>
113 class _Sp_counted_base
114 : public _Mutex_base<_Lp>
115 {
116 public:
117 _Sp_counted_base() noexcept
118 : _M_use_count(1), _M_weak_count(1) { }
119
120 virtual
121 ~_Sp_counted_base() noexcept
122 { }
123
124 // Called when _M_use_count drops to zero, to release the resources
125 // managed by *this.
126 virtual void
127 _M_dispose() noexcept = 0;
128
129 // Called when _M_weak_count drops to zero.
130 virtual void
131 _M_destroy() noexcept
132 { delete this; }
20
Memory is released
133
134 virtual void*
135 _M_get_deleter(const std::type_info&) noexcept = 0;
136
137 void
138 _M_add_ref_copy()
139 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
140
141 void
142 _M_add_ref_lock();
143
144 bool
145 _M_add_ref_lock_nothrow();
146
147 void
148 _M_release() noexcept
149 {
150 // Be race-detector-friendly. For more info see bits/c++config.
151 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
152 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
16
Taking true branch
36
Calling '__exchange_and_add_dispatch'
153 {
154 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
155 _M_dispose();
156 // There must be a memory barrier between dispose() and destroy()
157 // to ensure that the effects of dispose() are observed in the
158 // thread that runs destroy().
159 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
160 if (_Mutex_base<_Lp>::_S_need_barriers)
17
Taking false branch
161 {
162 __atomic_thread_fence (__ATOMIC_ACQ_REL4);
163 }
164
165 // Be race-detector-friendly. For more info see bits/c++config.
166 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
167 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
18
Taking true branch
168 -1) == 1)
169 {
170 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
171 _M_destroy();
19
Calling '_Sp_counted_base::_M_destroy'
21
Returning; memory was released
172 }
173 }
174 }
175
176 void
177 _M_weak_add_ref() noexcept
178 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
179
180 void
181 _M_weak_release() noexcept
182 {
183 // Be race-detector-friendly. For more info see bits/c++config.
184 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
185 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
186 {
187 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
188 if (_Mutex_base<_Lp>::_S_need_barriers)
189 {
190 // See _M_release(),
191 // destroy() must observe results of dispose()
192 __atomic_thread_fence (__ATOMIC_ACQ_REL4);
193 }
194 _M_destroy();
195 }
196 }
197
198 long
199 _M_get_use_count() const noexcept
200 {
201 // No memory barrier is used here so there is no synchronization
202 // with other threads.
203 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED0);
204 }
205
206 private:
207 _Sp_counted_base(_Sp_counted_base const&) = delete;
208 _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
209
210 _Atomic_word _M_use_count; // #shared
211 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
212 };
213
214 template<>
215 inline void
216 _Sp_counted_base<_S_single>::
217 _M_add_ref_lock()
218 {
219 if (_M_use_count == 0)
220 __throw_bad_weak_ptr();
221 ++_M_use_count;
222 }
223
224 template<>
225 inline void
226 _Sp_counted_base<_S_mutex>::
227 _M_add_ref_lock()
228 {
229 __gnu_cxx::__scoped_lock sentry(*this);
230 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
231 {
232 _M_use_count = 0;
233 __throw_bad_weak_ptr();
234 }
235 }
236
237 template<>
238 inline void
239 _Sp_counted_base<_S_atomic>::
240 _M_add_ref_lock()
241 {
242 // Perform lock-free add-if-not-zero operation.
243 _Atomic_word __count = _M_get_use_count();
244 do
245 {
246 if (__count == 0)
247 __throw_bad_weak_ptr();
248 // Replace the current counter value with the old value + 1, as
249 // long as it's not changed meanwhile.
250 }
251 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
252 true, __ATOMIC_ACQ_REL4,
253 __ATOMIC_RELAXED0));
254 }
255
256 template<>
257 inline bool
258 _Sp_counted_base<_S_single>::
259 _M_add_ref_lock_nothrow()
260 {
261 if (_M_use_count == 0)
262 return false;
263 ++_M_use_count;
264 return true;
265 }
266
267 template<>
268 inline bool
269 _Sp_counted_base<_S_mutex>::
270 _M_add_ref_lock_nothrow()
271 {
272 __gnu_cxx::__scoped_lock sentry(*this);
273 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
274 {
275 _M_use_count = 0;
276 return false;
277 }
278 return true;
279 }
280
281 template<>
282 inline bool
283 _Sp_counted_base<_S_atomic>::
284 _M_add_ref_lock_nothrow()
285 {
286 // Perform lock-free add-if-not-zero operation.
287 _Atomic_word __count = _M_get_use_count();
288 do
289 {
290 if (__count == 0)
291 return false;
292 // Replace the current counter value with the old value + 1, as
293 // long as it's not changed meanwhile.
294 }
295 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
296 true, __ATOMIC_ACQ_REL4,
297 __ATOMIC_RELAXED0));
298 return true;
299 }
300
301 template<>
302 inline void
303 _Sp_counted_base<_S_single>::_M_add_ref_copy()
304 { ++_M_use_count; }
305
306 template<>
307 inline void
308 _Sp_counted_base<_S_single>::_M_release() noexcept
309 {
310 if (--_M_use_count == 0)
311 {
312 _M_dispose();
313 if (--_M_weak_count == 0)
314 _M_destroy();
315 }
316 }
317
318 template<>
319 inline void
320 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
321 { ++_M_weak_count; }
322
323 template<>
324 inline void
325 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
326 {
327 if (--_M_weak_count == 0)
328 _M_destroy();
329 }
330
331 template<>
332 inline long
333 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
334 { return _M_use_count; }
335
336
337 // Forward declarations.
338 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
339 class __shared_ptr;
340
341 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
342 class __weak_ptr;
343
344 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
345 class __enable_shared_from_this;
346
347 template<typename _Tp>
348 class shared_ptr;
349
350 template<typename _Tp>
351 class weak_ptr;
352
353 template<typename _Tp>
354 struct owner_less;
355
356 template<typename _Tp>
357 class enable_shared_from_this;
358
359 template<_Lock_policy _Lp = __default_lock_policy>
360 class __weak_count;
361
362 template<_Lock_policy _Lp = __default_lock_policy>
363 class __shared_count;
364
365
366 // Counted ptr with no deleter or allocator support
367 template<typename _Ptr, _Lock_policy _Lp>
368 class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
369 {
370 public:
371 explicit
372 _Sp_counted_ptr(_Ptr __p) noexcept
373 : _M_ptr(__p) { }
374
375 virtual void
376 _M_dispose() noexcept
377 { delete _M_ptr; }
378
379 virtual void
380 _M_destroy() noexcept
381 { delete this; }
382
383 virtual void*
384 _M_get_deleter(const std::type_info&) noexcept
385 { return nullptr; }
386
387 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
388 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
389
390 private:
391 _Ptr _M_ptr;
392 };
393
394 template<>
395 inline void
396 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
397
398 template<>
399 inline void
400 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
401
402 template<>
403 inline void
404 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
405
406 template<int _Nm, typename _Tp,
407 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
408 struct _Sp_ebo_helper;
409
410 /// Specialization using EBO.
411 template<int _Nm, typename _Tp>
412 struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp
413 {
414 explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { }
415 explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { }
416
417 static _Tp&
418 _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); }
419 };
420
421 /// Specialization not using EBO.
422 template<int _Nm, typename _Tp>
423 struct _Sp_ebo_helper<_Nm, _Tp, false>
424 {
425 explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { }
426 explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { }
427
428 static _Tp&
429 _S_get(_Sp_ebo_helper& __eboh)
430 { return __eboh._M_tp; }
431
432 private:
433 _Tp _M_tp;
434 };
435
436 // Support for custom deleter and/or allocator
437 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
438 class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
439 {
440 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
441 {
442 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
443 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
444
445 public:
446 _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
447 : _M_ptr(__p), _Del_base(std::move(__d)), _Alloc_base(__a)
448 { }
449
450 _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); }
451 _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); }
452
453 _Ptr _M_ptr;
454 };
455
456 public:
457 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
458
459 // __d(__p) must not throw.
460 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
461 : _M_impl(__p, std::move(__d), _Alloc()) { }
462
463 // __d(__p) must not throw.
464 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
465 : _M_impl(__p, std::move(__d), __a) { }
466
467 ~_Sp_counted_deleter() noexcept { }
468
469 virtual void
470 _M_dispose() noexcept
471 { _M_impl._M_del()(_M_impl._M_ptr); }
472
473 virtual void
474 _M_destroy() noexcept
475 {
476 __allocator_type __a(_M_impl._M_alloc());
477 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
478 this->~_Sp_counted_deleter();
479 }
480
481 virtual void*
482 _M_get_deleter(const std::type_info& __ti) noexcept
483 {
484#if __cpp_rtti199711
485 // _GLIBCXX_RESOLVE_LIB_DEFECTS
486 // 2400. shared_ptr's get_deleter() should use addressof()
487 return __ti == typeid(_Deleter)
488 ? std::__addressof(_M_impl._M_del())
489 : nullptr;
490#else
491 return nullptr;
492#endif
493 }
494
495 private:
496 _Impl _M_impl;
497 };
498
499 // helpers for make_shared / allocate_shared
500
501 struct _Sp_make_shared_tag
502 {
503 private:
504 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
505 friend class _Sp_counted_ptr_inplace;
506
507 static const type_info&
508 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
509 {
510 alignas(type_info) static constexpr char __tag[sizeof(type_info)] = { };
511 return reinterpret_cast<const type_info&>(__tag);
512 }
513 };
514
515 template<typename _Alloc>
516 struct _Sp_alloc_shared_tag
517 {
518 const _Alloc& _M_a;
519 };
520
521 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
522 class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
523 {
524 class _Impl : _Sp_ebo_helper<0, _Alloc>
525 {
526 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
527
528 public:
529 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
530
531 _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); }
532
533 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
534 };
535
536 public:
537 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
538
539 template<typename... _Args>
540 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
541 : _M_impl(__a)
542 {
543 // _GLIBCXX_RESOLVE_LIB_DEFECTS
544 // 2070. allocate_shared should use allocator_traits<A>::construct
545 allocator_traits<_Alloc>::construct(__a, _M_ptr(),
546 std::forward<_Args>(__args)...); // might throw
547 }
548
549 ~_Sp_counted_ptr_inplace() noexcept { }
550
551 virtual void
552 _M_dispose() noexcept
553 {
554 allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr());
555 }
556
557 // Override because the allocator needs to know the dynamic type
558 virtual void
559 _M_destroy() noexcept
560 {
561 __allocator_type __a(_M_impl._M_alloc());
562 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
563 this->~_Sp_counted_ptr_inplace();
564 }
565
566 private:
567 friend class __shared_count<_Lp>; // To be able to call _M_ptr().
568
569 // No longer used, but code compiled against old libstdc++ headers
570 // might still call it from __shared_ptr ctor to get the pointer out.
571 virtual void*
572 _M_get_deleter(const std::type_info& __ti) noexcept override
573 {
574 // Check for the fake type_info first, so we don't try to access it
575 // as a real type_info object.
576 if (&__ti == &_Sp_make_shared_tag::_S_ti())
577 return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
578#if __cpp_rtti199711
579 // Callers compiled with old libstdc++ headers and RTTI enabled
580 // might pass this instead:
581 else if (__ti == typeid(_Sp_make_shared_tag))
582 return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
583#else
584 // Cannot detect a real type_info object. If the linker keeps a
585 // definition of this function compiled with -fno-rtti then callers
586 // that have RTTI enabled and pass a real type_info object will get
587 // a null pointer returned.
588#endif
589 return nullptr;
590 }
591
592 _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); }
593
594 _Impl _M_impl;
595 };
596
597 // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
598 struct __sp_array_delete
599 {
600 template<typename _Yp>
601 void operator()(_Yp* __p) const { delete[] __p; }
602 };
603
604 template<_Lock_policy _Lp>
605 class __shared_count
606 {
607 template<typename _Tp>
608 struct __not_alloc_shared_tag { using type = void; };
609
610 template<typename _Tp>
611 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
612
613 public:
614 constexpr __shared_count() noexcept : _M_pi(0)
615 { }
616
617 template<typename _Ptr>
618 explicit
619 __shared_count(_Ptr __p) : _M_pi(0)
620 {
621 __trytry
622 {
623 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
624 }
625 __catch(...)catch(...)
626 {
627 delete __p;
628 __throw_exception_againthrow;
629 }
630 }
631
632 template<typename _Ptr>
633 __shared_count(_Ptr __p, /* is_array = */ false_type)
634 : __shared_count(__p)
635 { }
636
637 template<typename _Ptr>
638 __shared_count(_Ptr __p, /* is_array = */ true_type)
639 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
640 { }
641
642 template<typename _Ptr, typename _Deleter,
643 typename = typename __not_alloc_shared_tag<_Deleter>::type>
644 __shared_count(_Ptr __p, _Deleter __d)
645 : __shared_count(__p, std::move(__d), allocator<void>())
646 { }
647
648 template<typename _Ptr, typename _Deleter, typename _Alloc,
649 typename = typename __not_alloc_shared_tag<_Deleter>::type>
650 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
651 {
652 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
653 __trytry
654 {
655 typename _Sp_cd_type::__allocator_type __a2(__a);
656 auto __guard = std::__allocate_guarded(__a2);
657 _Sp_cd_type* __mem = __guard.get();
658 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
659 _M_pi = __mem;
660 __guard = nullptr;
661 }
662 __catch(...)catch(...)
663 {
664 __d(__p); // Call _Deleter on __p.
665 __throw_exception_againthrow;
666 }
667 }
668
669 template<typename _Tp, typename _Alloc, typename... _Args>
670 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
671 _Args&&... __args)
672 {
673 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
674 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
675 auto __guard = std::__allocate_guarded(__a2);
676 _Sp_cp_type* __mem = __guard.get();
677 auto __pi = ::new (__mem)
678 _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
679 __guard = nullptr;
680 _M_pi = __pi;
681 __p = __pi->_M_ptr();
682 }
683
684#if _GLIBCXX_USE_DEPRECATED1
685#pragma GCC diagnostic push
686#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
687 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
688 template<typename _Tp>
689 explicit
690 __shared_count(std::auto_ptr<_Tp>&& __r);
691#pragma GCC diagnostic pop
692#endif
693
694 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
695 template<typename _Tp, typename _Del>
696 explicit
697 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
698 {
699 // _GLIBCXX_RESOLVE_LIB_DEFECTS
700 // 2415. Inconsistency between unique_ptr and shared_ptr
701 if (__r.get() == nullptr)
702 return;
703
704 using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
705 using _Del2 = typename conditional<is_reference<_Del>::value,
706 reference_wrapper<typename remove_reference<_Del>::type>,
707 _Del>::type;
708 using _Sp_cd_type
709 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
710 using _Alloc = allocator<_Sp_cd_type>;
711 using _Alloc_traits = allocator_traits<_Alloc>;
712 _Alloc __a;
713 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
714 _Alloc_traits::construct(__a, __mem, __r.release(),
715 __r.get_deleter()); // non-throwing
716 _M_pi = __mem;
717 }
718
719 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
720 explicit __shared_count(const __weak_count<_Lp>& __r);
721
722 // Does not throw if __r._M_get_use_count() == 0, caller must check.
723 explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
724
725 ~__shared_count() noexcept
726 {
727 if (_M_pi != nullptr)
728 _M_pi->_M_release();
729 }
730
731 __shared_count(const __shared_count& __r) noexcept
732 : _M_pi(__r._M_pi)
733 {
734 if (_M_pi != 0)
735 _M_pi->_M_add_ref_copy();
736 }
737
738 __shared_count&
739 operator=(const __shared_count& __r) noexcept
740 {
741 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
742 if (__tmp != _M_pi)
11
Taking true branch
31
Taking true branch
743 {
744 if (__tmp != 0)
12
Assuming '__tmp' is equal to null
13
Taking false branch
32
Assuming '__tmp' is equal to null
33
Taking false branch
745 __tmp->_M_add_ref_copy();
746 if (_M_pi != 0)
14
Taking true branch
34
Taking true branch
747 _M_pi->_M_release();
15
Calling '_Sp_counted_base::_M_release'
22
Returning; memory was released
35
Calling '_Sp_counted_base::_M_release'
748 _M_pi = __tmp;
749 }
750 return *this;
751 }
752
753 void
754 _M_swap(__shared_count& __r) noexcept
755 {
756 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
757 __r._M_pi = _M_pi;
758 _M_pi = __tmp;
759 }
760
761 long
762 _M_get_use_count() const noexcept
763 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
764
765 bool
766 _M_unique() const noexcept
767 { return this->_M_get_use_count() == 1; }
768
769 void*
770 _M_get_deleter(const std::type_info& __ti) const noexcept
771 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
772
773 bool
774 _M_less(const __shared_count& __rhs) const noexcept
775 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
776
777 bool
778 _M_less(const __weak_count<_Lp>& __rhs) const noexcept
779 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
780
781 // Friend function injected into enclosing namespace and found by ADL
782 friend inline bool
783 operator==(const __shared_count& __a, const __shared_count& __b) noexcept
784 { return __a._M_pi == __b._M_pi; }
785
786 private:
787 friend class __weak_count<_Lp>;
788
789 _Sp_counted_base<_Lp>* _M_pi;
790 };
791
792
793 template<_Lock_policy _Lp>
794 class __weak_count
795 {
796 public:
797 constexpr __weak_count() noexcept : _M_pi(nullptr)
798 { }
799
800 __weak_count(const __shared_count<_Lp>& __r) noexcept
801 : _M_pi(__r._M_pi)
802 {
803 if (_M_pi != nullptr)
804 _M_pi->_M_weak_add_ref();
805 }
806
807 __weak_count(const __weak_count& __r) noexcept
808 : _M_pi(__r._M_pi)
809 {
810 if (_M_pi != nullptr)
811 _M_pi->_M_weak_add_ref();
812 }
813
814 __weak_count(__weak_count&& __r) noexcept
815 : _M_pi(__r._M_pi)
816 { __r._M_pi = nullptr; }
817
818 ~__weak_count() noexcept
819 {
820 if (_M_pi != nullptr)
821 _M_pi->_M_weak_release();
822 }
823
824 __weak_count&
825 operator=(const __shared_count<_Lp>& __r) noexcept
826 {
827 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
828 if (__tmp != nullptr)
829 __tmp->_M_weak_add_ref();
830 if (_M_pi != nullptr)
831 _M_pi->_M_weak_release();
832 _M_pi = __tmp;
833 return *this;
834 }
835
836 __weak_count&
837 operator=(const __weak_count& __r) noexcept
838 {
839 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
840 if (__tmp != nullptr)
841 __tmp->_M_weak_add_ref();
842 if (_M_pi != nullptr)
843 _M_pi->_M_weak_release();
844 _M_pi = __tmp;
845 return *this;
846 }
847
848 __weak_count&
849 operator=(__weak_count&& __r) noexcept
850 {
851 if (_M_pi != nullptr)
852 _M_pi->_M_weak_release();
853 _M_pi = __r._M_pi;
854 __r._M_pi = nullptr;
855 return *this;
856 }
857
858 void
859 _M_swap(__weak_count& __r) noexcept
860 {
861 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
862 __r._M_pi = _M_pi;
863 _M_pi = __tmp;
864 }
865
866 long
867 _M_get_use_count() const noexcept
868 { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; }
869
870 bool
871 _M_less(const __weak_count& __rhs) const noexcept
872 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
873
874 bool
875 _M_less(const __shared_count<_Lp>& __rhs) const noexcept
876 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
877
878 // Friend function injected into enclosing namespace and found by ADL
879 friend inline bool
880 operator==(const __weak_count& __a, const __weak_count& __b) noexcept
881 { return __a._M_pi == __b._M_pi; }
882
883 private:
884 friend class __shared_count<_Lp>;
885
886 _Sp_counted_base<_Lp>* _M_pi;
887 };
888
889 // Now that __weak_count is defined we can define this constructor:
890 template<_Lock_policy _Lp>
891 inline
892 __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
893 : _M_pi(__r._M_pi)
894 {
895 if (_M_pi != nullptr)
896 _M_pi->_M_add_ref_lock();
897 else
898 __throw_bad_weak_ptr();
899 }
900
901 // Now that __weak_count is defined we can define this constructor:
902 template<_Lock_policy _Lp>
903 inline
904 __shared_count<_Lp>::
905 __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t)
906 : _M_pi(__r._M_pi)
907 {
908 if (_M_pi != nullptr)
909 if (!_M_pi->_M_add_ref_lock_nothrow())
910 _M_pi = nullptr;
911 }
912
913#define __cpp_lib_shared_ptr_arrays201603 201603
914
915 // Helper traits for shared_ptr of array:
916
917 // A pointer type Y* is said to be compatible with a pointer type T* when
918 // either Y* is convertible to T* or Y is U[N] and T is U cv [].
919 template<typename _Yp_ptr, typename _Tp_ptr>
920 struct __sp_compatible_with
921 : false_type
922 { };
923
924 template<typename _Yp, typename _Tp>
925 struct __sp_compatible_with<_Yp*, _Tp*>
926 : is_convertible<_Yp*, _Tp*>::type
927 { };
928
929 template<typename _Up, size_t _Nm>
930 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
931 : true_type
932 { };
933
934 template<typename _Up, size_t _Nm>
935 struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
936 : true_type
937 { };
938
939 template<typename _Up, size_t _Nm>
940 struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
941 : true_type
942 { };
943
944 template<typename _Up, size_t _Nm>
945 struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
946 : true_type
947 { };
948
949 // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
950 template<typename _Up, size_t _Nm, typename _Yp, typename = void>
951 struct __sp_is_constructible_arrN
952 : false_type
953 { };
954
955 template<typename _Up, size_t _Nm, typename _Yp>
956 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
957 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
958 { };
959
960 // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
961 template<typename _Up, typename _Yp, typename = void>
962 struct __sp_is_constructible_arr
963 : false_type
964 { };
965
966 template<typename _Up, typename _Yp>
967 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
968 : is_convertible<_Yp(*)[], _Up(*)[]>::type
969 { };
970
971 // Trait to check if shared_ptr<T> can be constructed from Y*.
972 template<typename _Tp, typename _Yp>
973 struct __sp_is_constructible;
974
975 // When T is U[N], Y(*)[N] shall be convertible to T*;
976 template<typename _Up, size_t _Nm, typename _Yp>
977 struct __sp_is_constructible<_Up[_Nm], _Yp>
978 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
979 { };
980
981 // when T is U[], Y(*)[] shall be convertible to T*;
982 template<typename _Up, typename _Yp>
983 struct __sp_is_constructible<_Up[], _Yp>
984 : __sp_is_constructible_arr<_Up, _Yp>::type
985 { };
986
987 // otherwise, Y* shall be convertible to T*.
988 template<typename _Tp, typename _Yp>
989 struct __sp_is_constructible
990 : is_convertible<_Yp*, _Tp*>::type
991 { };
992
993
994 // Define operator* and operator-> for shared_ptr<T>.
995 template<typename _Tp, _Lock_policy _Lp,
996 bool = is_array<_Tp>::value, bool = is_void<_Tp>::value>
997 class __shared_ptr_access
998 {
999 public:
1000 using element_type = _Tp;
1001
1002 element_type&
1003 operator*() const noexcept
1004 {
1005 __glibcxx_assert(_M_get() != nullptr);
1006 return *_M_get();
1007 }
1008
1009 element_type*
1010 operator->() const noexcept
1011 {
1012 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1013 return _M_get();
1014 }
1015
1016 private:
1017 element_type*
1018 _M_get() const noexcept
1019 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1020 };
1021
1022 // Define operator-> for shared_ptr<cv void>.
1023 template<typename _Tp, _Lock_policy _Lp>
1024 class __shared_ptr_access<_Tp, _Lp, false, true>
1025 {
1026 public:
1027 using element_type = _Tp;
1028
1029 element_type*
1030 operator->() const noexcept
1031 {
1032 auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
1033 _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr);
1034 return __ptr;
1035 }
1036 };
1037
1038 // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
1039 template<typename _Tp, _Lock_policy _Lp>
1040 class __shared_ptr_access<_Tp, _Lp, true, false>
1041 {
1042 public:
1043 using element_type = typename remove_extent<_Tp>::type;
1044
1045#if __cplusplus201103L <= 201402L
1046 [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
1047 element_type&
1048 operator*() const noexcept
1049 {
1050 __glibcxx_assert(_M_get() != nullptr);
1051 return *_M_get();
1052 }
1053
1054 [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
1055 element_type*
1056 operator->() const noexcept
1057 {
1058 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1059 return _M_get();
1060 }
1061#endif
1062
1063 element_type&
1064 operator[](ptrdiff_t __i) const
1065 {
1066 __glibcxx_assert(_M_get() != nullptr);
1067 __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
1068 return _M_get()[__i];
1069 }
1070
1071 private:
1072 element_type*
1073 _M_get() const noexcept
1074 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1075 };
1076
1077 template<typename _Tp, _Lock_policy _Lp>
1078 class __shared_ptr
1079 : public __shared_ptr_access<_Tp, _Lp>
1080 {
1081 public:
1082 using element_type = typename remove_extent<_Tp>::type;
1083
1084 private:
1085 // Constraint for taking ownership of a pointer of type _Yp*:
1086 template<typename _Yp>
1087 using _SafeConv
1088 = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1089
1090 // Constraint for construction from shared_ptr and weak_ptr:
1091 template<typename _Yp, typename _Res = void>
1092 using _Compatible = typename
1093 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1094
1095 // Constraint for assignment from shared_ptr and weak_ptr:
1096 template<typename _Yp>
1097 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1098
1099 // Constraint for construction from unique_ptr:
1100 template<typename _Yp, typename _Del, typename _Res = void,
1101 typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
1102 using _UniqCompatible = typename enable_if<__and_<
1103 __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
1104 >::value, _Res>::type;
1105
1106 // Constraint for assignment from unique_ptr:
1107 template<typename _Yp, typename _Del>
1108 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1109
1110 public:
1111
1112#if __cplusplus201103L > 201402L
1113 using weak_type = __weak_ptr<_Tp, _Lp>;
1114#endif
1115
1116 constexpr __shared_ptr() noexcept
1117 : _M_ptr(0), _M_refcount()
1118 { }
1119
1120 template<typename _Yp, typename = _SafeConv<_Yp>>
1121 explicit
1122 __shared_ptr(_Yp* __p)
1123 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1124 {
1125 static_assert( !is_void<_Yp>::value, "incomplete type" );
1126 static_assert( sizeof(_Yp) > 0, "incomplete type" );
1127 _M_enable_shared_from_this_with(__p);
1128 }
1129
1130 template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
1131 __shared_ptr(_Yp* __p, _Deleter __d)
1132 : _M_ptr(__p), _M_refcount(__p, std::move(__d))
1133 {
1134 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1135 "deleter expression d(p) is well-formed");
1136 _M_enable_shared_from_this_with(__p);
1137 }
1138
1139 template<typename _Yp, typename _Deleter, typename _Alloc,
1140 typename = _SafeConv<_Yp>>
1141 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1142 : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a))
1143 {
1144 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1145 "deleter expression d(p) is well-formed");
1146 _M_enable_shared_from_this_with(__p);
1147 }
1148
1149 template<typename _Deleter>
1150 __shared_ptr(nullptr_t __p, _Deleter __d)
1151 : _M_ptr(0), _M_refcount(__p, std::move(__d))
1152 { }
1153
1154 template<typename _Deleter, typename _Alloc>
1155 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1156 : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a))
1157 { }
1158
1159 template<typename _Yp>
1160 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
1161 element_type* __p) noexcept
1162 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
1163 { }
1164
1165 __shared_ptr(const __shared_ptr&) noexcept = default;
1166 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
10
Calling copy assignment operator for '__shared_count'
23
Returning; memory was released
30
Calling copy assignment operator for '__shared_count'
1167 ~__shared_ptr() = default;
1168
1169 template<typename _Yp, typename = _Compatible<_Yp>>
1170 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1171 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1172 { }
1173
1174 __shared_ptr(__shared_ptr&& __r) noexcept
1175 : _M_ptr(__r._M_ptr), _M_refcount()
1176 {
1177 _M_refcount._M_swap(__r._M_refcount);
1178 __r._M_ptr = 0;
1179 }
1180
1181 template<typename _Yp, typename = _Compatible<_Yp>>
1182 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1183 : _M_ptr(__r._M_ptr), _M_refcount()
1184 {
1185 _M_refcount._M_swap(__r._M_refcount);
1186 __r._M_ptr = 0;
1187 }
1188
1189 template<typename _Yp, typename = _Compatible<_Yp>>
1190 explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
1191 : _M_refcount(__r._M_refcount) // may throw
1192 {
1193 // It is now safe to copy __r._M_ptr, as
1194 // _M_refcount(__r._M_refcount) did not throw.
1195 _M_ptr = __r._M_ptr;
1196 }
1197
1198 // If an exception is thrown this constructor has no effect.
1199 template<typename _Yp, typename _Del,
1200 typename = _UniqCompatible<_Yp, _Del>>
1201 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1202 : _M_ptr(__r.get()), _M_refcount()
1203 {
1204 auto __raw = __to_address(__r.get());
1205 _M_refcount = __shared_count<_Lp>(std::move(__r));
1206 _M_enable_shared_from_this_with(__raw);
1207 }
1208
1209#if __cplusplus201103L <= 201402L && _GLIBCXX_USE_DEPRECATED1
1210 protected:
1211 // If an exception is thrown this constructor has no effect.
1212 template<typename _Tp1, typename _Del,
1213 typename enable_if<__and_<
1214 __not_<is_array<_Tp>>, is_array<_Tp1>,
1215 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1216 >::value, bool>::type = true>
1217 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1218 : _M_ptr(__r.get()), _M_refcount()
1219 {
1220 auto __raw = __to_address(__r.get());
1221 _M_refcount = __shared_count<_Lp>(std::move(__r));
1222 _M_enable_shared_from_this_with(__raw);
1223 }
1224 public:
1225#endif
1226
1227#if _GLIBCXX_USE_DEPRECATED1
1228#pragma GCC diagnostic push
1229#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1230 // Postcondition: use_count() == 1 and __r.get() == 0
1231 template<typename _Yp, typename = _Compatible<_Yp>>
1232 __shared_ptr(auto_ptr<_Yp>&& __r);
1233#pragma GCC diagnostic pop
1234#endif
1235
1236 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1237
1238 template<typename _Yp>
1239 _Assignable<_Yp>
1240 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1241 {
1242 _M_ptr = __r._M_ptr;
1243 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
1244 return *this;
1245 }
1246
1247#if _GLIBCXX_USE_DEPRECATED1
1248#pragma GCC diagnostic push
1249#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1250 template<typename _Yp>
1251 _Assignable<_Yp>
1252 operator=(auto_ptr<_Yp>&& __r)
1253 {
1254 __shared_ptr(std::move(__r)).swap(*this);
1255 return *this;
1256 }
1257#pragma GCC diagnostic pop
1258#endif
1259
1260 __shared_ptr&
1261 operator=(__shared_ptr&& __r) noexcept
1262 {
1263 __shared_ptr(std::move(__r)).swap(*this);
1264 return *this;
1265 }
1266
1267 template<class _Yp>
1268 _Assignable<_Yp>
1269 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1270 {
1271 __shared_ptr(std::move(__r)).swap(*this);
1272 return *this;
1273 }
1274
1275 template<typename _Yp, typename _Del>
1276 _UniqAssignable<_Yp, _Del>
1277 operator=(unique_ptr<_Yp, _Del>&& __r)
1278 {
1279 __shared_ptr(std::move(__r)).swap(*this);
1280 return *this;
1281 }
1282
1283 void
1284 reset() noexcept
1285 { __shared_ptr().swap(*this); }
1286
1287 template<typename _Yp>
1288 _SafeConv<_Yp>
1289 reset(_Yp* __p) // _Yp must be complete.
1290 {
1291 // Catch self-reset errors.
1292 __glibcxx_assert(__p == 0 || __p != _M_ptr);
1293 __shared_ptr(__p).swap(*this);
1294 }
1295
1296 template<typename _Yp, typename _Deleter>
1297 _SafeConv<_Yp>
1298 reset(_Yp* __p, _Deleter __d)
1299 { __shared_ptr(__p, std::move(__d)).swap(*this); }
1300
1301 template<typename _Yp, typename _Deleter, typename _Alloc>
1302 _SafeConv<_Yp>
1303 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1304 { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); }
1305
1306 element_type*
1307 get() const noexcept
1308 { return _M_ptr; }
1309
1310 explicit operator bool() const // never throws
1311 { return _M_ptr == 0 ? false : true; }
1312
1313 bool
1314 unique() const noexcept
1315 { return _M_refcount._M_unique(); }
1316
1317 long
1318 use_count() const noexcept
1319 { return _M_refcount._M_get_use_count(); }
1320
1321 void
1322 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1323 {
1324 std::swap(_M_ptr, __other._M_ptr);
1325 _M_refcount._M_swap(__other._M_refcount);
1326 }
1327
1328 template<typename _Tp1>
1329 bool
1330 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1331 { return _M_refcount._M_less(__rhs._M_refcount); }
1332
1333 template<typename _Tp1>
1334 bool
1335 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1336 { return _M_refcount._M_less(__rhs._M_refcount); }
1337
1338 protected:
1339 // This constructor is non-standard, it is used by allocate_shared.
1340 template<typename _Alloc, typename... _Args>
1341 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1342 : _M_ptr(), _M_refcount(_M_ptr, __tag, std::forward<_Args>(__args)...)
1343 { _M_enable_shared_from_this_with(_M_ptr); }
1344
1345 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1346 typename... _Args>
1347 friend __shared_ptr<_Tp1, _Lp1>
1348 __allocate_shared(const _Alloc& __a, _Args&&... __args);
1349
1350 // This constructor is used by __weak_ptr::lock() and
1351 // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
1352 __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1353 : _M_refcount(__r._M_refcount, std::nothrow)
1354 {
1355 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
1356 }
1357
1358 friend class __weak_ptr<_Tp, _Lp>;
1359
1360 private:
1361
1362 template<typename _Yp>
1363 using __esft_base_t = decltype(__enable_shared_from_this_base(
1364 std::declval<const __shared_count<_Lp>&>(),
1365 std::declval<_Yp*>()));
1366
1367 // Detect an accessible and unambiguous enable_shared_from_this base.
1368 template<typename _Yp, typename = void>
1369 struct __has_esft_base
1370 : false_type { };
1371
1372 template<typename _Yp>
1373 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1374 : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
1375
1376 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1377 typename enable_if<__has_esft_base<_Yp2>::value>::type
1378 _M_enable_shared_from_this_with(_Yp* __p) noexcept
1379 {
1380 if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1381 __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
1382 }
1383
1384 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1385 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1386 _M_enable_shared_from_this_with(_Yp*) noexcept
1387 { }
1388
1389 void*
1390 _M_get_deleter(const std::type_info& __ti) const noexcept
1391 { return _M_refcount._M_get_deleter(__ti); }
1392
1393 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1394 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1395
1396 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1397 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1398
1399 template<typename _Del, typename _Tp1>
1400 friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept;
1401
1402 element_type* _M_ptr; // Contained pointer.
1403 __shared_count<_Lp> _M_refcount; // Reference counter.
1404 };
1405
1406
1407 // 20.7.2.2.7 shared_ptr comparisons
1408 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1409 inline bool
1410 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1411 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1412 { return __a.get() == __b.get(); }
1413
1414 template<typename _Tp, _Lock_policy _Lp>
1415 inline bool
1416 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1417 { return !__a; }
1418
1419 template<typename _Tp, _Lock_policy _Lp>
1420 inline bool
1421 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1422 { return !__a; }
1423
1424 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1425 inline bool
1426 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1427 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1428 { return __a.get() != __b.get(); }
1429
1430 template<typename _Tp, _Lock_policy _Lp>
1431 inline bool
1432 operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1433 { return (bool)__a; }
1434
1435 template<typename _Tp, _Lock_policy _Lp>
1436 inline bool
1437 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1438 { return (bool)__a; }
1439
1440 template<typename _Tp, typename _Up, _Lock_policy _Lp>
1441 inline bool
1442 operator<(const __shared_ptr<_Tp, _Lp>& __a,
1443 const __shared_ptr<_Up, _Lp>& __b) noexcept
1444 {
1445 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1446 using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
1447 using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
1448 return less<_Vp>()(__a.get(), __b.get());
1449 }
1450
1451 template<typename _Tp, _Lock_policy _Lp>
1452 inline bool
1453 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1454 {
1455 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1456 return less<_Tp_elt*>()(__a.get(), nullptr);
1457 }
1458
1459 template<typename _Tp, _Lock_policy _Lp>
1460 inline bool
1461 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1462 {
1463 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1464 return less<_Tp_elt*>()(nullptr, __a.get());
1465 }
1466
1467 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1468 inline bool
1469 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1470 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1471 { return !(__b < __a); }
1472
1473 template<typename _Tp, _Lock_policy _Lp>
1474 inline bool
1475 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1476 { return !(nullptr < __a); }
1477
1478 template<typename _Tp, _Lock_policy _Lp>
1479 inline bool
1480 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1481 { return !(__a < nullptr); }
1482
1483 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1484 inline bool
1485 operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1486 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1487 { return (__b < __a); }
1488
1489 template<typename _Tp, _Lock_policy _Lp>
1490 inline bool
1491 operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1492 { return nullptr < __a; }
1493
1494 template<typename _Tp, _Lock_policy _Lp>
1495 inline bool
1496 operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1497 { return __a < nullptr; }
1498
1499 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1500 inline bool
1501 operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1502 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1503 { return !(__a < __b); }
1504
1505 template<typename _Tp, _Lock_policy _Lp>
1506 inline bool
1507 operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1508 { return !(__a < nullptr); }
1509
1510 template<typename _Tp, _Lock_policy _Lp>
1511 inline bool
1512 operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1513 { return !(nullptr < __a); }
1514
1515 template<typename _Sp>
1516 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
1517 {
1518 bool
1519 operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept
1520 {
1521 typedef typename _Sp::element_type element_type;
1522 return std::less<element_type*>()(__lhs.get(), __rhs.get());
1523 }
1524 };
1525
1526 template<typename _Tp, _Lock_policy _Lp>
1527 struct less<__shared_ptr<_Tp, _Lp>>
1528 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
1529 { };
1530
1531 // 20.7.2.2.8 shared_ptr specialized algorithms.
1532 template<typename _Tp, _Lock_policy _Lp>
1533 inline void
1534 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1535 { __a.swap(__b); }
1536
1537 // 20.7.2.2.9 shared_ptr casts
1538
1539 // The seemingly equivalent code:
1540 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1541 // will eventually result in undefined behaviour, attempting to
1542 // delete the same object twice.
1543 /// static_pointer_cast
1544 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1545 inline __shared_ptr<_Tp, _Lp>
1546 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1547 {
1548 using _Sp = __shared_ptr<_Tp, _Lp>;
1549 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
1550 }
1551
1552 // The seemingly equivalent code:
1553 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1554 // will eventually result in undefined behaviour, attempting to
1555 // delete the same object twice.
1556 /// const_pointer_cast
1557 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1558 inline __shared_ptr<_Tp, _Lp>
1559 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1560 {
1561 using _Sp = __shared_ptr<_Tp, _Lp>;
1562 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
1563 }
1564
1565 // The seemingly equivalent code:
1566 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1567 // will eventually result in undefined behaviour, attempting to
1568 // delete the same object twice.
1569 /// dynamic_pointer_cast
1570 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1571 inline __shared_ptr<_Tp, _Lp>
1572 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1573 {
1574 using _Sp = __shared_ptr<_Tp, _Lp>;
1575 if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
1576 return _Sp(__r, __p);
1577 return _Sp();
1578 }
1579
1580#if __cplusplus201103L > 201402L
1581 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1582 inline __shared_ptr<_Tp, _Lp>
1583 reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1584 {
1585 using _Sp = __shared_ptr<_Tp, _Lp>;
1586 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
1587 }
1588#endif
1589
1590 template<typename _Tp, _Lock_policy _Lp>
1591 class __weak_ptr
1592 {
1593 template<typename _Yp, typename _Res = void>
1594 using _Compatible = typename
1595 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1596
1597 // Constraint for assignment from shared_ptr and weak_ptr:
1598 template<typename _Yp>
1599 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1600
1601 public:
1602 using element_type = typename remove_extent<_Tp>::type;
1603
1604 constexpr __weak_ptr() noexcept
1605 : _M_ptr(nullptr), _M_refcount()
1606 { }
1607
1608 __weak_ptr(const __weak_ptr&) noexcept = default;
1609
1610 ~__weak_ptr() = default;
1611
1612 // The "obvious" converting constructor implementation:
1613 //
1614 // template<typename _Tp1>
1615 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1616 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1617 // { }
1618 //
1619 // has a serious problem.
1620 //
1621 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1622 // conversion may require access to *__r._M_ptr (virtual inheritance).
1623 //
1624 // It is not possible to avoid spurious access violations since
1625 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1626 template<typename _Yp, typename = _Compatible<_Yp>>
1627 __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
1628 : _M_refcount(__r._M_refcount)
1629 { _M_ptr = __r.lock().get(); }
1630
1631 template<typename _Yp, typename = _Compatible<_Yp>>
1632 __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1633 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1634 { }
1635
1636 __weak_ptr(__weak_ptr&& __r) noexcept
1637 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1638 { __r._M_ptr = nullptr; }
1639
1640 template<typename _Yp, typename = _Compatible<_Yp>>
1641 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1642 : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1643 { __r._M_ptr = nullptr; }
1644
1645 __weak_ptr&
1646 operator=(const __weak_ptr& __r) noexcept = default;
1647
1648 template<typename _Yp>
1649 _Assignable<_Yp>
1650 operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
1651 {
1652 _M_ptr = __r.lock().get();
1653 _M_refcount = __r._M_refcount;
1654 return *this;
1655 }
1656
1657 template<typename _Yp>
1658 _Assignable<_Yp>
1659 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1660 {
1661 _M_ptr = __r._M_ptr;
1662 _M_refcount = __r._M_refcount;
1663 return *this;
1664 }
1665
1666 __weak_ptr&
1667 operator=(__weak_ptr&& __r) noexcept
1668 {
1669 _M_ptr = __r._M_ptr;
1670 _M_refcount = std::move(__r._M_refcount);
1671 __r._M_ptr = nullptr;
1672 return *this;
1673 }
1674
1675 template<typename _Yp>
1676 _Assignable<_Yp>
1677 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1678 {
1679 _M_ptr = __r.lock().get();
1680 _M_refcount = std::move(__r._M_refcount);
1681 __r._M_ptr = nullptr;
1682 return *this;
1683 }
1684
1685 __shared_ptr<_Tp, _Lp>
1686 lock() const noexcept
1687 { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); }
1688
1689 long
1690 use_count() const noexcept
1691 { return _M_refcount._M_get_use_count(); }
1692
1693 bool
1694 expired() const noexcept
1695 { return _M_refcount._M_get_use_count() == 0; }
1696
1697 template<typename _Tp1>
1698 bool
1699 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept
1700 { return _M_refcount._M_less(__rhs._M_refcount); }
1701
1702 template<typename _Tp1>
1703 bool
1704 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept
1705 { return _M_refcount._M_less(__rhs._M_refcount); }
1706
1707 void
1708 reset() noexcept
1709 { __weak_ptr().swap(*this); }
1710
1711 void
1712 swap(__weak_ptr& __s) noexcept
1713 {
1714 std::swap(_M_ptr, __s._M_ptr);
1715 _M_refcount._M_swap(__s._M_refcount);
1716 }
1717
1718 private:
1719 // Used by __enable_shared_from_this.
1720 void
1721 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
1722 {
1723 if (use_count() == 0)
1724 {
1725 _M_ptr = __ptr;
1726 _M_refcount = __refcount;
1727 }
1728 }
1729
1730 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1731 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1732 friend class __enable_shared_from_this<_Tp, _Lp>;
1733 friend class enable_shared_from_this<_Tp>;
1734
1735 element_type* _M_ptr; // Contained pointer.
1736 __weak_count<_Lp> _M_refcount; // Reference counter.
1737 };
1738
1739 // 20.7.2.3.6 weak_ptr specialized algorithms.
1740 template<typename _Tp, _Lock_policy _Lp>
1741 inline void
1742 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1743 { __a.swap(__b); }
1744
1745 template<typename _Tp, typename _Tp1>
1746 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1747 {
1748 bool
1749 operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept
1750 { return __lhs.owner_before(__rhs); }
1751
1752 bool
1753 operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept
1754 { return __lhs.owner_before(__rhs); }
1755
1756 bool
1757 operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept
1758 { return __lhs.owner_before(__rhs); }
1759 };
1760
1761 template<>
1762 struct _Sp_owner_less<void, void>
1763 {
1764 template<typename _Tp, typename _Up>
1765 auto
1766 operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept
1767 -> decltype(__lhs.owner_before(__rhs))
1768 { return __lhs.owner_before(__rhs); }
1769
1770 using is_transparent = void;
1771 };
1772
1773 template<typename _Tp, _Lock_policy _Lp>
1774 struct owner_less<__shared_ptr<_Tp, _Lp>>
1775 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1776 { };
1777
1778 template<typename _Tp, _Lock_policy _Lp>
1779 struct owner_less<__weak_ptr<_Tp, _Lp>>
1780 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1781 { };
1782
1783
1784 template<typename _Tp, _Lock_policy _Lp>
1785 class __enable_shared_from_this
1786 {
1787 protected:
1788 constexpr __enable_shared_from_this() noexcept { }
1789
1790 __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
1791
1792 __enable_shared_from_this&
1793 operator=(const __enable_shared_from_this&) noexcept
1794 { return *this; }
1795
1796 ~__enable_shared_from_this() { }
1797
1798 public:
1799 __shared_ptr<_Tp, _Lp>
1800 shared_from_this()
1801 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1802
1803 __shared_ptr<const _Tp, _Lp>
1804 shared_from_this() const
1805 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1806
1807#if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11
1808 __weak_ptr<_Tp, _Lp>
1809 weak_from_this() noexcept
1810 { return this->_M_weak_this; }
1811
1812 __weak_ptr<const _Tp, _Lp>
1813 weak_from_this() const noexcept
1814 { return this->_M_weak_this; }
1815#endif
1816
1817 private:
1818 template<typename _Tp1>
1819 void
1820 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
1821 { _M_weak_this._M_assign(__p, __n); }
1822
1823 friend const __enable_shared_from_this*
1824 __enable_shared_from_this_base(const __shared_count<_Lp>&,
1825 const __enable_shared_from_this* __p)
1826 { return __p; }
1827
1828 template<typename, _Lock_policy>
1829 friend class __shared_ptr;
1830
1831 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1832 };
1833
1834 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1835 inline __shared_ptr<_Tp, _Lp>
1836 __allocate_shared(const _Alloc& __a, _Args&&... __args)
1837 {
1838 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
1839 std::forward<_Args>(__args)...);
1840 }
1841
1842 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1843 inline __shared_ptr<_Tp, _Lp>
1844 __make_shared(_Args&&... __args)
1845 {
1846 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1847 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1848 std::forward<_Args>(__args)...);
1849 }
1850
1851 /// std::hash specialization for __shared_ptr.
1852 template<typename _Tp, _Lock_policy _Lp>
1853 struct hash<__shared_ptr<_Tp, _Lp>>
1854 : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1855 {
1856 size_t
1857 operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
1858 {
1859 return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()(
1860 __s.get());
1861 }
1862 };
1863
1864_GLIBCXX_END_NAMESPACE_VERSION
1865} // namespace
1866
1867#endif // _SHARED_PTR_BASE_H

/usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/ext/atomicity.h

1// Support for atomic operations -*- C++ -*-
2
3// Copyright (C) 2004-2018 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// 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.
19
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/>.
24
25/** @file ext/atomicity.h
26 * This file is a GNU extension to the Standard C++ Library.
27 */
28
29#ifndef _GLIBCXX_ATOMICITY_H1
30#define _GLIBCXX_ATOMICITY_H1 1
31
32#pragma GCC system_header
33
34#include <bits/c++config.h>
35#include <bits/gthr.h>
36#include <bits/atomic_word.h>
37
38namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
39{
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
41
42 // Functions for portable atomic access.
43 // To abstract locking primitives across all thread policies, use:
44 // __exchange_and_add_dispatch
45 // __atomic_add_dispatch
46#ifdef _GLIBCXX_ATOMIC_BUILTINS1
47 static inline _Atomic_word
48 __exchange_and_add(volatile _Atomic_word* __mem, int __val)
49 { return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL4); }
50
51 static inline void
52 __atomic_add(volatile _Atomic_word* __mem, int __val)
53 { __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL4); }
54#else
55 _Atomic_word
56 __attribute__ ((__unused__))
57 __exchange_and_add(volatile _Atomic_word*, int) throw ();
58
59 void
60 __attribute__ ((__unused__))
61 __atomic_add(volatile _Atomic_word*, int) throw ();
62#endif
63
64 static inline _Atomic_word
65 __exchange_and_add_single(_Atomic_word* __mem, int __val)
66 {
67 _Atomic_word __result = *__mem;
40
Use of memory after it is freed
68 *__mem += __val;
69 return __result;
70 }
71
72 static inline void
73 __atomic_add_single(_Atomic_word* __mem, int __val)
74 { *__mem += __val; }
75
76 static inline _Atomic_word
77 __attribute__ ((__unused__))
78 __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
79 {
80#ifdef __GTHREADS1
81 if (__gthread_active_p())
37
Assuming the condition is false
38
Taking false branch
82 return __exchange_and_add(__mem, __val);
83 else
84 return __exchange_and_add_single(__mem, __val);
39
Calling '__exchange_and_add_single'
85#else
86 return __exchange_and_add_single(__mem, __val);
87#endif
88 }
89
90 static inline void
91 __attribute__ ((__unused__))
92 __atomic_add_dispatch(_Atomic_word* __mem, int __val)
93 {
94#ifdef __GTHREADS1
95 if (__gthread_active_p())
96 __atomic_add(__mem, __val);
97 else
98 __atomic_add_single(__mem, __val);
99#else
100 __atomic_add_single(__mem, __val);
101#endif
102 }
103
104_GLIBCXX_END_NAMESPACE_VERSION
105} // namespace
106
107// Even if the CPU doesn't need a memory barrier, we need to ensure
108// that the compiler doesn't reorder memory accesses across the
109// barriers.
110#ifndef _GLIBCXX_READ_MEM_BARRIER__atomic_thread_fence (2)
111#define _GLIBCXX_READ_MEM_BARRIER__atomic_thread_fence (2) __atomic_thread_fence (__ATOMIC_ACQUIRE2)
112#endif
113#ifndef _GLIBCXX_WRITE_MEM_BARRIER__atomic_thread_fence (3)
114#define _GLIBCXX_WRITE_MEM_BARRIER__atomic_thread_fence (3) __atomic_thread_fence (__ATOMIC_RELEASE3)
115#endif
116
117#endif