src/hbcore/core/hbsharedmemorymanager_p.cpp
changeset 28 b7da29130b0e
parent 23 e6ad4ef83b23
equal deleted inserted replaced
23:e6ad4ef83b23 28:b7da29130b0e
   251         // this is not server so just attach to shared memory chunk in ReadOnly mode
   251         // this is not server so just attach to shared memory chunk in ReadOnly mode
   252         success = chunk->attach(QSharedMemory::ReadOnly);
   252         success = chunk->attach(QSharedMemory::ReadOnly);
   253         writable = false;
   253         writable = false;
   254     }
   254     }
   255     if ( !success ) {
   255     if ( !success ) {
   256 #ifdef THEME_SERVER_TRACES
   256         THEME_GENERIC_DEBUG() << "HbSharedMemoryManager:: Could not initialize shared memory chunk";
   257         qDebug() << "HbSharedMemoryManager:: Could not initialize shared memory chunk";
       
   258 #endif //THEME_SERVER_TRACES
       
   259         delete chunk; 
   257         delete chunk; 
   260         chunk = 0;
   258         chunk = 0;
   261     }
   259     }
   262     if (success && isWritable()) {
   260     if (success && isWritable()) {
   263         // if we are recovering from theme server crash, shared chunk may
   261         // if we are recovering from theme server crash, shared chunk may
   285 #endif
   283 #endif
   286             // Put main allocator after the memory file or if memory file was not loaded, after chunk header.
   284             // Put main allocator after the memory file or if memory file was not loaded, after chunk header.
   287             chunkHeader->mainAllocatorOffset = memoryFileSize ? ALIGN(memoryFileSize)
   285             chunkHeader->mainAllocatorOffset = memoryFileSize ? ALIGN(memoryFileSize)
   288                                                               : sizeof(HbSharedChunkHeader);
   286                                                               : sizeof(HbSharedChunkHeader);
   289             // Clear also allocator identifier so that they will not try to re-connect
   287             // Clear also allocator identifier so that they will not try to re-connect
   290             int *mainAllocatorIdentifier = address<int>(chunkHeader->mainAllocatorOffset);
   288             quint32 *mainAllocatorIdentifier = address<quint32>(chunkHeader->mainAllocatorOffset);
   291             *mainAllocatorIdentifier = 0;
   289             *mainAllocatorIdentifier = 0;
   292             mainAllocator->initialize(chunk, chunkHeader->mainAllocatorOffset);
   290             mainAllocator->initialize(chunk, chunkHeader->mainAllocatorOffset);
   293             chunkHeader->subAllocatorOffset = alloc(SPACE_NEEDED_FOR_MULTISEGMENT_ALLOCATOR);
   291             chunkHeader->subAllocatorOffset = alloc(SPACE_NEEDED_FOR_MULTISEGMENT_ALLOCATOR);
   294             int *subAllocatorIdentifier = address<int>(chunkHeader->subAllocatorOffset);
   292             quint32 *subAllocatorIdentifier = address<quint32>(chunkHeader->subAllocatorOffset);
   295             *subAllocatorIdentifier = 0;
   293             *subAllocatorIdentifier = 0;
   296             subAllocator->initialize(chunk, chunkHeader->subAllocatorOffset, mainAllocator);
   294             subAllocator->initialize(chunk, chunkHeader->subAllocatorOffset, mainAllocator);
   297             chunkHeader->identifier = INITIALIZED_CHUNK_IDENTIFIER;
   295             chunkHeader->identifier = INITIALIZED_CHUNK_IDENTIFIER;
   298             
   296             
   299             if (!binCSSConverterApp) {
   297             if (!binCSSConverterApp) {
   411  */
   409  */
   412 void HbSharedMemoryManager::free(qptrdiff offset)
   410 void HbSharedMemoryManager::free(qptrdiff offset)
   413 {
   411 {
   414     // don't do anything when freeing NULL (pointer)offset
   412     // don't do anything when freeing NULL (pointer)offset
   415     if (isWritable() && (offset > 0)) {
   413     if (isWritable() && (offset > 0)) {
   416         int metaData = *address<int>(offset - sizeof(int));
   414         qptrdiff metaData = *address<qptrdiff>(offset - sizeof(qptrdiff));
   417 #ifdef HB_THEME_SERVER_MEMORY_REPORT
   415 #ifdef HB_THEME_SERVER_MEMORY_REPORT
   418         int size = 0;
   416         int size = 0;
   419         if (metaData & MAIN_ALLOCATOR_IDENTIFIER) {
   417         if (metaData & MAIN_ALLOCATOR_IDENTIFIER) {
   420             size = mainAllocator->allocatedSize(offset);
   418             size = mainAllocator->allocatedSize(offset);
   421         } else {
   419         } else {
   460         if (offset > 0) {
   458         if (offset > 0) {
   461 #ifdef HB_BIN_CSS
   459 #ifdef HB_BIN_CSS
   462             HbCssConverterUtils::cellMoved(offset, newOffset);
   460             HbCssConverterUtils::cellMoved(offset, newOffset);
   463 #endif
   461 #endif
   464             unsigned char *scrPtr = address<unsigned char>(offset);
   462             unsigned char *scrPtr = address<unsigned char>(offset);
   465             int metaData = *address<int>(offset - sizeof(int));
   463             qptrdiff metaData = *address<qptrdiff>(offset - sizeof(qptrdiff));
   466             if (metaData & MAIN_ALLOCATOR_IDENTIFIER) {
   464             if (metaData & MAIN_ALLOCATOR_IDENTIFIER) {
   467                 int oldSize = mainAllocator->allocatedSize(offset);
   465                 int oldSize = mainAllocator->allocatedSize(offset);
   468                 memcpy(address<unsigned char>(newOffset), scrPtr, qMin(oldSize, allocatedSize));
   466                 memcpy(address<unsigned char>(newOffset), scrPtr, qMin(oldSize, allocatedSize));
   469 #ifdef HB_THEME_SERVER_MEMORY_REPORT
   467 #ifdef HB_THEME_SERVER_MEMORY_REPORT
   470                 free(offset);
   468                 free(offset);
   591 HbMemoryManager *HbSharedMemoryManager::instance()
   589 HbMemoryManager *HbSharedMemoryManager::instance()
   592 {
   590 {
   593     if (!memManager) {
   591     if (!memManager) {
   594         memManager = new HbSharedMemoryManager();
   592         memManager = new HbSharedMemoryManager();
   595         if (!memManager->initialize()) {
   593         if (!memManager->initialize()) {
   596 #ifdef THEME_SERVER_TRACES
   594             THEME_GENERIC_DEBUG() << "HbSharedMemoryManager:Could not initialize shared memory";
   597             qWarning( "HbSharedMemoryManager:Could not initialize shared memory" );
       
   598 #endif
       
   599             delete memManager;
   595             delete memManager;
   600             memManager = 0;
   596             memManager = 0;
   601         }
   597         }
   602     }
   598     }
   603     return memManager;
   599     return memManager;
   631 }
   627 }
   632 
   628 
   633 int HbSharedMemoryManager::loadMemoryFile(const QString &filePath)
   629 int HbSharedMemoryManager::loadMemoryFile(const QString &filePath)
   634 {
   630 {
   635     int loadedSize = 0;
   631     int loadedSize = 0;
   636 
   632     THEME_CSSBIN_DEBUG() << "loading: " << filePath;
   637 #ifdef CSSBIN_TRACES
   633     
   638     qDebug() << "loading: " << filePath;
       
   639 #endif
       
   640     QFile file(filePath);
   634     QFile file(filePath);
   641     if(file.open(QFile::ReadOnly)) {
   635     if(file.open(QFile::ReadOnly)) {
   642         qint64 fileSize = file.size();
   636         qint64 fileSize = file.size();
   643         file.read(static_cast<char*>(chunk->data()), fileSize);
   637         file.read(static_cast<char*>(chunk->data()), fileSize);
   644         file.close();
   638         file.close();
   645         loadedSize = (int)fileSize;
   639         loadedSize = (int)fileSize;
   646     }
   640     }
   647 #ifdef CSSBIN_TRACES
   641     THEME_CSSBIN_DEBUG() << "Loading memory file status: " << (loadedSize > 0 ? "no error" : file.errorString());
   648     qDebug() << "Loading memory file status: " << (loadedSize > 0 ? "no error" : file.errorString());
       
   649 #endif
       
   650     return loadedSize;
   642     return loadedSize;
   651 }
   643 }
   652 
   644 
   653 #ifdef HB_THEME_SERVER_MEMORY_REPORT
   645 #ifdef HB_THEME_SERVER_MEMORY_REPORT
   654 bool pairGreaterThan(const QPair<quint32, QPair<quint32, quint32> > &p1,
   646 bool pairGreaterThan(const QPair<quint32, QPair<quint32, quint32> > &p1,
   683 #endif
   675 #endif
   684     filePath.append(".txt");
   676     filePath.append(".txt");
   685 
   677 
   686     QFile file(filePath);
   678     QFile file(filePath);
   687     if (!file.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
   679     if (!file.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
   688         qWarning() << "Create shared memory report error - can't write to file " << filePath;
   680         hbWarning() << "Create shared memory report error - can't write to file " << filePath;
   689         return;
   681         return;
   690     }
   682     }
   691 
   683 
   692     QTextStream reportWriter(&file);
   684     QTextStream reportWriter(&file);
   693 
   685 
   789         }
   781         }
   790     }
   782     }
   791 #endif
   783 #endif
   792     file.close();
   784     file.close();
   793     if (file.error()) {
   785     if (file.error()) {
   794         qWarning() << "Create shared memory report error - error with file " << filePath;
   786         hbWarning() << "Create shared memory report error - error with file " << filePath;
   795     }
   787     }
   796 }
   788 }
   797 #endif
   789 #endif