[PATCH] D119177: [demangler] Fix buffer growth
Nathan Sidwell via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 9 08:35:25 PST 2022
urnathan updated this revision to Diff 407172.
urnathan added a comment.
Let's make 1024 the minimum and use a constvar so we don't write it twice.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D119177/new/
https://reviews.llvm.org/D119177
Files:
libcxxabi/src/demangle/Utility.h
llvm/include/llvm/Demangle/Utility.h
Index: llvm/include/llvm/Demangle/Utility.h
===================================================================
--- llvm/include/llvm/Demangle/Utility.h
+++ llvm/include/llvm/Demangle/Utility.h
@@ -33,12 +33,18 @@
size_t CurrentPosition = 0;
size_t BufferCapacity = 0;
- // Ensure there is at least n more positions in buffer.
+ // Ensure there is at least N more positions in the buffer.
void grow(size_t N) {
- if (N + CurrentPosition >= BufferCapacity) {
+ size_t Need = N + CurrentPosition;
+ if (Need > BufferCapacity) {
BufferCapacity *= 2;
- if (BufferCapacity < N + CurrentPosition)
- BufferCapacity = N + CurrentPosition;
+ if (Need > BufferCapacity) {
+ // Avoid many reallocations during startup
+ constexpr size_t MinAlloc = 1024;
+ if (!BufferCapacity && Need < MinAlloc)
+ Need = MinAlloc;
+ BufferCapacity = Need;
+ }
Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity));
if (Buffer == nullptr)
std::terminate();
Index: libcxxabi/src/demangle/Utility.h
===================================================================
--- libcxxabi/src/demangle/Utility.h
+++ libcxxabi/src/demangle/Utility.h
@@ -33,12 +33,18 @@
size_t CurrentPosition = 0;
size_t BufferCapacity = 0;
- // Ensure there is at least n more positions in buffer.
+ // Ensure there is at least N more positions in the buffer.
void grow(size_t N) {
- if (N + CurrentPosition >= BufferCapacity) {
+ size_t Need = N + CurrentPosition;
+ if (Need > BufferCapacity) {
BufferCapacity *= 2;
- if (BufferCapacity < N + CurrentPosition)
- BufferCapacity = N + CurrentPosition;
+ if (Need > BufferCapacity) {
+ // Avoid many reallocations during startup
+ constexpr size_t MinAlloc = 1024;
+ if (!BufferCapacity && Need < MinAlloc)
+ Need = MinAlloc;
+ BufferCapacity = Need;
+ }
Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity));
if (Buffer == nullptr)
std::terminate();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119177.407172.patch
Type: text/x-patch
Size: 2094 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220209/d2adcf3d/attachment.bin>
More information about the llvm-commits
mailing list