[llvm] 169d453 - [ADT] Declare replaceAllocation in SmallVector.cpp (NFC) (#107469)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 5 15:47:32 PDT 2024
Author: Kazu Hirata
Date: 2024-09-05T15:47:28-07:00
New Revision: 169d453429ca9015046b42719ff5d13cda5d2c6f
URL: https://github.com/llvm/llvm-project/commit/169d453429ca9015046b42719ff5d13cda5d2c6f
DIFF: https://github.com/llvm/llvm-project/commit/169d453429ca9015046b42719ff5d13cda5d2c6f.diff
LOG: [ADT] Declare replaceAllocation in SmallVector.cpp (NFC) (#107469)
This patch changes replaceAllocation to a static function while moving
the declaration to SmallVector.cpp. Note that:
- replaceAllocation is used only within SmallVector.cpp.
- replaceAllocation doesn't access any class members.
Added:
Modified:
llvm/include/llvm/ADT/SmallVector.h
llvm/lib/Support/SmallVector.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 730f84ca038d60..bd3e887e36bce4 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -74,19 +74,6 @@ template <class Size_T> class SmallVectorBase {
/// This function will report a fatal error if it cannot increase capacity.
void grow_pod(void *FirstEl, size_t MinSize, size_t TSize);
- /// If vector was first created with capacity 0, getFirstEl() points to the
- /// memory right after, an area unallocated. If a subsequent allocation,
- /// that grows the vector, happens to return the same pointer as getFirstEl(),
- /// get a new allocation, otherwise isSmall() will falsely return that no
- /// allocation was done (true) and the memory will not be freed in the
- /// destructor. If a VSize is given (vector size), also copy that many
- /// elements to the new allocation - used if realloca fails to increase
- /// space, and happens to allocate precisely at BeginX.
- /// This is unlikely to be called often, but resolves a memory leak when the
- /// situation does occur.
- void *replaceAllocation(void *NewElts, size_t TSize, size_t NewCapacity,
- size_t VSize = 0);
-
public:
size_t size() const { return Size; }
size_t capacity() const { return Capacity; }
diff --git a/llvm/lib/Support/SmallVector.cpp b/llvm/lib/Support/SmallVector.cpp
index b6ce37842040b3..dceea4fbc630e8 100644
--- a/llvm/lib/Support/SmallVector.cpp
+++ b/llvm/lib/Support/SmallVector.cpp
@@ -108,10 +108,18 @@ static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) {
return std::clamp(NewCapacity, MinSize, MaxSize);
}
-template <class Size_T>
-void *SmallVectorBase<Size_T>::replaceAllocation(void *NewElts, size_t TSize,
- size_t NewCapacity,
- size_t VSize) {
+/// If vector was first created with capacity 0, getFirstEl() points to the
+/// memory right after, an area unallocated. If a subsequent allocation,
+/// that grows the vector, happens to return the same pointer as getFirstEl(),
+/// get a new allocation, otherwise isSmall() will falsely return that no
+/// allocation was done (true) and the memory will not be freed in the
+/// destructor. If a VSize is given (vector size), also copy that many
+/// elements to the new allocation - used if realloca fails to increase
+/// space, and happens to allocate precisely at BeginX.
+/// This is unlikely to be called often, but resolves a memory leak when the
+/// situation does occur.
+static void *replaceAllocation(void *NewElts, size_t TSize, size_t NewCapacity,
+ size_t VSize = 0) {
void *NewEltsReplace = llvm::safe_malloc(NewCapacity * TSize);
if (VSize)
memcpy(NewEltsReplace, NewElts, VSize * TSize);
More information about the llvm-commits
mailing list