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" |
24 |
25 |
25 // CONSTANTS |
26 // CONSTANTS |
26 |
27 |
27 _LIT( KMemManPanicDes, "MemMan:0" ); |
28 _LIT( KMemManPanicDes, "MemMan:0" ); |
28 |
29 |
29 // CLASS DECLARATION |
30 // CLASS DECLARATION |
30 |
31 |
31 // initializing a global memory pool. |
32 // initializing a global memory pool. |
32 static CMemoryPool *s_pool = 0; |
33 static CMemoryPool *s_pool = 0; |
33 |
34 |
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; |
|
44 |
35 |
45 //----------------------------------------------------------------------------- |
36 //----------------------------------------------------------------------------- |
46 // Pool() - a utility function for accessing the right memory pool |
37 // Pool() - a utility function for accessing the right memory pool |
47 //----------------------------------------------------------------------------- |
38 //----------------------------------------------------------------------------- |
48 inline CMemoryPool* Pool() |
39 inline CMemoryPool* Pool() |
56 |
47 |
57 return s_pool; |
48 return s_pool; |
58 } |
49 } |
59 |
50 |
60 //----------------------------------------------------------------------------- |
51 //----------------------------------------------------------------------------- |
|
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 //----------------------------------------------------------------------------- |
61 // MemoryManager::SwitchToFastAllocator |
84 // MemoryManager::SwitchToFastAllocator |
62 //----------------------------------------------------------------------------- |
85 //----------------------------------------------------------------------------- |
63 EXPORT_C RAllocator* MemoryManager::SwitchToFastAllocator() |
86 EXPORT_C RAllocator* MemoryManager::SwitchToFastAllocator() |
64 { |
87 { |
65 // create the right memory pool |
88 // create the right memory pool |
66 __ASSERT_DEBUG( s_pool == 0, User::Panic( KMemManPanicDes, 0 ) ); |
89 __ASSERT_DEBUG( s_pool == 0, User::Panic( KMemManPanicDes, 0 ) ); |
|
90 MEM_LOGF(_L8("MemoryManager::SwitchToFastAllocator - s_pool=%x"), s_pool); |
67 #ifdef __NEW_ALLOCATOR__ |
91 #ifdef __NEW_ALLOCATOR__ |
68 s_pool = new CNewSymbianHeapPool(); |
92 s_pool = new CNewSymbianHeapPool(); |
69 s_pool->Create(); |
93 s_pool->Create(); |
70 RSymbianDlAllocatorWrapper* allocator = new RSymbianDlAllocatorWrapper((CNewSymbianHeapPool*)s_pool); |
94 RSymbianDlAllocatorWrapper* allocator = new RSymbianDlAllocatorWrapper((CNewSymbianHeapPool*)s_pool); |
|
95 MEM_LOGF(_L8("MemoryManager::SwitchToFastAllocator - new s_pool=%x"), s_pool); |
71 return User::SwitchAllocator( allocator ); |
96 return User::SwitchAllocator( allocator ); |
72 #else |
97 #else |
73 s_pool = new CFastMemoryPool(); |
98 s_pool = new CFastMemoryPool(); |
74 s_pool->Create(); |
99 s_pool->Create(); |
75 RFastAllocator* allocator = new RFastAllocator((CFastMemoryPool*)s_pool); |
100 RFastAllocator* allocator = new RFastAllocator((CFastMemoryPool*)s_pool); |
76 return User::SwitchAllocator( allocator ); |
101 return User::SwitchAllocator( allocator ); |
77 #endif |
102 #endif |
78 } |
103 } |
79 |
104 |
80 //----------------------------------------------------------------------------- |
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 |
|
125 } |
|
126 |
|
127 //----------------------------------------------------------------------------- |
81 // MemoryManager::CloseFastAllocator |
128 // MemoryManager::CloseFastAllocator |
82 //----------------------------------------------------------------------------- |
129 //----------------------------------------------------------------------------- |
83 EXPORT_C void MemoryManager::CloseFastAllocator(RAllocator* aDefaultAllocator) |
130 EXPORT_C void MemoryManager::CloseFastAllocator(RAllocator* aDefaultAllocator) |
84 { |
131 { |
85 #ifdef __NEW_ALLOCATOR__ |
132 #ifdef __NEW_ALLOCATOR__ |