Index: include/llvm/Support/MemoryBuffer.h =================================================================== --- include/llvm/Support/MemoryBuffer.h +++ include/llvm/Support/MemoryBuffer.h @@ -117,9 +117,8 @@ static std::unique_ptr 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 getNewMemBuffer(size_t Size, StringRef BufferName = ""); @@ -196,6 +195,12 @@ static std::unique_ptr 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 + 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 @@ using MemoryBuffer::getFileOrSTDIN; using MemoryBuffer::getMemBuffer; using MemoryBuffer::getMemBufferCopy; - using MemoryBuffer::getNewMemBuffer; using MemoryBuffer::getOpenFile; using MemoryBuffer::getOpenFileSlice; using MemoryBuffer::getSTDIN; Index: lib/Object/WindowsResource.cpp =================================================================== --- lib/Object/WindowsResource.cpp +++ lib/Object/WindowsResource.cpp @@ -334,7 +334,7 @@ void writeDirectoryTree(); void writeDirectoryStringTable(); void writeFirstSectionRelocations(); - std::unique_ptr OutputBuffer; + std::unique_ptr OutputBuffer; char *BufferStart; uint64_t CurrentOffset = 0; COFF::MachineTypes MachineType; @@ -360,7 +360,7 @@ Data(Parser.getData()), StringTable(Parser.getStringTable()) { performFileLayout(); - OutputBuffer = MemoryBuffer::getNewMemBuffer(FileSize); + OutputBuffer = WritableMemoryBuffer::getNewMemBuffer(FileSize); } void WindowsResourceCOFFWriter::performFileLayout() { @@ -425,7 +425,7 @@ } std::unique_ptr WindowsResourceCOFFWriter::write() { - BufferStart = const_cast(OutputBuffer->getBufferStart()); + BufferStart = OutputBuffer->getBufferStart(); writeCOFFHeader(); writeFirstSectionHeader(); Index: lib/Support/MemoryBuffer.cpp =================================================================== --- lib/Support/MemoryBuffer.cpp +++ lib/Support/MemoryBuffer.cpp @@ -141,11 +141,7 @@ std::unique_ptr 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> @@ -306,6 +302,15 @@ return std::unique_ptr(Ret); } +std::unique_ptr +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, Index: unittests/Support/MemoryBufferTest.cpp =================================================================== --- unittests/Support/MemoryBufferTest.cpp +++ unittests/Support/MemoryBufferTest.cpp @@ -116,13 +116,13 @@ 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]);