kernel/eka/include/drivers/resourcecontrol.inl
changeset 244 a77889bee936
parent 0 a41df078684a
child 257 3e88ff8f41d5
equal deleted inserted replaced
243:c7a0ce20c48c 244:a77889bee936
    28 	}
    28 	}
    29 
    29 
    30 /** Second stage constructor
    30 /** Second stage constructor
    31  Allocates the specified size in kernel heap and creates a virtual link */
    31  Allocates the specified size in kernel heap and creates a virtual link */
    32 template <class T>
    32 template <class T>
    33 inline TInt DResourceCon<T>::Initialise(TUint16 aInitialSize)
    33 inline TInt DResourceCon<T>::Initialise(TInt aInitialSize)
    34 	{
    34 	{
    35     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Initialise"));
    35     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Initialise"));
    36     __KTRACE_OPT(KRESMANAGER, Kern::Printf("aInitialSize %d", aInitialSize));
    36     __KTRACE_OPT(KRESMANAGER, Kern::Printf("aInitialSize %d", aInitialSize));
    37     //Allocate memory for size specified.
    37     //Allocate memory for size specified.
    38 	if(aInitialSize != 0)
    38 	if(aInitialSize != 0)
    45 			}
    45 			}
    46 		//Create a virtual link by storing in each array position the index of next higher free location
    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++)
    47 		for(TInt c = 0; c < aInitialSize; c++)
    48 			iArray[c] = (T*)(c+1);
    48 			iArray[c] = (T*)(c+1);
    49 		}
    49 		}
    50     iAllocated = aInitialSize;
    50     iAllocated = (TUint16)aInitialSize;
    51     iGrowBy = aInitialSize < 2 ? aInitialSize : TUint16(aInitialSize/2);
    51     iGrowBy = (TUint16) (aInitialSize < 2 ? aInitialSize : aInitialSize/2);
    52     iCount = 0;
    52     iCount = 0;
    53     iInstanceCount = 0;
    53     iInstanceCount = 0;
    54     iFreeLoc = 0;
    54     iFreeLoc = 0;
    55 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DResourceCon<T>::Initialise"));
    55 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DResourceCon<T>::Initialise"));
    56 #ifdef PRM_INSTRUMENTATION_MACRO
    56 #ifdef PRM_INSTRUMENTATION_MACRO
    63 	return KErrNone;
    63 	return KErrNone;
    64 	}
    64 	}
    65 
    65 
    66 /** Resize the array */
    66 /** Resize the array */
    67 template <class T>
    67 template <class T>
    68 inline TInt DResourceCon<T>::ReSize(TUint16 aGrowBy)
    68 inline TInt DResourceCon<T>::ReSize(TInt aGrowBy)
    69 	{
    69 	{
    70     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::ReSize"));
    70     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::ReSize"));
    71     __KTRACE_OPT(KRESMANAGER, Kern::Printf("aGrowBy %d\n", aGrowBy));
    71     __KTRACE_OPT(KRESMANAGER, Kern::Printf("aGrowBy %d\n", aGrowBy));
    72 	// Allocate memory for already existing + new required size.
    72 	// Allocate memory for already existing + new required size.
    73 	TInt r = Kern::SafeReAlloc((TAny*&)iArray, iAllocated * sizeof(T*), (iAllocated+aGrowBy)*sizeof(T*));
    73 	TInt r = Kern::SafeReAlloc((TAny*&)iArray, iAllocated * sizeof(T*), (iAllocated+aGrowBy)*sizeof(T*));
    74 	if(r != KErrNone)
    74 	if(r != KErrNone)
    75 		return r;
    75 		return r;
    76     TUint16 c = iAllocated;
    76     TInt c = iAllocated;
    77     //Virtually link the free ones
    77     //Virtually link the free ones
    78     while(c<(iAllocated+aGrowBy))
    78     while(c<(iAllocated+aGrowBy))
    79 		{
    79 		{
    80          iArray[c]=(T*)(c+1);
    80          iArray[c]=(T*)(c+1);
    81          c++;
    81          c++;
    92 /** Delete the allocated array */
    92 /** Delete the allocated array */
    93 template <class T>
    93 template <class T>
    94 inline void DResourceCon<T>::Delete()
    94 inline void DResourceCon<T>::Delete()
    95 	{
    95 	{
    96     delete []iArray;
    96     delete []iArray;
       
    97     iArray = NULL;
    97 	}
    98 	}
    98 
    99 
    99 /** Find the object at the specified location */
   100 /** Find the object at the specified location */
   100 template <class T>
   101 template <class T>
   101 inline T* DResourceCon<T>::operator[](TUint16 anIndex)
   102 inline T* DResourceCon<T>::operator[](TInt anIndex)
   102 	{
   103 	{
   103     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::operator[], anIndex = %d", anIndex));
   104     __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 	// Check if passed index is inside allocated range and is not free.
   105     if(anIndex>=iAllocated || (((TUint)iArray[anIndex])<=iAllocated))
   106     if(anIndex>=iAllocated || (((TUint)iArray[anIndex])<=iAllocated))
   106 		return NULL;
   107 		return NULL;
   107     return iArray[anIndex];
   108     return iArray[anIndex];
   108 	}
   109 	}
   109 
   110 
   110 /** Remove the specified object from the container */
   111 /** Remove the specified object from the container */
   111 template <class T>
   112 template <class T>
   112 inline TInt DResourceCon<T>::Remove(T* /*aObj */, TUint16 aIndex)
   113 inline TInt DResourceCon<T>::Remove(T* /*aObj */, TInt aIndex)
   113 	{
   114 	{
   114     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove"));
   115     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove"));
   115     if(aIndex>=iAllocated)
   116     if(aIndex>=(TInt)iAllocated)
   116 		{
   117 		{
   117 		__KTRACE_OPT(KRESMANAGER, Kern::Printf("Object not found, iAllocated = %d, index = %d", iAllocated, aIndex));
   118 		__KTRACE_OPT(KRESMANAGER, Kern::Printf("Object not found, iAllocated = %d, index = %d", iAllocated, aIndex));
   118 		DPowerResourceController::Panic(DPowerResourceController::EObjectNotFoundInList);
   119 		DPowerResourceController::Panic(DPowerResourceController::EObjectNotFoundInList);
   119 		}
   120 		}
   120 	// Add the entry to the free location
   121 	// Add the entry to the free location
   121 	iArray[aIndex] = (T*)iFreeLoc;
   122 	iArray[aIndex] = (T*)iFreeLoc;
   122 	iFreeLoc = aIndex;
   123 	iFreeLoc = (TUint16)aIndex;
   123     iCount--; //Decrement valid client count
   124     iCount--; //Decrement valid client count
   124 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove"));
   125 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove"));
   125     return KErrNone;
   126     return KErrNone;
   126 	}
   127 	}
   127 
   128 
   134 	if(iFreeLoc == iAllocated)
   135 	if(iFreeLoc == iAllocated)
   135 		return KErrNoMemory;
   136 		return KErrNoMemory;
   136     //Update in the array in the free location
   137     //Update in the array in the free location
   137 	aId = ((++iInstanceCount & INSTANCE_COUNT_BIT_MASK) << INSTANCE_COUNT_POS); //Instance count
   138 	aId = ((++iInstanceCount & INSTANCE_COUNT_BIT_MASK) << INSTANCE_COUNT_POS); //Instance count
   138 	aId |= (iFreeLoc & ID_INDEX_BIT_MASK); //Array index
   139 	aId |= (iFreeLoc & ID_INDEX_BIT_MASK); //Array index
   139     TUint16 nextFreeLoc = (TUint16)(TUint)iArray[iFreeLoc];
   140     TUint nextFreeLoc = (TUint)iArray[iFreeLoc];
   140     iArray[iFreeLoc] = aObj;
   141     iArray[iFreeLoc] = aObj;
   141     iFreeLoc = nextFreeLoc;
   142     iFreeLoc = (TUint16)nextFreeLoc;
   142     __KTRACE_OPT(KRESMANAGER, Kern::Printf("iFreeLoc %d", iFreeLoc));
   143     __KTRACE_OPT(KRESMANAGER, Kern::Printf("iFreeLoc %d", iFreeLoc));
   143     iCount++;  //Increment the valid client count
   144     iCount++;  //Increment the valid client count
   144     return KErrNone;
   145     return KErrNone;
   145 	}
   146 	}
   146 
   147 
   149 inline TInt DResourceCon<T>::Find(T*& anEntry, TDesC8& aName)
   150 inline TInt DResourceCon<T>::Find(T*& anEntry, TDesC8& aName)
   150 	{
   151 	{
   151     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Find, aName %S", &aName));
   152     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Find, aName %S", &aName));
   152     anEntry = NULL;
   153     anEntry = NULL;
   153     T* pC=anEntry;
   154     T* pC=anEntry;
   154     for(TUint count = 0; count<iAllocated; count++)
   155     for(TInt count = 0; count < (TInt)iAllocated; count++)
   155 		{
   156 		{
   156         /* Check whether the location is free */
   157         /* Check whether the location is free */
   157         if(((TUint)iArray[count]) <= iAllocated)
   158         if(((TUint)iArray[count]) <= iAllocated)
   158             continue;
   159             continue;
   159         pC=(T*)iArray[count];
   160         pC=(T*)iArray[count];