| Home · All Classes · Main Classes · Deprecated |
MSharedData is a helper ilass that implements a shared memory buffer. It can have an arbitrary size and support complex data structures through serialization. Internally, MSharedData uses QSharedMemory. More...


Public Types | |
| enum | OpenMode { ReadOnly, Write } |
Public Member Functions | |
| MSharedData (const QString &key, int pageSize=8192) | |
| ~MSharedData () | |
| bool | open (OpenMode mode) |
| void | close () |
| QString | errorString () const |
| bool | setPageSize (int pageSize) |
| int | pageSize () const |
| qint64 | pos () const |
| bool | seek (qint64 offset) |
| MSharedData & | operator<< (int i) |
| MSharedData & | operator<< (bool flag) |
| MSharedData & | operator<< (qreal r) |
| MSharedData & | operator<< (QFont font) |
| MSharedData & | operator<< (QVariant v) |
| MSharedData & | operator<< (QString str) |
| MSharedData & | operator<< (QByteArray array) |
| MSharedData & | operator>> (int &i) |
| MSharedData & | operator>> (bool &i) |
| MSharedData & | operator>> (qreal &r) |
| MSharedData & | operator>> (QFont &font) |
| MSharedData & | operator>> (QVariant &v) |
| MSharedData & | operator>> (QString &str) |
| MSharedData & | operator>> (QByteArray &array) |
Protected Attributes | |
| MSharedDataPrivate *const | d_ptr |
Properties | |
| int | pageSize |
MSharedData is a helper ilass that implements a shared memory buffer. It can have an arbitrary size and support complex data structures through serialization. Internally, MSharedData uses QSharedMemory.
MSharedMemory is optimized in terms of its memory consumption. It uses paging to vary its size. Initially, only 1 page is allocated. However, additional pages are allocated as needed.
QSharedMemory provides a plain buffer. It is often needed to store data structures with internal pointers, for example QList. Serialization is a mechanism that allows saving such data structures to hard drives. We are employing the same mechanism to store such a data structure in a shared memory buffer. Similar to QDataStream, MSharedData provides operators << and >> to save and load complex data structures. It is possible to open MSharedData in either of two modes: ReadOnly and Write.
Even though MSharedData employs paging mechanism to facilitate gradual buffer growth, to the programmer it appears as one continuous area of memory. Therefore, functions pos() and seek() use continuous offset which always increases as more data is written into the memory, even after new pages are created.
The use case is as follows. The theme daemon creates a shared memory buffer and populates it with data:
MSharedData *shm = new MSharedData("buffer"); shm->open(MSharedData::Write); *shm << QString("data"); *shm << qVariantFromValue(QColor(Qt::white)); shm->close();
The application opens the shared buffer for reading and de-serializes the data:
MSharedData *shm = new MSharedData("buffer"); shm->open(MSharedData::ReadOnly); QString str; QVariant color; *shm >> str >> color; shm->close();
Definition at line 80 of file corelib/core/mshareddata.h.
Definition at line 88 of file corelib/core/mshareddata.h.
{ ReadOnly, Write };
| MSharedData::MSharedData | ( | const QString & | key, | |
| int | pageSize = 8192 | |||
| ) |
A constructor. Creates a shared memory buffer with a given key.
Definition at line 166 of file mshareddata.cpp.
: d_ptr(new MSharedDataPrivate) { Q_D(MSharedData); d->q_ptr = this; d->pageSize = pageSize; d->setKey(key); d->mode = -1; const int indexSize = 10000; if (!d->shm->create(indexSize)) { //qDebug() << "unable to allocate shared memory " << key; if (!d->shm->attach()) { d->errorString = QString("unable to attach ") + key; qDebug() << d->errorString; } } else { // clean the index page memset((unsigned char *)d->shm->data(), 0, d->shm->size()); } d->currentPage = NULL; }
| MSharedData::~MSharedData | ( | ) |
A destructor. Detaches from every page it was attached to.
Definition at line 193 of file mshareddata.cpp.
{
Q_D(MSharedData);
foreach(QSharedMemory * mem, d->ownedPages) {
mem->detach();
delete mem;
}
d->shm->detach();
delete d->shm;
delete d_ptr;
}

| void MSharedData::close | ( | ) |
Closes the memory buffer and flushes all updated pages.
Definition at line 535 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode == MSharedData::Write) {
if (d->dataBuffer.pos() != 0) {
d->flushPage();
} else {
// nothing was written on that page, destroy it
d->currentPage->detach();
d->ownedPages.removeOne(d->currentPage);
delete d->currentPage;
}
} else if (d->mode == MSharedData::ReadOnly) {
if (d->currentPage) {
d->currentPage->detach();
delete d->currentPage;
}
}
d->currentPage = 0;
d->dataBuffer.close();
d->mode = -1;
d->errorString = "";
}
| QString MSharedData::errorString | ( | ) | const |
Returns the error string. Empty if no error has occurred.
Definition at line 208 of file mshareddata.cpp.
{
Q_D(const MSharedData);
return d->errorString;
}
| bool MSharedData::open | ( | MSharedData::OpenMode | mode | ) |
Opens the shared memory buffer for reading or for writing.
Definition at line 499 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (!d->errorString.isEmpty())
return false;
if (d->mode != -1) {
d->errorString = "Cannot open again. This shared data object is already open";
qDebug() << d->errorString;
return false;
}
d->errorString = "";
d->mode = mode;
if (mode == MSharedData::Write) {
memset((unsigned char *)d->shm->data(), 0, d->shm->size());
if (d->allocateNewPage())
return true;
else
return false;
} else if (mode == MSharedData::ReadOnly) {
if (d->attachToPage(0))
return true;
else
return false;
} else {
d->errorString = "Unknown openmode. Only MSharedData::Write and MSharedData::ReadOnly are possible";
qDebug() << d->errorString;
return false;
}
}
| MSharedData & MSharedData::operator<< | ( | int | i | ) |
Writes integer to shared memory
Definition at line 214 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::Write) {
qDebug() << "cannot write. Buffer not open or ReadOnly";
return *this;
}
d->savePos();
d->dataStream << i;
if (d->pageOverflown()) {
d->dataStream << i;
}
return *this;
}
| MSharedData & MSharedData::operator<< | ( | bool | flag | ) |
Writes boolean to shared memory
Definition at line 231 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::Write) {
qDebug() << "cannot write. Buffer not open or ReadOnly";
return *this;
}
d->savePos();
d->dataStream << flag;
if (d->pageOverflown()) {
d->dataStream << flag;
}
return *this;
}
| MSharedData & MSharedData::operator<< | ( | qreal | r | ) |
Writes qreal to shared memory
Definition at line 248 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::Write) {
qDebug() << "cannot write. Buffer not open or ReadOnly";
return *this;
}
d->savePos();
d->dataStream << r;
if (d->pageOverflown()) {
d->dataStream << r;
}
return *this;
}
| MSharedData & MSharedData::operator<< | ( | QFont | font | ) |
Write QFont to shared memory
Definition at line 265 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::Write) {
qDebug() << "cannot write. Buffer not open or ReadOnly";
return *this;
}
d->savePos();
d->dataStream << font;
if (d->pageOverflown()) {
d->dataStream << font;
}
return *this;
}
| MSharedData & MSharedData::operator<< | ( | QVariant | v | ) |
Writes QVariant to shared memory
Definition at line 282 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::Write) {
qDebug() << "cannot write. Buffer not open or ReadOnly";
return *this;
}
d->savePos();
d->dataStream << v;
if (d->pageOverflown()) {
d->dataStream << v;
}
return *this;
}
| MSharedData & MSharedData::operator<< | ( | QString | str | ) |
Writes QString to shared memory
Definition at line 299 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::Write) {
qDebug() << "cannot write. Buffer not open or ReadOnly";
return *this;
}
d->savePos();
d->dataStream << str;
if (d->pageOverflown()) {
d->dataStream << str;
}
return *this;
}
| MSharedData & MSharedData::operator<< | ( | QByteArray | array | ) |
Writes QByteArray to shared memory
Definition at line 317 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::Write) {
qDebug() << "cannot write. Buffer not open or ReadOnly";
return *this;
}
d->savePos();
d->dataStream << array;
if (d->pageOverflown()) {
d->dataStream << array;
}
return *this;
}
| MSharedData & MSharedData::operator>> | ( | QVariant & | v | ) |
Reads QVariant from shared memory
Definition at line 395 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::ReadOnly) {
qDebug() << "cannot read. Buffer not open or open for writing";
return *this;
}
d->nextPage();
d->dataStream >> v;
return *this;
}
| MSharedData & MSharedData::operator>> | ( | bool & | i | ) |
Reads boolean from shared memory
Definition at line 350 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::ReadOnly) {
qDebug() << "cannot read. Buffer not open or open for writing";
return *this;
}
d->nextPage();
d->dataStream >> flag;
return *this;
}
| MSharedData & MSharedData::operator>> | ( | QString & | str | ) |
Reads QString from shared memory
Definition at line 410 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::ReadOnly) {
qDebug() << "cannot read. Buffer not open or open for writing";
return *this;
}
d->nextPage();
d->dataStream >> str;
return *this;
}
| MSharedData & MSharedData::operator>> | ( | QFont & | font | ) |
Reads QFont from shared memory
Definition at line 380 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::ReadOnly) {
qDebug() << "cannot read. Buffer not open or open for writing";
return *this;
}
d->nextPage();
d->dataStream >> font;
return *this;
}
| MSharedData & MSharedData::operator>> | ( | qreal & | r | ) |
Reads qreal from shared memory
Definition at line 365 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::ReadOnly) {
qDebug() << "cannot read. Buffer not open or open for writing";
return *this;
}
d->nextPage();
d->dataStream >> r;
return *this;
}
| MSharedData & MSharedData::operator>> | ( | QByteArray & | array | ) |
Reads QByteArray from shared memory
Definition at line 425 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::ReadOnly) {
qDebug() << "cannot read. Buffer not open or open for writing";
return *this;
}
d->nextPage();
d->dataStream >> array;
return *this;
}
| MSharedData & MSharedData::operator>> | ( | int & | i | ) |
Reads integer from shared memory
Definition at line 335 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != MSharedData::ReadOnly) {
qDebug() << "cannot read. Buffer not open or open for writing";
return *this;
}
d->nextPage();
d->dataStream >> i;
return *this;
}
| int MSharedData::pageSize | ( | ) | const |
Returns the page size.
| qint64 MSharedData::pos | ( | ) | const |
Returns the current offset within the shared memory buffer.
Definition at line 461 of file mshareddata.cpp.
{
Q_D(const MSharedData);
return d->currentPageOffset + d->dataBuffer.pos();
}
| bool MSharedData::seek | ( | qint64 | offset | ) |
Sets the current offset within the shared memory buffer.
Definition at line 467 of file mshareddata.cpp.
{
Q_D(MSharedData);
int i = 0, pageOffset;
bool res = false;
if (d->mode != MSharedData::ReadOnly) {
d->errorString = "Cannot seek in buffer which is not ReadOnly";
qDebug() << d->errorString;
return false;
}
while ((pageOffset = *((int *)d->shm->data() + i))) {
if (offset < pageOffset) {
d->attachToPage(i);
qint64 realOffset = (i == 0) ? offset : offset - *((int *)d->shm->data() + i - 1);
res = d->dataBuffer.seek(realOffset);
if (!res)
d->errorString = d->dataBuffer.errorString();
return res;
}
i++;
}
d->errorString = QString("Offset %1 not found").arg(offset);
qDebug() << d->errorString;
return false;
}
| bool MSharedData::setPageSize | ( | int | pageSize | ) |
Sets the page size. The default value is 8192 bytes. Returns true if the new page size was set, false if there was an error.
Definition at line 440 of file mshareddata.cpp.
{
Q_D(MSharedData);
if (d->mode != -1) {
d->errorString = "cannot change pageSize after shared data buffer was open";
qDebug() << d->errorString;
return false;
} else {
d->pageSize = pageSize;
return true;
}
}
MSharedDataPrivate* const MSharedData::d_ptr [protected] |
Definition at line 211 of file corelib/core/mshareddata.h.
int MSharedData::pageSize [read, write] |
Definition at line 84 of file corelib/core/mshareddata.h.
| Copyright © 2010 Nokia Corporation | Generated on Thu Nov 4 2010 18:14:26 (PDT) Doxygen 1.7.1 |
MeeGo Touch |