From 774b8688cace7d07b454dc1e6e20d5965ded3708 Mon Sep 17 00:00:00 2001 From: Michael Krasnyk Date: Sun, 25 Dec 2016 16:53:10 +0100 Subject: [PATCH] read_only if shmem size is 0, read_write otherwise --- include/storage/shared_memory.hpp | 13 +++++-------- src/storage/storage.cpp | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/include/storage/shared_memory.hpp b/include/storage/shared_memory.hpp index 296552caa..2e2c452d1 100644 --- a/include/storage/shared_memory.hpp +++ b/include/storage/shared_memory.hpp @@ -52,12 +52,9 @@ class SharedMemory template SharedMemory(const boost::filesystem::path &lock_file, const IdentifierT id, - const uint64_t size = 0, - bool read_write = false) + const uint64_t size = 0) : key(lock_file.string().c_str(), id) { - const auto access = - read_write ? boost::interprocess::read_write : boost::interprocess::read_only; // open only if (0 == size) { @@ -65,7 +62,7 @@ class SharedMemory util::Log(logDEBUG) << "opening " << shm.get_shmid() << " from id " << id; - region = boost::interprocess::mapped_region(shm, access); + region = boost::interprocess::mapped_region(shm, boost::interprocess::read_only); } // open or create else @@ -83,7 +80,7 @@ class SharedMemory } } #endif - region = boost::interprocess::mapped_region(shm, access); + region = boost::interprocess::mapped_region(shm, boost::interprocess::read_write); } } @@ -232,7 +229,7 @@ class SharedMemory template std::unique_ptr -makeSharedMemory(const IdentifierT &id, const uint64_t size = 0, bool read_write = false) +makeSharedMemory(const IdentifierT &id, const uint64_t size = 0) { try { @@ -248,7 +245,7 @@ makeSharedMemory(const IdentifierT &id, const uint64_t size = 0, bool read_write boost::filesystem::ofstream ofs(lock_file()); } } - return std::make_unique(lock_file(), id, size, read_write); + return std::make_unique(lock_file(), id, size); } catch (const boost::interprocess::interprocess_exception &e) { diff --git a/src/storage/storage.cpp b/src/storage/storage.cpp index 4662eb6c3..b2b2ee36a 100644 --- a/src/storage/storage.cpp +++ b/src/storage/storage.cpp @@ -175,14 +175,14 @@ Storage::ReturnCode Storage::Run(int max_wait) // Allocate shared memory block auto regions_size = sizeof(layout) + layout.GetSizeOfLayout(); util::Log() << "allocating shared memory of " << regions_size << " bytes"; - auto shared_memory = makeSharedMemory(data_region, regions_size, true); + auto shared_memory = makeSharedMemory(data_region, regions_size); // Copy memory layout to shared memory and populate data char *shared_memory_ptr = static_cast(shared_memory->Ptr()); memcpy(shared_memory_ptr, &layout, sizeof(layout)); PopulateData(layout, shared_memory_ptr + sizeof(layout)); - auto data_type_memory = makeSharedMemory(CURRENT_REGION, sizeof(SharedDataTimestamp), true); + auto data_type_memory = makeSharedMemory(CURRENT_REGION, sizeof(SharedDataTimestamp)); SharedDataTimestamp *data_timestamp_ptr = static_cast(data_type_memory->Ptr());