[llvm] r322094 - [Support] Add WritableMemoryBuffer::getNewMemBuffer
Pavel Labath via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 9 09:26:06 PST 2018
Author: labath
Date: Tue Jan 9 09:26:06 2018
New Revision: 322094
URL: http://llvm.org/viewvc/llvm-project?rev=322094&view=rev
Log:
[Support] Add WritableMemoryBuffer::getNewMemBuffer
Summary:
The idea is that it would replace
(non-Writable)MemoryBuffer::getNewMemBuffer, which is quite useless
unless you const_cast its contents to write to it (which all (both)
callers of this function were doing). This patch also fixes one of the usages in
COFFWriter. After fixing the other usage in clang, I plan to delete the old
function.
Reviewers: dblaikie, Bigcheese
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D41540
Modified:
llvm/trunk/include/llvm/Support/MemoryBuffer.h
llvm/trunk/lib/Object/WindowsResource.cpp
llvm/trunk/lib/Support/MemoryBuffer.cpp
llvm/trunk/unittests/Support/MemoryBufferTest.cpp
Modified: llvm/trunk/include/llvm/Support/MemoryBuffer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemoryBuffer.h?rev=322094&r1=322093&r2=322094&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MemoryBuffer.h (original)
+++ llvm/trunk/include/llvm/Support/MemoryBuffer.h Tue Jan 9 09:26:06 2018
@@ -117,9 +117,8 @@ public:
static std::unique_ptr<MemoryBuffer>
getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
- /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
- /// that the caller need not initialize the memory allocated by this method.
- /// The memory is owned by the MemoryBuffer object.
+ // TODO: Remove after all callers are switched to
+ // WritableMemoryBuffer::getNewMemBuffer
static std::unique_ptr<MemoryBuffer>
getNewMemBuffer(size_t Size, StringRef BufferName = "");
@@ -196,6 +195,12 @@ public:
static std::unique_ptr<WritableMemoryBuffer>
getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
+ /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
+ /// that the caller need not initialize the memory allocated by this method.
+ /// The memory is owned by the MemoryBuffer object.
+ static std::unique_ptr<WritableMemoryBuffer>
+ getNewMemBuffer(size_t Size, const Twine &BufferName = "");
+
private:
// Hide these base class factory function so one can't write
// WritableMemoryBuffer::getXXX()
@@ -204,7 +209,6 @@ private:
using MemoryBuffer::getFileOrSTDIN;
using MemoryBuffer::getMemBuffer;
using MemoryBuffer::getMemBufferCopy;
- using MemoryBuffer::getNewMemBuffer;
using MemoryBuffer::getOpenFile;
using MemoryBuffer::getOpenFileSlice;
using MemoryBuffer::getSTDIN;
Modified: llvm/trunk/lib/Object/WindowsResource.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/WindowsResource.cpp?rev=322094&r1=322093&r2=322094&view=diff
==============================================================================
--- llvm/trunk/lib/Object/WindowsResource.cpp (original)
+++ llvm/trunk/lib/Object/WindowsResource.cpp Tue Jan 9 09:26:06 2018
@@ -334,7 +334,7 @@ private:
void writeDirectoryTree();
void writeDirectoryStringTable();
void writeFirstSectionRelocations();
- std::unique_ptr<MemoryBuffer> OutputBuffer;
+ std::unique_ptr<WritableMemoryBuffer> OutputBuffer;
char *BufferStart;
uint64_t CurrentOffset = 0;
COFF::MachineTypes MachineType;
@@ -360,7 +360,7 @@ WindowsResourceCOFFWriter::WindowsResour
Data(Parser.getData()), StringTable(Parser.getStringTable()) {
performFileLayout();
- OutputBuffer = MemoryBuffer::getNewMemBuffer(FileSize);
+ OutputBuffer = WritableMemoryBuffer::getNewMemBuffer(FileSize);
}
void WindowsResourceCOFFWriter::performFileLayout() {
@@ -425,7 +425,7 @@ static std::time_t getTime() {
}
std::unique_ptr<MemoryBuffer> WindowsResourceCOFFWriter::write() {
- BufferStart = const_cast<char *>(OutputBuffer->getBufferStart());
+ BufferStart = OutputBuffer->getBufferStart();
writeCOFFHeader();
writeFirstSectionHeader();
Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=322094&r1=322093&r2=322094&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Tue Jan 9 09:26:06 2018
@@ -141,11 +141,7 @@ MemoryBuffer::getMemBufferCopy(StringRef
std::unique_ptr<MemoryBuffer>
MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
- auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName);
- if (!SB)
- return nullptr;
- memset(SB->getBufferStart(), 0, Size);
- return std::move(SB);
+ return WritableMemoryBuffer::getNewMemBuffer(Size, BufferName);
}
ErrorOr<std::unique_ptr<MemoryBuffer>>
@@ -306,6 +302,15 @@ WritableMemoryBuffer::getNewUninitMemBuf
return std::unique_ptr<WritableMemoryBuffer>(Ret);
}
+std::unique_ptr<WritableMemoryBuffer>
+WritableMemoryBuffer::getNewMemBuffer(size_t Size, const Twine &BufferName) {
+ auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName);
+ if (!SB)
+ return nullptr;
+ memset(SB->getBufferStart(), 0, Size);
+ return SB;
+}
+
static bool shouldUseMmap(int FD,
size_t FileSize,
size_t MapSize,
Modified: llvm/trunk/unittests/Support/MemoryBufferTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/MemoryBufferTest.cpp?rev=322094&r1=322093&r2=322094&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/MemoryBufferTest.cpp (original)
+++ llvm/trunk/unittests/Support/MemoryBufferTest.cpp Tue Jan 9 09:26:06 2018
@@ -116,13 +116,13 @@ TEST_F(MemoryBufferTest, make_new) {
EXPECT_TRUE(nullptr != Two.get());
// 0-initialized buffer with no name
- OwningBuffer Three(MemoryBuffer::getNewMemBuffer(321, data));
+ OwningBuffer Three(WritableMemoryBuffer::getNewMemBuffer(321, data));
EXPECT_TRUE(nullptr != Three.get());
for (size_t i = 0; i < 321; ++i)
EXPECT_EQ(0, Three->getBufferStart()[0]);
// 0-initialized buffer with name
- OwningBuffer Four(MemoryBuffer::getNewMemBuffer(123, "zeros"));
+ OwningBuffer Four(WritableMemoryBuffer::getNewMemBuffer(123, "zeros"));
EXPECT_TRUE(nullptr != Four.get());
for (size_t i = 0; i < 123; ++i)
EXPECT_EQ(0, Four->getBufferStart()[0]);
More information about the llvm-commits
mailing list