-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, sys::MappedFile::READ_ACCESS, ErrStr);
- if (Failure) return true;
-
- if (!File.map(ErrStr))
- return true;
-
- 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(const char *Filename, std::string *ErrStr,
+ int64_t FileSize) {
+ int OpenFlags = 0;
+#ifdef O_BINARY
+ OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
+#endif
+ int FD = ::open(Filename, O_RDONLY|OpenFlags);
+ if (FD == -1) {
+ if (ErrStr) *ErrStr = "could not open file";
+ return 0;