userlibandfileserver/fileserver/sfile/sf_pool.cpp
changeset 0 a41df078684a
child 42 a179b74831c9
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 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 the License "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 // f32\sfile\sf_pool.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "sf_pool.h"
       
    19 #ifdef SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
       
    20 #include "sf_notifier.h"
       
    21 #endif //SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
       
    22 
       
    23 //=====CFsPool=============================
       
    24 
       
    25 template <class T>
       
    26 CFsPool<T>* CFsPool<T>::New(TInt aPoolSize)
       
    27 	{
       
    28 	CFsPool<T>* pool = new CFsPool<T>();
       
    29 	if(!pool)
       
    30 		return NULL;
       
    31 	
       
    32 	TInt r = pool->Construct(aPoolSize);
       
    33 	if(r!=KErrNone)
       
    34 		{
       
    35 		delete pool;
       
    36 		return NULL;
       
    37 		}
       
    38 	else
       
    39 		return pool;
       
    40 	}
       
    41 
       
    42 
       
    43 template <class T>
       
    44 CFsPool<T>::CFsPool()
       
    45 	{
       
    46 	}
       
    47 
       
    48 template <class T>
       
    49 TInt CFsPool<T>::Construct(TInt aPoolSize)
       
    50 	{
       
    51 	TInt r = iPoolLock.CreateLocal(KNotificationPoolSize);
       
    52 	if(r != KErrNone)
       
    53 			return r;
       
    54 	
       
    55 	r = iFreeList.Reserve(KNotificationPoolSize);
       
    56 	if(r != KErrNone)
       
    57 		return r;
       
    58 	
       
    59 	TInt i = 0;
       
    60 	while(i < aPoolSize)
       
    61 		{
       
    62 		T* t = T::New();
       
    63 		if(!t)
       
    64 			{
       
    65 			return KErrNoMemory;
       
    66 			}
       
    67 		iFreeList.Append(t);	
       
    68 		i++;
       
    69 		}
       
    70 	
       
    71 	return KErrNone; 
       
    72 	}
       
    73 
       
    74 //This should only be called by the Manager when it holds
       
    75 //the manager's write lock meaning that all of the
       
    76 //blocks should be unallocated 
       
    77 template <class T>
       
    78 CFsPool<T>::~CFsPool()
       
    79 	{
       
    80 	for(TInt i=0; i < iFreeList.Count(); i++)
       
    81 		{
       
    82 		delete iFreeList[i];
       
    83 		}
       
    84 	iFreeList.Close();
       
    85 	}
       
    86 
       
    87 
       
    88 template <class T>
       
    89 T* CFsPool<T>::Allocate()
       
    90 	{
       
    91 	Lock(); //Waits when there are no free blocks left
       
    92 
       
    93 	TInt lastIndex = iFreeList.Count()-1;
       
    94 	T* t = iFreeList[lastIndex];
       
    95 	iFreeList.Remove(lastIndex);
       
    96 	
       
    97 	return t;
       
    98 	}
       
    99 
       
   100 template <class T>
       
   101 void CFsPool<T>::Free(T* aBlock)
       
   102 	{
       
   103 	iFreeList.Append(aBlock);
       
   104 	Unlock();
       
   105 	}
       
   106 
       
   107 template <class T>
       
   108 void CFsPool<T>::Lock()
       
   109 	{
       
   110 	iPoolLock.Wait();
       
   111 	}
       
   112 
       
   113 template <class T>
       
   114 void CFsPool<T>::Unlock()
       
   115 	{
       
   116 	iPoolLock.Signal();
       
   117 	}
       
   118 
       
   119 #ifdef SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
       
   120 //This is needed here because the compiler needs to know which types will be 
       
   121 //instantiating the template (because it's in a separate file)
       
   122 template class CFsPool<CFsNotificationBlock>;
       
   123 #endif //SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
       
   124