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