For PR797:
authorReid Spencer <rspencer@reidspencer.com>
Tue, 22 Aug 2006 16:04:22 +0000 (16:04 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Tue, 22 Aug 2006 16:04:22 +0000 (16:04 +0000)
Make MappedFile not throw any exceptions.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29816 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/System/MappedFile.h
lib/System/Unix/MappedFile.inc

index 3160427a6cd92efcfe038eb7fda4e0649289cf8e..a68303bfb05abdd7036e7ef22ba283dbd8b5aa4a 100644 (file)
@@ -48,8 +48,7 @@ namespace sys {
     /// Construct a MappedFile to the \p path in the operating system's file
     /// system with the mapping \p options provided.
     /// @throws std::string if an error occurs
-    MappedFile(const Path& path, int options = READ_ACCESS)
-      : path_(path), options_(options), base_(0), info_(0) { initialize(); }
+    MappedFile() : path_(), options_(READ_ACCESS), base_(0), info_(0) {}
 
     /// Destruct a MappedFile and release all memory associated with it.
     /// @throws std::string if an error occurs
@@ -101,6 +100,18 @@ namespace sys {
   /// @name Mutators
   /// @{
   public:
+    /// Open a file to be mapped and get its size but don't map it yet.
+    /// @returns true if an error occurred
+    bool open(
+      const sys::Path& p, ///< Path to file to be mapped
+      int options = READ_ACCESS, ///< Access mode for the mapping
+      std::string* ErrMsg = 0 ///< Optional error string pointer
+    ) {
+      path_ = p;
+      options_ = options;
+      return initialize(ErrMsg);
+    }
+
     /// The mapped file is removed from memory. If the file was mapped for
     /// write access, the memory contents will be automatically synchronized
     /// with the file's disk contents.
@@ -108,9 +119,12 @@ namespace sys {
     void unmap();
 
     /// The mapped file is put into memory.
-    /// @returns The base memory address of the mapped file.
+    /// @returns The base memory address of the mapped file or 0 if an error
+    /// occurred.
     /// @brief Map the file into memory.
-    void* map();
+    void* map(
+      std::string* ErrMsg ///< Optional error string pointer
+    );
 
     /// This method causes the size of the file, and consequently the size
     /// of the mapping to be set. This is logically the same as unmap(),
@@ -122,14 +136,17 @@ namespace sys {
     /// @brief Set the size of the file and memory mapping.
     void size(size_t new_size);
 
-    void close() { terminate(); }
+    void close() { if (info_) terminate(); }
 
   /// @}
   /// @name Implementation
   /// @{
   private:
-    void initialize(); ///< Initialize platform-specific portion
-    void terminate();  ///< Terminate platform-specific portion
+    /// @brief Initialize platform-specific portion
+    bool initialize(std::string* ErrMsg); 
+
+    /// @brief Terminate platform-specific portion
+    void terminate();  
 
   /// @}
   /// @name Data
index 341ee254890246d3d106181c770d69952b5a93e1..ef959bacab21751710818c8271d3117e3fba3720 100644 (file)
@@ -39,7 +39,7 @@ struct sys::MappedFileInfo {
   off_t Size;
 };
 
-void MappedFile::initialize() {
+bool MappedFile::initialize(std::string* ErrMsg) {
   int mode = 0;
   if (options_ & READ_ACCESS) 
     if (options_ & WRITE_ACCESS)
@@ -50,17 +50,20 @@ void MappedFile::initialize() {
     mode = O_WRONLY;
 
   int FD = ::open(path_.c_str(), mode);
-  if (FD < 0)
-    ThrowErrno(std::string("Can't open file: ") + path_.toString());
-
+  if (FD < 0) {
+    MakeErrMsg(ErrMsg, "can't open file '" + path_.toString() + "'");
+    return true;
+  } 
   struct stat sbuf;
   if(::fstat(FD, &sbuf) < 0) {
+    MakeErrMsg(ErrMsg, "can't stat file '"+ path_.toString() + "'");
     ::close(FD);
-    ThrowErrno(std::string("Can't stat file: ") + path_.toString());
+    return true;
   }
   info_ = new MappedFileInfo;
   info_->FD = FD;
   info_->Size = sbuf.st_size;
+  return false;
 }
 
 void MappedFile::terminate() {
@@ -79,7 +82,7 @@ void MappedFile::unmap() {
   }
 }
 
-void* MappedFile::map() {
+void* MappedFile::map(std::string* ErrMsg) {
   assert(info_ && "MappedFile not initialized");
   if (!isMapped()) {
     int prot = PROT_NONE;
@@ -106,8 +109,10 @@ void* MappedFile::map() {
       Process::GetPageSize();
 
     base_ = ::mmap(0, map_size, prot, flags, info_->FD, 0);
-    if (base_ == MAP_FAILED)
-      ThrowErrno(std::string("Can't map file:") + path_.toString());
+    if (base_ == MAP_FAILED) {
+      MakeErrMsg(ErrMsg, "Can't map file:" + path_.toString());
+      return 0;
+    }
   }
   return base_;
 }
@@ -139,8 +144,8 @@ void MappedFile::size(size_t new_size) {
     ::write(info_->FD, "\0", 1);
   }
 
-  // Seek to current end of file. 
-  this->map();
+  // Put the mapping back into memory.
+  this->map(0);
 }
 
 }