2004-04-21 06:39:20 +00:00
|
|
|
#include <mrw/auto.hpp>
|
|
|
|
#include <unistd.h> // close
|
|
|
|
#include <sys/stat.h> // fstat
|
|
|
|
#include <sys/mman.h> // mmap
|
|
|
|
|
|
|
|
namespace mrw {
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
AutoFile& AutoFile::reset(int fd) throw() {
|
|
|
|
if (_fd!=-1) close(_fd);
|
|
|
|
_fd = fd;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
AutoMapper::AutoMapper(int fd, size_t sz, void* addr,
|
2004-05-05 06:13:29 +00:00
|
|
|
int prot, int flags, off_t off) throw(): _cont(0) {
|
2004-04-21 06:39:20 +00:00
|
|
|
if (!(_sz=sz)) {
|
|
|
|
struct stat st;
|
2004-05-05 06:13:29 +00:00
|
|
|
if (fd==-1 || fstat(fd, &st)==-1) return;
|
2004-04-21 06:39:20 +00:00
|
|
|
_sz = st.st_size;
|
|
|
|
}
|
|
|
|
if ((_cont=mmap(addr, _sz, prot, flags, fd, off))==MAP_FAILED) release();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
AutoMapper::~AutoMapper() throw() {
|
|
|
|
if (_cont && _sz) munmap(_cont, _sz);
|
|
|
|
}
|
|
|
|
}
|