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