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(TInt aInitialSize) |
33 inline TInt DResourceCon<T>::Initialise(TUint16 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 = (TUint16)aInitialSize; |
50 iAllocated = aInitialSize; |
51 iGrowBy = (TUint16) (aInitialSize < 2 ? aInitialSize : aInitialSize/2); |
51 iGrowBy = aInitialSize < 2 ? aInitialSize : TUint16(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(TInt aGrowBy) |
68 inline TInt DResourceCon<T>::ReSize(TUint16 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 TInt c = iAllocated; |
76 TUint16 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; |
|
98 } |
97 } |
99 |
98 |
100 /** Find the object at the specified location */ |
99 /** Find the object at the specified location */ |
101 template <class T> |
100 template <class T> |
102 inline T* DResourceCon<T>::operator[](TInt anIndex) |
101 inline T* DResourceCon<T>::operator[](TUint16 anIndex) |
103 { |
102 { |
104 __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::operator[], anIndex = %d", anIndex)); |
103 __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::operator[], anIndex = %d", anIndex)); |
105 // Check if passed index is inside allocated range and is not free. |
104 // Check if passed index is inside allocated range and is not free. |
106 if(anIndex>=iAllocated || (((TUint)iArray[anIndex])<=iAllocated)) |
105 if(anIndex>=iAllocated || (((TUint)iArray[anIndex])<=iAllocated)) |
107 return NULL; |
106 return NULL; |
108 return iArray[anIndex]; |
107 return iArray[anIndex]; |
109 } |
108 } |
110 |
109 |
111 /** Remove the specified object from the container */ |
110 /** Remove the specified object from the container */ |
112 template <class T> |
111 template <class T> |
113 inline TInt DResourceCon<T>::Remove(T* /*aObj */, TInt aIndex) |
112 inline TInt DResourceCon<T>::Remove(T* /*aObj */, TUint16 aIndex) |
114 { |
113 { |
115 __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove")); |
114 __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove")); |
116 if(aIndex>=(TInt)iAllocated) |
115 if(aIndex>=iAllocated) |
117 { |
116 { |
118 __KTRACE_OPT(KRESMANAGER, Kern::Printf("Object not found, iAllocated = %d, index = %d", iAllocated, aIndex)); |
117 __KTRACE_OPT(KRESMANAGER, Kern::Printf("Object not found, iAllocated = %d, index = %d", iAllocated, aIndex)); |
119 DPowerResourceController::Panic(DPowerResourceController::EObjectNotFoundInList); |
118 DPowerResourceController::Panic(DPowerResourceController::EObjectNotFoundInList); |
120 } |
119 } |
121 // Add the entry to the free location |
120 // Add the entry to the free location |
122 iArray[aIndex] = (T*)iFreeLoc; |
121 iArray[aIndex] = (T*)iFreeLoc; |
123 iFreeLoc = (TUint16)aIndex; |
122 iFreeLoc = aIndex; |
124 iCount--; //Decrement valid client count |
123 iCount--; //Decrement valid client count |
125 __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove")); |
124 __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Remove")); |
126 return KErrNone; |
125 return KErrNone; |
127 } |
126 } |
128 |
127 |
135 if(iFreeLoc == iAllocated) |
134 if(iFreeLoc == iAllocated) |
136 return KErrNoMemory; |
135 return KErrNoMemory; |
137 //Update in the array in the free location |
136 //Update in the array in the free location |
138 aId = ((++iInstanceCount & INSTANCE_COUNT_BIT_MASK) << INSTANCE_COUNT_POS); //Instance count |
137 aId = ((++iInstanceCount & INSTANCE_COUNT_BIT_MASK) << INSTANCE_COUNT_POS); //Instance count |
139 aId |= (iFreeLoc & ID_INDEX_BIT_MASK); //Array index |
138 aId |= (iFreeLoc & ID_INDEX_BIT_MASK); //Array index |
140 TUint nextFreeLoc = (TUint)iArray[iFreeLoc]; |
139 TUint16 nextFreeLoc = (TUint16)(TUint)iArray[iFreeLoc]; |
141 iArray[iFreeLoc] = aObj; |
140 iArray[iFreeLoc] = aObj; |
142 iFreeLoc = (TUint16)nextFreeLoc; |
141 iFreeLoc = nextFreeLoc; |
143 __KTRACE_OPT(KRESMANAGER, Kern::Printf("iFreeLoc %d", iFreeLoc)); |
142 __KTRACE_OPT(KRESMANAGER, Kern::Printf("iFreeLoc %d", iFreeLoc)); |
144 iCount++; //Increment the valid client count |
143 iCount++; //Increment the valid client count |
145 return KErrNone; |
144 return KErrNone; |
146 } |
145 } |
147 |
146 |
150 inline TInt DResourceCon<T>::Find(T*& anEntry, TDesC8& aName) |
149 inline TInt DResourceCon<T>::Find(T*& anEntry, TDesC8& aName) |
151 { |
150 { |
152 __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Find, aName %S", &aName)); |
151 __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DResourceCon<T>::Find, aName %S", &aName)); |
153 anEntry = NULL; |
152 anEntry = NULL; |
154 T* pC=anEntry; |
153 T* pC=anEntry; |
155 for(TInt count = 0; count < (TInt)iAllocated; count++) |
154 for(TUint count = 0; count<iAllocated; count++) |
156 { |
155 { |
157 /* Check whether the location is free */ |
156 /* Check whether the location is free */ |
158 if(((TUint)iArray[count]) <= iAllocated) |
157 if(((TUint)iArray[count]) <= iAllocated) |
159 continue; |
158 continue; |
160 pC=(T*)iArray[count]; |
159 pC=(T*)iArray[count]; |