~ use local memory if no shared memory exists

This commit is contained in:
Julian Hinxlage 2019-11-12 19:05:33 +01:00
parent 7b2ddf9e0a
commit 5aa0a7862f
1 changed files with 12 additions and 10 deletions

View File

@ -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);
}
}
}