author | Pat Downey <patd@symbian.org> |
Wed, 01 Sep 2010 12:34:56 +0100 | |
branch | RCL_3 |
changeset 44 | 3e88ff8f41d5 |
parent 43 | c1f20ce4abcf |
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; |
|
20
597aaf25e343
Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
37 |
TInt r = DecommitSegments(iBase, segCnt); |
597aaf25e343
Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
38 |
if (r != KErrNone) // this 'if() {}' is to remove build warnings |
597aaf25e343
Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
39 |
{ |
597aaf25e343
Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
40 |
ASSERT(0); |
597aaf25e343
Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
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 |
{ |
|
4
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; |
|
4
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 |
||
4
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 |
|
4
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) |
4
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(); |
|
44 | 147 |
iReusablePagePool.Reserve(iReservedRegionMarkInSegs); |
42
a179b74831c9
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
20
diff
changeset
|
148 |
if (r != KErrNone) |
a179b74831c9
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
20
diff
changeset
|
149 |
{ |
a179b74831c9
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
20
diff
changeset
|
150 |
ASSERT(0); |
a179b74831c9
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
20
diff
changeset
|
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 |
{ |
|
4
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 |
||
44 | 177 |
// if we have used up reserved region, get new pages from reusable pool first |
0 | 178 |
if (iReusablePagePool.Count()) |
179 |
{ |
|
180 |
addr = iReusablePagePool[0]; |
|
181 |
iReusablePagePool.Remove(0); |
|
182 |
// __PRINT2(_L("!! USED REUSABLE POOL SEGS: addr=0x%x, reusable.Count()=%d"), addr, iReusablePagePool.Count()); |
|
183 |
} |
|
184 |
// or we grow the touched region flag |
|
185 |
else |
|
186 |
{ |
|
187 |
addr = iBase + (iTouchedRegionFlag << iSegSizeInBytesLog2); |
|
44 | 188 |
iTouchedRegionFlag += aSegmentCount; |
0 | 189 |
// __PRINT2(_L("!! GROW TOUCHED SEGS: addr=0x%x, touched=%d"), addr, iTouchedRegionFlag); |
190 |
} |
|
191 |
||
192 |
// parameter validation |
|
193 |
ASSERT(((addr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount <= iMaxSizeInSegs); |
|
194 |
if(((addr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs) |
|
195 |
{ |
|
196 |
ASSERT(0); |
|
197 |
return NULL; |
|
198 |
} |
|
199 |
||
200 |
TInt r = iManager.AllocateAndLockSegments(addr, aSegmentCount); |
|
201 |
if (r != KErrNone) |
|
202 |
return NULL; |
|
203 |
||
204 |
return addr; |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
Decommit a set of contiguous segments. |
|
209 |
@param aStartRamAddr the start ram address of the region to be decommitted. |
|
210 |
@param aSegmentCount the segment number to be decommited. |
|
211 |
@return KErrNone if succeed, otherwise a system-wide error code. |
|
212 |
||
213 |
@internalTechnology |
|
214 |
@released |
|
215 |
*/ |
|
216 |
EXPORT_C TInt CCacheMemoryClient::DecommitSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
217 |
{ |
|
218 |
__PRINT4(_L("CCacheMemoryClient::DecommitSegments(%S, reserved=%d, touched=%d, segNo=%d)"), iName, iReservedRegionMarkInSegs, iTouchedRegionFlag, aSegmentCount); |
|
219 |
// parameter validation |
|
220 |
if(aStartRamAddr < iBase || |
|
221 |
(((aStartRamAddr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs)) |
|
222 |
{ |
|
223 |
ASSERT(0); |
|
224 |
return KErrArgument; |
|
225 |
} |
|
226 |
||
227 |
TInt err = iManager.DecommitSegments(aStartRamAddr, aSegmentCount); |
|
228 |
ASSERT(err == KErrNone); |
|
229 |
if (err != KErrNone) |
|
230 |
return err; |
|
42
a179b74831c9
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
20
diff
changeset
|
231 |
err = iReusablePagePool.Append(aStartRamAddr); |
a179b74831c9
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
20
diff
changeset
|
232 |
if (err != KErrNone) |
a179b74831c9
Revision: 201033
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
20
diff
changeset
|
233 |
return err; |
0 | 234 |
return KErrNone; |
235 |
} |
|
236 |
||
237 |
/** |
|
238 |
Lock a set of contiguous segments. |
|
239 |
@param aStartRamAddr the start ram address of the region to be locked. |
|
240 |
@param aSegmentCount the segment number to be locked. |
|
241 |
@return KErrNone if succeed, otherwise a system-wide error code. |
|
242 |
||
243 |
@internalTechnology |
|
244 |
@released |
|
245 |
*/ |
|
246 |
EXPORT_C TInt CCacheMemoryClient::LockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
247 |
{ |
|
248 |
__PRINT4(_L("CCacheMemoryClient::LockSegments(%S, reserved=%d, touched=%d, segNo=%d)"), iName, iReservedRegionMarkInSegs, iTouchedRegionFlag, (aStartRamAddr - iBase) >> iSegSizeInBytesLog2); |
|
249 |
// parameter validation |
|
250 |
if(aStartRamAddr < iBase || |
|
251 |
(((aStartRamAddr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs)) |
|
252 |
{ |
|
253 |
ASSERT(0); |
|
254 |
return KErrArgument; |
|
255 |
} |
|
256 |
return iManager.LockSegments(aStartRamAddr, aSegmentCount); |
|
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
Unlock a set of contiguous segments. |
|
261 |
@param aStartRamAddr the start ram address of the region to be unlocked. |
|
262 |
@param aSegmentCount the segment number to be unlocked. |
|
263 |
@return KErrNone if succeed, otherwise a system-wide error code. |
|
264 |
||
265 |
@internalTechnology |
|
266 |
@released |
|
267 |
*/ |
|
268 |
EXPORT_C TInt CCacheMemoryClient::UnlockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
269 |
{ |
|
270 |
__PRINT4(_L("CCacheMemoryClient::UnlockSegments(%S, reserved=%d, touched=%d, segNo=%d)"), iName, iReservedRegionMarkInSegs, iTouchedRegionFlag, (aStartRamAddr - iBase) >> iSegSizeInBytesLog2); |
|
271 |
// validate parameters |
|
272 |
if(aStartRamAddr < iBase || |
|
273 |
(((aStartRamAddr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs)) |
|
274 |
{ |
|
275 |
ASSERT(0); |
|
276 |
return KErrArgument; |
|
277 |
} |
|
278 |
return iManager.UnlockSegments(aStartRamAddr, aSegmentCount); |
|
279 |
} |
|
280 |
||
281 |
//////////////////////////// auxiliary functions ///////////////////////// |
|
282 |
TUint32 CCacheMemoryClient::MaxSizeInBytes() |
|
283 |
{ |
|
284 |
return iMaxSizeInSegs << iSegSizeInBytesLog2; |
|
285 |
} |
|
286 |
||
287 |
const TDesC& CCacheMemoryClient::Name() const |
|
288 |
{ |
|
289 |
return *iName; |
|
290 |
} |
|
291 |