webengine/osswebengine/MemoryManager/Src/MemoryManager.cpp
changeset 65 5bfc169077b2
parent 37 cb62a4f66ebe
equal deleted inserted replaced
42:d39add9822e2 65:5bfc169077b2
    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_LOGF(_L8("MemoryManager::CreateFastAllocator - s_pool=%x"), s_pool);
       
    58 #ifdef __NEW_ALLOCATOR__
       
    59     CMemoryPool *pool = new CNewSymbianHeapPool();
       
    60     pool->Create();
       
    61     RSymbianDlAllocatorWrapper* allocator = new RSymbianDlAllocatorWrapper((CNewSymbianHeapPool*)pool);
       
    62     MEM_LOGF(_L8("MemoryManager::CreateFastAllocator - new pool=%x, allocator=%x"), pool, allocator);
       
    63     User::SwitchAllocator(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         {
       
   113         ((CNewSymbianHeapPool *)s_pool)->InitOOMDialog();
       
   114         }
       
   115 #endif
       
   116     }
       
   117 
       
   118 //-----------------------------------------------------------------------------
       
   119 // MemoryManager::ResetOOMDialogDisplayed
       
   120 //-----------------------------------------------------------------------------
       
   121 EXPORT_C void MemoryManager::ResetOOMDialogDisplayed()
       
   122     {
       
   123 #ifdef __NEW_ALLOCATOR__
       
   124     if (s_pool)
       
   125         {
       
   126         ((CNewSymbianHeapPool *)s_pool)->ResetOOMDialogDisplayed();
       
   127         }
       
   128 #endif
    77 #endif
   129     }
    78     }
   130 
    79 
   131 //-----------------------------------------------------------------------------
    80 //-----------------------------------------------------------------------------
   132 // MemoryManager::CloseFastAllocator
    81 // MemoryManager::CloseFastAllocator
   145 //-----------------------------------------------------------------------------
    94 //-----------------------------------------------------------------------------
   146 // MemoryManager::AddCollector
    95 // MemoryManager::AddCollector
   147 //-----------------------------------------------------------------------------
    96 //-----------------------------------------------------------------------------
   148 EXPORT_C void MemoryManager::AddCollector( MMemoryCollector* aCollector )
    97 EXPORT_C void MemoryManager::AddCollector( MMemoryCollector* aCollector )
   149     {
    98     {
   150     MEM_LOGF(_L8("MemoryManager::AddCollector - s_pool=%x"), s_pool);
       
   151     Pool()->AddCollector( aCollector );
    99     Pool()->AddCollector( aCollector );
   152     }
   100     }
   153 
   101 
   154 //-----------------------------------------------------------------------------
   102 //-----------------------------------------------------------------------------
   155 // MemoryManager::RemoveCollector
   103 // MemoryManager::RemoveCollector
   162 //-----------------------------------------------------------------------------
   110 //-----------------------------------------------------------------------------
   163 // MemoryManager::AddStopper
   111 // MemoryManager::AddStopper
   164 //-----------------------------------------------------------------------------
   112 //-----------------------------------------------------------------------------
   165 EXPORT_C void MemoryManager::AddStopper( MOOMStopper* aStopper )
   113 EXPORT_C void MemoryManager::AddStopper( MOOMStopper* aStopper )
   166     {
   114     {
   167     MEM_LOGF(_L8("MemoryManager::AddStopper - s_pool=%x"), s_pool);
       
   168     Pool()->AddStopper( aStopper );
   115     Pool()->AddStopper( aStopper );
   169     }
   116     }
   170 
   117 
   171 //-----------------------------------------------------------------------------
   118 //-----------------------------------------------------------------------------
   172 // MemoryManager::RemoveStopper
   119 // MemoryManager::RemoveStopper