forked from OSchip/llvm-project
[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 llvm-svn: 322094
This commit is contained in:
parent
40bab37551
commit
9aaf5d3e71
|
@ -117,9 +117,8 @@ public:
|
||||||
static std::unique_ptr<MemoryBuffer>
|
static std::unique_ptr<MemoryBuffer>
|
||||||
getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
|
getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
|
||||||
|
|
||||||
/// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
|
// TODO: Remove after all callers are switched to
|
||||||
/// that the caller need not initialize the memory allocated by this method.
|
// WritableMemoryBuffer::getNewMemBuffer
|
||||||
/// The memory is owned by the MemoryBuffer object.
|
|
||||||
static std::unique_ptr<MemoryBuffer>
|
static std::unique_ptr<MemoryBuffer>
|
||||||
getNewMemBuffer(size_t Size, StringRef BufferName = "");
|
getNewMemBuffer(size_t Size, StringRef BufferName = "");
|
||||||
|
|
||||||
|
@ -196,6 +195,12 @@ public:
|
||||||
static std::unique_ptr<WritableMemoryBuffer>
|
static std::unique_ptr<WritableMemoryBuffer>
|
||||||
getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
|
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:
|
private:
|
||||||
// Hide these base class factory function so one can't write
|
// Hide these base class factory function so one can't write
|
||||||
// WritableMemoryBuffer::getXXX()
|
// WritableMemoryBuffer::getXXX()
|
||||||
|
@ -204,7 +209,6 @@ private:
|
||||||
using MemoryBuffer::getFileOrSTDIN;
|
using MemoryBuffer::getFileOrSTDIN;
|
||||||
using MemoryBuffer::getMemBuffer;
|
using MemoryBuffer::getMemBuffer;
|
||||||
using MemoryBuffer::getMemBufferCopy;
|
using MemoryBuffer::getMemBufferCopy;
|
||||||
using MemoryBuffer::getNewMemBuffer;
|
|
||||||
using MemoryBuffer::getOpenFile;
|
using MemoryBuffer::getOpenFile;
|
||||||
using MemoryBuffer::getOpenFileSlice;
|
using MemoryBuffer::getOpenFileSlice;
|
||||||
using MemoryBuffer::getSTDIN;
|
using MemoryBuffer::getSTDIN;
|
||||||
|
|
|
@ -334,7 +334,7 @@ private:
|
||||||
void writeDirectoryTree();
|
void writeDirectoryTree();
|
||||||
void writeDirectoryStringTable();
|
void writeDirectoryStringTable();
|
||||||
void writeFirstSectionRelocations();
|
void writeFirstSectionRelocations();
|
||||||
std::unique_ptr<MemoryBuffer> OutputBuffer;
|
std::unique_ptr<WritableMemoryBuffer> OutputBuffer;
|
||||||
char *BufferStart;
|
char *BufferStart;
|
||||||
uint64_t CurrentOffset = 0;
|
uint64_t CurrentOffset = 0;
|
||||||
COFF::MachineTypes MachineType;
|
COFF::MachineTypes MachineType;
|
||||||
|
@ -360,7 +360,7 @@ WindowsResourceCOFFWriter::WindowsResourceCOFFWriter(
|
||||||
Data(Parser.getData()), StringTable(Parser.getStringTable()) {
|
Data(Parser.getData()), StringTable(Parser.getStringTable()) {
|
||||||
performFileLayout();
|
performFileLayout();
|
||||||
|
|
||||||
OutputBuffer = MemoryBuffer::getNewMemBuffer(FileSize);
|
OutputBuffer = WritableMemoryBuffer::getNewMemBuffer(FileSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowsResourceCOFFWriter::performFileLayout() {
|
void WindowsResourceCOFFWriter::performFileLayout() {
|
||||||
|
@ -425,7 +425,7 @@ static std::time_t getTime() {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<MemoryBuffer> WindowsResourceCOFFWriter::write() {
|
std::unique_ptr<MemoryBuffer> WindowsResourceCOFFWriter::write() {
|
||||||
BufferStart = const_cast<char *>(OutputBuffer->getBufferStart());
|
BufferStart = OutputBuffer->getBufferStart();
|
||||||
|
|
||||||
writeCOFFHeader();
|
writeCOFFHeader();
|
||||||
writeFirstSectionHeader();
|
writeFirstSectionHeader();
|
||||||
|
|
|
@ -141,11 +141,7 @@ MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
|
||||||
|
|
||||||
std::unique_ptr<MemoryBuffer>
|
std::unique_ptr<MemoryBuffer>
|
||||||
MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
|
MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
|
||||||
auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName);
|
return WritableMemoryBuffer::getNewMemBuffer(Size, BufferName);
|
||||||
if (!SB)
|
|
||||||
return nullptr;
|
|
||||||
memset(SB->getBufferStart(), 0, Size);
|
|
||||||
return std::move(SB);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
||||||
|
@ -306,6 +302,15 @@ WritableMemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName
|
||||||
return std::unique_ptr<WritableMemoryBuffer>(Ret);
|
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,
|
static bool shouldUseMmap(int FD,
|
||||||
size_t FileSize,
|
size_t FileSize,
|
||||||
size_t MapSize,
|
size_t MapSize,
|
||||||
|
|
|
@ -116,13 +116,13 @@ TEST_F(MemoryBufferTest, make_new) {
|
||||||
EXPECT_TRUE(nullptr != Two.get());
|
EXPECT_TRUE(nullptr != Two.get());
|
||||||
|
|
||||||
// 0-initialized buffer with no name
|
// 0-initialized buffer with no name
|
||||||
OwningBuffer Three(MemoryBuffer::getNewMemBuffer(321, data));
|
OwningBuffer Three(WritableMemoryBuffer::getNewMemBuffer(321, data));
|
||||||
EXPECT_TRUE(nullptr != Three.get());
|
EXPECT_TRUE(nullptr != Three.get());
|
||||||
for (size_t i = 0; i < 321; ++i)
|
for (size_t i = 0; i < 321; ++i)
|
||||||
EXPECT_EQ(0, Three->getBufferStart()[0]);
|
EXPECT_EQ(0, Three->getBufferStart()[0]);
|
||||||
|
|
||||||
// 0-initialized buffer with name
|
// 0-initialized buffer with name
|
||||||
OwningBuffer Four(MemoryBuffer::getNewMemBuffer(123, "zeros"));
|
OwningBuffer Four(WritableMemoryBuffer::getNewMemBuffer(123, "zeros"));
|
||||||
EXPECT_TRUE(nullptr != Four.get());
|
EXPECT_TRUE(nullptr != Four.get());
|
||||||
for (size_t i = 0; i < 123; ++i)
|
for (size_t i = 0; i < 123; ++i)
|
||||||
EXPECT_EQ(0, Four->getBufferStart()[0]);
|
EXPECT_EQ(0, Four->getBufferStart()[0]);
|
||||||
|
|
Loading…
Reference in New Issue