65
65
#include < __utility/is_pointer_in_range.h>
66
66
#include < __utility/move.h>
67
67
#include < __utility/pair.h>
68
+ #include < __utility/scope_guard.h>
68
69
#include < __utility/swap.h>
69
70
#include < initializer_list>
70
71
#include < limits>
@@ -479,9 +480,10 @@ class vector {
479
480
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __emplace_back_assume_capacity (_Args&&... __args) {
480
481
_LIBCPP_ASSERT_INTERNAL (
481
482
size () < capacity (), " We assume that we have enough space to insert an element at the end of the vector" );
482
- _ConstructTransaction __tx (*this , 1 );
483
- __alloc_traits::construct (this ->__alloc_ , std::__to_address (__tx.__pos_ ), std::forward<_Args>(__args)...);
484
- ++__tx.__pos_ ;
483
+ __annotate_delete ();
484
+ auto __guard = std::__make_scope_guard (__annotate_new_size (*this ));
485
+ __alloc_traits::construct (this ->__alloc_ , std::__to_address (__end_), std::forward<_Args>(__args)...);
486
+ ++__end_;
485
487
}
486
488
487
489
#if _LIBCPP_STD_VER >= 23
@@ -548,9 +550,9 @@ class vector {
548
550
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase (const_iterator __first, const_iterator __last);
549
551
550
552
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear () _NOEXCEPT {
551
- size_type __old_size = size ();
553
+ __annotate_delete ();
554
+ auto __guard = std::__make_scope_guard (__annotate_new_size (*this ));
552
555
__base_destruct_at_end (this ->__begin_ );
553
- __annotate_shrink (__old_size);
554
556
}
555
557
556
558
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize (size_type __sz);
@@ -708,9 +710,9 @@ class vector {
708
710
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign (vector& __c, false_type)
709
711
_NOEXCEPT_(__alloc_traits::is_always_equal::value);
710
712
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end (pointer __new_last) _NOEXCEPT {
711
- size_type __old_size = size ();
713
+ __annotate_delete ();
714
+ auto __guard = std::__make_scope_guard (__annotate_new_size (*this ));
712
715
__base_destruct_at_end (__new_last);
713
- __annotate_shrink (__old_size);
714
716
}
715
717
716
718
template <class ... _Args>
@@ -737,33 +739,11 @@ class vector {
737
739
__annotate_contiguous_container (data () + size (), data () + capacity ());
738
740
}
739
741
740
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_increase (size_type __n) const _NOEXCEPT {
741
- __annotate_contiguous_container (data () + size (), data () + size () + __n);
742
- }
743
-
744
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink (size_type __old_size) const _NOEXCEPT {
745
- __annotate_contiguous_container (data () + __old_size, data () + size ());
746
- }
747
-
748
- struct _ConstructTransaction {
749
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction (vector& __v, size_type __n)
750
- : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
751
- __v_.__annotate_increase (__n);
752
- }
753
-
754
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction () {
755
- __v_.__end_ = __pos_;
756
- if (__pos_ != __new_end_) {
757
- __v_.__annotate_shrink (__new_end_ - __v_.__begin_ );
758
- }
759
- }
760
-
761
- vector& __v_;
762
- pointer __pos_;
763
- const_pointer const __new_end_;
742
+ struct __annotate_new_size {
743
+ vector& __vec_;
764
744
765
- _ConstructTransaction (_ConstructTransaction const &) = delete ;
766
- _ConstructTransaction& operator =(_ConstructTransaction const &) = delete ;
745
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __annotate_new_size (vector& __vec) : __vec_(__vec) {}
746
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator ()() { __vec_. __annotate_new (__vec_. size ()); }
767
747
};
768
748
769
749
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __base_destruct_at_end (pointer __new_last) _NOEXCEPT {
@@ -850,6 +830,7 @@ template <class _Tp, class _Allocator>
850
830
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
851
831
vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v) {
852
832
__annotate_delete ();
833
+ auto __guard = std::__make_scope_guard (__annotate_new_size (*this ));
853
834
auto __new_begin = __v.__begin_ - (__end_ - __begin_);
854
835
std::__uninitialized_allocator_relocate (
855
836
this ->__alloc_ , std::__to_address (__begin_), std::__to_address (__end_), std::__to_address (__new_begin));
@@ -859,7 +840,6 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
859
840
std::swap (this ->__end_ , __v.__end_ );
860
841
std::swap (this ->__cap_ , __v.__cap_ );
861
842
__v.__first_ = __v.__begin_ ;
862
- __annotate_new (size ());
863
843
}
864
844
865
845
// __swap_out_circular_buffer relocates the objects in [__begin_, __p) into the front of __v, the objects in
@@ -870,6 +850,7 @@ template <class _Tp, class _Allocator>
870
850
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
871
851
vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p) {
872
852
__annotate_delete ();
853
+ auto __guard = std::__make_scope_guard (__annotate_new_size (size ()));
873
854
pointer __ret = __v.__begin_ ;
874
855
875
856
// Relocate [__p, __end_) first to avoid having a hole in [__begin_, __end_)
@@ -889,7 +870,6 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
889
870
std::swap (this ->__end_ , __v.__end_ );
890
871
std::swap (this ->__cap_ , __v.__cap_ );
891
872
__v.__first_ = __v.__begin_ ;
892
- __annotate_new (size ());
893
873
return __ret;
894
874
}
895
875
@@ -923,10 +903,12 @@ vector<_Tp, _Allocator>::__recommend(size_type __new_size) const {
923
903
// Postcondition: size() == size() + __n
924
904
template <class _Tp , class _Allocator >
925
905
_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(size_type __n) {
926
- _ConstructTransaction __tx (*this , __n);
927
- const_pointer __new_end = __tx.__new_end_ ;
928
- for (pointer __pos = __tx.__pos_ ; __pos != __new_end; __tx.__pos_ = ++__pos) {
929
- __alloc_traits::construct (this ->__alloc_ , std::__to_address (__pos));
906
+ __annotate_delete ();
907
+ auto __guard = std::__make_scope_guard (__annotate_new_size (*this ));
908
+
909
+ for (size_t __i = 0 ; __i != __n; ++__i) {
910
+ __alloc_traits::construct (this ->__alloc_ , std::__to_address (__end_));
911
+ ++__end_;
930
912
}
931
913
}
932
914
@@ -939,19 +921,21 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(s
939
921
template <class _Tp , class _Allocator >
940
922
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
941
923
vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
942
- _ConstructTransaction __tx (*this , __n);
943
- const_pointer __new_end = __tx.__new_end_ ;
944
- for (pointer __pos = __tx.__pos_ ; __pos != __new_end; __tx.__pos_ = ++__pos) {
945
- __alloc_traits::construct (this ->__alloc_ , std::__to_address (__pos), __x);
924
+ __annotate_delete ();
925
+ auto __guard = std::__make_scope_guard (__annotate_new_size (*this ));
926
+
927
+ for (size_t __i = 0 ; __i != __n; ++__i) {
928
+ __alloc_traits::construct (this ->__alloc_ , std::__to_address (__end_), __x);
929
+ ++__end_;
946
930
}
947
931
}
948
932
949
933
template <class _Tp , class _Allocator >
950
934
template <class _InputIterator , class _Sentinel >
951
935
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
952
936
vector<_Tp, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
953
- _ConstructTransaction __tx ( *this , __n );
954
- __tx. __pos_ = std::__uninitialized_allocator_copy (this ->__alloc_ , std::move (__first), std::move (__last), __tx. __pos_ );
937
+ auto __guard = std::__make_scope_guard ( __annotate_new_size ( *this ) );
938
+ __end_ = std::__uninitialized_allocator_copy (this ->__alloc_ , std::move (__first), std::move (__last), __end_ );
955
939
}
956
940
957
941
// Default constructs __n objects starting at __end_
@@ -1189,15 +1173,16 @@ vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last) {
1189
1173
template <class _Tp , class _Allocator >
1190
1174
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
1191
1175
vector<_Tp, _Allocator>::__move_range (pointer __from_s, pointer __from_e, pointer __to) {
1176
+ __annotate_delete ();
1177
+ auto __guard = std::__make_scope_guard (__annotate_new_size (*this ));
1192
1178
pointer __old_last = this ->__end_ ;
1193
1179
difference_type __n = __old_last - __to;
1194
- {
1195
- pointer __i = __from_s + __n;
1196
- _ConstructTransaction __tx (*this , __from_e - __i);
1197
- for (pointer __pos = __tx.__pos_ ; __i < __from_e; ++__i, (void )++__pos, __tx.__pos_ = __pos) {
1198
- __alloc_traits::construct (this ->__alloc_ , std::__to_address (__pos), std::move (*__i));
1199
- }
1180
+
1181
+ for (pointer __i = __from_s + __n; __i != __from_e; ++__i) {
1182
+ __alloc_traits::construct (this ->__alloc_ , std::__to_address (__end_), std::move (*__i));
1183
+ ++__end_;
1200
1184
}
1185
+
1201
1186
std::move_backward (__from_s, __from_s + __n, __old_last);
1202
1187
}
1203
1188
0 commit comments