From d1038c216f2488464a7feda2c5949e126e0c2f1a Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 25 Sep 2007 23:08:30 +0000 Subject: Use mmap to read from input files. --- gold/fileread.cc | 37 +++++++++++++++++++++++++++++++++---- gold/fileread.h | 8 +++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/gold/fileread.cc b/gold/fileread.cc index 3cb2685..d87f773 100644 --- a/gold/fileread.cc +++ b/gold/fileread.cc @@ -26,6 +26,7 @@ #include #include #include +#include #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(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(poff), + static_cast(psize), + strerror(errno)); + gold_exit(false); + } + + const unsigned char* pbytes = static_cast(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; } diff --git a/gold/fileread.h b/gold/fileread.h index 0e5caa9..cda5d9c 100644 --- a/gold/fileread.h +++ b/gold/fileread.h @@ -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; -- cgit v1.1