Skip to content

Commit c7df106

Browse files
authored
Unify naming of internal pointer members in std::vector and std::__split_buffer (#115517)
Related to PR #114423, this PR proposes to unify the naming of the internal pointer members in `std::vector` and `std::__split_buffer` for consistency and clarity. Both `std::vector` and `std::__split_buffer` originally used a `__compressed_pair<pointer, allocator_type>` member named `__end_cap_` to store an internal capacity pointer and an allocator. However, inconsistent naming changes have been made in both classes: - `std::vector` now uses `__cap_` and `__alloc_` for its internal pointer and allocator members. - In contrast, `std::__split_buffer` retains the name `__end_cap_` for the capacity pointer, along with `__alloc_`. This inconsistency between the names `__cap_` and `__end_cap_` has caused confusions (especially to myself when I was working on both classes). I suggest unifying these names by renaming `__end_cap_` to `__cap_` in `std::__split_buffer`.
1 parent 3e20bae commit c7df106

File tree

4 files changed

+52
-51
lines changed

4 files changed

+52
-51
lines changed

libcxx/include/__split_buffer

+42-42
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ _LIBCPP_PUSH_MACROS
4747
_LIBCPP_BEGIN_NAMESPACE_STD
4848

4949
// __split_buffer allocates a contiguous chunk of memory and stores objects in the range [__begin_, __end_).
50-
// It has uninitialized memory in the ranges [__first_, __begin_) and [__end_, __end_cap_.first()). That allows
50+
// It has uninitialized memory in the ranges [__first_, __begin_) and [__end_, __cap_). That allows
5151
// it to grow both in the front and back without having to move the data.
5252

5353
template <class _Tp, class _Allocator = allocator<_Tp> >
@@ -78,20 +78,20 @@ public:
7878
pointer __first_;
7979
pointer __begin_;
8080
pointer __end_;
81-
_LIBCPP_COMPRESSED_PAIR(pointer, __end_cap_, allocator_type, __alloc_);
81+
_LIBCPP_COMPRESSED_PAIR(pointer, __cap_, allocator_type, __alloc_);
8282

8383
__split_buffer(const __split_buffer&) = delete;
8484
__split_buffer& operator=(const __split_buffer&) = delete;
8585

8686
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer()
8787
_NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
88-
: __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr) {}
88+
: __first_(nullptr), __begin_(nullptr), __end_(nullptr), __cap_(nullptr) {}
8989

9090
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(__alloc_rr& __a)
91-
: __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr), __alloc_(__a) {}
91+
: __first_(nullptr), __begin_(nullptr), __end_(nullptr), __cap_(nullptr), __alloc_(__a) {}
9292

9393
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(const __alloc_rr& __a)
94-
: __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr), __alloc_(__a) {}
94+
: __first_(nullptr), __begin_(nullptr), __end_(nullptr), __cap_(nullptr), __alloc_(__a) {}
9595

9696
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
9797
__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
@@ -123,15 +123,15 @@ public:
123123
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }
124124

125125
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
126-
return static_cast<size_type>(__end_cap_ - __first_);
126+
return static_cast<size_type>(__cap_ - __first_);
127127
}
128128

129129
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
130130
return static_cast<size_type>(__begin_ - __first_);
131131
}
132132

133133
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
134-
return static_cast<size_type>(__end_cap_ - __end_);
134+
return static_cast<size_type>(__cap_ - __end_);
135135
}
136136

