From 5aa0a7862f74853752ecdad299ce58b975cd2783 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 19:05:33 +0100 Subject: [PATCH] ~ use local memory if no shared memory exists --- src/SharedMemoryAccess.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 456e47b..7e12c69 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -63,23 +63,25 @@ void writeSharedMemory(char *data, int size, int offset) { } char *getSharedMemory() { - constexpr bool useLocal = false; + bool useLocal = false; - if(useLocal){ + if(!useLocal){ + int shmId = shmget(impl.sharedMemoryKey, NULL, 0); + if(shmId < 0) { + //no shared memory found + useLocal = true; + } else { + return (char *) shmat(shmId, NULL, 0); + } + } + + if(useLocal) { //using a local buffer for shared memory testing if (localSharedMemory.empty()) { initSemaphore(); localSharedMemory.resize(impl.sharedMemorySize * 1024); } return &localSharedMemory[0]; - }else { - int shmId = shmget(impl.sharedMemoryKey, NULL, 0); - if(shmId < 0) { - // we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason - return nullptr; - } else { - return (char *) shmat(shmId, NULL, 0); - } } }