summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/sys/posix/MemoryMappedFile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/src/qpid/sys/posix/MemoryMappedFile.cpp')
-rw-r--r--cpp/src/qpid/sys/posix/MemoryMappedFile.cpp31
1 files changed, 22 insertions, 9 deletions
diff --git a/cpp/src/qpid/sys/posix/MemoryMappedFile.cpp b/cpp/src/qpid/sys/posix/MemoryMappedFile.cpp
index f647ab3943..b4292aa4bc 100644
--- a/cpp/src/qpid/sys/posix/MemoryMappedFile.cpp
+++ b/cpp/src/qpid/sys/posix/MemoryMappedFile.cpp
@@ -29,13 +29,14 @@
namespace qpid {
namespace sys {
namespace {
+const std::string PAGEFILE_PREFIX("pf_");
const std::string PATH_SEPARATOR("/");
const std::string ESCAPE("%");
const std::string VALID("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.");
std::string getFileName(const std::string& name, const std::string& dir)
{
std::stringstream filename;
- if (dir.size()) filename << dir << PATH_SEPARATOR;
+ if (dir.size()) filename << dir << PATH_SEPARATOR << PAGEFILE_PREFIX;
size_t start = 0;
while (true) {
size_t i = name.find_first_not_of(VALID, start);
@@ -55,31 +56,39 @@ std::string getFileName(const std::string& name, const std::string& dir)
class MemoryMappedFilePrivate
{
friend class MemoryMappedFile;
+ std::string path;
int fd;
MemoryMappedFilePrivate() : fd(0) {}
};
MemoryMappedFile::MemoryMappedFile() : state(new MemoryMappedFilePrivate) {}
MemoryMappedFile::~MemoryMappedFile() { delete state; }
-std::string MemoryMappedFile::open(const std::string& name, const std::string& directory)
+void MemoryMappedFile::open(const std::string& name, const std::string& directory)
{
- std::string path = getFileName(name, directory);
+ // Ensure directory exists
+ if ( ::mkdir(directory.c_str(), S_IRWXU | S_IRGRP | S_IXGRP )!=0 && errno!=EEXIST ) {
+ throw qpid::Exception(QPID_MSG("Failed to create memory mapped file directory " << directory << ": " << qpid::sys::strError(errno)));
+ }
+
+ state->path = getFileName(name, directory);
- int flags = O_CREAT | O_EXCL | O_RDWR;
- int fd = ::open(path.c_str(), flags, S_IRUSR | S_IWUSR);
- if (fd == -1) throw qpid::Exception(QPID_MSG("Failed to open memory mapped file " << path << ": " << qpid::sys::strError(errno) << " [flags=" << flags << "]"));
+ int flags = O_CREAT | O_TRUNC | O_RDWR;
+ int fd = ::open(state->path.c_str(), flags, S_IRUSR | S_IWUSR);
+ if (fd == -1) throw qpid::Exception(QPID_MSG("Failed to open memory mapped file " << state->path << ": " << qpid::sys::strError(errno) << " [flags=" << flags << "]"));
state->fd = fd;
- return path;
}
-void MemoryMappedFile::close(const std::string& path)
+
+void MemoryMappedFile::close()
{
::close(state->fd);
- ::unlink(path.c_str());
+ ::unlink(state->path.c_str());
}
+
size_t MemoryMappedFile::getPageSize()
{
return ::sysconf(_SC_PAGE_SIZE);
}
+
char* MemoryMappedFile::map(size_t offset, size_t size)
{
int protection = PROT_READ | PROT_WRITE;
@@ -90,20 +99,24 @@ char* MemoryMappedFile::map(size_t offset, size_t size)
return region;
}
+
void MemoryMappedFile::unmap(char* region, size_t size)
{
::munmap(region, size);
}
+
void MemoryMappedFile::flush(char* region, size_t size)
{
::msync(region, size, MS_ASYNC);
}
+
void MemoryMappedFile::expand(size_t offset)
{
if ((::lseek(state->fd, offset - 1, SEEK_SET) == -1) || (::write(state->fd, "", 1) == -1)) {
throw qpid::Exception(QPID_MSG("Failed to expand paged queue file: " << qpid::sys::strError(errno)));
}
}
+
bool MemoryMappedFile::isSupported()
{
return true;