[llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
Changes in directory llvm/lib/Support: MemoryBuffer.cpp updated: 1.6 - 1.7 --- Log message: fix a memory leak --- Diffs of the changes: (+1 -1) MemoryBuffer.cpp |2 +- 1 files changed, 1 insertion(+), 1 deletion(-) Index: llvm/lib/Support/MemoryBuffer.cpp diff -u llvm/lib/Support/MemoryBuffer.cpp:1.6 llvm/lib/Support/MemoryBuffer.cpp:1.7 --- llvm/lib/Support/MemoryBuffer.cpp:1.6 Mon May 7 10:21:46 2007 +++ llvm/lib/Support/MemoryBuffer.cpp Thu May 10 19:43:26 2007 @@ -39,7 +39,7 @@ BufferEnd = BufferStart+Size; memcpy(const_castchar*(BufferStart), BufStart, Size); *const_castchar*(BufferEnd) = 0; // Null terminate buffer. - MustDeleteBuffer = false; + MustDeleteBuffer = true; } /// init - Initialize this MemoryBuffer as a reference to externally allocated ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
Changes in directory llvm/lib/Support: MemoryBuffer.cpp updated: 1.5 - 1.6 --- Log message: Unbreak VC++ build. --- Diffs of the changes: (+2 -0) MemoryBuffer.cpp |2 ++ 1 files changed, 2 insertions(+) Index: llvm/lib/Support/MemoryBuffer.cpp diff -u llvm/lib/Support/MemoryBuffer.cpp:1.5 llvm/lib/Support/MemoryBuffer.cpp:1.6 --- llvm/lib/Support/MemoryBuffer.cpp:1.5 Sun May 6 18:32:36 2007 +++ llvm/lib/Support/MemoryBuffer.cpp Mon May 7 10:21:46 2007 @@ -14,6 +14,7 @@ #include llvm/Support/MemoryBuffer.h #include llvm/System/MappedFile.h #include llvm/System/Process.h +#include llvm/System/Program.h #include cassert #include cstdio #include cstring @@ -246,6 +247,7 @@ std::vectorchar FileData; // Read in all of the data from stdin, we cannot mmap stdin. + sys::Program::ChangeStdinToBinary(); while (size_t ReadBytes = fread(Buffer, 1, 4096*4, stdin)) FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes); ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
Changes in directory llvm/lib/Support: MemoryBuffer.cpp updated: 1.3 - 1.4 --- Log message: Fix MemoryBuffer::getFile to return null if it has an error opening the file instead of aborting. --- Diffs of the changes: (+20 -8) MemoryBuffer.cpp | 28 1 files changed, 20 insertions(+), 8 deletions(-) Index: llvm/lib/Support/MemoryBuffer.cpp diff -u llvm/lib/Support/MemoryBuffer.cpp:1.3 llvm/lib/Support/MemoryBuffer.cpp:1.4 --- llvm/lib/Support/MemoryBuffer.cpp:1.3 Sun Apr 29 09:43:31 2007 +++ llvm/lib/Support/MemoryBuffer.cpp Sun May 6 02:24:46 2007 @@ -111,7 +111,9 @@ class MemoryBufferMMapFile : public MemoryBuffer { sys::MappedFile File; public: - MemoryBufferMMapFile(const sys::Path Filename); + MemoryBufferMMapFile() {} + + bool open(const sys::Path Filename); virtual const char *getBufferIdentifier() const { return File.path().c_str(); @@ -121,12 +123,11 @@ }; } -MemoryBufferMMapFile::MemoryBufferMMapFile(const sys::Path Filename) { +bool MemoryBufferMMapFile::open(const sys::Path Filename) { // FIXME: This does an extra stat syscall to figure out the size, but we // already know the size! bool Failure = File.open(Filename); - Failure = Failure; // Silence warning in no-asserts mode. - assert(!Failure Can't open file??); + if (Failure) return true; File.map(); @@ -147,10 +148,12 @@ // No need to keep the file mapped any longer. File.unmap(); } + return false; } MemoryBufferMMapFile::~MemoryBufferMMapFile() { - File.unmap(); + if (File.isMapped()) +File.unmap(); } //===--===// @@ -161,7 +164,11 @@ int64_t FileSize) { sys::PathWithStatus P(FilenameStart, FnSize); #if 1 - return new MemoryBufferMMapFile(P); + MemoryBufferMMapFile *M = new MemoryBufferMMapFile(); + if (!M-open(P)) +return M; + delete M; + return 0; #else // FIXME: We need an efficient and portable method to open a file and then use // 'read' to copy the bits out. The unix implementation is below. This is @@ -177,8 +184,13 @@ } // If the file is larger than some threshold, use mmap, otherwise use 'read'. - if (FileSize = 4096*4) -return new MemoryBufferMMapFile(P); + if (FileSize = 4096*4) { +MemoryBufferMMapFile *M = new MemoryBufferMMapFile(); +if (!M-open(P)) + return M; +delete M; +return 0; + } MemoryBuffer *SB = getNewUninitMemBuffer(FileSize, FilenameStart); char *BufPtr = const_castchar*(SB-getBufferStart()); ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
Re: [llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
On Sun, 2007-05-06 at 02:25 -0500, Chris Lattner wrote: Changes in directory llvm/lib/Support: MemoryBuffer.cpp updated: 1.3 - 1.4 --- Log message: Fix MemoryBuffer::getFile to return null if it has an error opening the file instead of aborting. --- Diffs of the changes: (+20 -8) MemoryBuffer.cpp | 28 1 files changed, 20 insertions(+), 8 deletions(-) Index: llvm/lib/Support/MemoryBuffer.cpp diff -u llvm/lib/Support/MemoryBuffer.cpp:1.3 llvm/lib/Support/MemoryBuffer.cpp:1.4 --- llvm/lib/Support/MemoryBuffer.cpp:1.3 Sun Apr 29 09:43:31 2007 +++ llvm/lib/Support/MemoryBuffer.cpp Sun May 6 02:24:46 2007 @@ -111,7 +111,9 @@ class MemoryBufferMMapFile : public MemoryBuffer { sys::MappedFile File; public: - MemoryBufferMMapFile(const sys::Path Filename); + MemoryBufferMMapFile() {} + + bool open(const sys::Path Filename); Shouldn't this have a parameter to receive the error message ? virtual const char *getBufferIdentifier() const { return File.path().c_str(); @@ -121,12 +123,11 @@ }; } -MemoryBufferMMapFile::MemoryBufferMMapFile(const sys::Path Filename) { +bool MemoryBufferMMapFile::open(const sys::Path Filename) { // FIXME: This does an extra stat syscall to figure out the size, but we // already know the size! bool Failure = File.open(Filename); This needs to have an error message parameter to receive the error message. - Failure = Failure; // Silence warning in no-asserts mode. - assert(!Failure Can't open file??); + if (Failure) return true; This is insufficient. The caller doesn't know why the MemoryBufferMMapFile couldn't be opened. File.map(); @@ -147,10 +148,12 @@ // No need to keep the file mapped any longer. File.unmap(); } + return false; } MemoryBufferMMapFile::~MemoryBufferMMapFile() { - File.unmap(); + if (File.isMapped()) +File.unmap(); } //===--===// @@ -161,7 +164,11 @@ int64_t FileSize) { sys::PathWithStatus P(FilenameStart, FnSize); #if 1 - return new MemoryBufferMMapFile(P); + MemoryBufferMMapFile *M = new MemoryBufferMMapFile(); + if (!M-open(P)) +return M; + delete M; + return 0; #else // FIXME: We need an efficient and portable method to open a file and then use // 'read' to copy the bits out. The unix implementation is below. This is @@ -177,8 +184,13 @@ } // If the file is larger than some threshold, use mmap, otherwise use 'read'. - if (FileSize = 4096*4) -return new MemoryBufferMMapFile(P); + if (FileSize = 4096*4) { +MemoryBufferMMapFile *M = new MemoryBufferMMapFile(); +if (!M-open(P)) + return M; +delete M; +return 0; + } MemoryBuffer *SB = getNewUninitMemBuffer(FileSize, FilenameStart); char *BufPtr = const_castchar*(SB-getBufferStart()); ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
Re: [llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
+ MemoryBufferMMapFile() {} + + bool open(const sys::Path Filename); Shouldn't this have a parameter to receive the error message ? No, because membuffer doesn't know why the open failed. It would always return the same thing. -Chris ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
Re: [llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
On Sun, 2007-05-06 at 00:43 -0700, Chris Lattner wrote: + MemoryBufferMMapFile() {} + + bool open(const sys::Path Filename); Shouldn't this have a parameter to receive the error message ? No, because membuffer doesn't know why the open failed. It would always return the same thing. It must be passed up from lib/System. I suggested this in my other comments on these changes. It is not acceptable to force the application to print something like: file: can't open: don't know why, we just can't (I'm exaggerating but you get the point) Reid. -Chris ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
Re: [llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
On May 6, 2007, at 12:50 AM, Reid Spencer wrote: On Sun, 2007-05-06 at 00:43 -0700, Chris Lattner wrote: + MemoryBufferMMapFile() {} + + bool open(const sys::Path Filename); Shouldn't this have a parameter to receive the error message ? No, because membuffer doesn't know why the open failed. It would always return the same thing. It must be passed up from lib/System. I suggested this in my other comments on these changes. It is not acceptable to force the application to print something like: file: can't open: don't know why, we just can't (I'm exaggerating but you get the point) Ah, I didn't realize mapped file did this. This is great, I'll do this after my current set of changes. It will simplify several clients. -Chris ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
Changes in directory llvm/lib/Support: MemoryBuffer.cpp updated: 1.4 - 1.5 --- Log message: Enhance MemoryBuffer to return error messages in strings if they occur. --- Diffs of the changes: (+11 -7) MemoryBuffer.cpp | 18 +++--- 1 files changed, 11 insertions(+), 7 deletions(-) Index: llvm/lib/Support/MemoryBuffer.cpp diff -u llvm/lib/Support/MemoryBuffer.cpp:1.4 llvm/lib/Support/MemoryBuffer.cpp:1.5 --- llvm/lib/Support/MemoryBuffer.cpp:1.4 Sun May 6 02:24:46 2007 +++ llvm/lib/Support/MemoryBuffer.cpp Sun May 6 18:32:36 2007 @@ -113,7 +113,7 @@ public: MemoryBufferMMapFile() {} - bool open(const sys::Path Filename); + bool open(const sys::Path Filename, std::string *ErrStr); virtual const char *getBufferIdentifier() const { return File.path().c_str(); @@ -123,13 +123,15 @@ }; } -bool MemoryBufferMMapFile::open(const sys::Path Filename) { +bool MemoryBufferMMapFile::open(const sys::Path Filename, +std::string *ErrStr) { // FIXME: This does an extra stat syscall to figure out the size, but we // already know the size! - bool Failure = File.open(Filename); + bool Failure = File.open(Filename, sys::MappedFile::READ_ACCESS, ErrStr); if (Failure) return true; - File.map(); + if (!File.map(ErrStr)) +return true; size_t Size = File.size(); @@ -161,11 +163,13 @@ //===--===// MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize, -int64_t FileSize) { +std::string *ErrStr, int64_t FileSize){ + // FIXME: it would be nice if PathWithStatus didn't copy the filename into a + // temporary string. :( sys::PathWithStatus P(FilenameStart, FnSize); #if 1 MemoryBufferMMapFile *M = new MemoryBufferMMapFile(); - if (!M-open(P)) + if (!M-open(P, ErrStr)) return M; delete M; return 0; @@ -186,7 +190,7 @@ // If the file is larger than some threshold, use mmap, otherwise use 'read'. if (FileSize = 4096*4) { MemoryBufferMMapFile *M = new MemoryBufferMMapFile(); -if (!M-open(P)) +if (!M-open(P, ErrStr)) return M; delete M; return 0; ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
Changes in directory llvm/lib/Support: MemoryBuffer.cpp added (r1.1) --- Log message: Add a new memorybuffer class, to unify all the file reading code in the system --- Diffs of the changes: (+239 -0) MemoryBuffer.cpp | 239 +++ 1 files changed, 239 insertions(+) Index: llvm/lib/Support/MemoryBuffer.cpp diff -c /dev/null llvm/lib/Support/MemoryBuffer.cpp:1.1 *** /dev/null Sun Apr 29 01:59:02 2007 --- llvm/lib/Support/MemoryBuffer.cpp Sun Apr 29 01:58:52 2007 *** *** 0 --- 1,239 + //===--- MemoryBuffer.cpp - Memory Buffer implementation --===// + // + // The LLVM Compiler Infrastructure + // + // This file was developed by Chris Lattner and is distributed under + // the University of Illinois Open Source License. See LICENSE.TXT for details. + // + //===--===// + // + // This file implements the MemoryBuffer interface. + // + //===--===// + + #include llvm/Support/MemoryBuffer.h + #include llvm/System/MappedFile.h + #include llvm/System/Process.h + #include cstdio + #include cstring + #include cerrno + using namespace llvm; + + //===--===// + // MemoryBuffer implementation itself. + //===--===// + + MemoryBuffer::~MemoryBuffer() { + if (MustDeleteBuffer) + delete [] BufferStart; + } + + /// initCopyOf - Initialize this source buffer with a copy of the specified + /// memory range. We make the copy so that we can null terminate it + /// successfully. + void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) { + size_t Size = BufEnd-BufStart; + BufferStart = new char[Size+1]; + BufferEnd = BufferStart+Size; + memcpy(const_castchar*(BufferStart), BufStart, Size); + *const_castchar*(BufferEnd) = 0; // Null terminate buffer. + MustDeleteBuffer = false; + } + + /// init - Initialize this MemoryBuffer as a reference to externally allocated + /// memory, memory that we know is already null terminated. + void MemoryBuffer::init(const char *BufStart, const char *BufEnd) { + assert(BufEnd[0] == 0 Buffer is not null terminated!); + BufferStart = BufStart; + BufferEnd = BufEnd; + MustDeleteBuffer = false; + } + + //===--===// + // MemoryBufferMem implementation. + //===--===// + + namespace { + class MemoryBufferMem : public MemoryBuffer { + std::string FileID; + public: + MemoryBufferMem(const char *Start, const char *End, const char *FID) + : FileID(FID) { + init(Start, End); + } + + virtual const char *getBufferIdentifier() const { + return FileID.c_str(); + } + }; + } + + /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note + /// that EndPtr[0] must be a null byte and be accessible! + MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr, + const char *EndPtr, + const char *BufferName) { + return new MemoryBufferMem(StartPtr, EndPtr, BufferName); + } + + /// 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 + /// the MemoryBuffer object. + MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(unsigned Size, + const char *BufferName) { + char *Buf = new char[Size+1]; + Buf[Size] = 0; + MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName); + // The memory for this buffer is owned by the MemoryBuffer. + SB-MustDeleteBuffer = true; + return SB; + } + + /// 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 + /// the MemoryBuffer object. + MemoryBuffer *MemoryBuffer::getNewMemBuffer(unsigned Size, + const char *BufferName) { + MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName); + memset(const_castchar*(SB-getBufferStart()), 0, Size+1); + return SB; + } + + + //===--===// + // MemoryBufferMMapFile implementation. + //===--===// + + namespace { + class MemoryBufferMMapFile : public MemoryBuffer { + sys::MappedFile File; + public: + MemoryBufferMMapFile(const sys::Path Filename); +
[llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
Changes in directory llvm/lib/Support: MemoryBuffer.cpp updated: 1.1 - 1.2 --- Log message: Unbreak build. --- Diffs of the changes: (+2 -1) MemoryBuffer.cpp |3 ++- 1 files changed, 2 insertions(+), 1 deletion(-) Index: llvm/lib/Support/MemoryBuffer.cpp diff -u llvm/lib/Support/MemoryBuffer.cpp:1.1 llvm/lib/Support/MemoryBuffer.cpp:1.2 --- llvm/lib/Support/MemoryBuffer.cpp:1.1 Sun Apr 29 01:58:52 2007 +++ llvm/lib/Support/MemoryBuffer.cpp Sun Apr 29 09:21:44 2007 @@ -14,6 +14,7 @@ #include llvm/Support/MemoryBuffer.h #include llvm/System/MappedFile.h #include llvm/System/Process.h +#include cassert #include cstdio #include cstring #include cerrno @@ -158,7 +159,7 @@ MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize, int64_t FileSize) { - sys::PathWithStatus P(FilenameStart, FnSize); + sys::PathWithStatus P(std::string(FilenameStart, FnSize)); #if 1 return new MemoryBufferMMapFile(P); #else ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Support/MemoryBuffer.cpp
Changes in directory llvm/lib/Support: MemoryBuffer.cpp updated: 1.2 - 1.3 --- Log message: Fix MemoryBuffer breakage correctly. --- Diffs of the changes: (+1 -1) MemoryBuffer.cpp |2 +- 1 files changed, 1 insertion(+), 1 deletion(-) Index: llvm/lib/Support/MemoryBuffer.cpp diff -u llvm/lib/Support/MemoryBuffer.cpp:1.2 llvm/lib/Support/MemoryBuffer.cpp:1.3 --- llvm/lib/Support/MemoryBuffer.cpp:1.2 Sun Apr 29 09:21:44 2007 +++ llvm/lib/Support/MemoryBuffer.cpp Sun Apr 29 09:43:31 2007 @@ -159,7 +159,7 @@ MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize, int64_t FileSize) { - sys::PathWithStatus P(std::string(FilenameStart, FnSize)); + sys::PathWithStatus P(FilenameStart, FnSize); #if 1 return new MemoryBufferMMapFile(P); #else ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits