#include <cerrno>
#include <fcntl.h>
#include <unistd.h>
+#include <sys/mman.h>
#include "options.h"
#include "dirsearch.h"
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
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;
}
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();
const unsigned char* data_;
int lock_count_;
bool cache_;
+ bool mapped_;
};
friend class File_view;