From e75be684660e01b2110b0673cce1462e61a44405 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Fri, 9 Oct 2015 17:03:37 +0200 Subject: [PATCH] Only lock the virtual address space when shared memory was requested In addition, some improvements: - unlock only when locking succeeded - scoped exception safe RAII locker Reference: - https://github.com/Project-OSRM/osrm-backend/issues/1698#issuecomment-144003177 --- routed.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/routed.cpp b/routed.cpp index 89a9b3a18..96b954edd 100644 --- a/routed.cpp +++ b/routed.cpp @@ -91,11 +91,23 @@ int main(int argc, const char *argv[]) } #ifdef __linux__ - const int lock_flags = MCL_CURRENT | MCL_FUTURE; - if (-1 == mlockall(lock_flags)) + struct MemoryLocker final { - SimpleLogger().Write(logWARNING) << argv[0] << " could not be locked to RAM"; - } + explicit MemoryLocker(bool shouldLock_) : shouldLock(shouldLock_) + { + if (-1 == mlockall(MCL_CURRENT | MCL_FUTURE)) + { + couldLock = false; + SimpleLogger().Write(logWARNING) << "memory could not be locked to RAM"; + } + } + ~MemoryLocker() + { + if (couldLock) + (void)munlockall(); + } + bool shouldLock = false, couldLock = false; + } memoryLocker(lib_config.use_shared_memory); #endif SimpleLogger().Write() << "starting up engines, " << OSRM_VERSION; @@ -177,9 +189,6 @@ int main(int argc, const char *argv[]) SimpleLogger().Write(logWARNING) << "exception: " << e.what(); return 1; } -#ifdef __linux__ - munlockall(); -#endif return 0; }