author | hgs |
Fri, 23 Apr 2010 22:08:41 +0100 | |
changeset 121 | 661475905584 |
parent 90 | 947f0dc9f7a8 |
child 176 | af6ec97d9189 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
2 |
// All rights reserved. |
|
3 |
// This component and the accompanying materials are made available |
|
4 |
// under the terms of the License "Eclipse Public License v1.0" |
|
5 |
// which accompanies this distribution, and is available |
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// f32\sfile\sf_memory_man.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
/** |
|
19 |
@file |
|
20 |
@internalTechnology |
|
21 |
*/ |
|
22 |
||
23 |
||
24 |
#include <e32std.h> |
|
25 |
#include <e32std_private.h> |
|
26 |
#include "sf_std.h" |
|
27 |
#include <e32uid.h> |
|
28 |
#include <e32wins.h> |
|
29 |
#include <f32file.h> |
|
30 |
#include <hal.h> |
|
31 |
#include "sf_memory_man.h" |
|
32 |
#include "sf_memory_client.h" |
|
33 |
||
34 |
/** |
|
35 |
Destructor of the CCacheMemoryManager, need to destroy all registered clients. |
|
36 |
*/ |
|
37 |
CCacheMemoryManager::~CCacheMemoryManager() |
|
38 |
{ |
|
39 |
for (TInt i = 0; i < iRegisteredClients.Count(); i++) |
|
40 |
{ |
|
41 |
iRegisteredClients[i]->Reset(); |
|
42 |
delete iRegisteredClients[i]; |
|
43 |
} |
|
44 |
iRegisteredClients.Close(); |
|
45 |
} |
|
46 |
||
47 |
/** |
|
48 |
Static factory function of CCacheMemoryManager |
|
49 |
@param aCacheSize the total size of the virtual address space |
|
50 |
*/ |
|
51 |
CCacheMemoryManager* CCacheMemoryManager::NewL(TInt aCacheSize) |
|
52 |
{ |
|
53 |
CCacheMemoryManager* cacheMemoryManager = new (ELeave) CCacheMemoryManager(aCacheSize); |
|
54 |
||
55 |
CleanupStack::PushL(cacheMemoryManager); |
|
56 |
cacheMemoryManager->ConstructL(); |
|
57 |
CleanupStack::Pop(1, cacheMemoryManager); |
|
58 |
||
59 |
return cacheMemoryManager; |
|
60 |
} |
|
61 |
||
62 |
/** |
|
63 |
Constructor of CCacheMemoryManager |
|
64 |
@param aMaxSize the total size of the virtual address space |
|
65 |
*/ |
|
66 |
CCacheMemoryManager::CCacheMemoryManager(TUint32 aMaxSize) |
|
67 |
:iBase(NULL), |
|
68 |
iSizeInBytes(aMaxSize), |
|
69 |
iCurrentOffsetMark(0) |
|
70 |
{ |
|
71 |
} |
|
72 |
||
73 |
/** |
|
74 |
Second phase constructor of CCacheMemoryManager. |
|
75 |
Creates RChunk object and sets low memory threshold. |
|
76 |
*/ |
|
77 |
void CCacheMemoryManager::ConstructL() |
|
78 |
{ |
|
79 |
// calculate the low-memory threshold below which we fail any attempt to allocate memory |
|
80 |
TMemoryInfoV1Buf meminfo; |
|
81 |
TInt r = UserHal::MemoryInfo(meminfo); |
|
82 |
ASSERT(r==KErrNone); |
|
83 |
User::LeaveIfError(r); |
|
84 |
iLowMemoryThreshold = (meminfo().iTotalRamInBytes * TGlobalCacheMemorySettings::LowMemoryThreshold()) / 100; |
|
85 |
TChunkCreateInfo createInfo; |
|
86 |
createInfo.SetCache(iSizeInBytes); |
|
87 |
createInfo.SetOwner(EOwnerProcess); |
|
88 |
r = iChunk.Create(createInfo); |
|
89 |
ASSERT(r==KErrNone); |
|
90 |
User::LeaveIfError(r); |
|
91 |
UserSvr::RegisterTrustedChunk(iChunk.Handle()); |
|
92 |
iBase = iChunk.Base(); |
|
93 |
iRegisteredClients.ReserveL(10); |
|
94 |
__PRINT3(_L("CCacheMemoryManager::ConstructL(lowMem=%d, iSize=%d, base=0x%lx)"), iLowMemoryThreshold, iSizeInBytes, iBase); |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
Connect or register a client. |
|
99 |
Note: callers of this function should be constructor of various caches, it is their resposibility |
|
100 |
to make sure that parameters are sensible when calling this function |
|
101 |
||
102 |
@internalTechnology |
|
103 |
@released |
|
104 |
||
105 |
@param aClientName an identifier of the client to be connected |
|
106 |
@param aMinSizeInSegs minimum client size in segments |
|
107 |
@param aMaxSizeInSegs maximum client size in segments |
|
108 |
@return CCacheMemoryClient* pointer to the client that connected, or NULL if parameters are not valid. |
|
109 |
@leave if no memory |
|
110 |
*/ |
|
111 |
EXPORT_C CCacheMemoryClient* CCacheMemoryManager::ConnectClientL(const TDesC& aClientName, TUint32 aMinSizeInSegs, TUint32 aMaxSizeInSegs) |
|
112 |
{ |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
113 |
__PRINT3(_L("CCacheMemoryManager::ConnectClientL([%S], minSeg=%d, maxSeg=%d)"), &aClientName, aMinSizeInSegs, aMaxSizeInSegs); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
114 |
|
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
115 |
// search for existing clients by name |
0 | 116 |
for (TInt i = 0; i < iRegisteredClients.Count(); i++) |
117 |
{ |
|
118 |
if (aClientName.Compare(iRegisteredClients[i]->Name()) == 0) |
|
119 |
{ |
|
120 |
ASSERT(iRegisteredClients[i]->iTouchedRegionFlag == 0); |
|
121 |
__PRINT1(_L("CCacheMemoryManager::ConnectClientL: [%S] found!"), &aClientName); |
|
122 |
return iRegisteredClients[i]; |
|
123 |
} |
|
124 |
} |
|
125 |
||
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
126 |
// if it is a new drive/file system who wants to connect, create a new client for it |
0 | 127 |
// parameter validation |
128 |
if (iSizeInBytes < iCurrentOffsetMark + (aMaxSizeInSegs << SegmentSizeInBytesLog2())) |
|
129 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
130 |
__PRINT1(_L("CCacheMemoryManager::ConnectClientL([%S]) failed, please check \"GlobalCacheMemorySize\" setting!!!"), &aClientName); |
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
User::Leave(KErrArgument); |
0 | 132 |
} |
133 |
||
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
134 |
// note: client creation may leave under OOM conditions |
0 | 135 |
CCacheMemoryClient* client = CCacheMemoryClient::NewL(*this, aClientName, iCurrentOffsetMark, aMinSizeInSegs, aMaxSizeInSegs); |
136 |
||
137 |
// if error happens during client registration, the client will be deleted |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
138 |
// this may leave under OOM conditions |
0 | 139 |
TInt err = iRegisteredClients.Append(client); |
140 |
if (err != KErrNone) |
|
141 |
{ |
|
142 |
delete client; |
|
143 |
client = NULL; |
|
144 |
User::Leave(err); |
|
145 |
} |
|
146 |
||
147 |
// record current offset mark for next client |
|
148 |
iCurrentOffsetMark += (aMaxSizeInSegs << SegmentSizeInBytesLog2()); |
|
149 |
return client; |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
Commit a contiguous set of segments. |
|
154 |
@param aStartRamAddr the start ram address of the region to be committed. |
|
155 |
@param aSegmentCount the segment number of the contiguous region to be committed. |
|
156 |
@return TInt KErrNone if succeeded, KErrNoMemory if passed low memory threshold, otherwise a system-wide error code. |
|
157 |
*/ |
|
158 |
TInt CCacheMemoryManager::AllocateAndLockSegments(TUint8* aStartRamAddr, TInt aSegmentCount) |
|
159 |
{ |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
__PRINT2(_L("CCacheMemoryManager::AllocateAndLockSegments(base=0x%x, seg=%d)"), aStartRamAddr, aSegmentCount); |
0 | 161 |
TMemoryInfoV1Buf meminfo; |
162 |
TInt r = UserHal::MemoryInfo(meminfo); |
|
163 |
__ASSERT_DEBUG(r==KErrNone,Fault(EMemoryInfoFailed)); |
|
164 |
if (r != KErrNone) |
|
165 |
{ |
|
166 |
return r; |
|
167 |
} |
|
168 |
||
169 |
if (isMemoryLow || (meminfo().iFreeRamInBytes < iLowMemoryThreshold)) |
|
170 |
{ |
|
171 |
__PRINT(_L("CCacheMemoryManager: free RAM below threshold !!!")); |
|
172 |
return KErrNoMemory; |
|
173 |
} |
|
174 |
return Commit(aStartRamAddr, aSegmentCount); |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
Notify the change of memory status |
|
179 |
@param aIsMemoryLow the flag that sets current memory status |
|
180 |
*/ |
|
181 |
void CCacheMemoryManager::FreeMemoryChanged(TBool aIsMemoryLow) |
|
182 |
{ |
|
183 |
isMemoryLow = aIsMemoryLow; |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
Decommit a contiguous set of segments. |
|
188 |
@param aStartRamAddr the start ram address of the region to be decommitted. |
|
189 |
@param aSegmentCount the segment number of the contiguous region to be decommitted. |
|
190 |
@return TInt KErrNone if succeeded, otherwise a system-wide error code. |
|
191 |
*/ |
|
192 |
TInt CCacheMemoryManager::DecommitSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
193 |
{ |
|
194 |
return Decommit(aStartRamAddr, aSegmentCount); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
Lock a contiguous set of segments. |
|
199 |
@param aStartRamAddr the start ram address of the region to be locked. |
|
200 |
@param aSegmentCount the segment number of the contiguous region to be locked. |
|
201 |
@return TInt KErrNone if succeeded, otherwise a system-wide error code. |
|
202 |
*/ |
|
203 |
TInt CCacheMemoryManager::LockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
204 |
{ |
|
205 |
return Lock(aStartRamAddr, aSegmentCount); |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
Unlock a contiguous set of segments. |
|
210 |
@param aStartRamAddr the start ram address of the region to be unlocked. |
|
211 |
@param aSegmentCount the segment number of the contiguous region to be unlocked. |
|
212 |
@return TInt KErrNone if succeeded, otherwise a system-wide error code. |
|
213 |
*/ |
|
214 |
TInt CCacheMemoryManager::UnlockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
215 |
{ |
|
216 |
return Unlock(aStartRamAddr, aSegmentCount); |
|
217 |
} |
|
218 |
||
219 |
/** |
|
220 |
Commit a contiguous set of segments. |
|
221 |
@param aStartRamAddr the start ram address of the region to be committed. |
|
222 |
@param aSegmentCount the segment number of the contiguous region to be committed. |
|
223 |
@return TInt KErrNone if succeeded, otherwise a system-wide error code. |
|
224 |
*/ |
|
225 |
TInt CCacheMemoryManager::Commit(TUint8* aStartRamAddr, TInt aSegmentCount) |
|
226 |
{ |
|
227 |
TInt offset = aStartRamAddr - iBase; |
|
228 |
TInt r = iChunk.Commit(offset, aSegmentCount << KSegmentSizeLog2); |
|
229 |
return r; |
|
230 |
} |
|
231 |
||
232 |
/** |
|
233 |
Actual implementation of DecommitSegments(). |
|
234 |
@see CCacheMemoryManager::DecommitSegments(). |
|
235 |
*/ |
|
236 |
TInt CCacheMemoryManager::Decommit(TUint8* aStartRamAddr, TInt aSegmentCount) |
|
237 |
{ |
|
238 |
return iChunk.Decommit(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2); |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
Actual implementation of UnlockSegments(). |
|
243 |
@see CCacheMemoryManager::UnlockSegments(). |
|
244 |
*/ |
|
245 |
TInt CCacheMemoryManager::Unlock(TUint8* aStartRamAddr, TInt aSegmentCount) |
|
246 |
{ |
|
247 |
TInt r = iChunk.Unlock(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2); |
|
248 |
return r; |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
Actual implementation of LockSegments(). |
|
253 |
@see CCacheMemoryManager::LockSegments(). |
|
254 |
*/ |
|
255 |
TInt CCacheMemoryManager::Lock(TUint8* aStartRamAddr, TInt aSegmentCount) |
|
256 |
{ |
|
257 |
return iChunk.Lock(aStartRamAddr - iBase, aSegmentCount << KSegmentSizeLog2); |
|
258 |
} |
|
259 |
||
260 |
||
261 |
//===================================================================== |
|
262 |
TUint8* CCacheMemoryManager::Base() |
|
263 |
{ |
|
264 |
return iChunk.Base(); |
|
265 |
} |
|
266 |
||
267 |
/** |
|
268 |
Get function, returns log2 value of a segment size in bytes. |
|
269 |
||
270 |
@internalTechnology |
|
271 |
@released |
|
272 |
*/ |
|
273 |
EXPORT_C TUint CCacheMemoryManager::SegmentSizeInBytesLog2() const |
|
274 |
{ |
|
275 |
return KSegmentSizeLog2; |
|
276 |
} |
|
277 |
||
278 |
||
279 |
//============================================================================= |
|
280 |
/** |
|
281 |
The singleton of global cache memory manager |
|
282 |
*/ |
|
283 |
CCacheMemoryManager* CCacheMemoryManagerFactory::iCacheMemoryManager = NULL; |
|
284 |
||
285 |
/** |
|
286 |
Global factory function of CCacheMemoryManager. |
|
287 |
*/ |
|
288 |
void CCacheMemoryManagerFactory::CreateL() |
|
289 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
290 |
if (TGlobalCacheMemorySettings::CacheSize() > 0) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
291 |
iCacheMemoryManager = CCacheMemoryManager::NewL(TGlobalCacheMemorySettings::CacheSize()); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
292 |
else |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
293 |
__PRINT(_L("\"GlobalCacheMemorySize\" set <= 0, CCacheMemoryManager is not created!!!")); |
0 | 294 |
} |
295 |
||
296 |
/** |
|
297 |
Global get function of CCacheMemoryManager. |
|
298 |
||
299 |
@internalTechnology |
|
300 |
@released |
|
301 |
*/ |
|
302 |
EXPORT_C CCacheMemoryManager* CCacheMemoryManagerFactory::CacheMemoryManager() |
|
303 |
{ |
|
304 |
return iCacheMemoryManager; |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
Global destroy function of CCacheMemoryManager. |
|
309 |
*/ |
|
310 |
void CCacheMemoryManagerFactory::Destroy() |
|
311 |
{ |
|
312 |
delete iCacheMemoryManager; |
|
313 |
iCacheMemoryManager = NULL; |
|
314 |
} |
|
315 |
||
316 |
//============================================================================= |
|
317 |
const TInt KByteToByteShift = 10; |
|
318 |
TInt32 TGlobalCacheMemorySettings::iCacheSizeInBytes = KDefaultGlobalCacheMemorySize << KByteToByteShift; |
|
319 |
TInt32 TGlobalCacheMemorySettings::iLowMemoryThreshold = KDefaultLowMemoryThreshold; |
|
320 |
_LIT8(KLitSectionNameCacheMemory,"CacheMemory"); |
|
321 |
||
322 |
/** |
|
323 |
Read ESTART.TXT file for global cache memory settings |
|
324 |
*/ |
|
325 |
void TGlobalCacheMemorySettings::ReadPropertiesFile() |
|
326 |
{ |
|
327 |
// Get size of cache in kilobytes |
|
328 |
TInt32 cacheSizeInKBytes; |
|
329 |
if (F32Properties::GetInt(KLitSectionNameCacheMemory, _L8("GlobalCacheMemorySize"), cacheSizeInKBytes)) |
|
330 |
{ |
|
331 |
iCacheSizeInBytes = cacheSizeInKBytes << KByteToByteShift; |
|
332 |
} |
|
333 |
||
334 |
// Get low memory threshold |
|
335 |
TInt32 lowMemoryThreshold; |
|
336 |
if (F32Properties::GetInt(KLitSectionNameCacheMemory, _L8("LowMemoryThreshold"), lowMemoryThreshold)) |
|
337 |
iLowMemoryThreshold = lowMemoryThreshold; |
|
338 |
} |
|
339 |
||
340 |
TInt TGlobalCacheMemorySettings::CacheSize() |
|
341 |
{ |
|
342 |
return iCacheSizeInBytes; |
|
343 |
} |
|
344 |
||
345 |
TInt TGlobalCacheMemorySettings::LowMemoryThreshold() |
|
346 |
{ |
|
347 |
return iLowMemoryThreshold; |
|
348 |
} |
|
349 |
||
350 |