os/kernelhwsrv/userlibandfileserver/fileserver/sfile/sf_pool.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// f32\sfile\sf_pool.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "sf_pool.h"
sl@0
    19
#ifdef SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
sl@0
    20
#include "sf_notifier.h"
sl@0
    21
#endif //SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
sl@0
    22
sl@0
    23
//=====CFsPool=============================
sl@0
    24
sl@0
    25
template <class T>
sl@0
    26
CFsPool<T>* CFsPool<T>::New(TInt aPoolSize)
sl@0
    27
	{
sl@0
    28
	CFsPool<T>* pool = new CFsPool<T>();
sl@0
    29
	if(!pool)
sl@0
    30
		return NULL;
sl@0
    31
	
sl@0
    32
	TInt r = pool->Construct(aPoolSize);
sl@0
    33
	if(r!=KErrNone)
sl@0
    34
		{
sl@0
    35
		delete pool;
sl@0
    36
		return NULL;
sl@0
    37
		}
sl@0
    38
	else
sl@0
    39
		return pool;
sl@0
    40
	}
sl@0
    41
sl@0
    42
sl@0
    43
template <class T>
sl@0
    44
CFsPool<T>::CFsPool()
sl@0
    45
	{
sl@0
    46
	}
sl@0
    47
sl@0
    48
template <class T>
sl@0
    49
TInt CFsPool<T>::Construct(TInt aPoolSize)
sl@0
    50
	{
sl@0
    51
	TInt r = iPoolLock.CreateLocal(KNotificationPoolSize);
sl@0
    52
	if(r != KErrNone)
sl@0
    53
			return r;
sl@0
    54
	
sl@0
    55
	r = iFreeList.Reserve(KNotificationPoolSize);
sl@0
    56
	if(r != KErrNone)
sl@0
    57
		return r;
sl@0
    58
	
sl@0
    59
	TInt i = 0;
sl@0
    60
	while(i < aPoolSize)
sl@0
    61
		{
sl@0
    62
		T* t = T::New();
sl@0
    63
		if(!t)
sl@0
    64
			{
sl@0
    65
			return KErrNoMemory;
sl@0
    66
			}
sl@0
    67
		iFreeList.Append(t);	
sl@0
    68
		i++;
sl@0
    69
		}
sl@0
    70
	
sl@0
    71
	return KErrNone; 
sl@0
    72
	}
sl@0
    73
sl@0
    74
//This should only be called by the Manager when it holds
sl@0
    75
//the manager's write lock meaning that all of the
sl@0
    76
//blocks should be unallocated 
sl@0
    77
template <class T>
sl@0
    78
CFsPool<T>::~CFsPool()
sl@0
    79
	{
sl@0
    80
	for(TInt i=0; i < iFreeList.Count(); i++)
sl@0
    81
		{
sl@0
    82
		delete iFreeList[i];
sl@0
    83
		}
sl@0
    84
	iFreeList.Close();
sl@0
    85
	}
sl@0
    86
sl@0
    87
sl@0
    88
template <class T>
sl@0
    89
T* CFsPool<T>::Allocate()
sl@0
    90
	{
sl@0
    91
	Lock(); //Waits when there are no free blocks left
sl@0
    92
sl@0
    93
	TInt lastIndex = iFreeList.Count()-1;
sl@0
    94
	T* t = iFreeList[lastIndex];
sl@0
    95
	iFreeList.Remove(lastIndex);
sl@0
    96
	
sl@0
    97
	return t;
sl@0
    98
	}
sl@0
    99
sl@0
   100
template <class T>
sl@0
   101
void CFsPool<T>::Free(T* aBlock)
sl@0
   102
	{
sl@0
   103
	iFreeList.Append(aBlock);
sl@0
   104
	Unlock();
sl@0
   105
	}
sl@0
   106
sl@0
   107
template <class T>
sl@0
   108
void CFsPool<T>::Lock()
sl@0
   109
	{
sl@0
   110
	iPoolLock.Wait();
sl@0
   111
	}
sl@0
   112
sl@0
   113
template <class T>
sl@0
   114
void CFsPool<T>::Unlock()
sl@0
   115
	{
sl@0
   116
	iPoolLock.Signal();
sl@0
   117
	}
sl@0
   118
sl@0
   119
#ifdef SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
sl@0
   120
//This is needed here because the compiler needs to know which types will be 
sl@0
   121
//instantiating the template (because it's in a separate file)
sl@0
   122
template class CFsPool<CFsNotificationBlock>;
sl@0
   123
#endif //SYMBIAN_F32_ENHANCED_CHANGE_NOTIFICATION	
sl@0
   124