Skip to content

Commit a799487

Browse files
committed
chore: 更新 SmallVector
1 parent 0ec392b commit a799487

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

‎src/Shared/SmallVector.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ void SmallVectorBase<Size_T>::grow_pod(void* FirstEl, size_t MinSize,
131131
NewElts = replaceAllocation(NewElts, TSize, NewCapacity, size());
132132
}
133133

134-
this->BeginX = NewElts;
135-
this->Capacity = (Size_T)NewCapacity;
134+
this->set_allocation_range(NewElts, NewCapacity);
136135
}
137136

138137
template class SmallVectorBase<uint32_t>;

‎src/Shared/SmallVector.h‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// 1. 数据较少时没有堆分配
44
// 2. 没有强异常保证,因此某些情况下更快
55
// 3. 对于 POD 类型直接操作内存而不是使用啰嗦且低效的 Allocator
6-
// 移植自 https://github.com/llvm/llvm-project/blob/7fbdee3e29203f2ffd1996c2919096e0bfe7c93b/llvm/include/llvm/ADT/SmallVector.h 和 https://github.com/llvm/llvm-project/blob/7fbdee3e29203f2ffd1996c2919096e0bfe7c93b/llvm/lib/Support/SmallVector.cpp
6+
// 移植自 https://github.com/llvm/llvm-project/blob/a6eddf9a79709e3161d3aad86d44ab1097f57f22/llvm/include/llvm/ADT/SmallVector.h 和 https://github.com/llvm/llvm-project/blob/a6eddf9a79709e3161d3aad86d44ab1097f57f22/llvm/lib/Support/SmallVector.cpp
77
// 所作修改如下:
88
// 1. 删除跨编译器逻辑
99
// 2. 修复 MSVC 警告
@@ -63,7 +63,7 @@ template <class Size_T> class SmallVectorBase {
6363

6464
SmallVectorBase() = delete;
6565
SmallVectorBase(void* FirstEl, size_t TotalCapacity)
66-
: BeginX(FirstEl), Capacity((Size_T)TotalCapacity) {
66+
: BeginX(FirstEl), Capacity(static_cast<Size_T>(TotalCapacity)) {
6767
}
6868

6969
/// This is a helper for \a grow() that's out of line to reduce code
@@ -102,8 +102,18 @@ template <class Size_T> class SmallVectorBase {
102102
///
103103
/// This does not construct or destroy any elements in the vector.
104104
void set_size(size_t N) {
105-
assert(N <= capacity());
106-
Size = (Size_T)N;
105+
assert(N <= capacity()); // implies no overflow in assignment
106+
Size = static_cast<Size_T>(N);
107+
}
108+
109+
/// Set the array data pointer to \p Begin and capacity to \p N.
110+
///
111+
/// This does not construct or destroy any elements in the vector.
112+
// This does not clean up any existing allocation.
113+
void set_allocation_range(void* Begin, size_t N) {
114+
assert(N <= SizeTypeMax());
115+
BeginX = Begin;
116+
Capacity = static_cast<Size_T>(N);
107117
}
108118
};
109119

@@ -468,8 +478,7 @@ void SmallVectorTemplateBase<T, TriviallyCopyable>::takeAllocationForGrow(
468478
if (!this->isSmall())
469479
free(this->begin());
470480

471-
this->BeginX = NewElts;
472-
this->Capacity = (decltype(this->Capacity))NewCapacity;
481+
this->set_allocation_range(NewElts, NewCapacity);
473482
}
474483

475484
/// SmallVectorTemplateBase<TriviallyCopyable = true> - This is where we put
@@ -602,16 +611,16 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
602611
RHS.resetToSmall();
603612
}
604613

605-
public:
606-
SmallVectorImpl(const SmallVectorImpl&) = delete;
607-
608614
~SmallVectorImpl() {
609615
// Subclass has already destructed this vector's elements.
610616
// If this wasn't grown from the inline copy, deallocate the old space.
611617
if (!this->isSmall())
612618
free(this->begin());
613619
}
614620

621+
public:
622+
SmallVectorImpl(const SmallVectorImpl&) = delete;
623+
615624
void clear() {
616625
this->destroy_range(this->begin(), this->end());
617626
this->Size = 0;
@@ -1210,7 +1219,12 @@ class SmallVector : public SmallVectorImpl<T>,
12101219
this->destroy_range(this->begin(), this->end());
12111220
}
12121221

1213-
explicit SmallVector(size_t Size, const T& Value = T())
1222+
explicit SmallVector(size_t Size)
1223+
: SmallVectorImpl<T>(N) {
1224+
this->resize(Size);
1225+
}
1226+
1227+
SmallVector(size_t Size, const T& Value)
12141228
: SmallVectorImpl<T>(N) {
12151229
this->assign(Size, Value);
12161230
}

0 commit comments

Comments
 (0)