[llvm] 1131738 - Support: Avoid using SmallVector::set_size() in MemoryBuffer
Duncan P. N. Exon Smith via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 9 18:45:18 PST 2021
Author: Duncan P. N. Exon Smith
Date: 2021-12-09T18:44:40-08:00
New Revision: 1131738698c49b194be278da3a2a21a7eaa915a9
URL: https://github.com/llvm/llvm-project/commit/1131738698c49b194be278da3a2a21a7eaa915a9
DIFF: https://github.com/llvm/llvm-project/commit/1131738698c49b194be278da3a2a21a7eaa915a9.diff
LOG: Support: Avoid using SmallVector::set_size() in MemoryBuffer
Update getMemoryBufferForStream() to use `resize_for_overwrite()` and
`truncate()` instead of `reserve()` and `set_size()`.
Differential Revision: https://reviews.llvm.org/D115384
Added:
Modified:
llvm/lib/Support/MemoryBuffer.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp
index bcf13d828a5df..d3fa3c6f065de 100644
--- a/llvm/lib/Support/MemoryBuffer.cpp
+++ b/llvm/lib/Support/MemoryBuffer.cpp
@@ -227,17 +227,20 @@ static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getMemoryBufferForStream(sys::fs::file_t FD, const Twine &BufferName) {
const ssize_t ChunkSize = 4096*4;
SmallString<ChunkSize> Buffer;
+
// Read into Buffer until we hit EOF.
+ size_t Size = Buffer.size();
for (;;) {
- Buffer.reserve(Buffer.size() + ChunkSize);
+ Buffer.resize_for_overwrite(Size + ChunkSize);
Expected<size_t> ReadBytes = sys::fs::readNativeFile(
- FD, makeMutableArrayRef(Buffer.end(), ChunkSize));
+ FD, makeMutableArrayRef(Buffer.begin() + Size, ChunkSize));
if (!ReadBytes)
return errorToErrorCode(ReadBytes.takeError());
if (*ReadBytes == 0)
break;
- Buffer.set_size(Buffer.size() + *ReadBytes);
+ Size += *ReadBytes;
}
+ Buffer.truncate(Size);
return getMemBufferCopyImpl(Buffer, BufferName);
}
More information about the llvm-commits
mailing list