llvm.org GIT mirror llvm / 82e791d
Fix MemoryBuffer::getFile to return null if it has an error opening the file instead of aborting. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36858 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
1 changed file(s) with 20 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
110110 class MemoryBufferMMapFile : public MemoryBuffer {
111111 sys::MappedFile File;
112112 public:
113 MemoryBufferMMapFile(const sys::Path &Filename);
113 MemoryBufferMMapFile() {}
114
115 bool open(const sys::Path &Filename);
114116
115117 virtual const char *getBufferIdentifier() const {
116118 return File.path().c_str();
120122 };
121123 }
122124
123 MemoryBufferMMapFile::MemoryBufferMMapFile(const sys::Path &Filename) {
125 bool MemoryBufferMMapFile::open(const sys::Path &Filename) {
124126 // FIXME: This does an extra stat syscall to figure out the size, but we
125127 // already know the size!
126128 bool Failure = File.open(Filename);
127 Failure = Failure; // Silence warning in no-asserts mode.
128 assert(!Failure && "Can't open file??");
129 if (Failure) return true;
129130
130131 File.map();
131132
146147 // No need to keep the file mapped any longer.
147148 File.unmap();
148149 }
150 return false;
149151 }
150152
151153 MemoryBufferMMapFile::~MemoryBufferMMapFile() {
152 File.unmap();
154 if (File.isMapped())
155 File.unmap();
153156 }
154157
155158 //===----------------------------------------------------------------------===//
160163 int64_t FileSize) {
161164 sys::PathWithStatus P(FilenameStart, FnSize);
162165 #if 1
163 return new MemoryBufferMMapFile(P);
166 MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
167 if (!M->open(P))
168 return M;
169 delete M;
170 return 0;
164171 #else
165172 // FIXME: We need an efficient and portable method to open a file and then use
166173 // 'read' to copy the bits out. The unix implementation is below. This is
176183 }
177184
178185 // If the file is larger than some threshold, use mmap, otherwise use 'read'.
179 if (FileSize >= 4096*4)
180 return new MemoryBufferMMapFile(P);
186 if (FileSize >= 4096*4) {
187 MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
188 if (!M->open(P))
189 return M;
190 delete M;
191 return 0;
192 }
181193
182194 MemoryBuffer *SB = getNewUninitMemBuffer(FileSize, FilenameStart);
183195 char *BufPtr = const_cast(SB->getBufferStart());