|
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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #include "CAllocManager.h" |
|
22 #include <e32property.h> |
|
23 #include "t_oomdummyplugin_properties.h" |
|
24 |
|
25 const TInt KOneSecond = 1000000; |
|
26 |
|
27 CAllocManager::CAllocManager() |
|
28 { |
|
29 // No implementation required |
|
30 } |
|
31 |
|
32 CAllocManager::~CAllocManager() |
|
33 { |
|
34 delete iAllocationTimer; |
|
35 iSimulations.ResetAndDestroy(); |
|
36 } |
|
37 |
|
38 CAllocManager* CAllocManager::NewLC() |
|
39 { |
|
40 CAllocManager* self = new (ELeave) CAllocManager(); |
|
41 CleanupStack::PushL(self); |
|
42 self->ConstructL(); |
|
43 return self; |
|
44 } |
|
45 |
|
46 CAllocManager* CAllocManager::NewL() |
|
47 { |
|
48 CAllocManager* self = CAllocManager::NewLC(); |
|
49 CleanupStack::Pop(); // self; |
|
50 return self; |
|
51 } |
|
52 |
|
53 void CAllocManager::ConstructL() |
|
54 { |
|
55 iAllocationTimer = CPeriodic::NewL(CActive::EPriorityStandard); |
|
56 } |
|
57 |
|
58 TInt CAllocManager::SimulationTickStatic(TAny *aPtr) |
|
59 { |
|
60 ((CAllocManager*)aPtr)->SimulationTick(); |
|
61 return KErrNone; |
|
62 } |
|
63 |
|
64 void CAllocManager::SimulationTick() |
|
65 { |
|
66 for(TInt i=iSimulations.Count()-1; i>=0; i--) |
|
67 { |
|
68 iSimulations[i]->SimulationTick(); |
|
69 } |
|
70 } |
|
71 |
|
72 TInt CAllocManager::Reset() |
|
73 { |
|
74 StopAllocating(); |
|
75 iSimulations.ResetAndDestroy(); |
|
76 return KErrNone; |
|
77 } |
|
78 |
|
79 TInt CAllocManager::StartAllocating() |
|
80 { |
|
81 iAllocationTimer->Cancel(); |
|
82 iAllocationTimer->Start(KOneSecond, KOneSecond, TCallBack(SimulationTickStatic, this)); |
|
83 return KErrNone; |
|
84 } |
|
85 |
|
86 TInt CAllocManager::StopAllocating() |
|
87 { |
|
88 iAllocationTimer->Cancel(); |
|
89 return KErrNone; |
|
90 } |
|
91 |
|
92 void CAllocManager::ConfigureL(TUint aPlugin, TUint aAllocRate, TUint aAllocInitial, TUint aAllocLimit) |
|
93 { |
|
94 TInt sim = FindSimulation(aPlugin); |
|
95 if(sim>=0) |
|
96 { |
|
97 delete iSimulations[sim]; |
|
98 iSimulations.Remove(sim); |
|
99 } |
|
100 CAllocSimulation* newsim = CAllocSimulation::NewLC(aAllocLimit, aAllocInitial, aAllocRate, aPlugin); |
|
101 iSimulations.AppendL(newsim); |
|
102 CleanupStack::Pop(newsim); |
|
103 } |
|
104 |
|
105 TInt CAllocManager::MemoryLow(TUint aPlugin) |
|
106 { |
|
107 TInt sim = FindSimulation(aPlugin); |
|
108 if(sim>=0) |
|
109 { |
|
110 iSimulations[sim]->SetPaused(ETrue); |
|
111 iSimulations[sim]->FreeMemory(); |
|
112 return KErrNone; |
|
113 } |
|
114 else return KErrNotFound; |
|
115 } |
|
116 |
|
117 TInt CAllocManager::MemoryGood(TUint aPlugin) |
|
118 { |
|
119 TInt sim = FindSimulation(aPlugin); |
|
120 if(sim>=0) |
|
121 { |
|
122 iSimulations[sim]->SetPaused(EFalse); |
|
123 return KErrNone; |
|
124 } |
|
125 else return KErrNotFound; |
|
126 } |
|
127 |
|
128 TInt CAllocManager::FindSimulation(TUint aUid) |
|
129 { |
|
130 return iSimulations.Find<TUint>(aUid, CAllocSimulation::CompareTo); |
|
131 } |
|
132 |
|
133 CAllocSimulation::CAllocSimulation(TInt aAllocLimit, TInt aAllocInitial, |
|
134 TInt aAllocRate, TUint aUid) : |
|
135 iAllocLimit(aAllocLimit), |
|
136 iAllocInitial(aAllocInitial), |
|
137 iAllocRate(aAllocRate), |
|
138 iUid(aUid) |
|
139 { |
|
140 // No implementation required |
|
141 } |
|
142 |
|
143 CAllocSimulation::~CAllocSimulation() |
|
144 { |
|
145 iChunk.Close(); |
|
146 } |
|
147 |
|
148 CAllocSimulation* CAllocSimulation::NewLC(TInt aAllocLimit, TInt aAllocInitial, |
|
149 TInt aAllocRate, TUint aUid) |
|
150 { |
|
151 CAllocSimulation* self = new (ELeave) CAllocSimulation(aAllocLimit, aAllocInitial, |
|
152 aAllocRate, aUid); |
|
153 CleanupStack::PushL(self); |
|
154 self->ConstructL(); |
|
155 return self; |
|
156 } |
|
157 |
|
158 CAllocSimulation* CAllocSimulation::NewL(TInt aAllocLimit, TInt aAllocInitial, |
|
159 TInt aAllocRate, TUint aUid) |
|
160 { |
|
161 CAllocSimulation* self = CAllocSimulation::NewLC(aAllocLimit, aAllocInitial, |
|
162 aAllocRate, aUid); |
|
163 CleanupStack::Pop(); // self; |
|
164 return self; |
|
165 } |
|
166 |
|
167 void CAllocSimulation::ConstructL() |
|
168 { |
|
169 User::LeaveIfError(iChunk.CreateLocal(iAllocInitial, iAllocLimit)); |
|
170 } |
|
171 |
|
172 void CAllocSimulation::SimulationTick() |
|
173 { |
|
174 if(!iPaused) |
|
175 { |
|
176 TUint allocnext = iAllocCurrent + iAllocRate; |
|
177 if(allocnext < iAllocInitial) allocnext = iAllocInitial; |
|
178 else if(allocnext > iAllocLimit) allocnext = iAllocLimit; |
|
179 if(iAllocCurrent != allocnext && KErrNone == iChunk.Adjust(iAllocCurrent)) |
|
180 { |
|
181 iAllocCurrent = allocnext; |
|
182 RProperty::Set(KUidOomPropertyCategory, iUid + KOOMDummyPluginCurrentAllocationBytes, iAllocCurrent); |
|
183 } |
|
184 } |
|
185 } |
|
186 |
|
187 void CAllocSimulation::SetPaused(TBool aPaused) |
|
188 { |
|
189 iPaused = aPaused; |
|
190 } |
|
191 |
|
192 void CAllocSimulation::FreeMemory() |
|
193 { |
|
194 iChunk.Adjust(0); |
|
195 iAllocCurrent = 0; |
|
196 RProperty::Set(KUidOomPropertyCategory, iUid + KOOMDummyPluginCurrentAllocationBytes, iAllocCurrent); |
|
197 } |
|
198 |
|
199 TBool CAllocSimulation::CompareTo(const TUint* aUid, const CAllocSimulation& aSelf) |
|
200 { |
|
201 return *aUid==aSelf.iUid; |
|
202 } |
|
203 |