|
1 // Copyright (c) 1997-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 "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 // Maintains a free queue of single size RMBuf's on behalf of CMBufPoolManager |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file |
|
20 @internalComponent |
|
21 */ |
|
22 |
|
23 #include "MBufPoolChain.h" |
|
24 #include "MBufPool.h" |
|
25 #include "MBufPoolManager.h" |
|
26 |
|
27 RMBufPoolChain::RMBufPoolChain(TInt aMBufSize, CMBufPoolManager& aMBufPoolManager) |
|
28 : iMBufSize(aMBufSize), iInitialAllocation(0), iMBufPoolManager(aMBufPoolManager) |
|
29 { |
|
30 } |
|
31 |
|
32 RMBufPoolChain::RMBufPoolChain(TInt aMBufSize, TInt aInitialAllocation, TInt aMinGrowth, TInt aGrowthThreshold, CMBufPoolManager& aMBufPoolManager) |
|
33 : iMBufSize(aMBufSize), iInitialAllocation(aInitialAllocation), iMinGrowth(aMinGrowth), iGrowthThreshold(aGrowthThreshold), iMBufPoolManager(aMBufPoolManager) |
|
34 { |
|
35 iLenFree = 0; |
|
36 iLenTotal = 0; |
|
37 iFreeMBufs.Init(); |
|
38 iPools.Reset(); |
|
39 |
|
40 iSignalled = EFalse; |
|
41 iNumMBufsToAdd = 0; |
|
42 iNoMoreSpace = 0; |
|
43 iLastGrowth = 0; |
|
44 iMaxGrowthAttempt = 0; |
|
45 |
|
46 iDbgPoolLimit = 0; |
|
47 |
|
48 iPools.SetOffset(_FOFF(CMBufPool, iLink)); |
|
49 } |
|
50 |
|
51 void RMBufPoolChain::OpenL() |
|
52 { |
|
53 User::LeaveIfError(iLockFreeList.CreateLocal()); |
|
54 iPools.Reset(); |
|
55 iPools.SetOffset(_FOFF(CMBufPool, iLink)); |
|
56 } |
|
57 |
|
58 void RMBufPoolChain::Close() |
|
59 { |
|
60 iLockFreeList.Close(); |
|
61 } |
|
62 |
|
63 // get the growth amount |
|
64 TInt RMBufPoolChain::GrowthSize(TInt aSize) |
|
65 { |
|
66 // small request so grow by minimum size |
|
67 if (aSize < iMinGrowth) |
|
68 { |
|
69 return iMinGrowth; |
|
70 } |
|
71 |
|
72 // Have we tried as big as this in the past? |
|
73 if ( (iMaxGrowthAttempt > 0) && (aSize >= iMaxGrowthAttempt) ) |
|
74 { |
|
75 // try smaller size again |
|
76 aSize = iMaxGrowthAttempt; |
|
77 } |
|
78 |
|
79 // Attempt to grow the pool by requested amount |
|
80 if (!iNoMoreSpace) |
|
81 { |
|
82 return aSize; |
|
83 } |
|
84 |
|
85 // Last growth attempt failed so check if its worth |
|
86 // trying again |
|
87 if ( iLastGrowth < (iMinGrowth + 2)) |
|
88 { |
|
89 return 0; |
|
90 } |
|
91 |
|
92 // Last growth attempt failed so lets try a smaller |
|
93 iMaxGrowthAttempt = iMinGrowth + (iLastGrowth - iMinGrowth) / 2; |
|
94 |
|
95 return iMaxGrowthAttempt; |
|
96 } |
|
97 |
|
98 // merge new pool into the pool chain |
|
99 void RMBufPoolChain::ExtendFreeList(CMBufPool* aPool) |
|
100 { |
|
101 // The caller must already have acquired the freelist lock |
|
102 // - justification; |
|
103 // a. limitation in RWorkerLock's RFastLock which does not support nested requests by the same thread without hanging the thread |
|
104 // b. performance is adversely affected by continuoulsy locking & releasing the lock by the caller, refer ::Alloc |
|
105 // - assumption is checked for debug builds, but not for release builds as it is too expensive & indicates a serous coding error that |
|
106 // could/should of been fixed within a debug build anyhow |
|
107 __ASSERT_DEBUG(iLockFreeList.IsOwned(), CMBufManager::Panic(EMBuf_FreeLockNotOwned)); |
|
108 |
|
109 iFreeMBufs.Append(aPool->MBufQ()); |
|
110 iLenFree += aPool->Count() * iMBufSize; |
|
111 iLenTotal += aPool->Count() * iMBufSize; |
|
112 } |
|
113 |
|
114 // allocate a chain of MBufs from the pool chain |
|
115 TInt RMBufPoolChain::Alloc(RMBufQ& aList, TInt aSize, TBool aIsAllocPool) |
|
116 { |
|
117 |
|
118 // Lock the freelist so that the transfer of mbufs and statistics update is an atomic operation |
|
119 iLockFreeList.Wait(); |
|
120 |
|
121 TInt transfered = iFreeMBufs.Transfer(aList, aSize); |
|
122 iLenFree -= transfered; |
|
123 |
|
124 // If necessary gather data from the poolChain within the freelist lock to ensure integrity of statistics used |
|
125 TInt preAllocationLength = 0; |
|
126 if (aIsAllocPool) |
|
127 { |
|
128 preAllocationLength = iGrowthThreshold - (iLenFree/iMBufSize); // determine in terms of number of mbufs. |
|
129 } |
|
130 |
|
131 // adjust the growth if necessary |
|
132 TInt growth = 0; |
|
133 if (preAllocationLength >= 0) |
|
134 { |
|
135 growth = GrowthSize(preAllocationLength); // determine in terms of number of mbufs. |
|
136 } |
|
137 |
|
138 // Now free the freelist lock |
|
139 iLockFreeList.Signal(); |
|
140 |
|
141 // check if the growth threshold trigger has been exceeded, and if so perform a background pool (pre)allocation |
|
142 if (aIsAllocPool) |
|
143 { |
|
144 if (preAllocationLength >= 0) |
|
145 { |
|
146 // a failed background (pre)allocation shall not error the user since they shouldn't know (and hence care), thus |
|
147 // the return code is only used to avoid a perpetual loop |
|
148 if (growth > 0) |
|
149 { |
|
150 if (KErrNoMBufs == iMBufPoolManager.AllocPool(*this, growth, EFalse)) |
|
151 { |
|
152 iMaxGrowthAttempt = growth; |
|
153 } |
|
154 } |
|
155 } |
|
156 } |
|
157 return transfered; |
|
158 } |
|
159 |
|
160 // compare function to find the suitable pool chain |
|
161 TInt RMBufPoolChain::Compare(const TInt* aMBufSize, const RMBufPoolChain& aRHSPoolChain) |
|
162 { |
|
163 if (*aMBufSize < aRHSPoolChain.iMBufSize) |
|
164 { |
|
165 return -1; |
|
166 } |
|
167 else if (*aMBufSize > aRHSPoolChain.iMBufSize) |
|
168 { |
|
169 return 1; |
|
170 } |
|
171 return 0; |
|
172 } |
|
173 |
|
174 TInt RMBufPoolChain::Compare(const RMBufPoolChain& aLHSPoolChain, const RMBufPoolChain& aRHSPoolChain) |
|
175 { |
|
176 return Compare((TInt*)&(aLHSPoolChain.iMBufSize), aRHSPoolChain); |
|
177 } |
|
178 |
|
179 CMBufPoolManager& RMBufPoolChain::MBufPoolManager() |
|
180 { |
|
181 return iMBufPoolManager; |
|
182 } |
|
183 |
|
184 |