os/kernelhwsrv/userlibandfileserver/fileserver/sfile/sf_pool.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/userlibandfileserver/fileserver/sfile/sf_pool.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,124 @@
     1.4 +// Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// f32\sfile\sf_pool.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include "sf_pool.h"
    1.22 +#ifdef SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
    1.23 +#include "sf_notifier.h"
    1.24 +#endif //SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
    1.25 +
    1.26 +//=====CFsPool=============================
    1.27 +
    1.28 +template <class T>
    1.29 +CFsPool<T>* CFsPool<T>::New(TInt aPoolSize)
    1.30 +	{
    1.31 +	CFsPool<T>* pool = new CFsPool<T>();
    1.32 +	if(!pool)
    1.33 +		return NULL;
    1.34 +	
    1.35 +	TInt r = pool->Construct(aPoolSize);
    1.36 +	if(r!=KErrNone)
    1.37 +		{
    1.38 +		delete pool;
    1.39 +		return NULL;
    1.40 +		}
    1.41 +	else
    1.42 +		return pool;
    1.43 +	}
    1.44 +
    1.45 +
    1.46 +template <class T>
    1.47 +CFsPool<T>::CFsPool()
    1.48 +	{
    1.49 +	}
    1.50 +
    1.51 +template <class T>
    1.52 +TInt CFsPool<T>::Construct(TInt aPoolSize)
    1.53 +	{
    1.54 +	TInt r = iPoolLock.CreateLocal(KNotificationPoolSize);
    1.55 +	if(r != KErrNone)
    1.56 +			return r;
    1.57 +	
    1.58 +	r = iFreeList.Reserve(KNotificationPoolSize);
    1.59 +	if(r != KErrNone)
    1.60 +		return r;
    1.61 +	
    1.62 +	TInt i = 0;
    1.63 +	while(i < aPoolSize)
    1.64 +		{
    1.65 +		T* t = T::New();
    1.66 +		if(!t)
    1.67 +			{
    1.68 +			return KErrNoMemory;
    1.69 +			}
    1.70 +		iFreeList.Append(t);	
    1.71 +		i++;
    1.72 +		}
    1.73 +	
    1.74 +	return KErrNone; 
    1.75 +	}
    1.76 +
    1.77 +//This should only be called by the Manager when it holds
    1.78 +//the manager's write lock meaning that all of the
    1.79 +//blocks should be unallocated 
    1.80 +template <class T>
    1.81 +CFsPool<T>::~CFsPool()
    1.82 +	{
    1.83 +	for(TInt i=0; i < iFreeList.Count(); i++)
    1.84 +		{
    1.85 +		delete iFreeList[i];
    1.86 +		}
    1.87 +	iFreeList.Close();
    1.88 +	}
    1.89 +
    1.90 +
    1.91 +template <class T>
    1.92 +T* CFsPool<T>::Allocate()
    1.93 +	{
    1.94 +	Lock(); //Waits when there are no free blocks left
    1.95 +
    1.96 +	TInt lastIndex = iFreeList.Count()-1;
    1.97 +	T* t = iFreeList[lastIndex];
    1.98 +	iFreeList.Remove(lastIndex);
    1.99 +	
   1.100 +	return t;
   1.101 +	}
   1.102 +
   1.103 +template <class T>
   1.104 +void CFsPool<T>::Free(T* aBlock)
   1.105 +	{
   1.106 +	iFreeList.Append(aBlock);
   1.107 +	Unlock();
   1.108 +	}
   1.109 +
   1.110 +template <class T>
   1.111 +void CFsPool<T>::Lock()
   1.112 +	{
   1.113 +	iPoolLock.Wait();
   1.114 +	}
   1.115 +
   1.116 +template <class T>
   1.117 +void CFsPool<T>::Unlock()
   1.118 +	{
   1.119 +	iPoolLock.Signal();
   1.120 +	}
   1.121 +
   1.122 +#ifdef SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
   1.123 +//This is needed here because the compiler needs to know which types will be 
   1.124 +//instantiating the template (because it's in a separate file)
   1.125 +template class CFsPool<CFsNotificationBlock>;
   1.126 +#endif //SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
   1.127 +