change the archive stuff to use MemoryBuffer instead of mappedfile.
MemoryBuffer is higher level and more closely matches the model
needed.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49029 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Archive/Archive.cpp b/lib/Archive/Archive.cpp
index c5da114..e32c716 100644
--- a/lib/Archive/Archive.cpp
+++ b/lib/Archive/Archive.cpp
@@ -17,7 +17,6 @@
 #include "llvm/ModuleProvider.h"
 #include "llvm/Module.h"
 #include "llvm/Support/MemoryBuffer.h"
-#include "llvm/System/MappedFile.h"
 #include "llvm/System/Process.h"
 #include <memory>
 #include <cstring>
@@ -145,25 +144,19 @@
 }
 
 bool
-Archive::mapToMemory(std::string* ErrMsg)
-{
-  mapfile = new sys::MappedFile();
-  if (mapfile->open(archPath, ErrMsg))
+Archive::mapToMemory(std::string* ErrMsg) {
+  mapfile = MemoryBuffer::getFile(archPath.c_str(), archPath.size(), ErrMsg);
+  if (mapfile == 0)
     return true;
-  if (!(base = (char*) mapfile->map(ErrMsg)))
-    return true;
+  base = mapfile->getBufferStart();
   return false;
 }
 
 void Archive::cleanUpMemory() {
   // Shutdown the file mapping
-  if (mapfile) {
-    mapfile->close();
-    delete mapfile;
-    
-    mapfile = 0;
-    base = 0;
-  }
+  delete mapfile;
+  mapfile = 0;
+  base = 0;
   
   // Forget the entire symbol table
   symTab.clear();
diff --git a/lib/Archive/ArchiveReader.cpp b/lib/Archive/ArchiveReader.cpp
index e279a3d..fd0e30a 100644
--- a/lib/Archive/ArchiveReader.cpp
+++ b/lib/Archive/ArchiveReader.cpp
@@ -14,7 +14,6 @@
 #include "ArchiveInternals.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Support/MemoryBuffer.h"
-#include "llvm/System/MappedFile.h"
 #include "llvm/Module.h"
 #include <memory>
 using namespace llvm;
@@ -239,7 +238,7 @@
 bool
 Archive::checkSignature(std::string* error) {
   // Check the magic string at file's header
-  if (mapfile->size() < 8 || memcmp(base, ARFILE_MAGIC, 8)) {
+  if (mapfile->getBufferSize() < 8 || memcmp(base, ARFILE_MAGIC, 8)) {
     if (error)
       *error = "invalid signature for an archive file";
     return false;
@@ -257,7 +256,7 @@
   members.clear();
   symTab.clear();
   const char *At = base;
-  const char *End = base + mapfile->size();
+  const char *End = mapfile->getBufferEnd();
 
   if (!checkSignature(error))
     return false;
@@ -370,7 +369,7 @@
   members.clear();
   symTab.clear();
   const char *At = base;
-  const char *End = base + mapfile->size();
+  const char *End = mapfile->getBufferEnd();
 
   // Make sure we're dealing with an archive
   if (!checkSignature(ErrorMsg))
@@ -478,7 +477,8 @@
 
   // Module hasn't been loaded yet, we need to load it
   const char* modptr = base + fileOffset;
-  ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size(),ErrMsg);
+  ArchiveMember* mbr = parseMemberHeader(modptr, mapfile->getBufferEnd(),
+                                         ErrMsg);
   if (!mbr)
     return 0;
 
@@ -517,8 +517,8 @@
     // below.
 
     // Get a pointer to the first file
-    const char* At  = ((const char*)base) + firstFileOffset;
-    const char* End = ((const char*)base) + mapfile->size();
+    const char* At  = base + firstFileOffset;
+    const char* End = mapfile->getBufferEnd();
 
     while ( At < End) {
       // Compute the offset to be put in the symbol table
diff --git a/lib/Archive/ArchiveWriter.cpp b/lib/Archive/ArchiveWriter.cpp
index 6a71026..afe9819 100644
--- a/lib/Archive/ArchiveWriter.cpp
+++ b/lib/Archive/ArchiveWriter.cpp
@@ -13,7 +13,8 @@
 
 #include "ArchiveInternals.h"
 #include "llvm/Bitcode/ReaderWriter.h"
-#include "llvm/System/MappedFile.h"
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/System/Signals.h"
 #include "llvm/System/Process.h"
 #include "llvm/ModuleProvider.h"
@@ -208,15 +209,15 @@
   // Get the data and its size either from the
   // member's in-memory data or directly from the file.
   size_t fSize = member.getSize();
-  const char* data = (const char*)member.getData();
-  sys::MappedFile* mFile = 0;
+  const char *data = (const char*)member.getData();
+  MemoryBuffer *mFile = 0;
   if (!data) {
-    mFile = new sys::MappedFile();
-    if (mFile->open(member.getPath(), ErrMsg))
+    mFile = MemoryBuffer::getFile(member.getPath().c_str(),
+                                  member.getPath().size(), ErrMsg);
+    if (mFile == 0)
       return true;
-    if (!(data = (const char*) mFile->map(ErrMsg)))
-      return true;
-    fSize = mFile->size();
+    data = mFile->getBufferStart();
+    fSize = mFile->getBufferSize();
   }
 
   // Now that we have the data in memory, update the
@@ -247,10 +248,7 @@
       // We don't need this module any more.
       delete MP;
     } else {
-      if (mFile != 0) {
-        mFile->close();
-        delete mFile;
-      }
+      delete mFile;
       if (ErrMsg)
         *ErrMsg = "Can't parse bitcode member: " + member.getPath().toString()
           + ": " + *ErrMsg;
@@ -281,10 +279,7 @@
     ARFile << ARFILE_PAD;
 
   // Close the mapped file if it was opened
-  if (mFile != 0) {
-    mFile->close();
-    delete mFile;
-  }
+  delete mFile;
   return false;
 }
 
@@ -349,7 +344,7 @@
 {
   // Make sure they haven't opened up the file, not loaded it,
   // but are now trying to write it which would wipe out the file.
-  if (members.empty() && mapfile && mapfile->size() > 8) {
+  if (members.empty() && mapfile && mapfile->getBufferSize() > 8) {
     if (ErrMsg)
       *ErrMsg = "Can't write an archive not opened for writing";
     return true;
@@ -408,18 +403,17 @@
     // ensure compatibility with other archivers we need to put the symbol
     // table first in the file. Unfortunately, this means mapping the file
     // we just wrote back in and copying it to the destination file.
+    sys::Path FinalFilePath = archPath;
 
     // Map in the archive we just wrote.
-    sys::MappedFile arch;
-    if (arch.open(TmpArchive, ErrMsg))
-      return true;
-    const char* base;
-    if (!(base = (const char*) arch.map(ErrMsg)))
-      return true;
+    {
+    OwningPtr<MemoryBuffer> arch(MemoryBuffer::getFile(TmpArchive.c_str(),
+                                                       TmpArchive.size()));
+    if (arch == 0) return true;
+    const char* base = arch->getBufferStart();
 
     // Open another temporary file in order to avoid invalidating the 
     // mmapped data
-    sys::Path FinalFilePath = archPath;
     if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
       return true;
     sys::RemoveFileOnSignal(FinalFilePath);
@@ -456,11 +450,11 @@
     // Copy the temporary file contents being sure to skip the file's magic
     // number.
     FinalFile.write(base + sizeof(ARFILE_MAGIC)-1,
-      arch.size()-sizeof(ARFILE_MAGIC)+1);
+      arch->getBufferSize()-sizeof(ARFILE_MAGIC)+1);
 
     // Close up shop
     FinalFile.close();
-    arch.close();
+    } // free arch.
     
     // Move the final file over top of TmpArchive
     if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))