102 lines
2.8 KiB
C++
102 lines
2.8 KiB
C++
#include "shared_mem_buffer.h"
|
|
|
|
#include <QDebug>
|
|
#include <atomic>
|
|
|
|
#ifdef QT_POSIX_IPC
|
|
// The POSIX backend can be explicitly selected using the -feature-ipc_posix
|
|
// option to the Qt configure script. If it is enabled, the QT_POSIX_IPC
|
|
// macro will be defined. -> we use SystemV shared memory
|
|
#error "QT_POSIX_IPC defined"
|
|
#else
|
|
#ifdef __linux__
|
|
#include <sys/ipc.h> // ftok
|
|
#endif
|
|
#endif
|
|
|
|
//static bool shdMemFirstUse;
|
|
|
|
|
|
//QSharedMemory *SharedMemBuffer::getShm(std::size_t size) {
|
|
QSharedMemory *SharedMem::getShm(std::size_t size) {
|
|
static QSharedMemory shMem;
|
|
if (size > 0) {
|
|
#ifdef __linux__
|
|
//static const long nativeKey = ftok("/etc/os-release", 'H');
|
|
//static const QString fkey = std::to_string(nativeKey).c_str();
|
|
static const QString fkey = "0123456?000=7";
|
|
#else
|
|
static const QString fkey = "0123456?000=9";
|
|
#endif
|
|
|
|
shMem.setKey(fkey);
|
|
if (!shMem.isAttached()) {
|
|
if (shMem.create(size)) {
|
|
return &shMem;
|
|
} else {
|
|
if (shMem.error() == QSharedMemory::AlreadyExists) {
|
|
if (shMem.attach()) {
|
|
return &shMem;
|
|
}
|
|
}
|
|
}
|
|
qCritical() << shMem.nativeKey() << shMem.key() << shMem.data()
|
|
<< shMem.error() << shMem.errorString();
|
|
return nullptr;
|
|
}
|
|
}
|
|
return &shMem;
|
|
}
|
|
|
|
|
|
// std::atomic_bool SharedMemBuffer::__sharedMemLocked{false};
|
|
/*
|
|
//QSharedMemory *SharedMemBuffer::getShm(std::size_t size) {
|
|
QSharedMemory *SharedMem::getShm(std::size_t size) {
|
|
|
|
static QSharedMemory shMem;
|
|
if (size > 0) {
|
|
#ifdef __linux__
|
|
static const long nativeKey = ftok("/etc/os-release", 'H');
|
|
static const QString fkey = std::to_string(nativeKey).c_str();
|
|
#else
|
|
static const QString fkey = "0123456?000=9";
|
|
#endif
|
|
shdMemFirstUse=false;
|
|
shMem.setKey(fkey);
|
|
if (!shMem.isAttached())
|
|
{
|
|
if (shMem.create(size))
|
|
{
|
|
// sm was created successful, did not exist before
|
|
shdMemFirstUse=true;
|
|
return &shMem;
|
|
} else
|
|
{
|
|
// create was false because mem already existed
|
|
if (shMem.error() == QSharedMemory::AlreadyExists)
|
|
{
|
|
if (shMem.attach())
|
|
{
|
|
return &shMem;
|
|
}
|
|
}
|
|
}
|
|
qCritical() << shMem.nativeKey() << shMem.key() << shMem.data()
|
|
<< shMem.error() << shMem.errorString();
|
|
return nullptr;
|
|
}
|
|
}
|
|
return &shMem;
|
|
}
|
|
|
|
|
|
bool shdMem_firstUse(void)
|
|
{
|
|
return shdMemFirstUse;
|
|
}
|
|
*/
|
|
|
|
|
|
|