mpx/commonframework/common/inc/mpxheapmanager.h
changeset 0 a2952bb97e68
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Manages global heap/chunk
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #ifndef RMPXHEAPMANAGER_H
       
    21 #define RMPXHEAPMANAGER_H
       
    22 
       
    23 #include "mpxdataarray.h"
       
    24 
       
    25 
       
    26 class RMPXHeapCell
       
    27     {
       
    28     public:
       
    29         RMPXHeapCell(TInt aNextOffset, TInt aLen):
       
    30             iNextOffset(aNextOffset),
       
    31             iLen(aLen)
       
    32             {
       
    33             }
       
    34         RMPXHeapCell()
       
    35             {
       
    36             }
       
    37     public:
       
    38         /**
       
    39         * Offset of next cell from the base of the chunk to the next heap
       
    40         * cell, or 0 if none
       
    41         */
       
    42         TInt iNextOffset;
       
    43         /**
       
    44         * Length of the cell, which includes the length of header
       
    45         */
       
    46         TInt iLen;
       
    47         /**
       
    48         * Is this cell's contents paged to file storage
       
    49         */
       
    50         // TBool iPaged;
       
    51         };
       
    52 
       
    53 /**
       
    54 *  Class RMPXHeapManager
       
    55 */
       
    56 class RMPXHeapManager
       
    57     {
       
    58 public:
       
    59     /**
       
    60     * The heap manager
       
    61     *
       
    62     * @param aClientHandle, client handle to the global chunk
       
    63     */
       
    64     static inline RMPXHeapManager& HeapManager(TUint aClientHandle);
       
    65 
       
    66     /**
       
    67     * Gets the total size and the currently used size of the global heap
       
    68     *
       
    69     * @param aTotal Total size of memory in bytes (out parameter)
       
    70     * @param aUsed Used size of memory in bytes (out parameter)
       
    71     * @leave Leave with KErrNotFound if failed to open global chunk
       
    72     */
       
    73     static void HeapMemoryInfoL( TInt& aTotal, TInt& aUsed );
       
    74 
       
    75 
       
    76 public:
       
    77     /**
       
    78     * Handle which represents the current client (thread).
       
    79     * This should be stored by the client and provided in all methods;
       
    80     * by so doing, the Heap Manager can optimally cope with the chunk mapped
       
    81     * to different addresses, thread relative handles for the chunk and mutex,
       
    82     * etc. Handle should never be zero: zero indicates that it has failed.
       
    83     *
       
    84     * @return client handle to the global chunk
       
    85     */
       
    86     static TUint ClientHandle();
       
    87 
       
    88     /**
       
    89     * Returns absolute address from an offset from the base of the chunk
       
    90     *
       
    91     * @param aClientHandle, client handle to the global chunk
       
    92     * @param aOffset, offset to the glboal chunk
       
    93     * @return pointer to the global address
       
    94     */
       
    95     template<typename T>
       
    96     static inline T* Ptr(TUint aClientHandle,TInt aOffset=0);
       
    97 
       
    98     /**
       
    99     * Returns offset from the base of the chunk from absolute pointer
       
   100     *
       
   101     * @param aClientHandle, client handle to the global chunk
       
   102     * @param aPtr pointer to the global address
       
   103     * @return offset to the glboal chunk
       
   104     */
       
   105     static inline TInt Offset(TUint aClientHandle,TAny* aPtr);
       
   106 
       
   107 public:
       
   108     /**
       
   109     * Add another/new reference to a client (thread)
       
   110     *
       
   111     * @param aClientHandle, client handle to the global chunk
       
   112     */
       
   113     void AddRef(TUint aClientHandle);
       
   114 
       
   115     /**
       
   116     * Remove a reference to a client (thread)
       
   117     * @param aClientHandle, client handle to the global chunk
       
   118     */
       
   119     void Release(TUint aClientHandle);
       
   120 
       
   121     /**
       
   122     * Allocate an object of type T on the chunk. An absolute pointer to the
       
   123     * object created is returned, though the object must contain no absolute
       
   124     * pointers within itself
       
   125     *
       
   126     * @param aClientHandle, client handle to the global chunk
       
   127     * @return pointer to the memory
       
   128     */
       
   129     template<typename T>
       
   130     inline T* Alloc(TUint aClientHandle);
       
   131 
       
   132     /**
       
   133     * Allocate a buffer of size aSize and return an absolute pointer to the
       
   134     * start of the buffer.
       
   135     *
       
   136     * @param aClientHandle, client handle to the global chunk
       
   137     * @param aSize, size of the memory
       
   138     * @return pointer to the memory
       
   139     */
       
   140     TAny* Alloc(TUint aClientHandle,TInt aSize);
       
   141 
       
   142     /**
       
   143     * Allocate a buffer of size aSize, copy aSrcOffset to the buffer,
       
   144     * and return an offset to the start of the buffer.
       
   145     *
       
   146     * @param aClientHandle, client handle to the global chunk
       
   147     * @param aSrcOffset, source data offset
       
   148     * @param aSize, size of the data buffer
       
   149     * @return system error code
       
   150     */
       
   151     inline TInt Copy(TUint aClientHandle,TInt aSrcOffset,TInt aSize);
       
   152 
       
   153     /**
       
   154     * Allocate a buffer of size aSize, copy aSrc to the buffer,
       
   155     * and return an offset to the start of the buffer.
       
   156     *
       
   157     * @param aClientHandle, client handle to the global chunk
       
   158     * @param aSrc, source
       
   159     * @param aSize, size of the data buffer
       
   160     * @return system error code
       
   161     */
       
   162     TInt Copy(TUint aClientHandle,TAny* aSrc,TInt aSize);
       
   163 
       
   164     /**
       
   165     * Delete an object/buffer on the chunk. Can return error
       
   166     *
       
   167     * @param aClientHandle, client handle to the global chunk
       
   168     * @param aSrcPtr, memory address to be freed
       
   169     * @return system error code
       
   170     */
       
   171     TInt Free(TUint aClientHandle,TAny* aPtr);
       
   172 
       
   173     /**
       
   174     * Delete an object/buffer on the chunk. Can return error
       
   175     *
       
   176     * @param aClientHandle, client handle to the global chunk
       
   177     * @param aOffset, offset to global memory to be freed
       
   178     * @return system error code
       
   179     */
       
   180     inline TInt Free(TUint aClientHandle,TInt aOffset);
       
   181 
       
   182     /**
       
   183     * Locks heap
       
   184     *
       
   185     * @param aClientHandle, client handle to the global chunk
       
   186     */
       
   187     void Lock(TUint aClientHandle);
       
   188 
       
   189     /**
       
   190     * Unocks heap
       
   191     *
       
   192     * @param aClientHandle, client handle to the global chunk
       
   193     */
       
   194     void Unlock(TUint aClientHandle);
       
   195 
       
   196     /**
       
   197      * Increment consecutive number
       
   198      *
       
   199      * @return current number
       
   200      */
       
   201     TInt IncrementCounter();
       
   202 
       
   203 #ifdef __ENABLE_MPX_GARBAGE_COLLECTOR
       
   204     /**
       
   205     * Run the Garbage Collector
       
   206     */
       
   207     void RunGc(TUint aClientHandle); //GC
       
   208 
       
   209     /**
       
   210     * Associate data with this client
       
   211     */
       
   212     TInt AddData(TUint aClientHandle,TUint aDataHandle); //GC
       
   213 
       
   214     /**
       
   215     * Unassociate data with this client
       
   216     */
       
   217     TInt RemoveData(TUint aClientHandle,TUint aDataHandle,TBool aRemoveAll); //GC
       
   218 #endif // __ENABLE_MPX_GARBAGE_COLLECTOR
       
   219 
       
   220 private:
       
   221     /**
       
   222     * Data associated with a client (thread)
       
   223     */
       
   224     class TClientContext
       
   225         {
       
   226     public:
       
   227         TClientContext();
       
   228         TClientContext(TUint8* aBase,TInt aChunkHandle,TInt aMutexHandle);
       
   229     public:
       
   230         TThreadId iTid;
       
   231         RChunk iChunk; // Thread relative
       
   232         RMutex iMutex; // Thread relative
       
   233         TInt iCount; // Number of clients in this thread (media objects)
       
   234         TUint8* iBase; // Base of the chunk for this process
       
   235 #ifdef __ENABLE_MPX_GARBAGE_COLLECTOR
       
   236         RMPXDataItemArray iData; // Dynamic array of TUint //GC
       
   237 #endif // __ENABLE_MPX_GARBAGE_COLLECTOR
       
   238         };
       
   239 private:
       
   240     RMPXHeapManager(const RChunk& aChunk);
       
   241 
       
   242 private:
       
   243     /**
       
   244     * Index which represents the current client (thread).
       
   245     * Refers to iClients array.
       
   246     */
       
   247     TInt ClientIndex(const TClientContext& aContext);
       
   248 
       
   249     /**
       
   250     * Client index from the handle. Refers to iClients array.
       
   251     */
       
   252     static inline TInt ClientIndex(TUint aClientHandle);
       
   253 
       
   254     /**
       
   255     * Returns index of first entry for that TID. There will
       
   256     * only ever be one, unless the search is for KNullThreadId
       
   257     */
       
   258     TInt Find(const TThreadId& aTid);
       
   259 
       
   260     /**
       
   261     * Creates client handle
       
   262     */
       
   263     static inline TUint ClientHandle(TUint8* aChunkBase,TInt aClientIndex);
       
   264 
       
   265     /**
       
   266     * Base address of the chunk for this client
       
   267     */
       
   268     static inline TUint8* ChunkBase(TUint aClientHandle);
       
   269 
       
   270     /**
       
   271     * Finds (or creates) first Heap Cell which can fit aSize bytes
       
   272     */
       
   273     RMPXHeapCell* HeapCell(TUint aClientHandle,TInt aSize);
       
   274 
       
   275     /**
       
   276     * Finds (or creates) first Heap Cell which can fit aSize bytes
       
   277     */
       
   278     RMPXHeapCell* DoHeapCell(TUint aClientHandle,TInt aSize,RMPXHeapCell*& aLastFree);
       
   279 
       
   280     /**
       
   281     * Try to grow the global chunk
       
   282     */
       
   283     TInt TryToGrowHeap(TUint aClientHandle, TInt aSize, RMPXHeapCell* aLastFree);
       
   284     
       
   285     /**
       
   286     * Number of clients (threads)
       
   287     */
       
   288     TInt ClientCount() const;
       
   289     
       
   290     /**
       
   291     * Check if it is the last cell
       
   292     */
       
   293     TBool IsLastCell(TUint aClientHandle, RMPXHeapCell* aCell);
       
   294 
       
   295 private:
       
   296 
       
   297     enum {ENumClients=0x100};
       
   298     enum {EHeapCellHeaderSize = sizeof(RMPXHeapCell)};
       
   299     enum {EMinCellSize = sizeof(RMPXHeapCell) + 4 };
       
   300     
       
   301 private:
       
   302     /**
       
   303     * Client data (per thread)
       
   304     */
       
   305     TFixedArray<TClientContext,ENumClients> iClients;
       
   306     /**
       
   307     * Offset from the base of the chunk to the end of the chunk, i.e.
       
   308     * the size of the chunk
       
   309     */
       
   310     TInt iEndOffset;
       
   311     /**
       
   312      * Head to free cell list
       
   313      */
       
   314     RMPXHeapCell iFree;
       
   315     /**
       
   316      * Counter
       
   317      */
       
   318     TInt iCounter;
       
   319 
       
   320     /**
       
   321      * Total memory in bytes used in Global Chunk
       
   322      */
       
   323     TInt iUsedMemory;
       
   324 
       
   325     /**
       
   326      * Maximum size of  memory in bytes used in Global Chunk
       
   327      */
       
   328     TInt iMaxMemory;
       
   329 
       
   330 #ifdef __ENABLE_MPX_GARBAGE_COLLECTOR
       
   331     TInt iDeadContextIndex;
       
   332 #endif // __ENABLE_MPX_GARBAGE_COLLECTOR
       
   333     };
       
   334 
       
   335 #include "mpxheapmanager.inl"
       
   336 
       
   337 #endif // RMPXHEAPMANAGER_H