30 int headerSize =
sizeof(
struct shm_header) + buffers *
sizeof(qint32);
33 int size = buffers * sizeEach + headerSize;
34 if (!mBuffer.create(size))
36 if (mBuffer.error() == QSharedMemory::AlreadyExists)
38 qWarning(
"Reusing existing buffer -- this should generally not happen");
44 qWarning(
"Failed to create shared memory buffer of size %d: %s",
45 size, mBuffer.errorString().toLatin1().constData());
56 memset(header->
buffer, 0,
sizeof(qint32) * buffers);
73 for (
int i = mCurrentBuffer + 1; i < header->
numBuffers && !found; i++)
75 if (header->
buffer[i] == 0)
78 internalRelease(
false);
82 for (
int i = 0; i < header->
numBuffers && !found; i++)
84 if (header->
buffer[i] == 0)
87 internalRelease(
false);
93 qWarning(
"Could not find an available write buffer");
108 void SharedMemoryServer::internalRelease(
bool lock)
111 if (header && mCurrentBuffer >= 0)
113 if (lock) mBuffer.lock();
116 mLastTimestamp = QDateTime::currentDateTime();
117 header->
timestamp = mLastTimestamp.toMSecsSinceEpoch();
118 if (lock) mBuffer.unlock();
125 internalRelease(
true);
138 bool success = mBuffer.attach(QSharedMemory::ReadWrite);
150 return mBuffer.detach();
160 ( onlyNew && header->
lastDone == mCurrentBuffer) )
165 if (mCurrentBuffer >= 0 && header->
buffer[mCurrentBuffer] > 0)
167 header->
buffer[mCurrentBuffer]--;
172 mTimestamp.setMSecsSinceEpoch(header->
timestamp);
187 if (header && mCurrentBuffer >= 0)
190 if (header->
buffer[mCurrentBuffer] > 0)
192 header->
buffer[mCurrentBuffer]--;
void release()
Release our read buffer.
SharedMemoryClient(QObject *parent=0)
const void * buffer(bool onlyNew=false)
Grab and lock a read buffer.
const void * isNew()
Return new buffer only if new is available, otherwise return NULL.
SharedMemoryServer(QString key, int buffers, int sizeEach, QObject *parent=0)
bool attach(const QString &key)
void release()
Release our write buffer. Buffer will not be used before it is released.
void * buffer()
Grab and lock a write buffer.
Namespace for all CustusX production code.