kernel/eka/include/drivers/resourcecontrol.inl
changeset 0 a41df078684a
child 244 a77889bee936
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2007-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 // e32\include\drivers\resourcecontrol.inl
       
    15 // 
       
    16 // WARNING: This file contains some APIs which are internal and are subject
       
    17 //          to change without noticed. Such APIs should therefore not be used
       
    18 //          outside the Kernel and Hardware Services package.
       
    19 //
       
    20 
       
    21 /* The Driver's Version */
       
    22 inline TVersion DResConPddFactory::VersionRequired()
       
    23 	{
       
    24 	const TInt KResConMajorVersionNumber=1;
       
    25 	const TInt KResConMinorVersionNumber=0;
       
    26 	const TInt KResConBuildVersionNumber=KE32BuildVersionNumber;
       
    27 	return TVersion(KResConMajorVersionNumber,KResConMinorVersionNumber,KResConBuildVersionNumber);
       
    28 	}
       
    29 
       
    30 /** Second stage constructor
       
    31  Allocates the specified size in kernel heap and creates a virtual link */
       
    32 template <class T>
       
    33 inline TInt DResourceCon<T>::Initialise(TUint16 aInitialSize)
       
    34 	{
       
    35     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Initialise"));
       
    36     __KTRACE_OPT(KRESMANAGER, Kern::Printf("aInitialSize %d", aInitialSize));
       
    37     //Allocate memory for size specified.
       
    38 	if(aInitialSize != 0)
       
    39 		{
       
    40 		iArray = (T**) new (T*[aInitialSize]);
       
    41 		if((!iArray))
       
    42 			{
       
    43 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("No Sufficient memory for iArray allocation"));
       
    44 			return KErrNoMemory;
       
    45 			}
       
    46 		//Create a virtual link by storing in each array position the index of next higher free location
       
    47 		for(TInt c = 0; c < aInitialSize; c++)
       
    48 			iArray[c] = (T*)(c+1);
       
    49 		}
       
    50     iAllocated = aInitialSize;
       
    51     iGrowBy = aInitialSize < 2 ? aInitialSize : TUint16(aInitialSize/2);
       
    52     iCount = 0;
       
    53     iInstanceCount = 0;
       
    54     iFreeLoc = 0;
       
    55 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DResourceCon<T>::Initialise"));
       
    56 #ifdef PRM_INSTRUMENTATION_MACRO
       
    57 	if(aInitialSize)
       
    58 		{
       
    59 		TUint size = aInitialSize * 4;
       
    60 		PRM_MEMORY_USAGE_TRACE
       
    61 		}
       
    62 #endif
       
    63 	return KErrNone;
       
    64 	}
       
    65 
       
    66 /** Resize the array */
       
    67 template <class T>
       
    68 inline TInt DResourceCon<T>::ReSize(TUint16 aGrowBy)
       
    69 	{
       
    70     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::ReSize"));
       
    71     __KTRACE_OPT(KRESMANAGER, Kern::Printf("aGrowBy %d\n", aGrowBy));
       
    72 	// Allocate memory for already existing + new required size.
       
    73 	TInt r = Kern::SafeReAlloc((TAny*&)iArray, iAllocated * sizeof(T*), (iAllocated+aGrowBy)*sizeof(T*));
       
    74 	if(r != KErrNone)
       
    75 		return r;
       
    76     TUint16 c = iAllocated;
       
    77     //Virtually link the free ones
       
    78     while(c<(iAllocated+aGrowBy))
       
    79 		{
       
    80          iArray[c]=(T*)(c+1);
       
    81          c++;
       
    82 		}
       
    83     iAllocated = TUint16(iAllocated + aGrowBy);
       
    84 #ifdef PRM_INSTRUMENTATION_MACRO
       
    85     TUint size = aGrowBy*4;
       
    86     PRM_MEMORY_USAGE_TRACE
       
    87 #endif
       
    88 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("< DResourceCon<T>::ReSize, iAllocated = %d", iAllocated));
       
    89 	return KErrNone;
       
    90 	}
       
    91 
       
    92 /** Delete the allocated array */
       
    93 template <class T>
       
    94 inline void DResourceCon<T>::Delete()
       
    95 	{
       
    96     delete []iArray;
       
    97 	}
       
    98 
       
    99 /** Find the object at the specified location */
       
   100 template <class T>
       
   101 inline T* DResourceCon<T>::operator[](TUint16 anIndex)
       
   102 	{
       
   103     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::operator[], anIndex = %d", anIndex));
       
   104 	// Check if passed index is inside allocated range and is not free.
       
   105     if(anIndex>=iAllocated || (((TUint)iArray[anIndex])<=iAllocated))
       
   106 		return NULL;
       
   107     return iArray[anIndex];
       
   108 	}
       
   109 
       
   110 /** Remove the specified object from the container */
       
   111 template <class T>
       
   112 inline TInt DResourceCon<T>::Remove(T* /*aObj */, TUint16 aIndex)
       
   113 	{
       
   114     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove"));
       
   115     if(aIndex>=iAllocated)
       
   116 		{
       
   117 		__KTRACE_OPT(KRESMANAGER, Kern::Printf("Object not found, iAllocated = %d, index = %d", iAllocated, aIndex));
       
   118 		DPowerResourceController::Panic(DPowerResourceController::EObjectNotFoundInList);
       
   119 		}
       
   120 	// Add the entry to the free location
       
   121 	iArray[aIndex] = (T*)iFreeLoc;
       
   122 	iFreeLoc = aIndex;
       
   123     iCount--; //Decrement valid client count
       
   124 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove"));
       
   125     return KErrNone;
       
   126 	}
       
   127 
       
   128 /** Add the specified object into the container */
       
   129 template <class T>
       
   130 inline TInt DResourceCon<T>::Add(T* aObj, TUint &aId)
       
   131 	{
       
   132     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Add"));
       
   133 	//Check for array full
       
   134 	if(iFreeLoc == iAllocated)
       
   135 		return KErrNoMemory;
       
   136     //Update in the array in the free location
       
   137 	aId = ((++iInstanceCount & INSTANCE_COUNT_BIT_MASK) << INSTANCE_COUNT_POS); //Instance count
       
   138 	aId |= (iFreeLoc & ID_INDEX_BIT_MASK); //Array index
       
   139     TUint16 nextFreeLoc = (TUint16)(TUint)iArray[iFreeLoc];
       
   140     iArray[iFreeLoc] = aObj;
       
   141     iFreeLoc = nextFreeLoc;
       
   142     __KTRACE_OPT(KRESMANAGER, Kern::Printf("iFreeLoc %d", iFreeLoc));
       
   143     iCount++;  //Increment the valid client count
       
   144     return KErrNone;
       
   145 	}
       
   146 
       
   147 /** Find the entry with specified name and update in anEntry */
       
   148 template <class T>
       
   149 inline TInt DResourceCon<T>::Find(T*& anEntry, TDesC8& aName)
       
   150 	{
       
   151     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Find, aName %S", &aName));
       
   152     anEntry = NULL;
       
   153     T* pC=anEntry;
       
   154     for(TUint count = 0; count<iAllocated; count++)
       
   155 		{
       
   156         /* Check whether the location is free */
       
   157         if(((TUint)iArray[count]) <= iAllocated)
       
   158             continue;
       
   159         pC=(T*)iArray[count];
       
   160         if(!aName.Compare(*(const TDesC8*)pC->iName))
       
   161 			{
       
   162 			anEntry=pC;
       
   163 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DResourceCon<T>::Find, Entry Found"));
       
   164 			return KErrNone;
       
   165 			}
       
   166 		}
       
   167 
       
   168 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DResourceCon<T>::Find, Entry Not Found"));
       
   169     return KErrNotFound;
       
   170 	}