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