Skip to content

Commit

Permalink
[Support] Add WritableMemoryBuffer::getNewMemBuffer
Browse files Browse the repository at this point in the history
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@322094 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
labath committed Jan 9, 2018
1 parent 4f407a2 commit 861f6eb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
12 changes: 8 additions & 4 deletions include/llvm/Support/MemoryBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ class MemoryBuffer {
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 = "");

Expand Down Expand Up @@ -196,6 +195,12 @@ class WritableMemoryBuffer : public MemoryBuffer {
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()
Expand All @@ -204,7 +209,6 @@ class WritableMemoryBuffer : public MemoryBuffer {
using MemoryBuffer::getFileOrSTDIN;
using MemoryBuffer::getMemBuffer;
using MemoryBuffer::getMemBufferCopy;
using MemoryBuffer::getNewMemBuffer;
using MemoryBuffer::getOpenFile;
using MemoryBuffer::getOpenFileSlice;
using MemoryBuffer::getSTDIN;
Expand Down
6 changes: 3 additions & 3 deletions lib/Object/WindowsResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class WindowsResourceCOFFWriter {
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;
Expand All @@ -360,7 +360,7 @@ WindowsResourceCOFFWriter::WindowsResourceCOFFWriter(
Data(Parser.getData()), StringTable(Parser.getStringTable()) {
performFileLayout();

OutputBuffer = MemoryBuffer::getNewMemBuffer(FileSize);
OutputBuffer = WritableMemoryBuffer::getNewMemBuffer(FileSize);
}

void WindowsResourceCOFFWriter::performFileLayout() {
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 10 additions & 5 deletions lib/Support/MemoryBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {

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>>
Expand Down Expand Up @@ -306,6 +302,15 @@ WritableMemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName
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,
Expand Down
4 changes: 2 additions & 2 deletions unittests/Support/MemoryBufferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 861f6eb

Please sign in to comment.