Use mmap to read from input files.

This commit is contained in:
Ian Lance Taylor 2007-09-25 23:08:30 +00:00
parent 9eb9fa57c2
commit d1038c216f
2 changed files with 38 additions and 7 deletions

View 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;
}

View 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;