137137
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *__begin_; }
@@ -215,14 +215,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __split_buffer<_Tp, _Allocator>::__invariants
215215
return false;
216216
if (__end_ != nullptr)
217217
return false;
218-
if (__end_cap_ != nullptr)
218+
if (__cap_ != nullptr)
219219
return false;
220220
} else {
221221
if (__begin_ < __first_)
222222
return false;
223223
if (__end_ < __begin_)
224224
return false;
225-
if (__end_cap_ < __end_)
225+
if (__cap_ < __end_)
226226
return false;
227227
}
228228
return true;
@@ -262,8 +262,8 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void
262262
__split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
263263
__alloc_rr& __a = __alloc_;
264264
for (; __first != __last; ++__first) {
265-
if (__end_ == __end_cap_) {
266-
size_type __old_cap = __end_cap_ - __first_;
265+
if (__end_ == __cap_) {
266+
size_type __old_cap = __cap_ - __first_;
267267
size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
268268
__split_buffer __buf(__new_cap, 0, __a);
269269
for (pointer __p = __begin_; __p != __end_; ++__p, (void)++__buf.__end_)
@@ -320,7 +320,7 @@ __split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type
320320
template <class _Tp, class _Allocator>
321321
_LIBCPP_CONSTEXPR_SINCE_CXX20
322322
__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
323-
: __end_cap_(nullptr), __alloc_(__a) {
323+
: __cap_(nullptr), __alloc_(__a) {
324324
if (__cap == 0) {
325325
__first_ = nullptr;
326326
} else {
@@ -329,7 +329,7 @@ __split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __sta
329329
__cap = __allocation.count;
330330
}
331331
__begin_ = __end_ = __first_ + __start;
332-
__end_cap_ = __first_ + __cap;
332+
__cap_ = __first_ + __cap;
333333
}
334334

335335
template <class _Tp, class _Allocator>
@@ -345,32 +345,32 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::__split_buffer(__
345345
: __first_(std::move(__c.__first_)),
346346
__begin_(std::move(__c.__begin_)),
347347
__end_(std::move(__c.__end_)),
348-
__end_cap_(std::move(__c.__end_cap_)),
348+
__cap_(std::move(__c.__cap_)),
349349
__alloc_(std::move(__c.__alloc_)) {
350-
__c.__first_ = nullptr;
351-
__c.__begin_ = nullptr;
352-
__c.__end_ = nullptr;
353-
__c.__end_cap_ = nullptr;
350+
__c.__first_ = nullptr;
351+
__c.__begin_ = nullptr;
352+
__c.__end_ = nullptr;
353+
__c.__cap_ = nullptr;
354354
}
355355

356356
template <class _Tp, class _Allocator>
357357
_LIBCPP_CONSTEXPR_SINCE_CXX20
358358
__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
359-
: __end_cap_(nullptr), __alloc_(__a) {
359+
: __cap_(nullptr), __alloc_(__a) {
360360
if (__a == __c.__alloc_) {
361-
__first_ = __c.__first_;
362-
__begin_ = __c.__begin_;
363-
__end_ = __c.__end_;
364-
__end_cap_ = __c.__end_cap_;
365-
__c.__first_ = nullptr;
366-
__c.__begin_ = nullptr;
367-
__c.__end_ = nullptr;
368-
__c.__end_cap_ = nullptr;
361+
__first_ = __c.__first_;
362+
__begin_ = __c.__begin_;
363+
__end_ = __c.__end_;
364+
__cap_ = __c.__cap_;
365+
__c.__first_ = nullptr;
366+
__c.__begin_ = nullptr;
367+
__c.__end_ = nullptr;
368+
__c.__cap_ = nullptr;
369369
} else {
370370
auto __allocation = std::__allocate_at_least(__alloc_, __c.size());
371371
__first_ = __allocation.ptr;
372372
__begin_ = __end_ = __first_;
373-
__end_cap_ = __first_ + __allocation.count;
373+
__cap_ = __first_ + __allocation.count;
374374
typedef move_iterator<iterator> _Ip;
375375
__construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
376376
}
@@ -384,12 +384,12 @@ __split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
384384
!__alloc_traits::propagate_on_container_move_assignment::value) {
385385
clear();
386386
shrink_to_fit();
387-
__first_ = __c.__first_;
388-
__begin_ = __c.__begin_;
389-
__end_ = __c.__end_;
390-
__end_cap_ = __c.__end_cap_;
387+
__first_ = __c.__first_;
388+
__begin_ = __c.__begin_;
389+
__end_ = __c.__end_;
390+
__cap_ = __c.__cap_;
391391
__move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
392-
__c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap_ = nullptr;
392+
__c.__first_ = __c.__begin_ = __c.__end_ = __c.__cap_ = nullptr;
393393
return *this;
394394
}
395395

@@ -399,7 +399,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split
399399
std::swap(__first_, __x.__first_);
400400
std::swap(__begin_, __x.__begin_);
401401
std::swap(__end_, __x.__end_);
402-
std::swap(__end_cap_, __x.__end_cap_);
402+
std::swap(__cap_, __x.__cap_);
403403
std::__swap_allocator(__alloc_, __x.__alloc_);
404404
}
405405

@@ -415,7 +415,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fi
415415
std::swap(__first_, __t.__first_);
416416
std::swap(__begin_, __t.__begin_);
417417
std::swap(__end_, __t.__end_);
418-
std::swap(__end_cap_, __t.__end_cap_);
418+
std::swap(__cap_, __t.__cap_);
419419
#if _LIBCPP_HAS_EXCEPTIONS
420420
} catch (...) {
421421
}
@@ -427,19 +427,19 @@ template <class _Tp, class _Allocator>
427427
template <class... _Args>
428428
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_front(_Args&&... __args) {
429429
if (__begin_ == __first_) {
430-
if (__end_ < __end_cap_) {
431-
difference_type __d = __end_cap_ - __end_;
430+
if (__end_ < __cap_) {
431+
difference_type __d = __cap_ - __end_;
432432
__d = (__d + 1) / 2;
433433
__begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
434434
__end_ += __d;
435435
} else {
436-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
436+
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__cap_ - __first_), 1);
437437
__split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc_);
438438
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
439439
std::swap(__first_, __t.__first_);
440440
std::swap(__begin_, __t.__begin_);
441441
std::swap(__end_, __t.__end_);
442-
std::swap(__end_cap_, __t.__end_cap_);
442+
std::swap(__cap_, __t.__cap_);
443443
}
444444
}
445445
__alloc_traits::construct(__alloc_, std::__to_address(__begin_ - 1), std::forward<_Args>(__args)...);
@@ -449,20 +449,20 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_fron
449449
template <class _Tp, class _Allocator>
450450
template <class... _Args>
451451
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
452-
if (__end_ == __end_cap_) {
452+
if (__end_ == __cap_) {
453453
if (__begin_ > __first_) {
454454
difference_type __d = __begin_ - __first_;
455455
__d = (__d + 1) / 2;
456456
__end_ = std::move(__begin_, __end_, __begin_ - __d);
457457
__begin_ -= __d;
458458
} else {
459-
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
459+
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__cap_ - __first_), 1);
460460
__split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc_);
461461
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
462462
std::swap(__first_, __t.__first_);
463463
std::swap(__begin_, __t.__begin_);
464464
std::swap(__end_, __t.__end_);
465-
std::swap(__end_cap_, __t.__end_cap_);
465+
std::swap(__cap_, __t.__cap_);
466466
}
467467
}
468468
__alloc_traits::construct(__alloc_, std::__to_address(__end_), std::forward<_Args>(__args)...);

libcxx/include/__vector/vector.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
822822
__end_ = __begin_; // All the objects have been destroyed by relocating them.
823823
std::swap(this->__begin_, __v.__begin_);
824824
std::swap(this->__end_, __v.__end_);
825-
std::swap(this->__end_cap(), __v.__end_cap_);
825+
std::swap(this->__end_cap(), __v.__cap_);
826826
__v.__first_ = __v.__begin_;
827827
__annotate_new(size());
828828
}
@@ -852,7 +852,7 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
852852

