change the archive stuff to use MemoryBuffer instead of mappedfile.
authorChris Lattner <sabre@nondot.org>
Tue, 1 Apr 2008 04:26:46 +0000 (04:26 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 1 Apr 2008 04:26:46 +0000 (04:26 +0000)
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

include/llvm/Bitcode/Archive.h
lib/Archive/Archive.cpp
lib/Archive/ArchiveReader.cpp
lib/Archive/ArchiveWriter.cpp

index 5b98c086be0ab185df00b883c85b648f2dfd12fc..1dcbe3abe27e18d6fd555f08bbaa39c679429d53 100644 (file)
@@ -24,7 +24,7 @@
 #include <fstream>
 
 namespace llvm {
-  namespace sys { class MappedFile; }
+  class MemoryBuffer;
 
 // Forward declare classes
 class ModuleProvider;      // From VMCore
@@ -534,7 +534,7 @@ class Archive {
   protected:
     sys::Path archPath;       ///< Path to the archive file we read/write
     MembersList members;      ///< The ilist of ArchiveMember
-    sys::MappedFile* mapfile; ///< Raw Archive contents mapped into memory
+    MemoryBuffer *mapfile;    ///< Raw Archive contents mapped into memory
     const char* base;         ///< Base of the memory mapped file data
     SymTabType symTab;        ///< The symbol table
     std::string strtab;       ///< The string table for long file names
index c5da1145384e17f80ca544a73b7036cd53797ad5..e32c7164bd10a7d00a75fb68cfdb5d6c5c335ac2 100644 (file)
@@ -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 @@ Archive::Archive(const sys::Path& filename)
 }
 
 bool
-Archive::mapToMemory(std::string* ErrMsg)
-{
-  mapfile = new sys::MappedFile();
-  if (mapfile->open(archPath, ErrMsg))
-    return true;
-  if (!(base = (char*) mapfile->map(ErrMsg)))
+Archive::mapToMemory(std::string* ErrMsg) {
+  mapfile = MemoryBuffer::getFile(archPath.c_str(), archPath.size(), ErrMsg);
+  if (mapfile == 0)
     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();
index e279a3d559e57a2c5f03beb809bb130165a35682..fd0e30a71bfb4fa8b02a8a6c8b0ddb8c92dc9175 100644 (file)
@@ -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 @@ Archive::parseMemberHeader(const char*& At, const char* End, std::string* error)
 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 @@ Archive::loadArchive(std::string* error) {
   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 @@ Archive::loadSymbolTable(std::string* ErrorMsg) {
   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 @@ Archive::findModuleDefiningSymbol(const std::string& symbol,
 
   // 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 @@ Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
     // 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
index 6a71026a0ff46d05349023096cc1c1d964d26abe..afe9819deeace613d6555ce60865729e829a4e4d 100644 (file)
@@ -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 @@ Archive::writeMember(
   // 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 chardata = (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 @@ Archive::writeMember(
       // 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 @@ Archive::writeMember(
     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 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
 {
   // 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 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
     // 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 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
     // 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))