author | hgs |
Wed, 20 Oct 2010 13:58:28 +0100 | |
changeset 293 | 0659d0e1a03c |
parent 189 | a5496987b1da |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2002-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_obj.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
#include "sf_std.h" |
|
19 |
||
20 |
const TInt KObjectIxGranularity=8; |
|
21 |
const TInt KObjectConGranularity=8; |
|
22 |
const TInt KObjectConIxGranularity=4; |
|
23 |
const TInt KObjectIndexMask=0x7fff; |
|
24 |
const TInt KObjectMaxIndex=0x7fff; |
|
25 |
const TInt KObjectInstanceShift=16; |
|
26 |
const TInt KObjectInstanceMask=0x3fff; |
|
27 |
const TInt KObjectUniqueIDShift=16; |
|
28 |
const TInt KObjectIxMaxHandles=0x8000; |
|
29 |
||
30 |
inline TInt index(TInt aHandle) |
|
31 |
{return(aHandle&KObjectIndexMask);} |
|
32 |
inline TInt instance(TInt aHandle) |
|
33 |
{return((aHandle>>KObjectInstanceShift)&KObjectInstanceMask);} |
|
34 |
inline TInt instanceLimit(TInt& aCount) |
|
35 |
{return ((aCount&KObjectInstanceMask)==0) ? ((++aCount)&KObjectInstanceMask) : aCount&KObjectInstanceMask;} |
|
36 |
inline TInt makeHandle(TInt anIndex,TInt anInstance) |
|
37 |
{return((TInt)((anInstance<<KObjectInstanceShift)|anIndex));} |
|
38 |
||
39 |
inline TInt uniqueID(TInt aHandle) |
|
40 |
{return((aHandle>>KObjectUniqueIDShift)&KObjectInstanceMask);} |
|
41 |
inline TInt makeFindHandle(TInt anIndex,TInt anUniqueID) |
|
42 |
{return((TInt)((anUniqueID<<KObjectUniqueIDShift)|anIndex));} |
|
43 |
||
44 |
||
45 |
CFsObjectConIx* CFsObjectConIx::NewL() |
|
46 |
// |
|
47 |
// Create an instance of this class. |
|
48 |
// |
|
49 |
{ |
|
50 |
||
51 |
return new(ELeave) CFsObjectConIx; |
|
52 |
} |
|
53 |
||
54 |
CFsObjectConIx::CFsObjectConIx() |
|
55 |
// |
|
56 |
// Constructor |
|
57 |
// |
|
58 |
: iNextUniqueID(1) |
|
59 |
{ |
|
60 |
} |
|
61 |
||
62 |
CFsObjectConIx::~CFsObjectConIx() |
|
63 |
// |
|
64 |
// Destructor |
|
65 |
// |
|
66 |
{ |
|
67 |
if (iCount) |
|
68 |
{ |
|
69 |
TInt i=-1; |
|
70 |
while(++i<iCount) |
|
71 |
{ |
|
72 |
CFsObjectCon* pS=iContainers[i]; |
|
73 |
delete pS; |
|
74 |
} |
|
75 |
} |
|
76 |
delete iContainers; |
|
77 |
} |
|
78 |
||
79 |
void CFsObjectConIx::CreateContainerL(CFsObjectCon*& aContainer) |
|
80 |
// |
|
81 |
// Actually create the container |
|
82 |
// |
|
83 |
{ |
|
84 |
||
85 |
aContainer=CFsObjectCon::NewL(); |
|
86 |
aContainer->iUniqueID=iNextUniqueID; |
|
87 |
if (iCount==iAllocated) |
|
88 |
{ |
|
89 |
TInt newAlloc=iAllocated+KObjectConIxGranularity; |
|
90 |
iContainers=(CFsObjectCon**)User::ReAllocL(iContainers, newAlloc*sizeof(CFsObjectCon*)); |
|
91 |
iAllocated=newAlloc; |
|
92 |
} |
|
93 |
iContainers[iCount++]=aContainer; |
|
94 |
} |
|
95 |
||
96 |
CFsObjectCon* CFsObjectConIx::CreateL() |
|
97 |
// |
|
98 |
// Create a new container. |
|
99 |
// |
|
100 |
{ |
|
101 |
||
102 |
CFsObjectCon* pC=NULL; |
|
103 |
TRAPD(r,CreateContainerL(pC)) |
|
104 |
if (r!=KErrNone) |
|
105 |
{ |
|
106 |
delete pC; |
|
107 |
User::Leave(r); |
|
108 |
} |
|
109 |
iNextUniqueID++; |
|
110 |
return pC; |
|
111 |
} |
|
112 |
||
113 |
void CFsObjectConIx::Remove(CFsObjectCon* aCon) |
|
114 |
// |
|
115 |
// Remove a container from the index. |
|
116 |
// |
|
117 |
{ |
|
118 |
if (!aCon) |
|
119 |
return; |
|
120 |
CFsObjectCon** pS=iContainers; |
|
121 |
CFsObjectCon** pE=pS+iCount; |
|
122 |
while(pS<pE) |
|
123 |
{ |
|
124 |
if (*pS==aCon) |
|
125 |
{ |
|
126 |
Mem::Move((TAny*)pS,(TAny*)(pS+1),TInt(pE)-TInt(pS)-sizeof(CFsObjectCon*)); |
|
127 |
TInt newAlloc=--iCount; |
|
128 |
newAlloc=(newAlloc+(KObjectConIxGranularity-1))&~(KObjectConIxGranularity-1); |
|
129 |
if (newAlloc!=iAllocated) |
|
130 |
{ |
|
131 |
if (newAlloc) |
|
189 | 132 |
{ |
0 | 133 |
iContainers=(CFsObjectCon**)User::ReAlloc(iContainers,newAlloc*sizeof(CFsObjectCon*)); |
189 | 134 |
if(!iContainers) |
135 |
{ |
|
136 |
Fault(EContainerHeapCorruptionOnRemove); |
|
137 |
} |
|
138 |
} |
|
0 | 139 |
else |
140 |
{ |
|
141 |
delete iContainers; |
|
142 |
iContainers=NULL; |
|
143 |
} |
|
144 |
iAllocated=newAlloc; |
|
145 |
} |
|
146 |
delete aCon; |
|
147 |
return; |
|
148 |
} |
|
149 |
pS++; |
|
150 |
} |
|
151 |
Fault(EObjRemoveContainerNotFound); |
|
152 |
} |
|
153 |
||
154 |
||
155 |
/** |
|
156 |
Constructs the object and initializes the reference count to one. |
|
157 |
||
158 |
Once constructed, a reference counting object cannot be deleted until its |
|
159 |
reference count is reduced to zero. |
|
160 |
||
161 |
@see CFsObject::Close |
|
162 |
*/ |
|
163 |
EXPORT_C CFsObject::CFsObject() |
|
164 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
15
diff
changeset
|
165 |
#if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
15
diff
changeset
|
166 |
__e32_atomic_add_ord32(&ObjectCount, 1); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
15
diff
changeset
|
167 |
#endif |
0 | 168 |
// iContainer=NULL; |
169 |
// iName=NULL; |
|
170 |
iAccessCount=1; |
|
171 |
} |
|
172 |
||
173 |
||
174 |
/** |
|
175 |
Destructor. |
|
176 |
||
177 |
Deallocates memory associated with this objects name, if a name |
|
178 |
has been set. |
|
179 |
||
180 |
@panic FSERV 104 if the reference count is not zero when |
|
181 |
the destructor is called. |
|
182 |
*/ |
|
183 |
EXPORT_C CFsObject::~CFsObject() |
|
184 |
{ |
|
185 |
__PRINT1(_L("CFsObject::~CFsObject() 0x%x"),this); |
|
186 |
__ASSERT_ALWAYS(Dec()==0,Fault(EObjDestructorAccessCount)); |
|
187 |
__ASSERT_ALWAYS(!iContainer,Fault(EObjDestructorContainer)); |
|
188 |
if(iName) |
|
189 |
User::Free(iName); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
15
diff
changeset
|
190 |
#if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
15
diff
changeset
|
191 |
__e32_atomic_add_ord32(&ObjectCount, (TUint32) -1); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
15
diff
changeset
|
192 |
#endif |
0 | 193 |
} |
194 |
||
195 |
||
196 |
/** |
|
197 |
Opens this reference counting object. |
|
198 |
||
199 |
The default behaviour increments the reference count by one. |
|
200 |
||
201 |
Where a derived class implements its own version of this function, it must |
|
202 |
either use the protected member function Inc() to increment the reference |
|
203 |
count or make a base call to this function. |
|
204 |
||
15
4122176ea935
Revision: 200948 + Removing redundant base integration tests and fixing build errors
John Imhofe <john.imhofe@nokia.com>
parents:
0
diff
changeset
|
205 |
@return KErrNone, or another system-wide error code. |
0 | 206 |
*/ |
207 |
||
208 |
EXPORT_C TInt CFsObject::Open() |
|
209 |
{ |
|
210 |
TInt count=Inc(); |
|
15
4122176ea935
Revision: 200948 + Removing redundant base integration tests and fixing build errors
John Imhofe <john.imhofe@nokia.com>
parents:
0
diff
changeset
|
211 |
(void)count; |
0 | 212 |
__THRD_PRINT2(_L("CFsObject::Open() object=0x%x count=%d"),this,count); |
15
4122176ea935
Revision: 200948 + Removing redundant base integration tests and fixing build errors
John Imhofe <john.imhofe@nokia.com>
parents:
0
diff
changeset
|
213 |
__ASSERT_DEBUG(count>=1,Fault(EFsObjectOpen)); |
4122176ea935
Revision: 200948 + Removing redundant base integration tests and fixing build errors
John Imhofe <john.imhofe@nokia.com>
parents:
0
diff
changeset
|
214 |
return KErrNone; |
0 | 215 |
} |
216 |
||
217 |
||
218 |
/** |
|
219 |
Removes an assigned container object then deletes this reference |
|
220 |
counting object. |
|
221 |
*/ |
|
222 |
void CFsObject::DoClose() |
|
223 |
{ |
|
224 |
__THRD_PRINT(_L("CFsObject::DoClose()")); |
|
225 |
if(iContainer) |
|
226 |
{ |
|
227 |
iContainer->Remove(this,ETrue); |
|
228 |
iContainer=NULL; |
|
229 |
} |
|
230 |
delete(this); |
|
231 |
} |
|
232 |
||
233 |
||
234 |
/** |
|
235 |
Closes this reference counting object. |
|
236 |
||
237 |
The default behaviour decrements the reference count by one. If this becomes |
|
238 |
zero, then the function calls DoClose on this reference counting object. |
|
239 |
||
240 |
Where a derived class implements its own version of this function, it can |
|
241 |
use the protected member functions Dec() & DoClose() or make a base call to |
|
242 |
this function. |
|
243 |
||
244 |
@see CFsObject::DoClose |
|
245 |
*/ |
|
246 |
EXPORT_C void CFsObject::Close() |
|
247 |
{ |
|
248 |
__THRD_PRINT(_L("CFsObject::Close()")); |
|
249 |
if(Dec()==1) |
|
250 |
DoClose(); |
|
251 |
} |
|
252 |
||
253 |
||
254 |
/** |
|
255 |
Determine if this object is within the correct drive thread context |
|
256 |
for file based operations. |
|
257 |
||
258 |
The default behaviour is to return True. |
|
259 |
||
260 |
A derived class implementation is required. |
|
261 |
||
262 |
@return ETrue |
|
263 |
*/ |
|
264 |
EXPORT_C TBool CFsObject::IsCorrectThread() |
|
265 |
{ |
|
266 |
return(ETrue); |
|
267 |
} |
|
268 |
||
269 |
||
270 |
||
271 |
/** |
|
272 |
Sets or clears this reference counting object's name. |
|
273 |
||
274 |
To set the name, the specified descriptor must contain the name to be set. |
|
275 |
Once the name has been successfully set, then the specified source descriptor |
|
276 |
can be discarded. |
|
277 |
||
278 |
To clear an existing name, specify a NULL argument. |
|
279 |
||
280 |
@param aName A pointer to the descriptor containing the name to be set, or |
|
281 |
NULL if an existing name is to be cleared. |
|
282 |
||
283 |
@return KErrNone if the function is successful; |
|
284 |
KErrNoMemory if there is insufficient memory available. |
|
285 |
||
286 |
@panic USER 11 if the length of aName is greater than KMaxName |
|
287 |
for a 16-bit descriptor. |
|
288 |
@panic USER 23 if the length of aName is greater than KMaxName |
|
289 |
for an 8-bit descriptor. |
|
290 |
*/ |
|
291 |
EXPORT_C TInt CFsObject::SetName(const TDesC *aName) |
|
292 |
{ |
|
293 |
User::Free(iName); |
|
294 |
iName=NULL; |
|
295 |
if (aName!=NULL) |
|
296 |
{ |
|
297 |
iName=aName->Alloc(); |
|
298 |
if (iName==NULL) |
|
299 |
return(KErrNoMemory); |
|
300 |
} |
|
301 |
return(KErrNone); |
|
302 |
} |
|
303 |
||
304 |
||
305 |
/** |
|
306 |
Gets the name of this reference counting object. |
|
307 |
||
308 |
The default behaviour provided by this function depends on whether a name |
|
309 |
has been explicitly set into the object: |
|
310 |
||
311 |
If a name has previously been set, then the function returns that name. |
|
312 |
||
313 |
If a name has not been set, then the function returns NULL. |
|
314 |
||
315 |
@return A modifiable buffer descriptor with a defined maximum length containing |
|
316 |
the name of this reference counting object or a TName with no contents. |
|
317 |
*/ |
|
318 |
EXPORT_C TName CFsObject::Name() const |
|
319 |
{ |
|
320 |
if (iName) |
|
321 |
return(*iName); |
|
322 |
||
323 |
TName empty; |
|
324 |
return empty; |
|
325 |
} |
|
326 |
||
327 |
||
328 |
/** |
|
329 |
Gets the Unique ID of the assigned object container to this object. |
|
330 |
||
331 |
@return A unique number for file system container objects. |
|
332 |
*/ |
|
333 |
TInt CFsObject::UniqueID() const {return(iContainer->UniqueID());} |
|
334 |
||
335 |
||
336 |
CFsObjectIx* CFsObjectIx::NewL() |
|
337 |
// |
|
338 |
// Create an instance of this class. |
|
339 |
// |
|
340 |
{ |
|
341 |
CFsObjectIx* pO=new(ELeave) CFsObjectIx; |
|
342 |
TInt r=pO->iLock.CreateLocal(); |
|
343 |
if(r!=KErrNone) |
|
344 |
{ |
|
345 |
delete(pO); |
|
346 |
User::Leave(r); |
|
347 |
} |
|
348 |
return(pO); |
|
349 |
} |
|
350 |
||
351 |
CFsObjectIx::CFsObjectIx() |
|
352 |
// |
|
353 |
// Constructor |
|
354 |
// |
|
355 |
: iNextInstance(1) |
|
356 |
{ |
|
357 |
// iAllocated=0; |
|
358 |
// iNumEntries=0; |
|
359 |
// iHighWaterMark=0; |
|
360 |
// iObjects=NULL; |
|
361 |
} |
|
362 |
||
363 |
||
364 |
/** |
|
365 |
Close all objects that were created in the main file server thread. |
|
366 |
For sync. drives all objects must be closed in the main file server thread, because |
|
367 |
they are created in this thread, as soon as all synch. requests are processed there. |
|
368 |
*/ |
|
369 |
void CFsObjectIx::CloseMainThreadObjects() |
|
370 |
{ |
|
371 |
__ASSERT_DEBUG(FsThreadManager::IsMainThread(),Fault(EObjectIxMainThread)); |
|
372 |
__PRINT(_L("CFsObjectIx::CloseThreadObjects()")); |
|
373 |
||
374 |
Lock(); |
|
375 |
// We have to be very careful here. Calling Close() on the objects in the array |
|
376 |
// may result in other entries being removed from the array before we delete |
|
377 |
// them here, and may result in the array being ReAlloc()ed, corrupting the removed |
|
378 |
// entries, hence we must check the iHighWaterMark value each time round the loop. |
|
379 |
TInt i=-1; |
|
380 |
while(++i<iHighWaterMark) |
|
381 |
{ |
|
382 |
SFsObjectIxRec* pS=iObjects+i; |
|
383 |
CFsObject *pO=pS->obj; |
|
384 |
if (pO && pO->IsCorrectThread()) |
|
385 |
{ |
|
386 |
// invalidate entry before closing it |
|
387 |
pS->obj=NULL; |
|
388 |
pO->Close(); |
|
389 |
} |
|
390 |
} |
|
391 |
Unlock(); |
|
392 |
} |
|
393 |
||
394 |
||
395 |
CFsObjectIx::~CFsObjectIx() |
|
396 |
// |
|
397 |
// Destructor |
|
398 |
// Assumes that no need to lock |
|
399 |
// |
|
400 |
{ |
|
401 |
__PRINT1(_L("CFsObjectIx::~CFsObjectIx() 0x%x"),this); |
|
402 |
||
403 |
// We have to be very careful here. Calling Close() on the objects in the array |
|
404 |
// may result in other entries being removed from the array before we delete |
|
405 |
// them here, and may result in the array being ReAlloc()ed, corrupting the removed |
|
406 |
// entries, hence we must check the iHighWaterMark value each time round the loop. |
|
407 |
TInt i=-1; |
|
408 |
while(++i<iHighWaterMark) |
|
409 |
{ |
|
410 |
SFsObjectIxRec* pS=iObjects+i; |
|
411 |
CFsObject *pO=pS->obj; |
|
412 |
if (pO) |
|
413 |
{ |
|
414 |
// invalidate entry before closing it |
|
415 |
pS->obj=NULL; |
|
416 |
pO->Close(); |
|
417 |
} |
|
418 |
} |
|
419 |
delete iObjects; |
|
420 |
iLock.Close(); |
|
421 |
} |
|
422 |
||
423 |
TInt CFsObjectIx::AddL(CFsObject* anObj,TBool aLock) |
|
424 |
// |
|
425 |
// Add a new object to the index. |
|
426 |
// |
|
427 |
{ |
|
428 |
if(aLock) |
|
429 |
Lock(); |
|
430 |
SFsObjectIxRec *pS=iObjects; |
|
431 |
SFsObjectIxRec *pE=pS+iHighWaterMark; |
|
432 |
TInt i=0; |
|
433 |
TInt inc=0; |
|
434 |
while(pS<pE && pS->obj) |
|
435 |
pS++, i++; |
|
436 |
if (pS==pE) |
|
437 |
inc=1; |
|
438 |
if (pS==pE && iAllocated==iHighWaterMark) |
|
439 |
{ |
|
440 |
// no slots free, so reallocate array |
|
441 |
if (iHighWaterMark==KObjectIxMaxHandles) |
|
442 |
{ |
|
443 |
if(aLock) |
|
444 |
Unlock(); |
|
445 |
User::LeaveNoMemory(); |
|
446 |
} |
|
447 |
TInt newAlloc=iAllocated + KObjectIxGranularity; |
|
448 |
SFsObjectIxRec* pA=(SFsObjectIxRec*)User::ReAlloc(iObjects, newAlloc*sizeof(SFsObjectIxRec)); |
|
449 |
if(!pA) |
|
450 |
{ |
|
451 |
if(aLock) |
|
452 |
Unlock(); |
|
453 |
User::Leave(KErrNoMemory); |
|
454 |
} |
|
455 |
iObjects=pA; |
|
456 |
iAllocated=newAlloc; |
|
457 |
i=iHighWaterMark; |
|
458 |
pS=pA+i; |
|
459 |
} |
|
460 |
pS->obj=anObj; |
|
461 |
pS->uniqueID=(TUint16)anObj->UniqueID(); |
|
462 |
pS->instance=(TUint16)instanceLimit(iNextInstance); |
|
463 |
iNextInstance++; |
|
464 |
iHighWaterMark+=inc; |
|
465 |
++iNumEntries; |
|
466 |
TInt h=makeHandle(i,pS->instance); |
|
467 |
if(aLock) |
|
468 |
Unlock(); |
|
469 |
return(h); |
|
470 |
} |
|
471 |
||
472 |
void CFsObjectIx::Remove(TInt aHandle,TBool aLock) |
|
473 |
// |
|
474 |
// Remove an object from the index. |
|
475 |
// |
|
476 |
{ |
|
477 |
if(aLock) |
|
478 |
Lock(); |
|
479 |
TInt i=index(aHandle); |
|
480 |
__ASSERT_ALWAYS(i<iHighWaterMark,Fault(EObjRemoveBadHandle)); |
|
481 |
SFsObjectIxRec* pR=iObjects+i; |
|
482 |
CFsObject *pO=pR->obj; |
|
483 |
if (!pO || pR->instance!=instance(aHandle) || pR->uniqueID!=pO->UniqueID()) |
|
484 |
Fault(EObjRemoveBadHandle); |
|
485 |
pR->obj=NULL; |
|
486 |
--iNumEntries; |
|
487 |
if (i==iHighWaterMark-1) |
|
488 |
{ |
|
489 |
do |
|
490 |
{ |
|
491 |
i--; |
|
492 |
pR--; |
|
493 |
} while(i>=0 && !pR->obj); |
|
494 |
TInt newAlloc=(i+KObjectIxGranularity)&~(KObjectIxGranularity-1); |
|
495 |
if (newAlloc!=iAllocated) |
|
496 |
{ |
|
497 |
if (newAlloc) |
|
189 | 498 |
{ |
0 | 499 |
iObjects=(SFsObjectIxRec*)User::ReAlloc(iObjects,newAlloc*sizeof(SFsObjectIxRec)); |
189 | 500 |
if(!iObjects) |
501 |
{ |
|
502 |
Fault(EContainerHeapCorruptionOnRemove); |
|
503 |
} |
|
504 |
} |
|
0 | 505 |
else |
506 |
{ |
|
507 |
delete iObjects; |
|
508 |
iObjects=NULL; |
|
509 |
} |
|
510 |
iAllocated=newAlloc; |
|
511 |
} |
|
512 |
iHighWaterMark=i+1; |
|
513 |
} |
|
514 |
if(aLock) |
|
515 |
Unlock(); |
|
516 |
pO->Close(); |
|
517 |
} |
|
518 |
||
519 |
CFsObject *CFsObjectIx::At(TInt aHandle,TInt aUniqueID,TBool aLock) |
|
520 |
// |
|
521 |
// Return the object from its handle. |
|
522 |
// |
|
523 |
{ |
|
524 |
if(aLock) |
|
525 |
Lock(); |
|
526 |
TInt i=index(aHandle); |
|
527 |
if (i>=iHighWaterMark) |
|
528 |
{ |
|
529 |
if(aLock) |
|
530 |
Unlock(); |
|
531 |
return NULL; |
|
532 |
} |
|
533 |
SFsObjectIxRec *pS=iObjects+i; |
|
534 |
if (pS->instance!=instance(aHandle) || pS->uniqueID!=aUniqueID) |
|
535 |
{ |
|
536 |
if(aLock) |
|
537 |
Unlock(); |
|
538 |
return NULL; |
|
539 |
} |
|
540 |
if(aLock) |
|
541 |
Unlock(); |
|
542 |
return pS->obj; |
|
543 |
} |
|
544 |
||
545 |
CFsObject *CFsObjectIx::At(TInt aHandle,TBool aLock) |
|
546 |
// |
|
547 |
// Return the object from its handle. |
|
548 |
// |
|
549 |
{ |
|
550 |
if(aLock) |
|
551 |
Lock(); |
|
552 |
TInt i=index(aHandle); |
|
553 |
if (i>=iHighWaterMark) |
|
554 |
{ |
|
555 |
if(aLock) |
|
556 |
Unlock(); |
|
557 |
return NULL; |
|
558 |
} |
|
559 |
SFsObjectIxRec *pS=iObjects+i; |
|
560 |
if (pS->instance!=instance(aHandle)) |
|
561 |
{ |
|
562 |
if(aLock) |
|
563 |
Unlock(); |
|
564 |
return NULL; |
|
565 |
} |
|
566 |
if(aLock) |
|
567 |
Unlock(); |
|
568 |
return pS->obj; |
|
569 |
} |
|
570 |
||
571 |
TInt CFsObjectIx::At(const CFsObject* anObj,TBool aLock) |
|
572 |
// |
|
573 |
// Return the handle from an object. |
|
574 |
// |
|
575 |
{ |
|
576 |
if(aLock) |
|
577 |
Lock(); |
|
578 |
if (iHighWaterMark) |
|
579 |
{ |
|
580 |
SFsObjectIxRec* pS=iObjects; |
|
581 |
SFsObjectIxRec* pE=pS+iHighWaterMark; |
|
582 |
TInt i=0; |
|
583 |
while(pS<pE && pS->obj!=anObj) |
|
584 |
pS++, i++; |
|
585 |
if (pS<pE) |
|
586 |
{ |
|
587 |
TInt h=makeHandle(i,pS->instance); |
|
588 |
if(aLock) |
|
589 |
Unlock(); |
|
590 |
return(h); |
|
591 |
} |
|
592 |
} |
|
593 |
if(aLock) |
|
594 |
Unlock(); |
|
595 |
return KErrNotFound; |
|
596 |
} |
|
597 |
||
598 |
||
599 |
CFsObject* CFsObjectIx::operator[](TInt anIndex) |
|
600 |
// |
|
601 |
// Return the object at anIndex |
|
602 |
// |
|
603 |
{ |
|
604 |
||
605 |
__ASSERT_ALWAYS(anIndex>=0 && anIndex<iHighWaterMark,Fault(EArrayIndexOutOfRange)); |
|
606 |
return iObjects[anIndex].obj; |
|
607 |
} |
|
608 |
||
609 |
CFsObjectCon* CFsObjectCon::NewL() |
|
610 |
// |
|
611 |
// Create a new instance of this class. |
|
612 |
// |
|
613 |
{ |
|
614 |
CFsObjectCon* pO=new(ELeave) CFsObjectCon(ENotOwnerID); |
|
615 |
TInt r=pO->iLock.CreateLocal(); |
|
616 |
if(r!=KErrNone) |
|
617 |
{ |
|
618 |
delete(pO); |
|
619 |
User::Leave(r); |
|
620 |
} |
|
621 |
return(pO); |
|
622 |
} |
|
623 |
||
624 |
CFsObjectCon::CFsObjectCon(TInt aUniqueID) |
|
625 |
// |
|
626 |
// Constructor |
|
627 |
// |
|
628 |
: iUniqueID(aUniqueID) |
|
629 |
{ |
|
630 |
// iAllocated=0; |
|
631 |
// iCount=0; |
|
632 |
// iObjects=NULL; |
|
633 |
} |
|
634 |
||
635 |
CFsObjectCon::~CFsObjectCon() |
|
636 |
// |
|
637 |
// Destructor |
|
638 |
// |
|
639 |
{ |
|
640 |
__ASSERT_ALWAYS(iCount==0,Fault(EObjectConDestructor)); |
|
641 |
iLock.Close(); |
|
642 |
delete iObjects; |
|
643 |
} |
|
644 |
||
645 |
void CFsObjectCon::AddL(CFsObject* anObj,TBool aLock) |
|
646 |
// |
|
647 |
// Install a new object to the container. The full name must be unique. |
|
648 |
// |
|
649 |
{ |
|
650 |
if(anObj->iName) |
|
651 |
User::LeaveIfError(CheckUniqueName(anObj)); |
|
652 |
if(aLock) |
|
653 |
Lock(); |
|
654 |
if (iCount==iAllocated) |
|
655 |
{ |
|
656 |
TInt newAlloc=iAllocated+KObjectConGranularity; |
|
657 |
CFsObject** pO=(CFsObject**)User::ReAlloc(iObjects, newAlloc*sizeof(CFsObject*)); |
|
658 |
if(!pO) |
|
659 |
{ |
|
660 |
if(aLock) |
|
661 |
Unlock(); |
|
662 |
User::Leave(KErrNoMemory); |
|
663 |
} |
|
664 |
iObjects=pO; |
|
665 |
iAllocated=newAlloc; |
|
666 |
} |
|
667 |
iObjects[iCount++]=anObj; |
|
668 |
if (iUniqueID!=ENotOwnerID) |
|
669 |
anObj->iContainer=this; |
|
670 |
if(aLock) |
|
671 |
Unlock(); |
|
672 |
} |
|
673 |
||
674 |
void CFsObjectCon::Remove(CFsObject *anObj,TBool aLock) |
|
675 |
// |
|
676 |
// Remove an object from the container. |
|
677 |
// This assumes that close is called by the calling function |
|
678 |
// |
|
679 |
{ |
|
680 |
if(aLock) |
|
681 |
Lock(); |
|
682 |
CFsObject** pS=iObjects; |
|
683 |
CFsObject** pE=pS+iCount; |
|
684 |
while(pS<pE) |
|
685 |
{ |
|
686 |
if (*pS==anObj) |
|
687 |
{ |
|
688 |
Mem::Move((TAny*)pS,(TAny*)(pS+1),TInt(pE)-TInt(pS)-sizeof(CFsObject*)); |
|
689 |
TInt newAlloc=--iCount; |
|
690 |
newAlloc=(newAlloc+(KObjectConGranularity-1))&~(KObjectConGranularity-1); |
|
691 |
if (newAlloc!=iAllocated) |
|
692 |
{ |
|
693 |
if (newAlloc) |
|
189 | 694 |
{ |
0 | 695 |
iObjects=(CFsObject**)User::ReAlloc(iObjects,newAlloc*sizeof(CFsObject*)); |
189 | 696 |
if(!iObjects) |
697 |
{ |
|
698 |
Fault(EContainerHeapCorruptionOnRemove); |
|
699 |
} |
|
700 |
} |
|
0 | 701 |
else |
702 |
{ |
|
703 |
delete iObjects; |
|
704 |
iObjects=NULL; |
|
705 |
} |
|
706 |
iAllocated=newAlloc; |
|
707 |
} |
|
708 |
if(aLock) |
|
709 |
Unlock(); |
|
710 |
anObj->iContainer = NULL; |
|
711 |
return; |
|
712 |
} |
|
713 |
pS++; |
|
714 |
} |
|
715 |
Fault(EObjRemoveObjectNotFound); |
|
716 |
} |
|
717 |
||
718 |
CFsObject *CFsObjectCon::operator[](TInt anIndex) |
|
719 |
// |
|
720 |
// Return the object at anIndex. |
|
721 |
// |
|
722 |
{ |
|
723 |
__ASSERT_ALWAYS(anIndex>=0 && anIndex<iCount, Fault(EArrayIndexOutOfRange)); |
|
724 |
return iObjects[anIndex]; |
|
725 |
} |
|
726 |
||
727 |
CFsObject *CFsObjectCon::At(TInt aFindHandle) const |
|
728 |
// |
|
729 |
// Return the object at aFindHandle. |
|
730 |
// Should only be used there is no other access to the CFsObject |
|
731 |
// |
|
732 |
{ |
|
733 |
||
734 |
__ASSERT_ALWAYS(uniqueID(aFindHandle)==iUniqueID,Fault(EObjFindBadHandle)); |
|
735 |
TInt ix=index(aFindHandle); |
|
736 |
__ASSERT_ALWAYS(ix<iCount,Fault(EObjFindIndexOutOfRange)); |
|
737 |
return iObjects[ix]; |
|
738 |
} |
|
739 |
||
740 |
CFsObject *CFsObjectCon::AtL(TInt aFindHandle) const |
|
741 |
// |
|
742 |
// Return the object at aFindHandle. |
|
743 |
// Should only be used if no other access to the CFsObject |
|
744 |
// |
|
745 |
{ |
|
746 |
||
747 |
__ASSERT_ALWAYS(uniqueID(aFindHandle)==iUniqueID,User::Leave(KErrBadHandle)); |
|
748 |
TInt ix=index(aFindHandle); |
|
749 |
__ASSERT_ALWAYS(ix<iCount,User::Leave(KErrArgument)); |
|
750 |
return iObjects[ix]; |
|
751 |
} |
|
752 |
||
753 |
LOCAL_C TInt validateName(const TDesC &aName) |
|
754 |
// |
|
755 |
// Return KErrBadName if the name is invalid. |
|
756 |
// |
|
757 |
{ |
|
758 |
||
759 |
if (aName.Locate('*')!=KErrNotFound || aName.Locate('?')!=KErrNotFound || aName.Locate(':')!=KErrNotFound) |
|
760 |
return(KErrBadName); |
|
761 |
return(KErrNone); |
|
762 |
} |
|
763 |
||
764 |
||
765 |
TBool CFsObjectCon::NamesMatch(const TName& anObjectName, const CFsObject* aCurrentObject) const |
|
766 |
// |
|
767 |
// |
|
768 |
// |
|
769 |
{ |
|
770 |
||
771 |
if (aCurrentObject->iName==NULL) // current object has no name, therefore not the same |
|
772 |
return(EFalse); |
|
773 |
return(anObjectName.Compare(*aCurrentObject->iName)==0); // short names are different, therefore not the same |
|
774 |
} |
|
775 |
||
776 |
TInt CFsObjectCon::CheckUniqueName(const CFsObject* anObject) const |
|
777 |
// |
|
778 |
// Returns KErrBadName if the name is invalid or |
|
779 |
// returns KErrAlreadyExists if the full name is not unique. |
|
780 |
// |
|
781 |
{ |
|
782 |
||
783 |
TName name(*(anObject->iName)); |
|
784 |
TInt r=validateName(name); |
|
785 |
if (r!=KErrNone) |
|
786 |
return r; |
|
787 |
||
788 |
if (!iCount) |
|
789 |
return KErrNone; |
|
790 |
||
791 |
CFsObject** pS=iObjects; |
|
792 |
CFsObject** pE=pS+iCount; |
|
793 |
||
794 |
// if it's name is null, just need to check it's not already there |
|
795 |
if (!anObject->iName) |
|
796 |
{ |
|
797 |
do |
|
798 |
{ |
|
799 |
if (*pS==anObject) |
|
800 |
return KErrAlreadyExists; |
|
801 |
} while(++pS<pE); |
|
802 |
return KErrNone; |
|
803 |
} |
|
804 |
||
805 |
do |
|
806 |
{ |
|
807 |
if (NamesMatch(name,*pS)) |
|
808 |
return KErrAlreadyExists; |
|
809 |
} while(++pS<pE); |
|
810 |
return KErrNone; |
|
811 |
} |
|
812 |
||
813 |
TInt CFsObjectCon::FindByName(TInt &aFindHandle,const TDesC &aMatch) const |
|
814 |
// |
|
815 |
// Find an object starting at aFindHandle which matches aMatch |
|
816 |
// just using the objects name. |
|
817 |
// |
|
818 |
{ |
|
819 |
||
820 |
if (!iCount) |
|
821 |
return KErrNotFound; |
|
822 |
TInt ix=0; |
|
823 |
if (aFindHandle!=0) |
|
824 |
{ |
|
825 |
__ASSERT_ALWAYS(uniqueID(aFindHandle)==iUniqueID,Fault(EObjFindBadHandle)); |
|
826 |
ix=index(aFindHandle)+1; |
|
827 |
}; |
|
828 |
CFsObject** pS=iObjects; |
|
829 |
CFsObject** pE=pS+iCount; |
|
830 |
pS+=ix; |
|
831 |
while(pS<pE) |
|
832 |
{ |
|
833 |
TName name=*((*pS++)->iName); |
|
834 |
if (name.MatchF(aMatch)!=KErrNotFound) |
|
835 |
{ |
|
836 |
aFindHandle=makeFindHandle(ix,iUniqueID); |
|
837 |
return KErrNone; |
|
838 |
} |
|
839 |
ix++; |
|
840 |
} |
|
841 |
aFindHandle=makeFindHandle(KObjectMaxIndex,iUniqueID); |
|
842 |
return KErrNotFound; |
|
843 |
} |
|
844 |
||
845 |
/** |
|
846 |
Constructs the object and initializes the Drive number as an invalid drive. |
|
847 |
||
848 |
Once constructed, a dispatch object cannot be deleted until its reference |
|
849 |
count is reduced to zero. |
|
850 |
||
851 |
@see CFsDispatchObject::Close |
|
852 |
*/ |
|
853 |
CFsDispatchObject::CFsDispatchObject() |
|
854 |
: iDriveNumber(-1) |
|
855 |
{} |
|
856 |
||
857 |
/** |
|
858 |
Initialises dispatch object |
|
859 |
||
860 |
Creates an internal request and sets it as a file resource close operation. |
|
861 |
Initialises drive number as specified. |
|
862 |
||
863 |
@param aDrvNumber Drive for which the request is intended. |
|
864 |
*/ |
|
865 |
void CFsDispatchObject::DoInitL(TInt aDrvNumber) |
|
866 |
{ |
|
867 |
CFsInternalRequest* PR= new(ELeave) CFsInternalRequest; |
|
868 |
__THRD_PRINT1(_L("internal request = 0x%x"),PR); |
|
869 |
PR->Set(DispatchObjectCloseOp,NULL); |
|
870 |
iRequest = PR; |
|
871 |
iRequest->SetDriveNumber(aDrvNumber); |
|
872 |
iRequest->SetScratchValue((TUint)this); |
|
873 |
iDriveNumber=aDrvNumber; |
|
874 |
} |
|
875 |
||
876 |
/** |
|
877 |
Closes this dispatch object. |
|
878 |
||
879 |
Decrements the reference count by one. If this becomes zero then the request |
|
880 |
will either call Dispatch() if currently not within the correct thread |
|
881 |
context otherwise the function calls DoClose() on this dispatch object. |
|
882 |
||
883 |
@see CFsDispatchObject::IsCorrectThread |
|
884 |
CFsDispatchObject::Dispatch |
|
885 |
CFsObject::DoClose |
|
886 |
*/ |
|
887 |
EXPORT_C void CFsDispatchObject::Close() |
|
888 |
{ |
|
889 |
__THRD_PRINT1(_L("CFsDispatchObject::Close() 0x%x"),this); |
|
890 |
if(Dec()!=1) |
|
891 |
return; |
|
892 |
if(!IsCorrectThread()) |
|
893 |
Dispatch(); |
|
894 |
else |
|
895 |
DoClose(); |
|
896 |
} |
|
897 |
||
898 |
/** |
|
899 |
Destructor. |
|
900 |
||
901 |
Deletes assigned CFsRequest object. |
|
902 |
*/ |
|
903 |
CFsDispatchObject::~CFsDispatchObject() |
|
904 |
{ |
|
905 |
__THRD_PRINT(_L("CFsDispatchObject::~CFsDispatchObject()")); |
|
906 |
if(iRequest) |
|
907 |
delete(iRequest); |
|
908 |
} |
|
909 |
||
910 |
/** |
|
911 |
Dispatches an assigned CFsRequest object. |
|
912 |
||
913 |
@see CFsRequest::Dispatch() |
|
914 |
*/ |
|
915 |
void CFsDispatchObject::Dispatch() |
|
916 |
{ |
|
917 |
__ASSERT_DEBUG(iRequest,Fault(EDispatchObjectDispatch)); |
|
918 |
iRequest->Dispatch(); |
|
919 |
} |
|
920 |
||
921 |
||
922 |
/** |
|
923 |
Determine if this object is within the correct drive thread context |
|
924 |
for file resource request. |
|
925 |
For example subsession close request. |
|
926 |
||
927 |
@return ETrue if within the correct drive thread context. |
|
928 |
*/ |
|
929 |
EXPORT_C TBool CFsDispatchObject::IsCorrectThread() |
|
930 |
{ |
|
931 |
if(!iRequest) |
|
932 |
return(ETrue); |
|
933 |
FsThreadManager::LockDrive(iRequest->DriveNumber()); |
|
934 |
TBool b; |
|
935 |
__ASSERT_ALWAYS(FsThreadManager::IsDriveAvailable(iRequest->DriveNumber(),EFalse) || FsThreadManager::IsMainThread(), Fault(EDispatchObjectThread)); |
|
936 |
if(!FsThreadManager::IsDriveAvailable(iRequest->DriveNumber(),EFalse) && FsThreadManager::IsMainThread()) |
|
937 |
b=ETrue; |
|
938 |
else if(FsThreadManager::IsDriveSync(iRequest->DriveNumber(),EFalse) && FsThreadManager::IsMainThread()) |
|
939 |
b=ETrue; |
|
940 |
else |
|
941 |
b=FsThreadManager::IsDriveThread(iRequest->DriveNumber(),EFalse); |
|
942 |
FsThreadManager::UnlockDrive(iRequest->DriveNumber()); |
|
943 |
return(b); |
|
944 |
} |
|
945 |
||
946 |
TInt TFsCloseObject::Initialise(CFsRequest* /*aRequest*/) |
|
947 |
// |
|
948 |
// |
|
949 |
// |
|
950 |
{ |
|
951 |
return(KErrNone); |
|
952 |
} |
|
953 |
||
954 |
TInt TFsCloseObject::DoRequestL(CFsRequest* /*aRequest*/) |
|
955 |
// |
|
956 |
// |
|
957 |
// |
|
958 |
{ |
|
959 |
__PRINT(_L("TFsCloseObject::DoRequestL()")); |
|
960 |
return(KErrNone); |
|
961 |
} |
|
962 |
||
963 |
TInt TFsCloseObject::Complete(CFsRequest* aRequest) |
|
964 |
// |
|
965 |
// |
|
966 |
// |
|
967 |
{ |
|
968 |
__PRINT(_L("TFsCloseObject::Complete()")); |
|
969 |
CFsDispatchObject* pO=(CFsDispatchObject*)aRequest->ScratchValue(); |
|
970 |
// set CFsDispatchObject::iRequest to NULL since request will be deleted in Free() |
|
971 |
pO->iRequest=NULL; |
|
972 |
pO->DoClose(); |
|
973 |
return(KErrNone); |
|
974 |
} |
|
975 |
||
976 |