Use mmap to read from input files.
authorIan Lance Taylor <iant@google.com>
Tue, 25 Sep 2007 23:08:30 +0000 (23:08 +0000)
committerIan Lance Taylor <iant@google.com>
Tue, 25 Sep 2007 23:08:30 +0000 (23:08 +0000)
gold/fileread.cc
gold/fileread.h

index 3cb268559b8d63a6653c58f1619150657f83f532..d87f7731d50f10a406dd6b088629e3bffcaa8f7b 100644 (file)
@@ -26,6 +26,7 @@
 #include <cerrno>
 #include <fcntl.h>
 #include <unistd.h>
+#include <sys/mman.h>
 
 #include "options.h"
 #include "dirsearch.h"
@@ -39,7 +40,14 @@ namespace gold
 File_read::View::~View()
 {
   gold_assert(!this->is_locked());
-  delete[] this->data_;
+  if (!this->mapped_)
+    delete[] this->data_;
+  else
+    {
+      if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
+        fprintf(stderr, _("%s: munmap failed: %s\n"),
+                program_name, strerror(errno));
+    }
 }
 
 void
@@ -258,11 +266,32 @@ File_read::find_or_make_view(off_t start, off_t size, bool cache)
       gold_assert(psize >= size);
     }
 
-  unsigned char* p = new unsigned char[psize];
+  File_read::View* v;
 
-  this->do_read(poff, psize, p);
+  if (this->contents_ != NULL)
+    {
+      unsigned char* p = new unsigned char[psize];
+      this->do_read(poff, psize, p);
+      v = new File_read::View(poff, psize, p, cache, false);
+    }
+  else
+    {
+      void* p = ::mmap(NULL, psize, PROT_READ, MAP_SHARED,
+                       this->descriptor_, poff);
+      if (p == MAP_FAILED)
+        {
+          fprintf(stderr, _("%s: %s: mmap offset %lld size %lld failed: %s\n"),
+                  program_name, this->filename().c_str(),
+                  static_cast<long long>(poff),
+                  static_cast<long long>(psize),
+                  strerror(errno));
+          gold_exit(false);
+        }
+
+      const unsigned char* pbytes = static_cast<const unsigned char*>(p);
+      v = new File_read::View(poff, psize, pbytes, cache, true);
+    }
 
-  File_read::View* v = new File_read::View(poff, psize, p, cache);
   ins.first->second = v;
   return v;
 }
index 0e5caa9a06cf2049776224a34984c1c8dd8fcbf7..cda5d9ce43512aaa2330622b2d2c2ecc290d143a 100644 (file)
@@ -114,13 +114,14 @@ class File_read
   File_read(const File_read&);
   File_read& operator=(const File_read&);
 
-  // A view into the file when not using mmap.
+  // A view into the file.
   class View
   {
    public:
-    View(off_t start, off_t size, const unsigned char* data, bool cache)
+    View(off_t start, off_t size, const unsigned char* data, bool cache,
+         bool mapped)
       : start_(start), size_(size), data_(data), lock_count_(0),
-       cache_(cache)
+       cache_(cache), mapped_(mapped)
     { }
 
     ~View();
@@ -163,6 +164,7 @@ class File_read
     const unsigned char* data_;
     int lock_count_;
     bool cache_;
+    bool mapped_;
   };
 
   friend class File_view;