webengine/osswebengine/MemoryManager/Src/MemoryManager.cpp
branchRCL_3
changeset 93 79859ed3eea9
parent 91 30342f40acbf
child 94 919f36ff910f
equal deleted inserted replaced
92:e1bea15f9a39 93:79859ed3eea9
    16 *
    16 *
    17 */
    17 */
    18 
    18 
    19 // INCLUDE FILES
    19 // INCLUDE FILES
    20 
    20 
    21 #include <MemoryManager.h>
    21 #include "MemoryManager.h"
    22 #include "MemoryPool.h"
    22 #include "MemoryPool.h"
    23 #include "FastAllocator.h"
    23 #include "FastAllocator.h"
    24 #include "MemoryLogger.h"
       
    25 
    24 
    26 // CONSTANTS
    25 // CONSTANTS
    27 
    26 
    28 _LIT( KMemManPanicDes, "MemMan:0"  );
    27 _LIT( KMemManPanicDes, "MemMan:0"  );
    29 
    28 
    30 // CLASS DECLARATION
    29 // CLASS DECLARATION
    31 
    30 
    32 //  initializing a global memory pool.
    31 //  initializing a global memory pool.
    33 static CMemoryPool *s_pool = 0;
    32 static CMemoryPool *s_pool = 0;
    34 
    33 
       
    34 struct cleanupMemoryPool {
       
    35     ~cleanupMemoryPool() {
       
    36     	if(s_pool)
       
    37     		{
       
    38     		delete s_pool;
       
    39     		s_pool = NULL;
       
    40     		}
       
    41     }
       
    42 };
       
    43 static cleanupMemoryPool deleteMemoryPool;
    35 
    44 
    36 //-----------------------------------------------------------------------------
    45 //-----------------------------------------------------------------------------
    37 // Pool() - a utility function for accessing the right memory pool
    46 // Pool() - a utility function for accessing the right memory pool
    38 //-----------------------------------------------------------------------------
    47 //-----------------------------------------------------------------------------
    39 inline CMemoryPool* Pool() 
    48 inline CMemoryPool* Pool() 
    47 
    56 
    48     return s_pool;
    57     return s_pool;
    49     }
    58     }
    50 
    59 
    51 //-----------------------------------------------------------------------------
    60 //-----------------------------------------------------------------------------
    52 // MemoryManager::CreateAllocator
       
    53 //-----------------------------------------------------------------------------
       
    54 EXPORT_C void MemoryManager::CreateFastAllocator()
       
    55     {
       
    56     // create the right memory pool
       
    57     MEM_LOG_CREATE();    
       
    58 #ifdef __NEW_ALLOCATOR__
       
    59     CMemoryPool *pool = new CNewSymbianHeapPool();
       
    60     pool->Create();
       
    61     RSymbianDlAllocatorWrapper* allocator = new RSymbianDlAllocatorWrapper((CNewSymbianHeapPool*)pool);
       
    62     User::SwitchAllocator(allocator);
       
    63     MEM_LOGF(_L8("MemoryManager::CreateFastAllocator - new pool=%x, allocator=%x"), pool, allocator);
       
    64 #endif
       
    65     }
       
    66 
       
    67 //-----------------------------------------------------------------------------
       
    68 // MemoryManager::InitAllocator
       
    69 //-----------------------------------------------------------------------------
       
    70 EXPORT_C void MemoryManager::InitFastAllocator()
       
    71     {
       
    72     // Initialize s_pool variable from current allocator, assumption is that main program has already called CreateAllocator()
       
    73     // It is special case when this allocator is created in SetupThreadHeap() where can not initialize static data. It also
       
    74     // solves problems due to static data destruction in Symbian 9.5.
       
    75 #ifdef __NEW_ALLOCATOR__    
       
    76     RAllocator &aAllocator = User::Allocator();
       
    77     RSymbianDlAllocatorWrapper* allocator = (RSymbianDlAllocatorWrapper*) &aAllocator;
       
    78     s_pool = allocator->iPool;
       
    79     MEM_LOGF(_L8("MemoryManager::InitFastAllocator - s_pool=%x, allocator=%x"), s_pool, allocator);
       
    80 #endif    
       
    81     }
       
    82 
       
    83 //-----------------------------------------------------------------------------
       
    84 // MemoryManager::SwitchToFastAllocator
    61 // MemoryManager::SwitchToFastAllocator
    85 //-----------------------------------------------------------------------------
    62 //-----------------------------------------------------------------------------
    86 EXPORT_C RAllocator* MemoryManager::SwitchToFastAllocator()
    63 EXPORT_C RAllocator* MemoryManager::SwitchToFastAllocator()
    87     {
    64     {
    88     // create the right memory pool
    65     // create the right memory pool
    89     __ASSERT_DEBUG( s_pool == 0, User::Panic( KMemManPanicDes, 0 ) );
    66     __ASSERT_DEBUG( s_pool == 0, User::Panic( KMemManPanicDes, 0 ) );
    90     MEM_LOGF(_L8("MemoryManager::SwitchToFastAllocator - s_pool=%x"), s_pool);
       
    91 #ifdef __NEW_ALLOCATOR__
    67 #ifdef __NEW_ALLOCATOR__
    92     s_pool = new CNewSymbianHeapPool();
    68     s_pool = new CNewSymbianHeapPool();
    93     s_pool->Create();
    69     s_pool->Create();
    94     RSymbianDlAllocatorWrapper* allocator = new RSymbianDlAllocatorWrapper((CNewSymbianHeapPool*)s_pool);
    70     RSymbianDlAllocatorWrapper* allocator = new RSymbianDlAllocatorWrapper((CNewSymbianHeapPool*)s_pool);
    95     MEM_LOGF(_L8("MemoryManager::SwitchToFastAllocator - new s_pool=%x"), s_pool);
       
    96     return User::SwitchAllocator( allocator );
    71     return User::SwitchAllocator( allocator );
    97 #else
    72 #else
    98     s_pool = new CFastMemoryPool();
    73     s_pool = new CFastMemoryPool();
    99     s_pool->Create();
    74     s_pool->Create();
   100     RFastAllocator* allocator = new RFastAllocator((CFastMemoryPool*)s_pool);
    75     RFastAllocator* allocator = new RFastAllocator((CFastMemoryPool*)s_pool);
   101     return User::SwitchAllocator( allocator );
    76     return User::SwitchAllocator( allocator );
   102 #endif
       
   103     }
       
   104 
       
   105 //-----------------------------------------------------------------------------
       
   106 // MemoryManager::InitOOMHandler
       
   107 //-----------------------------------------------------------------------------
       
   108 EXPORT_C void MemoryManager::InitOOMDialog()
       
   109     {
       
   110 #ifdef __NEW_ALLOCATOR__
       
   111     if (s_pool)
       
   112         s_pool->InitOOMDialog();
       
   113 #endif
       
   114     }
       
   115 
       
   116 //-----------------------------------------------------------------------------
       
   117 // MemoryManager::ResetOOMDialogDisplayed
       
   118 //-----------------------------------------------------------------------------
       
   119 EXPORT_C void MemoryManager::ResetOOMDialogDisplayed()
       
   120     {
       
   121 #ifdef __NEW_ALLOCATOR__
       
   122     if (s_pool)
       
   123         s_pool->ResetOOMDialog();
       
   124 #endif
    77 #endif
   125     }
    78     }
   126 
    79 
   127 //-----------------------------------------------------------------------------
    80 //-----------------------------------------------------------------------------
   128 // MemoryManager::CloseFastAllocator
    81 // MemoryManager::CloseFastAllocator
   240 EXPORT_C TUint MemoryManager::MemorySize( TAny* aPtr )
   193 EXPORT_C TUint MemoryManager::MemorySize( TAny* aPtr )
   241     {
   194     {
   242     return Pool()->MemorySize( aPtr );
   195     return Pool()->MemorySize( aPtr );
   243     }
   196     }
   244 
   197 
   245 
       
   246 EXPORT_C void MemoryManager::DumpMemoryLogs()
       
   247     {
       
   248 #if defined(OOM_LOGGING) && defined (__NEW_ALLOCATOR__)
       
   249     CNewSymbianHeapPool *hPool = (CNewSymbianHeapPool *)Pool();
       
   250     hPool->DumpHeapLogs(0);
       
   251 #endif    
       
   252     }