servicediscoveryandcontrol/pnp/test/upnp/chunkmgr/src/rchunkpool.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 // Copyright (c) 2008-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 // @file
       
    15 // @internalComponent
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32base.h>
       
    20 #include "cchunkmanager.h"
       
    21 #include "rmemcell.h"
       
    22 #include "cmemchunk.h"
       
    23 #include "rchunkpool.h"
       
    24 
       
    25 
       
    26 RChunkPool::RChunkPool ( TInt aBlockSize, TInt aThresholdValue, TInt aMinGrowth, CChunkManager& aChunkManager )
       
    27 		: iBlockSize ( aBlockSize ),
       
    28 	      iChunkManager ( aChunkManager ),
       
    29 		  iThresholdValue ( aThresholdValue ),
       
    30 	      iMinGrowth ( aMinGrowth )
       
    31 	{
       
    32 	iFreeBlocks.Init ();
       
    33 	iFreeSpace = 0;
       
    34 	}
       
    35 
       
    36 RChunkPool::RChunkPool ( CChunkManager& aChunkManager, TInt aBlockSize )
       
    37 	: iBlockSize ( aBlockSize ),
       
    38 	  iChunkManager ( aChunkManager )
       
    39 	{	
       
    40 	}
       
    41 	
       
    42 void RChunkPool::OpenL ()
       
    43 	{
       
    44 	User::LeaveIfError ( iFreeListLock.CreateLocal () );
       
    45 	}
       
    46 	
       
    47 void RChunkPool::Close ()
       
    48 	{
       
    49 	iFreeListLock.Close ();
       
    50 	iAllocatedChunks.Close ();
       
    51 	}
       
    52 	
       
    53 TInt RChunkPool::Compare ( const RChunkPool& aLHSPool, const RChunkPool& aRHSPool )
       
    54 	{
       
    55 	if ( aLHSPool.BlockSize () < aRHSPool.BlockSize () )
       
    56 		{
       
    57 		return -1;
       
    58 		}
       
    59 	else if ( aLHSPool.BlockSize () > aRHSPool.BlockSize ()  )
       
    60 		{
       
    61 		return 1;
       
    62 		}
       
    63 	return 0;
       
    64 	}
       
    65 		
       
    66 void RChunkPool::AppendToFreeList ( CMemChunk* aChunk )
       
    67 	{
       
    68 	iFreeSpace += iBlockSize * aChunk->NumBlocks ();
       
    69 	iFreeBlocks.Append ( aChunk->List () );
       
    70 	}	
       
    71 
       
    72 
       
    73 // transfer's blocks from free list and makes self growth if necessary
       
    74 TInt RChunkPool::Alloc ( RMemCellList& aList, TInt aSize )
       
    75 	{
       
    76 	// Lock the freelist so that 
       
    77 	// Transfer of free blocks will be an atomic operation
       
    78 	iFreeListLock.Wait ();
       
    79 	
       
    80 	TInt bytesTransfered = iFreeBlocks.Transfer ( aList, aSize );
       
    81 	iFreeSpace -= bytesTransfered;
       
    82 	
       
    83 	TInt freeHeapSpace = iChunkManager.BytesAvailable ();
       
    84 	
       
    85 	if ( freeHeapSpace ) // makes sure that heap has memory atleast for CMemChunk object, but not for its chunk
       
    86 		{
       
    87 		// check if freeblocks is down the threshold value and make self growth if necessary
       
    88 		// determine min number of blocks to allocate
       
    89 		TInt minGrowth = iThresholdValue - ( iFreeSpace / iBlockSize );
       
    90 		
       
    91 		if ( minGrowth >= 0 )
       
    92 			{
       
    93 			if ( GrowPool ( iMinGrowth ) == KErrNoMemory )
       
    94 				{
       
    95 			 	// unable to grow the minimum value set by the client
       
    96 			 	if ( minGrowth < iMinGrowth )
       
    97 				 	{
       
    98 			 		// so, now try with the min value adjusted with threshold value
       
    99 			 		GrowPool ( minGrowth );
       
   100 				 	}
       
   101 			 	}
       
   102 			}
       
   103 		}
       
   104 	
       
   105 	// free the freelistlock
       
   106 	iFreeListLock.Signal ();
       
   107 	
       
   108 	return bytesTransfered;
       
   109 	}
       
   110 	
       
   111 TInt RChunkPool::GrowPool ( TUint aNumBlocks )
       
   112 	{
       
   113 	return iChunkManager.AllocChunk ( *this, aNumBlocks );
       
   114 	}
       
   115 	
       
   116 TInt RChunkPool::GrowAndAlloc ( RMemCellList& aList, TInt aSize, TInt aNumBlocks )
       
   117 	{
       
   118 	// Lock the freelist so that
       
   119 	// Transfer of free blocks will be an atomic operation
       
   120 	TInt bytesTransfered = 0;
       
   121 	iFreeListLock.Wait ();
       
   122 	
       
   123 	if ( GrowPool ( aNumBlocks ) != KErrNoMemory )
       
   124 	 	{
       
   125 	 	bytesTransfered = iFreeBlocks.Transfer ( aList, aSize );
       
   126 		iFreeSpace -= bytesTransfered;
       
   127 	 	}
       
   128 	
       
   129 	// free the freelistlock
       
   130 	iFreeListLock.Signal ();
       
   131 	
       
   132 	return bytesTransfered;
       
   133 	}
       
   134 	
       
   135 void RChunkPool::FreeToPool ( RMemCell* aMemCell )
       
   136 	{
       
   137 	iFreeBlocks.Append ( aMemCell );
       
   138 	iFreeSpace += aMemCell->Size ();
       
   139 	}
       
   140 
       
   141