[llvm-commits] [llvm] r49029 - in /llvm/trunk: include/llvm/Bitcode/Archive.h lib/Archive/Archive.cpp lib/Archive/ArchiveReader.cpp lib/Archive/ArchiveWriter.cpp

Chris Lattner sabre at nondot.org
Mon Mar 31 21:26:46 PDT 2008


Author: lattner
Date: Mon Mar 31 23:26:46 2008
New Revision: 49029

URL: http://llvm.org/viewvc/llvm-project?rev=49029&view=rev
Log:
change the archive stuff to use MemoryBuffer instead of mappedfile.
MemoryBuffer is higher level and more closely matches the model
needed.

Modified:
    llvm/trunk/include/llvm/Bitcode/Archive.h
    llvm/trunk/lib/Archive/Archive.cpp
    llvm/trunk/lib/Archive/ArchiveReader.cpp
    llvm/trunk/lib/Archive/ArchiveWriter.cpp

Modified: llvm/trunk/include/llvm/Bitcode/Archive.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/Archive.h?rev=49029&r1=49028&r2=49029&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Bitcode/Archive.h (original)
+++ llvm/trunk/include/llvm/Bitcode/Archive.h Mon Mar 31 23:26:46 2008
@@ -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 @@
   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

Modified: llvm/trunk/lib/Archive/Archive.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Archive/Archive.cpp?rev=49029&r1=49028&r2=49029&view=diff

==============================================================================
--- llvm/trunk/lib/Archive/Archive.cpp (original)
+++ llvm/trunk/lib/Archive/Archive.cpp Mon Mar 31 23:26:46 2008
@@ -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))
-    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();

Modified: llvm/trunk/lib/Archive/ArchiveReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Archive/ArchiveReader.cpp?rev=49029&r1=49028&r2=49029&view=diff

==============================================================================
--- llvm/trunk/lib/Archive/ArchiveReader.cpp (original)
+++ llvm/trunk/lib/Archive/ArchiveReader.cpp Mon Mar 31 23:26:46 2008
@@ -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

Modified: llvm/trunk/lib/Archive/ArchiveWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Archive/ArchiveWriter.cpp?rev=49029&r1=49028&r2=49029&view=diff

==============================================================================
--- llvm/trunk/lib/Archive/ArchiveWriter.cpp (original)
+++ llvm/trunk/lib/Archive/ArchiveWriter.cpp Mon Mar 31 23:26:46 2008
@@ -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))





More information about the llvm-commits mailing list