Files
mrw-cxx/mrw/auto.cpp
2004-04-21 06:39:20 +00:00

32 lines
924 B
C++

#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,
int prot, int flags, off_t off) throw() {
if (!(_sz=sz)) {
struct stat st;
if (fd==-1 || fstat(fd, &st)==-1) {release(); return;}
_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);
}
}