author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 14 Apr 2010 17:22:59 +0300 | |
branch | RCL_3 |
changeset 89 | 1df514389a47 |
parent 80 | 597aaf25e343 |
child 249 | a179b74831c9 |
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; |
|
80
597aaf25e343
Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
62
diff
changeset
|
37 |
TInt r = DecommitSegments(iBase, segCnt); |
597aaf25e343
Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
62
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:
62
diff
changeset
|
39 |
{ |
597aaf25e343
Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
62
diff
changeset
|
40 |
ASSERT(0); |
597aaf25e343
Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
62
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(); |
|
147 |
iReusablePagePool.Reserve(iReservedRegionMarkInSegs); |
|
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
Commit an unused set of contiguous segments. |
|
152 |
@param aSegmentCount the segment number to be commited. |
|
153 |
@return TUint8* the starting ram address of the commited segments. |
|
154 |
||
155 |
@internalTechnology |
|
156 |
@released |
|
157 |
*/ |
|
158 |
EXPORT_C TUint8* CCacheMemoryClient::AllocateAndLockSegments(TUint32 aSegmentCount) |
|
159 |
{ |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
__PRINT4(_L("CCacheMemoryClient::AllocateAndLockSegments(%S, segs=%d, reserved=%d, touched=%d)"), iName, aSegmentCount, iReservedRegionMarkInSegs, iTouchedRegionFlag); |
0 | 161 |
// __PRINT2(_L("iBase = 0x%x, segcnt = %d"), iBase, aSegmentCount); |
162 |
TUint8* addr = NULL; |
|
163 |
// if we are walking through the reserved region first time, we should |
|
164 |
// make assumption that this area is locked already |
|
165 |
if (iTouchedRegionFlag + aSegmentCount <= iReservedRegionMarkInSegs) |
|
166 |
{ |
|
167 |
addr = iBase + (iTouchedRegionFlag << iSegSizeInBytesLog2); |
|
168 |
iTouchedRegionFlag += aSegmentCount; |
|
169 |
// __PRINT3(_L("!! USED RESERVED SEGS: addr=0x%x, touched=%d, reserved=%d"), addr, iTouchedRegionFlag, iReservedRegionMarkInSegs); |
|
170 |
return addr; |
|
171 |
} |
|
172 |
||
173 |
// if we have used up reserved region, get new pages from reusable pool first |
|
174 |
if (iReusablePagePool.Count()) |
|
175 |
{ |
|
176 |
addr = iReusablePagePool[0]; |
|
177 |
iReusablePagePool.Remove(0); |
|
178 |
// __PRINT2(_L("!! USED REUSABLE POOL SEGS: addr=0x%x, reusable.Count()=%d"), addr, iReusablePagePool.Count()); |
|
179 |
} |
|
180 |
// or we grow the touched region flag |
|
181 |
else |
|
182 |
{ |
|
183 |
addr = iBase + (iTouchedRegionFlag << iSegSizeInBytesLog2); |
|
184 |
iTouchedRegionFlag += aSegmentCount; |
|
185 |
// __PRINT2(_L("!! GROW TOUCHED SEGS: addr=0x%x, touched=%d"), addr, iTouchedRegionFlag); |
|
186 |
} |
|
187 |
||
188 |
// parameter validation |
|
189 |
ASSERT(((addr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount <= iMaxSizeInSegs); |
|
190 |
if(((addr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs) |
|
191 |
{ |
|
192 |
ASSERT(0); |
|
193 |
return NULL; |
|
194 |
} |
|
195 |
||
196 |
TInt r = iManager.AllocateAndLockSegments(addr, aSegmentCount); |
|
197 |
if (r != KErrNone) |
|
198 |
return NULL; |
|
199 |
||
200 |
return addr; |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
Decommit a set of contiguous segments. |
|
205 |
@param aStartRamAddr the start ram address of the region to be decommitted. |
|
206 |
@param aSegmentCount the segment number to be decommited. |
|
207 |
@return KErrNone if succeed, otherwise a system-wide error code. |
|
208 |
||
209 |
@internalTechnology |
|
210 |
@released |
|
211 |
*/ |
|
212 |
EXPORT_C TInt CCacheMemoryClient::DecommitSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
213 |
{ |
|
214 |
__PRINT4(_L("CCacheMemoryClient::DecommitSegments(%S, reserved=%d, touched=%d, segNo=%d)"), iName, iReservedRegionMarkInSegs, iTouchedRegionFlag, aSegmentCount); |
|
215 |
// parameter validation |
|
216 |
if(aStartRamAddr < iBase || |
|
217 |
(((aStartRamAddr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs)) |
|
218 |
{ |
|
219 |
ASSERT(0); |
|
220 |
return KErrArgument; |
|
221 |
} |
|
222 |
||
223 |
TInt err = iManager.DecommitSegments(aStartRamAddr, aSegmentCount); |
|
224 |
ASSERT(err == KErrNone); |
|
225 |
if (err != KErrNone) |
|
226 |
return err; |
|
227 |
iReusablePagePool.Append(aStartRamAddr); |
|
228 |
return KErrNone; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
Lock a set of contiguous segments. |
|
233 |
@param aStartRamAddr the start ram address of the region to be locked. |
|
234 |
@param aSegmentCount the segment number to be locked. |
|
235 |
@return KErrNone if succeed, otherwise a system-wide error code. |
|
236 |
||
237 |
@internalTechnology |
|
238 |
@released |
|
239 |
*/ |
|
240 |
EXPORT_C TInt CCacheMemoryClient::LockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
241 |
{ |
|
242 |
__PRINT4(_L("CCacheMemoryClient::LockSegments(%S, reserved=%d, touched=%d, segNo=%d)"), iName, iReservedRegionMarkInSegs, iTouchedRegionFlag, (aStartRamAddr - iBase) >> iSegSizeInBytesLog2); |
|
243 |
// parameter validation |
|
244 |
if(aStartRamAddr < iBase || |
|
245 |
(((aStartRamAddr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs)) |
|
246 |
{ |
|
247 |
ASSERT(0); |
|
248 |
return KErrArgument; |
|
249 |
} |
|
250 |
return iManager.LockSegments(aStartRamAddr, aSegmentCount); |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
Unlock a set of contiguous segments. |
|
255 |
@param aStartRamAddr the start ram address of the region to be unlocked. |
|
256 |
@param aSegmentCount the segment number to be unlocked. |
|
257 |
@return KErrNone if succeed, otherwise a system-wide error code. |
|
258 |
||
259 |
@internalTechnology |
|
260 |
@released |
|
261 |
*/ |
|
262 |
EXPORT_C TInt CCacheMemoryClient::UnlockSegments(TUint8* aStartRamAddr, TUint32 aSegmentCount) |
|
263 |
{ |
|
264 |
__PRINT4(_L("CCacheMemoryClient::UnlockSegments(%S, reserved=%d, touched=%d, segNo=%d)"), iName, iReservedRegionMarkInSegs, iTouchedRegionFlag, (aStartRamAddr - iBase) >> iSegSizeInBytesLog2); |
|
265 |
// validate parameters |
|
266 |
if(aStartRamAddr < iBase || |
|
267 |
(((aStartRamAddr - iBase) >> iSegSizeInBytesLog2) + aSegmentCount > iMaxSizeInSegs)) |
|
268 |
{ |
|
269 |
ASSERT(0); |
|
270 |
return KErrArgument; |
|
271 |
} |
|
272 |
return iManager.UnlockSegments(aStartRamAddr, aSegmentCount); |
|
273 |
} |
|
274 |
||
275 |
//////////////////////////// auxiliary functions ///////////////////////// |
|
276 |
TUint32 CCacheMemoryClient::MaxSizeInBytes() |
|
277 |
{ |
|
278 |
return iMaxSizeInSegs << iSegSizeInBytesLog2; |
|
279 |
} |
|
280 |
||
281 |
const TDesC& CCacheMemoryClient::Name() const |
|
282 |
{ |
|
283 |
return *iName; |
|
284 |
} |
|
285 |