-MemoryBufferMMapFile::MemoryBufferMMapFile(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??");
-
- File.map();
-
- size_t Size = File.size();
-
- static unsigned PageSize = sys::Process::GetPageSize();
- assert(((PageSize & (PageSize-1)) == 0) && PageSize &&
- "Page size is not a power of 2!");
-
- // If this file is not an exact multiple of the system page size (common
- // case), then the OS has zero terminated the buffer for us.
- if ((Size & (PageSize-1))) {
- init(File.charBase(), File.charBase()+Size);
- } else {
- // Otherwise, we allocate a new memory buffer and copy the data over
- initCopyOf(File.charBase(), File.charBase()+Size);
-
- // No need to keep the file mapped any longer.
- File.unmap();
+MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, std::string *ErrStr,
+ int64_t FileSize, struct stat *FileInfo) {
+ int OpenFlags = 0;
+#ifdef O_BINARY
+ OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
+#endif
+ SmallString<256> PathBuf(Filename.begin(), Filename.end());
+ int FD = ::open(PathBuf.c_str(), O_RDONLY|OpenFlags);
+ if (FD == -1) {
+ if (ErrStr) *ErrStr = strerror(errno);
+ return 0;