1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kernel/eka/include/drivers/resourcecontrol.inl Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,170 @@
1.4 +// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32\include\drivers\resourcecontrol.inl
1.18 +//
1.19 +// WARNING: This file contains some APIs which are internal and are subject
1.20 +// to change without noticed. Such APIs should therefore not be used
1.21 +// outside the Kernel and Hardware Services package.
1.22 +//
1.23 +
1.24 +/* The Driver's Version */
1.25 +inline TVersion DResConPddFactory::VersionRequired()
1.26 + {
1.27 + const TInt KResConMajorVersionNumber=1;
1.28 + const TInt KResConMinorVersionNumber=0;
1.29 + const TInt KResConBuildVersionNumber=KE32BuildVersionNumber;
1.30 + return TVersion(KResConMajorVersionNumber,KResConMinorVersionNumber,KResConBuildVersionNumber);
1.31 + }
1.32 +
1.33 +/** Second stage constructor
1.34 + Allocates the specified size in kernel heap and creates a virtual link */
1.35 +template <class T>
1.36 +inline TInt DResourceCon<T>::Initialise(TUint16 aInitialSize)
1.37 + {
1.38 + __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Initialise"));
1.39 + __KTRACE_OPT(KRESMANAGER, Kern::Printf("aInitialSize %d", aInitialSize));
1.40 + //Allocate memory for size specified.
1.41 + if(aInitialSize != 0)
1.42 + {
1.43 + iArray = (T**) new (T*[aInitialSize]);
1.44 + if((!iArray))
1.45 + {
1.46 + __KTRACE_OPT(KRESMANAGER, Kern::Printf("No Sufficient memory for iArray allocation"));
1.47 + return KErrNoMemory;
1.48 + }
1.49 + //Create a virtual link by storing in each array position the index of next higher free location
1.50 + for(TInt c = 0; c < aInitialSize; c++)
1.51 + iArray[c] = (T*)(c+1);
1.52 + }
1.53 + iAllocated = aInitialSize;
1.54 + iGrowBy = aInitialSize < 2 ? aInitialSize : TUint16(aInitialSize/2);
1.55 + iCount = 0;
1.56 + iInstanceCount = 0;
1.57 + iFreeLoc = 0;
1.58 + __KTRACE_OPT(KRESMANAGER, Kern::Printf("<DResourceCon<T>::Initialise"));
1.59 +#ifdef PRM_INSTRUMENTATION_MACRO
1.60 + if(aInitialSize)
1.61 + {
1.62 + TUint size = aInitialSize * 4;
1.63 + PRM_MEMORY_USAGE_TRACE
1.64 + }
1.65 +#endif
1.66 + return KErrNone;
1.67 + }
1.68 +
1.69 +/** Resize the array */
1.70 +template <class T>
1.71 +inline TInt DResourceCon<T>::ReSize(TUint16 aGrowBy)
1.72 + {
1.73 + __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::ReSize"));
1.74 + __KTRACE_OPT(KRESMANAGER, Kern::Printf("aGrowBy %d\n", aGrowBy));
1.75 + // Allocate memory for already existing + new required size.
1.76 + TInt r = Kern::SafeReAlloc((TAny*&)iArray, iAllocated * sizeof(T*), (iAllocated+aGrowBy)*sizeof(T*));
1.77 + if(r != KErrNone)
1.78 + return r;
1.79 + TUint16 c = iAllocated;
1.80 + //Virtually link the free ones
1.81 + while(c<(iAllocated+aGrowBy))
1.82 + {
1.83 + iArray[c]=(T*)(c+1);
1.84 + c++;
1.85 + }
1.86 + iAllocated = TUint16(iAllocated + aGrowBy);
1.87 +#ifdef PRM_INSTRUMENTATION_MACRO
1.88 + TUint size = aGrowBy*4;
1.89 + PRM_MEMORY_USAGE_TRACE
1.90 +#endif
1.91 + __KTRACE_OPT(KRESMANAGER, Kern::Printf("< DResourceCon<T>::ReSize, iAllocated = %d", iAllocated));
1.92 + return KErrNone;
1.93 + }
1.94 +
1.95 +/** Delete the allocated array */
1.96 +template <class T>
1.97 +inline void DResourceCon<T>::Delete()
1.98 + {
1.99 + delete []iArray;
1.100 + }
1.101 +
1.102 +/** Find the object at the specified location */
1.103 +template <class T>
1.104 +inline T* DResourceCon<T>::operator[](TUint16 anIndex)
1.105 + {
1.106 + __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::operator[], anIndex = %d", anIndex));
1.107 + // Check if passed index is inside allocated range and is not free.
1.108 + if(anIndex>=iAllocated || (((TUint)iArray[anIndex])<=iAllocated))
1.109 + return NULL;
1.110 + return iArray[anIndex];
1.111 + }
1.112 +
1.113 +/** Remove the specified object from the container */
1.114 +template <class T>
1.115 +inline TInt DResourceCon<T>::Remove(T* /*aObj */, TUint16 aIndex)
1.116 + {
1.117 + __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove"));
1.118 + if(aIndex>=iAllocated)
1.119 + {
1.120 + __KTRACE_OPT(KRESMANAGER, Kern::Printf("Object not found, iAllocated = %d, index = %d", iAllocated, aIndex));
1.121 + DPowerResourceController::Panic(DPowerResourceController::EObjectNotFoundInList);
1.122 + }
1.123 + // Add the entry to the free location
1.124 + iArray[aIndex] = (T*)iFreeLoc;
1.125 + iFreeLoc = aIndex;
1.126 + iCount--; //Decrement valid client count
1.127 + __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove"));
1.128 + return KErrNone;
1.129 + }
1.130 +
1.131 +/** Add the specified object into the container */
1.132 +template <class T>
1.133 +inline TInt DResourceCon<T>::Add(T* aObj, TUint &aId)
1.134 + {
1.135 + __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Add"));
1.136 + //Check for array full
1.137 + if(iFreeLoc == iAllocated)
1.138 + return KErrNoMemory;
1.139 + //Update in the array in the free location
1.140 + aId = ((++iInstanceCount & INSTANCE_COUNT_BIT_MASK) << INSTANCE_COUNT_POS); //Instance count
1.141 + aId |= (iFreeLoc & ID_INDEX_BIT_MASK); //Array index
1.142 + TUint16 nextFreeLoc = (TUint16)(TUint)iArray[iFreeLoc];
1.143 + iArray[iFreeLoc] = aObj;
1.144 + iFreeLoc = nextFreeLoc;
1.145 + __KTRACE_OPT(KRESMANAGER, Kern::Printf("iFreeLoc %d", iFreeLoc));
1.146 + iCount++; //Increment the valid client count
1.147 + return KErrNone;
1.148 + }
1.149 +
1.150 +/** Find the entry with specified name and update in anEntry */
1.151 +template <class T>
1.152 +inline TInt DResourceCon<T>::Find(T*& anEntry, TDesC8& aName)
1.153 + {
1.154 + __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Find, aName %S", &aName));
1.155 + anEntry = NULL;
1.156 + T* pC=anEntry;
1.157 + for(TUint count = 0; count<iAllocated; count++)
1.158 + {
1.159 + /* Check whether the location is free */
1.160 + if(((TUint)iArray[count]) <= iAllocated)
1.161 + continue;
1.162 + pC=(T*)iArray[count];
1.163 + if(!aName.Compare(*(const TDesC8*)pC->iName))
1.164 + {
1.165 + anEntry=pC;
1.166 + __KTRACE_OPT(KRESMANAGER, Kern::Printf("<DResourceCon<T>::Find, Entry Found"));
1.167 + return KErrNone;
1.168 + }
1.169 + }
1.170 +
1.171 + __KTRACE_OPT(KRESMANAGER, Kern::Printf("<DResourceCon<T>::Find, Entry Not Found"));
1.172 + return KErrNotFound;
1.173 + }