mirror of
https://github.com/ksherlock/profuse.git
synced 2025-01-10 23:29:42 +00:00
git-svn-id: https://profuse.googlecode.com/svn/branches/v2@200 aa027e90-d47c-11dd-86d7-074df07e0730
This commit is contained in:
parent
2754cdd24a
commit
6d4e06b403
61
MappedFile.cpp
Normal file
61
MappedFile.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <cerrno>
|
||||
|
||||
class MappedFile
|
||||
{
|
||||
public:
|
||||
|
||||
MappedFile(int fd, bool readOnly)
|
||||
{
|
||||
_fd = fd;
|
||||
_length = ::lseek(fd, 0, SEEK_END);
|
||||
_address = MAP_FAILED;
|
||||
if (_length > 0)
|
||||
{
|
||||
_address = ::mmap(0, _length,
|
||||
readOnly ? PROT_READ : PROT_READ | PROT_WRITE,
|
||||
MAP_FILE, fd, 0);
|
||||
}
|
||||
}
|
||||
|
||||
MappedFile(void *address, size_t size)
|
||||
{
|
||||
_fd = -1;
|
||||
_address = address;
|
||||
_size = size;
|
||||
}
|
||||
|
||||
~MappedFile()
|
||||
{
|
||||
if (_address != MAP_FAILED) ::munmap(_address, _length);
|
||||
if (_fd != -1) ::close(_fd);
|
||||
}
|
||||
|
||||
void *address() const
|
||||
{
|
||||
return _address;
|
||||
}
|
||||
|
||||
size_t length()
|
||||
{
|
||||
return _length;
|
||||
}
|
||||
|
||||
int sync()
|
||||
{
|
||||
|
||||
if (::msync(_address, _length, MS_SYNC) == 0)
|
||||
return 0;
|
||||
return errno;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private:
|
||||
int _fd;
|
||||
void *_address;
|
||||
size_t _length;
|
||||
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user