From cdab043a4b217ee831709ab45c619e7b480fdbff Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 21 Sep 2016 23:05:27 +0200 Subject: [PATCH] Added sharedServer abandon() function, changed sharedServer.myPages container from set to deque --- lib/shared_memory.cpp | 20 +++++++++++++++----- lib/shared_memory.h | 3 ++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/shared_memory.cpp b/lib/shared_memory.cpp index 49030a22..7c98dd70 100644 --- a/lib/shared_memory.cpp +++ b/lib/shared_memory.cpp @@ -790,10 +790,20 @@ namespace IPC { return myPages.size(); } + ///Sets all currently loaded memory pages to non-master, so they are not cleaned up on destruction, but left behind. + ///Useful for doing rolling updates and such. + void sharedServer::abandon(){ + if (!myPages.size()){return;} + VERYHIGH_MSG("Abandoning %llu memory pages, leaving them behind on purpose", myPages.size()); + for (std::deque::iterator it = myPages.begin(); it != myPages.end(); it++) { + (*it).master = false; + } + } + ///\brief Creates the next page with the correct size void sharedServer::newPage() { sharedPage tmp(std::string(baseName.substr(1) + (char)(myPages.size() + (int)'A')), std::min(((8192 * 2) << myPages.size()), (32 * 1024 * 1024)), true); - myPages.insert(tmp); + myPages.push_back(tmp); tmp.master = false; DEBUG_MSG(DLVL_VERYHIGH, "Created a new page: %s", tmp.name.c_str()); } @@ -804,13 +814,13 @@ namespace IPC { DEBUG_MSG(DLVL_WARN, "Can't remove last page for %s", baseName.c_str()); return; } - myPages.erase((*myPages.rbegin())); + myPages.pop_back(); } ///\brief Determines whether an id is currently in use or not bool sharedServer::isInUse(unsigned int id) { unsigned int i = 0; - for (std::set::iterator it = myPages.begin(); it != myPages.end(); it++) { + for (std::deque::iterator it = myPages.begin(); it != myPages.end(); it++) { //return if we reached the end if (!it->mapped || !it->len) { return false; @@ -859,7 +869,7 @@ namespace IPC { } semGuard tmpGuard(&mySemaphore); unsigned int id = 0; - for (std::set::iterator it = myPages.begin(); it != myPages.end(); it++) { + for (std::deque::iterator it = myPages.begin(); it != myPages.end(); it++) { if (!it->mapped || !it->len) { DEBUG_MSG(DLVL_FAIL, "Something went terribly wrong?"); return 0; @@ -900,7 +910,7 @@ namespace IPC { unsigned int userCount = 0; unsigned int emptyCount = 0; connectedUsers = 0; - for (std::set::iterator it = myPages.begin(); it != myPages.end(); it++) { + for (std::deque::iterator it = myPages.begin(); it != myPages.end(); it++) { if (!it->mapped || !it->len) { DEBUG_MSG(DLVL_FAIL, "Something went terribly wrong?"); break; diff --git a/lib/shared_memory.h b/lib/shared_memory.h index 920ca1b8..bc0fe13c 100644 --- a/lib/shared_memory.h +++ b/lib/shared_memory.h @@ -187,6 +187,7 @@ namespace IPC { unsigned int amount; unsigned int connectedUsers; void finishEach(); + void abandon(); private: bool isInUse(unsigned int id); void newPage(); @@ -196,7 +197,7 @@ namespace IPC { ///\brief The length of each consecutive piece of payload unsigned int payLen; ///\brief The set of sharedPage structures to manage the actual memory - std::set myPages; + std::deque myPages; ///\brief A semaphore that is locked upon creation and deletion of the page, to ensure no new data is allocated during this step. semaphore mySemaphore; ///\brief Whether the payload has a counter, if so, it is added in front of the payload