[PATCH] D115384: Support: Avoid using SmallVector::set_size() in MemoryBuffer
Duncan P. N. Exon Smith via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 8 12:56:19 PST 2021
dexonsmith created this revision.
dexonsmith added a reviewer: dblaikie.
Herald added a subscriber: hiraditya.
dexonsmith requested review of this revision.
Herald added a project: LLVM.
Update getMemoryBufferForStream() to use `resize_for_overwrite()` and
`truncate()` instead of `reserve()` and `set_size()`.
Blocker for https://reviews.llvm.org/D115380.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D115384
Files:
llvm/lib/Support/MemoryBuffer.cpp
Index: llvm/lib/Support/MemoryBuffer.cpp
===================================================================
--- llvm/lib/Support/MemoryBuffer.cpp
+++ llvm/lib/Support/MemoryBuffer.cpp
@@ -227,17 +227,20 @@
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);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115384.392896.patch
Type: text/x-patch
Size: 1007 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211208/2ed801a6/attachment.bin>
More information about the llvm-commits
mailing list