author | hgs |
Thu, 12 Aug 2010 12:51:24 +0100 | |
changeset 245 | 647ab20fee2e |
parent 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_client.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
/** |
|
19 |
@file |
|
20 |
@internalTechnology |
|
21 |
*/ |
|
22 |
||
23 |
#include <e32std.h> |
|
24 |
#include <e32std_private.h> |
|
25 |
#include "sf_std.h" |
|
26 |
#include <e32uid.h> |
|
27 |
#include <e32wins.h> |
|
28 |
#include <f32file.h> |
|
29 |
#include "sf_memory_man.h" |
|
30 |
#include "sf_memory_client.h" |
|
31 |
||
32 |
||
33 |
CCacheMemoryClient::~CCacheMemoryClient() |
|
34 |
{ |
|
35 |
const TUint32 segCnt = iTouchedRegionFlag <= iReservedRegionMarkInSegs ? |
|
36 |
iReservedRegionMarkInSegs : iTouchedRegionFlag; |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
37 |
TInt r = DecommitSegments(iBase, segCnt); |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
38 |
if (r != KErrNone) // this 'if() {}' is to remove build warnings |
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
39 |
{ |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
40 |
ASSERT(0); |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
41 |
} |
0 | 42 |
iReusablePagePool.Close(); |
43 |
delete iName; |
|
44 |
} |
|
45 |
||
46 |
/** |
|
47 |
Static factory function for CCacheMemoryClient |
|
48 |
@param aManager reference of CCacheMemoryManager that this client register with |
|
49 |
@param aClientName the unique identification of CCacheMemoryClient |
|
50 |
@param aOffsetInBytes the offset to the base position of CCacheMemoryManager it registered |
|
51 |
@param aMinSizeInSegs the minimum client size in segments, equals to the number of reserved segments |
|
52 |
@param aMaxSizeInSegs the maximum client size in segments |
|
53 |
*/ |
|
54 |
CCacheMemoryClient* CCacheMemoryClient::NewL(CCacheMemoryManager& aManager, const TDesC& aClientName, TUint32 aOffsetInBytes, TUint32 aMinSizeInSegs, TUint32 aMaxSizeInSegs) |
|
55 |
{ |
|
56 |
// only create clients when sensible parameters are provided, otherwise will cause fatal error to file server |
|
57 |
if (aMinSizeInSegs > 0 && aMaxSizeInSegs >= aMinSizeInSegs && aClientName.Length() > 0) |
|
58 |
{ |
|
59 |
CCacheMemoryClient* self = new(ELeave) CCacheMemoryClient(aManager, aMinSizeInSegs, aMaxSizeInSegs); |
|
60 |
CleanupStack::PushL(self); |
|
61 |
self->ConstructL(aClientName, aOffsetInBytes); |
|
62 |
CleanupStack::Pop(1, self); |
|
63 |
return self; |
|
64 |
} |
|
65 |
return NULL; |
|
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
Constructor of CCacheMemoryClient |
|
70 |
@param aManager reference of CCacheMemoryManager that this client register with |
|
71 |
@param aMinSizeInSegs the minimum client size in segments, equals to the number of reserved segments |
|
72 |
@param aMaxSizeInSegs the maximum client size in segments |
|
73 |
*/ |
|
74 |
CCacheMemoryClient::CCacheMemoryClient(CCacheMemoryManager& aManager, TUint32 aMinSizeInSegs, TUint32 aMaxSizeInSegs) |
|
75 |
:iManager(aManager), |
|
76 |
iMinSizeInSegs(aMinSizeInSegs), |
|
77 |
iMaxSizeInSegs(aMaxSizeInSegs) |
|
78 |
{ |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
Second phase constructor of CCacheMemoryClient |
|
83 |
@param aClientName the unique identification of CCacheMemoryClient |
|
84 |
@param aOffsetInBytes the offset to the base position of CCacheMemoryManager it registered |
|
85 |
*/ |
|
86 |
void CCacheMemoryClient::ConstructL(const TDesC& aClientName, TUint32 aOffsetInBytes) |
|
87 |
{ |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
__PRINT4(_L("CCacheMemoryClient::ConstructL(%S, min=%d, max=%d, base=0x%X)"), &aClientName, iMinSizeInSegs, iMaxSizeInSegs, iBase); |
0 | 89 |
iName = HBufC::NewMaxL(aClientName.Length()); |
90 |
*iName = aClientName; |
|
91 |
iSegSizeInBytesLog2 = iManager.SegmentSizeInBytesLog2(); |
|
92 |
iSegSizeInBytes = 1 << iSegSizeInBytesLog2; |
|
93 |
iReusablePagePool.Close(); |
|
94 |
iReusablePagePool.ReserveL(iMinSizeInSegs); |
|
95 |
iBase = iManager.Base() + aOffsetInBytes; |
|
96 |
iReservedRegionMarkInSegs = iMinSizeInSegs; |
|
97 |
TInt r = iManager.AllocateAndLockSegments(iBase, iReservedRegionMarkInSegs); |
|
98 |
User::LeaveIfError(r); |
|
99 |
iTouchedRegionFlag = 0; |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
100 |
__PRINT(_L("CCacheMemoryClient::ConstructL() return 0")); |
0 | 101 |
} |
102 |
||
103 |
/** |
|
104 |
Reset the client state, this is normally issued from the cache it connected with. |
|
105 |
For exmaple, a FAT volume is dismounted so the directory cache needs to be reset. |
|
106 |
Although the cache is reset, the CCacheMemoryClient object should not be destroyed and the reserved region should still |
|
107 |
be hold to make sure when the cache re-connect with the CCacheMemoryClient, it will not fail due to OOM conditions. |
|
108 |
||
109 |
@internalTechnology |
|
110 |
@released |
|
111 |
*/ |
|
112 |
EXPORT_C void CCacheMemoryClient::Reset() |
|
113 |
{ |
|
114 |
__PRINT3(_L("CCacheMemoryClient::Reset(%S, reserved=%d, touched=%d)"), iName, iReservedRegionMarkInSegs, iTouchedRegionFlag); |
|
115 |
||
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
116 |
// reset the cache memeory client to initial states: |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
117 |
// 1. all memory that have been 'touched' should be decommitted |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
118 |
// 2. the reserved region of memory should be re-locked |
0 | 119 |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
120 |
// if we have touched more than reserved region of memory, we shall decommit all of them |
0 | 121 |
if (iTouchedRegionFlag > iReservedRegionMarkInSegs) |
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
122 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
123 |
TInt r = DecommitSegments(iBase, iTouchedRegionFlag); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
124 |
if (r != KErrNone) // this 'if() {}' is to remove build warnings in debug mode, same is below |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
125 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
126 |
ASSERT(0); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
127 |
} |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
128 |
} |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
129 |
else // otherwise we decommit the reserved region of memory only. |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
130 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
TInt r = DecommitSegments(iBase, iReservedRegionMarkInSegs); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
132 |
if (r != KErrNone) // this 'if() {}' is to remove build warnings in debug mode, same is below |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
133 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
134 |
ASSERT(0); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
135 |
} |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
} |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
|
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
138 |
// re-lock the reserved region of memory |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
139 |
TInt r = iManager.AllocateAndLockSegments(iBase, iReservedRegionMarkInSegs); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
140 |
if (r != KErrNone) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
141 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
142 |
ASSERT(0); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
143 |
} |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
144 |
|
0 | 145 |
iTouchedRegionFlag = 0; |
146 |
iReusablePagePool.Close(); |
|
245 | 147 |
r = iReusablePagePool.Reserve(iReservedRegionMarkInSegs); |
148 |
if (r != KErrNone) |
|
149 |
{ |
|
150 |
ASSERT(0); |
|
151 |
} |
|
0 | 152 |
} |
153 |
||
154 |
/** |
|
155 |
Commit an unused set of contiguous segments. |
|
156 |
@param aSegmentCount the segment number to be commited. |
|
157 |
@return TUint8* the starting ram address of the commited segments. |
|
158 |
||
159 |
@internalTechnology |
|
160 |
@released |
|
161 |
*/ |
|
162 |
EXPORT_C TUint8* CCacheMemoryClient::AllocateAndLockSegments(TUint32 aSegmentCount) |
|
163 |
{ |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
__PRINT4(_L("CCacheMemoryClient::AllocateAndLockSegments(%S, segs=%d, reserved=%d, touched=%d)"), iName, aSegmentCount, iReservedRegionMarkInSegs, iTouchedRegionFlag); |
0 | 165 |
// __PRINT2(_L("iBase = 0x%x, segcnt = %d"), iBase, aSegmentCount); |
166 |
TUint8* addr = NULL; |
|
167 |
// if we are walking through the reserved region first time, we should |
|
168 |
// make assumption that this area is locked already |
|
169 |
if (iTouchedRegionFlag + aSegmentCount <= iReservedRegionMarkInSegs) |
|
170 |
{ |
|
171 |
addr = iBase + (iTouchedRegionFlag << iSegSizeInBytesLog2); |
|
172 |
iTouchedRegionFlag += aSegmentCount; |
|
173 |
// __PRINT3(_L("!! USED RESERVED SEGS: addr=0x%x, touched=%d, reserved=%d"), addr, iTouchedRegionFlag, iReservedRegionMarkInSegs); |
|
174 |
return addr; |
|
175 |
} |
|
176 |
||
176 | 177 |
TBool touchedMore = EFalse; |
178 |
// if we have touched beyond reserved region, get new pages from reusable pool first |
|
0 | 179 |
if (iReusablePagePool.Count()) |
180 |
{ |
|
181 |
addr = iReusablePagePool[0]; |
|
182 |
iReusablePagePool.Remove(0); |
|
183 |
// __PRINT2(_L("!! USED REUSABLE POOL SEGS: addr=0x%x, reusable.Count()=%d"), addr, iReusablePagePool.Count()); |
|
184 |
} |
|
185 |
// or we grow the touched region flag |
|
186 |
else |
|
187 |
{ |
|
188 |
addr = iBase + (iTouchedRegionFlag << iSegSizeInBytesLog2); |
|
176 | 189 |
touchedMore = ETrue; |
0 | 190 |
// __PRINT2(_L("!! GROW TOUCHED SEGS: addr=0x%x, touched=%d"), addr, iTouchedRegionFlag); |
191 |
} |
|
192 |
||
193 |
// parameter validation |
|
194 |
ASSERT(((addr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount <= iMaxSizeInSegs); |
|
195 |
if(((addr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs) |
|
196 |
{ |
|
197 |
ASSERT(0); |
|
198 |
return NULL; |
|
199 |
} |
|
200 |
||
201 |
TInt r = iManager.AllocateAndLockSegments(addr, aSegmentCount); |
|
202 |
if (r != KErrNone) |
|
203 |
return NULL; |
|
204 |
||
176 | 205 |
if (touchedMore) |
206 |
{ |
|
207 |
iTouchedRegionFlag += aSegmentCount; |
|
208 |
} |
|
0 | 209 |
return addr; |
210 |
} |
|
211 |
||
212 |
/** |
|
213 |
Decommit a set of contiguous segments. |
|
214 |
@param aStartRamAddr the start ram address of the region to be decommitted. |
|
215 |
@param aSegmentCount the segment number to be decommited. |
|
216 |
@return KErrNone if succeed, otherwise a system-wide error code. |
|
217 |
||
218 |
@internalTechnology |
|
219 |
@released |
|
220 |
*/ |
|
221 |
EXPORT_C TInt CCacheMemoryClient::DecommitSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
222 |
{ |
|
223 |
__PRINT4(_L("CCacheMemoryClient::DecommitSegments(%S, reserved=%d, touched=%d, segNo=%d)"), iName, iReservedRegionMarkInSegs, iTouchedRegionFlag, aSegmentCount); |
|
224 |
// parameter validation |
|
225 |
if(aStartRamAddr < iBase || |
|
226 |
(((aStartRamAddr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs)) |
|
227 |
{ |
|
228 |
ASSERT(0); |
|
229 |
return KErrArgument; |
|
230 |
} |
|
231 |
||
232 |
TInt err = iManager.DecommitSegments(aStartRamAddr, aSegmentCount); |
|
233 |
ASSERT(err == KErrNone); |
|
234 |
if (err != KErrNone) |
|
235 |
return err; |
|
245 | 236 |
err = iReusablePagePool.Append(aStartRamAddr); |
237 |
if (err != KErrNone) |
|
238 |
return err; |
|
0 | 239 |
return KErrNone; |
240 |
} |
|
241 |
||
242 |
/** |
|
243 |
Lock a set of contiguous segments. |
|
244 |
@param aStartRamAddr the start ram address of the region to be locked. |
|
245 |
@param aSegmentCount the segment number to be locked. |
|
246 |
@return KErrNone if succeed, otherwise a system-wide error code. |
|
247 |
||
248 |
@internalTechnology |
|
249 |
@released |
|
250 |
*/ |
|
251 |
EXPORT_C TInt CCacheMemoryClient::LockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
252 |
{ |
|
253 |
__PRINT4(_L("CCacheMemoryClient::LockSegments(%S, reserved=%d, touched=%d, segNo=%d)"), iName, iReservedRegionMarkInSegs, iTouchedRegionFlag, (aStartRamAddr - iBase) >> iSegSizeInBytesLog2); |
|
254 |
// parameter validation |
|
255 |
if(aStartRamAddr < iBase || |
|
256 |
(((aStartRamAddr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs)) |
|
257 |
{ |
|
258 |
ASSERT(0); |
|
259 |
return KErrArgument; |
|
260 |
} |
|
261 |
return iManager.LockSegments(aStartRamAddr, aSegmentCount); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
Unlock a set of contiguous segments. |
|
266 |
@param aStartRamAddr the start ram address of the region to be unlocked. |
|
267 |
@param aSegmentCount the segment number to be unlocked. |
|
268 |
@return KErrNone if succeed, otherwise a system-wide error code. |
|
269 |
||
270 |
@internalTechnology |
|
271 |
@released |
|
272 |
*/ |
|
273 |
EXPORT_C TInt CCacheMemoryClient::UnlockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
274 |
{ |
|
275 |
__PRINT4(_L("CCacheMemoryClient::UnlockSegments(%S, reserved=%d, touched=%d, segNo=%d)"), iName, iReservedRegionMarkInSegs, iTouchedRegionFlag, (aStartRamAddr - iBase) >> iSegSizeInBytesLog2); |
|
276 |
// validate parameters |
|
277 |
if(aStartRamAddr < iBase || |
|
278 |
(((aStartRamAddr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs)) |
|
279 |
{ |
|
280 |
ASSERT(0); |
|
281 |
return KErrArgument; |
|
282 |
} |
|
283 |
return iManager.UnlockSegments(aStartRamAddr, aSegmentCount); |
|
284 |
} |
|
285 |
||
286 |
//////////////////////////// auxiliary functions ///////////////////////// |
|
287 |
TUint32 CCacheMemoryClient::MaxSizeInBytes() |
|
288 |
{ |
|
289 |
return iMaxSizeInSegs << iSegSizeInBytesLog2; |
|
290 |
} |
|
291 |
||
292 |
const TDesC& CCacheMemoryClient::Name() const |
|
293 |
{ |
|
294 |
return *iName; |
|
295 |
} |
|
296 |