|
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 the License "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: |
|
15 * |
|
16 * |
|
17 */ |
|
18 #include "StopScheduler.h" |
|
19 #include "MemoryPool.h" |
|
20 #include "fast_malloc.h" |
|
21 |
|
22 // MEMBER FUNCTIONS |
|
23 static const TUint KLessRAM = ( 2*1024*1024 ); |
|
24 static const TUint KMinimumRAM = (1024 * 1024); |
|
25 static const TTimeIntervalMicroSeconds32 KMemoryCheckIntervalSlow = 1000000; // 1 second |
|
26 static const TTimeIntervalMicroSeconds32 KMemoryCheckIntervalFast = 500000; // 0.5 second |
|
27 |
|
28 //----------------------------------------------------------------------------- |
|
29 // CStopScheduler::CStopScheduler |
|
30 //----------------------------------------------------------------------------- |
|
31 CStopScheduler::CStopScheduler(CMemoryPool& aMemoryPool) |
|
32 : CActive( CActive::EPriorityHigh ), iMemoryPool( aMemoryPool ), iState( EIdle ), iNextStop( EAllStop ) |
|
33 { |
|
34 CActiveScheduler::Add( this ); |
|
35 iCheckTimer.CreateLocal(); |
|
36 } |
|
37 |
|
38 //----------------------------------------------------------------------------- |
|
39 // CStopScheduler::~CStopScheduler |
|
40 //----------------------------------------------------------------------------- |
|
41 CStopScheduler::~CStopScheduler() |
|
42 { |
|
43 iCheckTimer.Close(); |
|
44 Cancel(); |
|
45 } |
|
46 |
|
47 //----------------------------------------------------------------------------- |
|
48 // CStopScheduler::Start |
|
49 //----------------------------------------------------------------------------- |
|
50 void CStopScheduler::Start( TSchedulerState aState, TInt aMemSize ) |
|
51 { |
|
52 // stopping has a higher priority than collecting |
|
53 if( aState <= iState ) return; |
|
54 |
|
55 Cancel(); |
|
56 |
|
57 iMemRequested = aMemSize; |
|
58 iState = aState; |
|
59 SelfComplete(); |
|
60 |
|
61 iNextStop = EAllStop; |
|
62 } |
|
63 |
|
64 void CStopScheduler::SelfComplete() |
|
65 { |
|
66 TRequestStatus* status = &iStatus; |
|
67 SetActive(); |
|
68 User::RequestComplete( status, iState ); |
|
69 } |
|
70 |
|
71 //----------------------------------------------------------------------------- |
|
72 // CStopScheduler::RunL |
|
73 //----------------------------------------------------------------------------- |
|
74 void CStopScheduler::RunL() |
|
75 { |
|
76 if( iState == EStopLoading ) |
|
77 { |
|
78 StopLoading( EOOM_PriorityLow ); |
|
79 iNextStop &= ~ENormalStop; |
|
80 CheckMemoryDefered( KMemoryCheckIntervalFast ); |
|
81 } |
|
82 else if( iState == ECheckMemory ) |
|
83 { |
|
84 TFreeMem freeMem; |
|
85 TInt total = iMemoryPool.FreeMemory( freeMem ); |
|
86 |
|
87 // see if free memory is enough to restore all collectors |
|
88 if( freeMem.iHal >= KLessRAM ) |
|
89 { |
|
90 iMemoryPool.RestoreCollectors( EOOM_PriorityLow ); |
|
91 iState = EIdle; |
|
92 iNextStop = EAllStop; |
|
93 |
|
94 // recover the rescue buffer, if it is already released. |
|
95 iMemoryPool.RestoreRescueBuffer(); |
|
96 } |
|
97 else if( freeMem.iHal >= KMinimumRAM/2 ) |
|
98 { |
|
99 iMemoryPool.RestoreCollectors( EOOM_PriorityMiddle ); |
|
100 CheckMemoryDefered( KMemoryCheckIntervalSlow ); |
|
101 iNextStop = EAllStop; |
|
102 } |
|
103 else if( freeMem.iHal >= KMinimumRAM/4 ) |
|
104 { |
|
105 if( iNextStop & ENormalStop ) |
|
106 { |
|
107 StopLoading( EOOM_PriorityLow ); |
|
108 iNextStop &= ~ENormalStop; |
|
109 } |
|
110 CheckMemoryDefered( KMemoryCheckIntervalFast ); |
|
111 } |
|
112 else |
|
113 { |
|
114 if( iNextStop & EEmergencyStop ) |
|
115 { |
|
116 StopLoading( EOOM_PriorityHigh ); |
|
117 iNextStop &= ~EEmergencyStop; |
|
118 } |
|
119 CheckMemoryDefered( KMemoryCheckIntervalFast ); |
|
120 } |
|
121 } |
|
122 } |
|
123 |
|
124 //----------------------------------------------------------------------------- |
|
125 // CStopScheduler::DoCancel |
|
126 //----------------------------------------------------------------------------- |
|
127 void CStopScheduler::DoCancel() |
|
128 { |
|
129 iState = EIdle; |
|
130 } |
|
131 |
|
132 //----------------------------------------------------------------------------- |
|
133 // CStopScheduler::StopLoading |
|
134 //----------------------------------------------------------------------------- |
|
135 void CStopScheduler::StopLoading( TOOMPriority aPriority ) |
|
136 { |
|
137 // stop operations |
|
138 for( TInt i=0; i<iMemoryPool.Stoppers().Count(); ++i ) |
|
139 { |
|
140 MOOMStopper* stopper = iMemoryPool.Stoppers()[i]; |
|
141 if( stopper->Priority() == aPriority ) |
|
142 stopper->Stop(); |
|
143 } |
|
144 iMemoryPool.SetStopping( EFalse ); |
|
145 } |
|
146 |
|
147 //----------------------------------------------------------------------------- |
|
148 // CStopScheduler::CheckMemoryDefered |
|
149 //----------------------------------------------------------------------------- |
|
150 void CStopScheduler::CheckMemoryDefered( TTimeIntervalMicroSeconds32 aDelay ) |
|
151 { |
|
152 iState = ECheckMemory; |
|
153 if( !IsActive() ) |
|
154 { |
|
155 iCheckTimer.After( iStatus, aDelay ); |
|
156 SetActive(); |
|
157 } |
|
158 } |
|
159 // END OF FILE |