src/hbtools/hbbincssmaker/hbcssconverterutils.cpp
changeset 2 06ff229162e9
child 5 627c4a0fd0e7
equal deleted inserted replaced
1:f7ac710697a9 2:06ff229162e9
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbTools module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include <QMap>
       
    27 #include "hbcssconverterutils_p.h"
       
    28 #include "hbsharedmemorymanager_p.h"
       
    29 #include "hbsharedmemoryallocators_p.h"
       
    30 
       
    31 // Global list that stores pointers to the member variables where shared container instances
       
    32 // store offsets returned my memory allocator.
       
    33 // CSS converter utilizes it to automatically adjust the offsets if allocated cells are moved.
       
    34 
       
    35 // Map is used only to get faster lookups, item's value is obsolete
       
    36 static QMap<int *, int> registered;
       
    37 
       
    38 
       
    39 // Shared chunk allocation information for css data
       
    40 static int totalAllocated = 0;
       
    41 // Using map instead of hash to guarantee that the items are in order
       
    42 // so the cell with offset 0 which is the HbCss::StyleSheet structure
       
    43 // is always first in the cell list, so its offset does not get changed
       
    44 // when the chunk is defragmented.
       
    45 static QMap<int, int> cells;
       
    46 
       
    47 
       
    48 void HbCssConverterUtils::registerOffsetHolder(int *offset)
       
    49 {
       
    50     registered.insert(offset, 1);
       
    51 }
       
    52 
       
    53 void HbCssConverterUtils::unregisterOffsetHolder(int *offset)
       
    54 {
       
    55     registered.remove(offset);
       
    56 }
       
    57 
       
    58 
       
    59 QList<int *> HbCssConverterUtils::registeredOffsetHolders()
       
    60 {
       
    61     return registered.keys();
       
    62 }
       
    63 
       
    64 void HbCssConverterUtils::unregisterAll()
       
    65 {
       
    66     registered.clear();
       
    67 }
       
    68 
       
    69 
       
    70 void HbCssConverterUtils::cellAllocated(int offset, int size)
       
    71 {
       
    72     cells.insert(offset, ALIGN(size));
       
    73     totalAllocated += ALIGN(size);
       
    74 }
       
    75 
       
    76 void HbCssConverterUtils::cellFreed(int offset)
       
    77 {
       
    78     int size = cells.value(offset, 0);
       
    79     totalAllocated -= size;
       
    80     cells.remove(offset);
       
    81 
       
    82     if (size > 0) {
       
    83         // Make sure there are no registered offset holders in the freed cell any more
       
    84         GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
       
    85         HbSharedMemoryManager *shared = static_cast<HbSharedMemoryManager*>(manager);
       
    86         const char *chunkBase = static_cast<const char *>(shared->base());
       
    87 
       
    88         QList<int *> offsetHolders = HbCssConverterUtils::registeredOffsetHolders();
       
    89         for (int i = 0; i<offsetHolders.count(); ++i) {
       
    90             int *holder = offsetHolders.at(i);
       
    91             if ((char*)holder >= chunkBase + offset && (char*)holder < chunkBase + offset + size) {
       
    92                 HbCssConverterUtils::unregisterOffsetHolder(holder);
       
    93             }
       
    94         }
       
    95     }
       
    96 }
       
    97 
       
    98 void HbCssConverterUtils::cellMoved(int offset, int newOffset)
       
    99 {
       
   100     int size = cells.value(offset, 0);
       
   101 
       
   102     if (size > 0) {
       
   103         // Check if there were registered offset holders in the old cell
       
   104         // and register corresponding ones in the reallocated cell.
       
   105         QList<int *> holders = HbCssConverterUtils::registeredOffsetHolders();
       
   106 
       
   107         GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
       
   108         HbSharedMemoryManager *shared = static_cast<HbSharedMemoryManager*>(manager);
       
   109         const char *chunkBase = static_cast<const char *>(shared->base());    
       
   110         
       
   111         for (int i=0; i<holders.count(); i++) {
       
   112             int *holder = holders.at(i);
       
   113             char *holderC = (char*)holder;
       
   114             if (holderC >= chunkBase + offset && holderC < chunkBase + offset + size) {
       
   115                 HbCssConverterUtils::unregisterOffsetHolder(holder);
       
   116                 HbCssConverterUtils::registerOffsetHolder((int*)(holderC + newOffset - offset));
       
   117             }
       
   118         }
       
   119     }
       
   120 }
       
   121 
       
   122 /**
       
   123 * Defragments the shared chunk contents and places defragmented buffer in the beginning of the chunk.
       
   124 * Registered chunk offset holders are updated during the process.
       
   125 * Returns the next free offset in the chunk.
       
   126 */
       
   127 int HbCssConverterUtils::defragmentChunk()
       
   128 {
       
   129     GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
       
   130     HbSharedMemoryManager *shared = static_cast<HbSharedMemoryManager*>(manager);
       
   131 
       
   132     // Register shared cache pointer in chunk header as shared cache may also be moved in defragmentation
       
   133     HbSharedChunkHeader *chunkHeader = static_cast<HbSharedChunkHeader*>(shared->base());    
       
   134     HbCssConverterUtils::registerOffsetHolder(reinterpret_cast<int *>(&chunkHeader->sharedCacheOffset));
       
   135 
       
   136     QList<int *> offsetHolders = HbCssConverterUtils::registeredOffsetHolders();
       
   137 
       
   138     // Create new buffer where the current chunk contents are defragmented
       
   139     void *buffer = ::malloc(shared->size());
       
   140     int newCurrentOffset = 0;
       
   141 
       
   142     // Create new cell order and update offset holders
       
   143     QMap<int,int>::const_iterator i = cells.constBegin();
       
   144 
       
   145     while (i != cells.constEnd()) {
       
   146         // Get the old cell
       
   147         int offset = i.key();
       
   148         int size = i.value();
       
   149         
       
   150         // Update registered offset holders
       
   151 
       
   152         // TODO: optimize this, now there's linear search for each cell!
       
   153 		for (int j=0; j<offsetHolders.count(); ++j) {
       
   154 			int *holder = offsetHolders.at(j);
       
   155 			if (*holder == offset) {
       
   156 				// Change stored offset value
       
   157 				*holder = newCurrentOffset + sizeof(HbSharedChunkHeader);
       
   158 			}
       
   159 		}
       
   160 
       
   161         newCurrentOffset += size;
       
   162         i++;
       
   163     }
       
   164 
       
   165     i = cells.constBegin();
       
   166     newCurrentOffset = 0;
       
   167 
       
   168     // Move allocated cells to a linear buffer
       
   169     while (i != cells.constEnd()) {
       
   170         // Get the old cell
       
   171         int offset = i.key();
       
   172         int size = i.value();
       
   173         // Copy to new chunk
       
   174         memcpy((char*)buffer + newCurrentOffset, (char*)shared->base() + offset, size);
       
   175 
       
   176         newCurrentOffset += size;
       
   177         i++;
       
   178     }
       
   179 
       
   180     // Free all cells from the shared chunk and move the defragmented buffer in the beginning of the chunk.
       
   181     // Note that chunk memory management is screwed up after this point, so no more allocations should be
       
   182     // done in it after this.
       
   183 
       
   184     HbCssConverterUtils::unregisterAll();
       
   185     QList<int> keys = cells.keys();
       
   186 
       
   187     for (int j=0; j<keys.count(); ++j) {
       
   188         shared->free(keys.at(j));
       
   189     }
       
   190 
       
   191     // CSS binary data is placed after the chunk header.
       
   192     int cssBinaryOffset = sizeof(HbSharedChunkHeader);
       
   193     char *address = HbMemoryUtils::getAddress<char>(HbMemoryManager::SharedMemory, cssBinaryOffset);
       
   194     memcpy(address, buffer, newCurrentOffset);
       
   195 
       
   196     cells.clear();
       
   197     totalAllocated = 0;
       
   198 
       
   199     // Free the temp buffer
       
   200     ::free(buffer);
       
   201 
       
   202     // Return the next free address in the chunk
       
   203     return cssBinaryOffset + newCurrentOffset;
       
   204 }