From 3daae2701b76293c31c1cbdafc9782352321e1f0 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 9 Oct 2007 21:46:38 +0000 Subject: [PATCH] Add new MemoryBuffer::getMemBufferCopy method. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42815 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/MemoryBuffer.h | 6 ++++++ lib/Support/MemoryBuffer.cpp | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/llvm/Support/MemoryBuffer.h b/include/llvm/Support/MemoryBuffer.h index 1e3a154ede6..aa13bfc65a8 100644 --- a/include/llvm/Support/MemoryBuffer.h +++ b/include/llvm/Support/MemoryBuffer.h @@ -61,6 +61,12 @@ public: static MemoryBuffer *getMemBuffer(const char *StartPtr, const char *EndPtr, const char *BufferName = ""); + /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer, + /// copying the contents and taking ownership of it. This has no requirements + /// on EndPtr[0]. + static MemoryBuffer *getMemBufferCopy(const char *StartPtr,const char *EndPtr, + const char *BufferName = ""); + /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that /// is completely initialized to zeros. Note that the caller should /// initialize the memory allocated by this method. The memory is owned by diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index 0ae5676acce..f8779e122d5 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -59,9 +59,13 @@ namespace { class MemoryBufferMem : public MemoryBuffer { std::string FileID; public: - MemoryBufferMem(const char *Start, const char *End, const char *FID) + MemoryBufferMem(const char *Start, const char *End, const char *FID, + bool Copy = false) : FileID(FID) { - init(Start, End); + if (!Copy) + init(Start, End); + else + initCopyOf(Start, End); } virtual const char *getBufferIdentifier() const { @@ -78,6 +82,15 @@ MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr, return new MemoryBufferMem(StartPtr, EndPtr, BufferName); } +/// getMemBufferCopy - Open the specified memory range as a MemoryBuffer, +/// copying the contents and taking ownership of it. This has no requirements +/// on EndPtr[0]. +MemoryBuffer *MemoryBuffer::getMemBufferCopy(const char *StartPtr, + const char *EndPtr, + const char *BufferName) { + return new MemoryBufferMem(StartPtr, EndPtr, BufferName, true); +} + /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size /// that is completely initialized to zeros. Note that the caller should /// initialize the memory allocated by this method. The memory is owned by