llvm.org GIT mirror llvm / 861f6eb
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@322094 91177308-0d34-0410-b5e6-96231b3b80d8 Pavel Labath 2 years ago
4 changed file(s) with 23 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
116116 static std::unique_ptr
117117 getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
118118
119 /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
120 /// that the caller need not initialize the memory allocated by this method.
121 /// The memory is owned by the MemoryBuffer object.
119 // TODO: Remove after all callers are switched to
120 // WritableMemoryBuffer::getNewMemBuffer
122121 static std::unique_ptr
123122 getNewMemBuffer(size_t Size, StringRef BufferName = "");
124123
194193 /// method. The memory is owned by the MemoryBuffer object.
195194 static std::unique_ptr
196195 getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "");
196
197 /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
198 /// that the caller need not initialize the memory allocated by this method.
199 /// The memory is owned by the MemoryBuffer object.
200 static std::unique_ptr
201 getNewMemBuffer(size_t Size, const Twine &BufferName = "");
197202
198203 private:
199204 // Hide these base class factory function so one can't write
203208 using MemoryBuffer::getFileOrSTDIN;
204209 using MemoryBuffer::getMemBuffer;
205210 using MemoryBuffer::getMemBufferCopy;
206 using MemoryBuffer::getNewMemBuffer;
207211 using MemoryBuffer::getOpenFile;
208212 using MemoryBuffer::getOpenFileSlice;
209213 using MemoryBuffer::getSTDIN;
333333 void writeDirectoryTree();
334334 void writeDirectoryStringTable();
335335 void writeFirstSectionRelocations();
336 std::unique_ptr<MemoryBuffer> OutputBuffer;
336 std::unique_ptr<WritableMemoryBuffer> OutputBuffer;
337337 char *BufferStart;
338338 uint64_t CurrentOffset = 0;
339339 COFF::MachineTypes MachineType;
359359 Data(Parser.getData()), StringTable(Parser.getStringTable()) {
360360 performFileLayout();
361361
362 OutputBuffer = MemoryBuffer::getNewMemBuffer(FileSize);
362 OutputBuffer = WritableMemoryBuffer::getNewMemBuffer(FileSize);
363363 }
364364
365365 void WindowsResourceCOFFWriter::performFileLayout() {
424424 }
425425
426426 std::unique_ptr WindowsResourceCOFFWriter::write() {
427 BufferStart = const_cast(OutputBuffer->getBufferStart());
427 BufferStart = OutputBuffer->getBufferStart();
428428
429429 writeCOFFHeader();
430430 writeFirstSectionHeader();
140140
141141 std::unique_ptr
142142 MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
143 auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName);
144 if (!SB)
145 return nullptr;
146 memset(SB->getBufferStart(), 0, Size);
147 return std::move(SB);
143 return WritableMemoryBuffer::getNewMemBuffer(Size, BufferName);
148144 }
149145
150146 ErrorOr>
303299
304300 auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true);
305301 return std::unique_ptr(Ret);
302 }
303
304 std::unique_ptr
305 WritableMemoryBuffer::getNewMemBuffer(size_t Size, const Twine &BufferName) {
306 auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName);
307 if (!SB)
308 return nullptr;
309 memset(SB->getBufferStart(), 0, Size);
310 return SB;
306311 }
307312
308313 static bool shouldUseMmap(int FD,
115115 EXPECT_TRUE(nullptr != Two.get());
116116
117117 // 0-initialized buffer with no name
118 OwningBuffer Three(MemoryBuffer::getNewMemBuffer(321, data));
118 OwningBuffer Three(WritableMemoryBuffer::getNewMemBuffer(321, data));
119119 EXPECT_TRUE(nullptr != Three.get());
120120 for (size_t i = 0; i < 321; ++i)
121121 EXPECT_EQ(0, Three->getBufferStart()[0]);
122122
123123 // 0-initialized buffer with name
124 OwningBuffer Four(MemoryBuffer::getNewMemBuffer(123, "zeros"));
124 OwningBuffer Four(WritableMemoryBuffer::getNewMemBuffer(123, "zeros"));
125125 EXPECT_TRUE(nullptr != Four.get());
126126 for (size_t i = 0; i < 123; ++i)
127127 EXPECT_EQ(0, Four->getBufferStart()[0]);