From a7994875b38bc95a3f53afc9bdc6a69cbf593f36 Mon Sep 17 00:00:00 2001 From: Xu Date: Tue, 18 Jun 2024 19:32:36 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=20SmallVector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Shared/SmallVector.cpp | 3 +-- src/Shared/SmallVector.h | 34 ++++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/Shared/SmallVector.cpp b/src/Shared/SmallVector.cpp index b55685e03..6a8b33630 100644 --- a/src/Shared/SmallVector.cpp +++ b/src/Shared/SmallVector.cpp @@ -131,8 +131,7 @@ void SmallVectorBase::grow_pod(void* FirstEl, size_t MinSize, NewElts = replaceAllocation(NewElts, TSize, NewCapacity, size()); } - this->BeginX = NewElts; - this->Capacity = (Size_T)NewCapacity; + this->set_allocation_range(NewElts, NewCapacity); } template class SmallVectorBase; diff --git a/src/Shared/SmallVector.h b/src/Shared/SmallVector.h index a73d8bc4e..315250ff8 100644 --- a/src/Shared/SmallVector.h +++ b/src/Shared/SmallVector.h @@ -3,7 +3,7 @@ // 1. 数据较少时没有堆分配 // 2. 没有强异常保证,因此某些情况下更快 // 3. 对于 POD 类型直接操作内存而不是使用啰嗦且低效的 Allocator -// 移植自 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 +// 移植自 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 // 所作修改如下: // 1. 删除跨编译器逻辑 // 2. 修复 MSVC 警告 @@ -63,7 +63,7 @@ template class SmallVectorBase { SmallVectorBase() = delete; SmallVectorBase(void* FirstEl, size_t TotalCapacity) - : BeginX(FirstEl), Capacity((Size_T)TotalCapacity) { + : BeginX(FirstEl), Capacity(static_cast(TotalCapacity)) { } /// This is a helper for \a grow() that's out of line to reduce code @@ -102,8 +102,18 @@ template class SmallVectorBase { /// /// This does not construct or destroy any elements in the vector. void set_size(size_t N) { - assert(N <= capacity()); - Size = (Size_T)N; + assert(N <= capacity()); // implies no overflow in assignment + Size = static_cast(N); + } + + /// Set the array data pointer to \p Begin and capacity to \p N. + /// + /// This does not construct or destroy any elements in the vector. + // This does not clean up any existing allocation. + void set_allocation_range(void* Begin, size_t N) { + assert(N <= SizeTypeMax()); + BeginX = Begin; + Capacity = static_cast(N); } }; @@ -468,8 +478,7 @@ void SmallVectorTemplateBase::takeAllocationForGrow( if (!this->isSmall()) free(this->begin()); - this->BeginX = NewElts; - this->Capacity = (decltype(this->Capacity))NewCapacity; + this->set_allocation_range(NewElts, NewCapacity); } /// SmallVectorTemplateBase - This is where we put @@ -602,9 +611,6 @@ class SmallVectorImpl : public SmallVectorTemplateBase { RHS.resetToSmall(); } -public: - SmallVectorImpl(const SmallVectorImpl&) = delete; - ~SmallVectorImpl() { // Subclass has already destructed this vector's elements. // If this wasn't grown from the inline copy, deallocate the old space. @@ -612,6 +618,9 @@ class SmallVectorImpl : public SmallVectorTemplateBase { free(this->begin()); } +public: + SmallVectorImpl(const SmallVectorImpl&) = delete; + void clear() { this->destroy_range(this->begin(), this->end()); this->Size = 0; @@ -1210,7 +1219,12 @@ class SmallVector : public SmallVectorImpl, this->destroy_range(this->begin(), this->end()); } - explicit SmallVector(size_t Size, const T& Value = T()) + explicit SmallVector(size_t Size) + : SmallVectorImpl(N) { + this->resize(Size); + } + + SmallVector(size_t Size, const T& Value) : SmallVectorImpl(N) { this->assign(Size, Value); }