sl@0: // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32\include\drivers\resourcecontrol.inl sl@0: // sl@0: // WARNING: This file contains some APIs which are internal and are subject sl@0: // to change without noticed. Such APIs should therefore not be used sl@0: // outside the Kernel and Hardware Services package. sl@0: // sl@0: sl@0: /* The Driver's Version */ sl@0: inline TVersion DResConPddFactory::VersionRequired() sl@0: { sl@0: const TInt KResConMajorVersionNumber=1; sl@0: const TInt KResConMinorVersionNumber=0; sl@0: const TInt KResConBuildVersionNumber=KE32BuildVersionNumber; sl@0: return TVersion(KResConMajorVersionNumber,KResConMinorVersionNumber,KResConBuildVersionNumber); sl@0: } sl@0: sl@0: /** Second stage constructor sl@0: Allocates the specified size in kernel heap and creates a virtual link */ sl@0: template sl@0: inline TInt DResourceCon::Initialise(TUint16 aInitialSize) sl@0: { sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon::Initialise")); sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf("aInitialSize %d", aInitialSize)); sl@0: //Allocate memory for size specified. sl@0: if(aInitialSize != 0) sl@0: { sl@0: iArray = (T**) new (T*[aInitialSize]); sl@0: if((!iArray)) sl@0: { sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf("No Sufficient memory for iArray allocation")); sl@0: return KErrNoMemory; sl@0: } sl@0: //Create a virtual link by storing in each array position the index of next higher free location sl@0: for(TInt c = 0; c < aInitialSize; c++) sl@0: iArray[c] = (T*)(c+1); sl@0: } sl@0: iAllocated = aInitialSize; sl@0: iGrowBy = aInitialSize < 2 ? aInitialSize : TUint16(aInitialSize/2); sl@0: iCount = 0; sl@0: iInstanceCount = 0; sl@0: iFreeLoc = 0; sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf("::Initialise")); sl@0: #ifdef PRM_INSTRUMENTATION_MACRO sl@0: if(aInitialSize) sl@0: { sl@0: TUint size = aInitialSize * 4; sl@0: PRM_MEMORY_USAGE_TRACE sl@0: } sl@0: #endif sl@0: return KErrNone; sl@0: } sl@0: sl@0: /** Resize the array */ sl@0: template sl@0: inline TInt DResourceCon::ReSize(TUint16 aGrowBy) sl@0: { sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon::ReSize")); sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf("aGrowBy %d\n", aGrowBy)); sl@0: // Allocate memory for already existing + new required size. sl@0: TInt r = Kern::SafeReAlloc((TAny*&)iArray, iAllocated * sizeof(T*), (iAllocated+aGrowBy)*sizeof(T*)); sl@0: if(r != KErrNone) sl@0: return r; sl@0: TUint16 c = iAllocated; sl@0: //Virtually link the free ones sl@0: while(c<(iAllocated+aGrowBy)) sl@0: { sl@0: iArray[c]=(T*)(c+1); sl@0: c++; sl@0: } sl@0: iAllocated = TUint16(iAllocated + aGrowBy); sl@0: #ifdef PRM_INSTRUMENTATION_MACRO sl@0: TUint size = aGrowBy*4; sl@0: PRM_MEMORY_USAGE_TRACE sl@0: #endif sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf("< DResourceCon::ReSize, iAllocated = %d", iAllocated)); sl@0: return KErrNone; sl@0: } sl@0: sl@0: /** Delete the allocated array */ sl@0: template sl@0: inline void DResourceCon::Delete() sl@0: { sl@0: delete []iArray; sl@0: } sl@0: sl@0: /** Find the object at the specified location */ sl@0: template sl@0: inline T* DResourceCon::operator[](TUint16 anIndex) sl@0: { sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon::operator[], anIndex = %d", anIndex)); sl@0: // Check if passed index is inside allocated range and is not free. sl@0: if(anIndex>=iAllocated || (((TUint)iArray[anIndex])<=iAllocated)) sl@0: return NULL; sl@0: return iArray[anIndex]; sl@0: } sl@0: sl@0: /** Remove the specified object from the container */ sl@0: template sl@0: inline TInt DResourceCon::Remove(T* /*aObj */, TUint16 aIndex) sl@0: { sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon::Remove")); sl@0: if(aIndex>=iAllocated) sl@0: { sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf("Object not found, iAllocated = %d, index = %d", iAllocated, aIndex)); sl@0: DPowerResourceController::Panic(DPowerResourceController::EObjectNotFoundInList); sl@0: } sl@0: // Add the entry to the free location sl@0: iArray[aIndex] = (T*)iFreeLoc; sl@0: iFreeLoc = aIndex; sl@0: iCount--; //Decrement valid client count sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon::Remove")); sl@0: return KErrNone; sl@0: } sl@0: sl@0: /** Add the specified object into the container */ sl@0: template sl@0: inline TInt DResourceCon::Add(T* aObj, TUint &aId) sl@0: { sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon::Add")); sl@0: //Check for array full sl@0: if(iFreeLoc == iAllocated) sl@0: return KErrNoMemory; sl@0: //Update in the array in the free location sl@0: aId = ((++iInstanceCount & INSTANCE_COUNT_BIT_MASK) << INSTANCE_COUNT_POS); //Instance count sl@0: aId |= (iFreeLoc & ID_INDEX_BIT_MASK); //Array index sl@0: TUint16 nextFreeLoc = (TUint16)(TUint)iArray[iFreeLoc]; sl@0: iArray[iFreeLoc] = aObj; sl@0: iFreeLoc = nextFreeLoc; sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf("iFreeLoc %d", iFreeLoc)); sl@0: iCount++; //Increment the valid client count sl@0: return KErrNone; sl@0: } sl@0: sl@0: /** Find the entry with specified name and update in anEntry */ sl@0: template sl@0: inline TInt DResourceCon::Find(T*& anEntry, TDesC8& aName) sl@0: { sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon::Find, aName %S", &aName)); sl@0: anEntry = NULL; sl@0: T* pC=anEntry; sl@0: for(TUint count = 0; countiName)) sl@0: { sl@0: anEntry=pC; sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf("::Find, Entry Found")); sl@0: return KErrNone; sl@0: } sl@0: } sl@0: sl@0: __KTRACE_OPT(KRESMANAGER, Kern::Printf("::Find, Entry Not Found")); sl@0: return KErrNotFound; sl@0: }