Skip to content

Commit 9277fc1

Browse files
committed
[libc++] Refactor vector's ASan annotations to only ever delete and add the annotations
1 parent 900d20d commit 9277fc1

File tree

1 file changed

+36
-51
lines changed

1 file changed

+36
-51
lines changed

libcxx/include/__vector/vector.h

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include <__utility/is_pointer_in_range.h>
6666
#include <__utility/move.h>
6767
#include <__utility/pair.h>
68+
#include <__utility/scope_guard.h>
6869
#include <__utility/swap.h>
6970
#include <initializer_list>
7071
#include <limits>
@@ -479,9 +480,10 @@ class vector {
479480
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __emplace_back_assume_capacity(_Args&&... __args) {
480481
_LIBCPP_ASSERT_INTERNAL(
481482
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_;
485487
}
486488

487489
#if _LIBCPP_STD_VER >= 23
@@ -548,9 +550,9 @@ class vector {
548550
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last);
549551

550552
_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));
552555
__base_destruct_at_end(this->__begin_);
553-
__annotate_shrink(__old_size);
554556
}
555557

556558
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz);
@@ -708,9 +710,9 @@ class vector {
708710
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign(vector& __c, false_type)
709711
_NOEXCEPT_(__alloc_traits::is_always_equal::value);
710712
_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));
712715
__base_destruct_at_end(__new_last);
713-
__annotate_shrink(__old_size);
714716
}
715717

716718
template <class... _Args>
@@ -737,33 +739,11 @@ class vector {
737739
__annotate_contiguous_container(data() + size(), data() + capacity());
738740
}
739741

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_;
764744

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()); }
767747
};
768748

769749
_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>
850830
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
851831
vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v) {
852832
__annotate_delete();
833+
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
853834
auto __new_begin = __v.__begin_ - (__end_ - __begin_);
854835
std::__uninitialized_allocator_relocate(
855836
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
859840
std::swap(this->__end_, __v.__end_);
860841
std::swap(this->__cap_, __v.__cap_);
861842
__v.__first_ = __v.__begin_;
862-
__annotate_new(size());
863843
}
864844

865845
// __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>
870850
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
871851
vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p) {
872852
__annotate_delete();
853+
auto __guard = std::__make_scope_guard(__annotate_new_size(size()));
873854
pointer __ret = __v.__begin_;
874855

875856
// 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
889870
std::swap(this->__end_, __v.__end_);
890871
std::swap(this->__cap_, __v.__cap_);
891872
__v.__first_ = __v.__begin_;
892-
__annotate_new(size());
893873
return __ret;
894874
}
895875

@@ -923,10 +903,12 @@ vector<_Tp, _Allocator>::__recommend(size_type __new_size) const {
923903
// Postcondition: size() == size() + __n
924904
template <class _Tp, class _Allocator>
925905
_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_;
930912
}
931913
}
932914

@@ -939,19 +921,21 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(s
939921
template <class _Tp, class _Allocator>
940922
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
941923
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_;
946930
}
947931
}
948932

949933
template <class _Tp, class _Allocator>
950934
template <class _InputIterator, class _Sentinel>
951935
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
952936
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_);
955939
}
956940

957941
// Default constructs __n objects starting at __end_
@@ -1189,15 +1173,16 @@ vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last) {
11891173
template <class _Tp, class _Allocator>
11901174
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
11911175
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));
11921178
pointer __old_last = this->__end_;
11931179
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_;
12001184
}
1185+
12011186
std::move_backward(__from_s, __from_s + __n, __old_last);
12021187
}
12031188

0 commit comments

Comments
 (0)