Skip to content

Commit 169d453

Browse files
[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.
1 parent c1c4251 commit 169d453

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

llvm/include/llvm/ADT/SmallVector.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,6 @@ template <class Size_T> class SmallVectorBase {
7474
/// This function will report a fatal error if it cannot increase capacity.
7575
void grow_pod(void *FirstEl, size_t MinSize, size_t TSize);
7676

77-
/// If vector was first created with capacity 0, getFirstEl() points to the
78-
/// memory right after, an area unallocated. If a subsequent allocation,
79-
/// that grows the vector, happens to return the same pointer as getFirstEl(),
80-
/// get a new allocation, otherwise isSmall() will falsely return that no
81-
/// allocation was done (true) and the memory will not be freed in the
82-
/// destructor. If a VSize is given (vector size), also copy that many
83-
/// elements to the new allocation - used if realloca fails to increase
84-
/// space, and happens to allocate precisely at BeginX.
85-
/// This is unlikely to be called often, but resolves a memory leak when the
86-
/// situation does occur.
87-
void *replaceAllocation(void *NewElts, size_t TSize, size_t NewCapacity,
88-
size_t VSize = 0);
89-
9077
public:
9178
size_t size() const { return Size; }
9279
size_t capacity() const { return Capacity; }

llvm/lib/Support/SmallVector.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,18 @@ static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) {
108108
return std::clamp(NewCapacity, MinSize, MaxSize);
109109
}
110110

111-
template <class Size_T>
112-
void *SmallVectorBase<Size_T>::replaceAllocation(void *NewElts, size_t TSize,
113-
size_t NewCapacity,
114-
size_t VSize) {
111+
/// If vector was first created with capacity 0, getFirstEl() points to the
112+
/// memory right after, an area unallocated. If a subsequent allocation,
113+
/// that grows the vector, happens to return the same pointer as getFirstEl(),
114+
/// get a new allocation, otherwise isSmall() will falsely return that no
115+
/// allocation was done (true) and the memory will not be freed in the
116+
/// destructor. If a VSize is given (vector size), also copy that many
117+
/// elements to the new allocation - used if realloca fails to increase
118+
/// space, and happens to allocate precisely at BeginX.
119+
/// This is unlikely to be called often, but resolves a memory leak when the
120+
/// situation does occur.
121+
static void *replaceAllocation(void *NewElts, size_t TSize, size_t NewCapacity,
122+
size_t VSize = 0) {
115123
void *NewEltsReplace = llvm::safe_malloc(NewCapacity * TSize);
116124
if (VSize)
117125
memcpy(NewEltsReplace, NewElts, VSize * TSize);

0 commit comments

Comments
 (0)