commsfwutils/commsbufs/mbufmgrimpl/src/MBufPoolChain.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     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 
       
    24 
       
    25 #include "MBufPoolChain.h"
       
    26 #include "MBufPool.h"
       
    27 #include "MBufPoolManager.h"	
       
    28 #include "commsbufpool.h"
       
    29 
       
    30 
       
    31 CMBufPoolChain::CMBufPoolChain(TInt aMBufSize, TInt aInitialAllocation, TInt aMinGrowth, TInt aGrowthThreshold, TInt aPoolCeiling, CMBufPoolManager& aMBufPoolManager) 
       
    32     : CCommsBufPool(aMBufPoolManager.Manager(), aMBufSize),
       
    33     iLenFree(0),
       
    34     iLenTotal(0),    
       
    35       iInitialAllocation(aInitialAllocation), 
       
    36       iMinGrowth(aMinGrowth), 
       
    37       iGrowthThreshold(aGrowthThreshold), 
       
    38       iSignalled(EFalse),
       
    39       iNumMBufsToAdd(0),
       
    40       iNoMoreSpace(0),
       
    41       iLastGrowth(0),
       
    42       iMaxGrowthAttempt(0),
       
    43       iDbgPoolLimit(0),
       
    44       iPoolCeiling(aPoolCeiling),
       
    45       iMBufPoolManager(aMBufPoolManager)      
       
    46 	{
       
    47 	}
       
    48 CMBufPoolChain::CMBufPoolChain(TInt aMBufSize, CMBufPoolManager& aMBufPoolManager)
       
    49 : CCommsBufPool(aMBufPoolManager.Manager(), aMBufSize),
       
    50  iInitialAllocation(0),
       
    51  iMBufPoolManager(aMBufPoolManager)
       
    52 	{
       
    53 		
       
    54 	}
       
    55 
       
    56 CMBufPoolChain* CMBufPoolChain::NewL(TInt aMBufSize, TInt aInitialAllocation, TInt aMinGrowth, TInt aGrowthThreshold, TInt aPoolCeiling, CMBufPoolManager& aMBufPoolManager)
       
    57 	{
       
    58 	CMBufPoolChain* self = new(ELeave)CMBufPoolChain(aMBufSize, aInitialAllocation, aMinGrowth, aGrowthThreshold, aPoolCeiling, aMBufPoolManager);
       
    59 	CleanupStack::PushL(self);
       
    60 	self->ConstructL();
       
    61 	CleanupStack::Pop(); // self
       
    62 	return self;
       
    63 	}
       
    64 
       
    65 void CMBufPoolChain::ConstructL()
       
    66 	{
       
    67 	iFreeMBufs.Init();
       
    68 	User::LeaveIfError(iLockFreeList.CreateLocal());
       
    69 	iPools.Reset();
       
    70 	iPools.SetOffset(_FOFF(CMBufPool, iLink));		
       
    71 	}
       
    72 	
       
    73 CMBufPoolChain::~CMBufPoolChain()
       
    74 	{
       
    75 	iLockFreeList.Close();
       
    76 	}
       
    77 
       
    78 
       
    79 
       
    80 // get the growth amount
       
    81 TInt CMBufPoolChain::GrowthSize(TInt aSize)
       
    82 {
       
    83 	// small request so grow by minimum size
       
    84 	if (aSize < iMinGrowth)
       
    85 		{
       
    86 		return iMinGrowth;
       
    87 		}
       
    88 	
       
    89 	// Have we tried as big as this in the past?
       
    90 	if ( (iMaxGrowthAttempt > 0) && (aSize >= iMaxGrowthAttempt) )
       
    91 		{
       
    92 		// try smaller size again
       
    93 		aSize = iMaxGrowthAttempt;
       
    94 		}
       
    95 	
       
    96 	// Attempt to grow the pool by requested amount
       
    97 	if (!iNoMoreSpace)
       
    98 		{
       
    99 		return aSize;
       
   100 		}
       
   101 	
       
   102 	// Last growth attempt failed so check if its worth
       
   103 	// trying again
       
   104 	if ( iLastGrowth < (iMinGrowth + 2))
       
   105 		{
       
   106 		return 0;
       
   107 		}
       
   108 	
       
   109 	// Last growth attempt failed so lets try a smaller
       
   110 	iMaxGrowthAttempt = iMinGrowth + (iLastGrowth - iMinGrowth) / 2;
       
   111 	
       
   112 	return iMaxGrowthAttempt;
       
   113 }
       
   114 
       
   115 // merge new pool into the pool chain
       
   116 void CMBufPoolChain::ExtendFreeList(CMBufPool* aPool)
       
   117 	{
       
   118 	// The caller must already have acquired the freelist lock
       
   119 	// - justification;
       
   120 	//   a. limitation in RWorkerLock's RFastLock which does not support nested requests by the same thread without hanging the thread
       
   121 	//   b. performance is adversely affected by continuoulsy locking & releasing the lock by the caller, refer ::Alloc
       
   122 	// - assumption is checked for debug builds, but not for release builds as it is too expensive & indicates a serous coding error that
       
   123 	//   could/should of been fixed within a debug build anyhow
       
   124 	__ASSERT_DEBUG(iLockFreeList.IsOwned(), CMBufManager::Panic(EMBuf_FreeLockNotOwned));
       
   125 	
       
   126 	iFreeMBufs.Append(aPool->MBufQ());
       
   127     iLenFree += aPool->Count() * iBufSize;
       
   128     iLenTotal += aPool->Count() * iBufSize;
       
   129 	}
       
   130 
       
   131 // allocate a chain of MBufs from the pool chain
       
   132 TInt CMBufPoolChain::Alloc(RMBufQ& aList, TInt aSize, TBool aIsAllocPool)
       
   133 	{
       
   134 
       
   135 	// Lock the freelist so that the transfer of mbufs and statistics update is an atomic operation
       
   136 	iLockFreeList.Wait();
       
   137 
       
   138 	TInt transfered = iFreeMBufs.Transfer(aList, aSize, BufSize());
       
   139 	iLenFree -= transfered;
       
   140 	
       
   141 	// If necessary gather data from the poolChain within the freelist lock to ensure integrity of statistics used
       
   142 	TInt preAllocationLength = 0;
       
   143 	if (aIsAllocPool)
       
   144 		{
       
   145 		preAllocationLength = iGrowthThreshold - (iLenFree/iBufSize); // determine in terms of number of mbufs.
       
   146 		}
       
   147 
       
   148 	// adjust the growth if necessary
       
   149 	TInt growth = 0;
       
   150 	if (preAllocationLength >= 0)
       
   151 		{
       
   152 		growth = GrowthSize(preAllocationLength);
       
   153 		}
       
   154 	
       
   155 	// Now free the freelist lock
       
   156 	iLockFreeList.Signal();
       
   157 		
       
   158 	// check if the growth threshold trigger has been exceeded, and if so perform a background pool (pre)allocation
       
   159 	if (aIsAllocPool)
       
   160 		{
       
   161 		if (preAllocationLength >= 0)
       
   162 			{
       
   163 			// a failed background (pre)allocation shall not error the user since they shouldn't know (and hence care), thus
       
   164 			// the return code is only used to avoid a perpetual loop
       
   165 			if (growth > 0)
       
   166 				{
       
   167 				if (KErrNoMBufs == iMBufPoolManager.AllocPool(*this, growth, EFalse))
       
   168 					{
       
   169 					iMaxGrowthAttempt = growth;
       
   170 					}
       
   171 				}
       
   172 			}
       
   173 		}
       
   174 	return transfered;
       
   175 	}
       
   176 
       
   177 // compare function to find the suitable pool chain
       
   178 TInt CMBufPoolChain::Compare(const TInt* aMBufSize, const CMBufPoolChain& aRHSPoolChain)
       
   179 	{
       
   180 	if (*aMBufSize < aRHSPoolChain.BufSize())
       
   181 		{
       
   182 		return -1;
       
   183 		}
       
   184 	else if (*aMBufSize > aRHSPoolChain.BufSize())
       
   185 		{
       
   186 		return 1;
       
   187 		}
       
   188 	return 0;
       
   189 	}
       
   190 
       
   191 TInt CMBufPoolChain::Compare(const CMBufPoolChain& aLHSPoolChain, const CMBufPoolChain& aRHSPoolChain)
       
   192 	{
       
   193 	return Compare((TInt*)&(aLHSPoolChain.iBufSize), aRHSPoolChain);	
       
   194 	}
       
   195 
       
   196 CMBufPoolManager& CMBufPoolChain::MBufPoolManager()
       
   197 	{
       
   198 	return iMBufPoolManager;
       
   199 	}
       
   200 
       
   201 TBool CMBufPoolChain::CanExtend() const
       
   202 	{
       
   203 	return iLenTotal < iPoolCeiling;		
       
   204 	}
       
   205 	
       
   206 TInt CMBufPoolChain::ExtendSize() const
       
   207 	{
       
   208 	if(iLenTotal == 0)
       
   209 		{
       
   210 		// Always return the initial allocation value if we haven't allocated anything yet.
       
   211 		return iInitialAllocation;			
       
   212 		}
       
   213 
       
   214 	
       
   215 	TInt toGrow = (iPoolCeiling - iLenTotal)/iBufSize;	
       
   216 	return (toGrow < iMinGrowth) ? toGrow : iMinGrowth ;	
       
   217 	}
       
   218 
       
   219 TBool CMBufPoolChain::HonourPoolCeiling() const
       
   220 	{
       
   221 	return iPoolCeiling != KErrNotFound;	
       
   222 	}
       
   223 
       
   224