853853
std::swap(this->__begin_, __v.__begin_);
854854
std::swap(this->__end_, __v.__end_);
855-
std::swap(this->__end_cap(), __v.__end_cap_);
855+
std::swap(this->__end_cap(), __v.__cap_);
856856
__v.__first_ = __v.__begin_;
857857
__annotate_new(size());
858858
return __ret;

libcxx/include/deque

+4-4
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,7 @@ void deque<_Tp, _Allocator>::__add_front_capacity() {
20732073
std::swap(__map_.__first_, __buf.__first_);
20742074
std::swap(__map_.__begin_, __buf.__begin_);
20752075
std::swap(__map_.__end_, __buf.__end_);
2076-
std::swap(__map_.__end_cap_, __buf.__end_cap_);
2076+
std::swap(__map_.__cap_, __buf.__cap_);
20772077
__start_ = __map_.size() == 1 ? __block_size / 2 : __start_ + __block_size;
20782078
}
20792079
__annotate_whole_block(0, __asan_poison);
@@ -2150,7 +2150,7 @@ void deque<_Tp, _Allocator>::__add_front_capacity(size_type __n) {
21502150
std::swap(__map_.__first_, __buf.__first_);
21512151
std::swap(__map_.__begin_, __buf.__begin_);
21522152
std::swap(__map_.__end_, __buf.__end_);
2153-
std::swap(__map_.__end_cap_, __buf.__end_cap_);
2153+
std::swap(__map_.__cap_, __buf.__cap_);
21542154
__start_ += __ds;
21552155
}
21562156
}
@@ -2196,7 +2196,7 @@ void deque<_Tp, _Allocator>::__add_back_capacity() {
21962196
std::swap(__map_.__first_, __buf.__first_);
21972197
std::swap(__map_.__begin_, __buf.__begin_);
21982198
std::swap(__map_.__end_, __buf.__end_);
2199-
std::swap(__map_.__end_cap_, __buf.__end_cap_);
2199+
std::swap(__map_.__cap_, __buf.__cap_);
22002200
__annotate_whole_block(__map_.size() - 1, __asan_poison);
22012201
}
22022202
}
@@ -2275,7 +2275,7 @@ void deque<_Tp, _Allocator>::__add_back_capacity(size_type __n) {
22752275
std::swap(__map_.__first_, __buf.__first_);
22762276
std::swap(__map_.__begin_, __buf.__begin_);
22772277
std::swap(__map_.__end_, __buf.__end_);
2278-
std::swap(__map_.__end_cap_, __buf.__end_cap_);
2278+
std::swap(__map_.__cap_, __buf.__cap_);
22792279
__start_ -= __ds;
22802280
}
22812281
}

lldb/examples/synthetic/libcxx.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,10 @@ def update(self):
764764
map_.GetChildMemberWithName("__end_cap_")
765765
)
766766
else:
767-
map_endcap = map_.GetChildMemberWithName(
768-
"__end_cap_"
769-
).GetValueAsUnsigned(0)
767+
map_endcap = map_.GetChildMemberWithName("__cap_")
768+
if not map_endcap.IsValid():
769+
map_endcap = map_.GetChildMemberWithName("__end_cap_")
770+
map_endcap = map_endcap.GetValueAsUnsigned(0)
770771

771772
# check consistency
772773
if not map_first <= map_begin <= map_end <= map_endcap:

0 commit comments

Comments
 (0)