51 int headerSize =
sizeof(
struct shm_header) + buffers *
sizeof(qint32);
54 int size = buffers * sizeEach + headerSize;
55 if (!mBuffer.create(size))
57 if (mBuffer.error() == QSharedMemory::AlreadyExists)
59 qWarning(
"Reusing existing buffer -- this should generally not happen");
65 qWarning(
"Failed to create shared memory buffer of size %d: %s",
66 size, mBuffer.errorString().toLatin1().constData());
77 memset(header->
buffer, 0,
sizeof(qint32) * buffers);
94 for (
int i = mCurrentBuffer + 1; i < header->
numBuffers && !found; i++)
96 if (header->
buffer[i] == 0)
99 internalRelease(
false);
103 for (
int i = 0; i < header->
numBuffers && !found; i++)
105 if (header->
buffer[i] == 0)
108 internalRelease(
false);
114 qWarning(
"Could not find an available write buffer");
129 void SharedMemoryServer::internalRelease(
bool lock)
132 if (header && mCurrentBuffer >= 0)
134 if (lock) mBuffer.lock();
137 mLastTimestamp = QDateTime::currentDateTime();
138 header->
timestamp = mLastTimestamp.toMSecsSinceEpoch();
139 if (lock) mBuffer.unlock();
146 internalRelease(
true);
159 bool success = mBuffer.attach(QSharedMemory::ReadWrite);
171 return mBuffer.detach();
181 ( onlyNew && header->
lastDone == mCurrentBuffer) )
186 if (mCurrentBuffer >= 0 && header->
buffer[mCurrentBuffer] > 0)
188 header->
buffer[mCurrentBuffer]--;
193 mTimestamp.setMSecsSinceEpoch(header->
timestamp);
208 if (header && mCurrentBuffer >= 0)
211 if (header->
buffer[mCurrentBuffer] > 0)
213 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.