summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@google.com>2007-09-25 23:08:30 +0000
committerIan Lance Taylor <iant@google.com>2007-09-25 23:08:30 +0000
commit2f71826da5e11a1d15615837e3702cb780381ad4 (patch)
tree1d806713560f46ff83a449c165a8b38c43a7313c
parente97ea7deba00a33e4a7be2d57b959d9786c002fe (diff)
downloadbinutils-redhat-2f71826da5e11a1d15615837e3702cb780381ad4.tar.gz
Use mmap to read from input files.
-rw-r--r--gold/fileread.cc37
-rw-r--r--gold/fileread.h8
2 files changed, 38 insertions, 7 deletions
diff --git a/gold/fileread.cc b/gold/fileread.cc
index 3cb268559b..d87f7731d5 100644
--- a/gold/fileread.cc
+++ b/gold/fileread.cc
@@ -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;
}
diff --git a/gold/fileread.h b/gold/fileread.h
index 0e5caa9a06..cda5d9ce43 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;