os/kernelhwsrv/kernel/eka/memmodel/epoc/flexible/mmu/maddrcont.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2007-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
//
sl@0
    15
sl@0
    16
#ifndef MADDRCONT_H
sl@0
    17
#define MADDRCONT_H
sl@0
    18
sl@0
    19
/**
sl@0
    20
A container for storing objects keyed on a virtual address.
sl@0
    21
*/
sl@0
    22
class RAddressedContainer
sl@0
    23
	{
sl@0
    24
public:
sl@0
    25
	/**
sl@0
    26
	@param aReadLock	Fast mutex used to synchronise read operations,
sl@0
    27
					 	i.e. the Find functions. This may be the null pointer to
sl@0
    28
						indicate that extra locking is required.
sl@0
    29
	@param aWriteLock	Reference to the mutex used to synchronise write operations,
sl@0
    30
					 	i.e. #Add and Remove functions. This mutex if not used by
sl@0
    31
						the RAddressedContainer class but it is used for asserting
sl@0
    32
						correct preconditions in debug builds
sl@0
    33
	*/
sl@0
    34
	RAddressedContainer(NFastMutex* aReadLock, DMutex*& aWriteLock);
sl@0
    35
sl@0
    36
	~RAddressedContainer();
sl@0
    37
sl@0
    38
	/**
sl@0
    39
	Add an object to the container.
sl@0
    40
sl@0
    41
	@param aAddress The address key for the object.
sl@0
    42
	@param aObject  Pointer to the object to add. This may not be the null pointer.
sl@0
    43
sl@0
    44
	@pre The write lock must be held.
sl@0
    45
	*/
sl@0
    46
	TInt Add(TLinAddr aAddress, TAny* aObject);
sl@0
    47
sl@0
    48
	/**
sl@0
    49
	Remove an object from the container.
sl@0
    50
sl@0
    51
	@param aAddress The address key for the object.
sl@0
    52
sl@0
    53
	@return The pointer of the object removed, or the null pointer if there
sl@0
    54
			was none with the specified address key.
sl@0
    55
sl@0
    56
	@pre The write lock must be held.
sl@0
    57
	*/
sl@0
    58
	TAny* Remove(TLinAddr aAddress);
sl@0
    59
sl@0
    60
	/**
sl@0
    61
	Find an object in the container.
sl@0
    62
sl@0
    63
	@param aAddress The address key for the object.
sl@0
    64
sl@0
    65
	@return The pointer of the object found, or the null pointer if there
sl@0
    66
			was none with the specified address key.
sl@0
    67
sl@0
    68
	@pre The read lock must be held, or if there is no read lock, the write lock must be held.
sl@0
    69
	*/
sl@0
    70
	TAny* Find(TLinAddr aAddress);
sl@0
    71
sl@0
    72
	/**
sl@0
    73
	Find the object in the container which has the highest value
sl@0
    74
	address key less-than-or-equal to the specified address.
sl@0
    75
sl@0
    76
	@param aAddress The address key to search on.
sl@0
    77
sl@0
    78
	@param[out] aOffset Reference to an value which will be set to the difference
sl@0
    79
						between \a aAddress and the address key of the found object.
sl@0
    80
sl@0
    81
	@return The pointer of the object found, or the null pointer if there
sl@0
    82
			was none matching the search criteria.
sl@0
    83
sl@0
    84
	@pre The read lock must be held, or if there is no read lock, the write lock must be held.
sl@0
    85
	*/
sl@0
    86
	TAny* Find(TLinAddr aAddress, TUint& aOffset);
sl@0
    87
sl@0
    88
	/** Acquire the read lock. */
sl@0
    89
	FORCE_INLINE void ReadLock()
sl@0
    90
		{
sl@0
    91
		if(iReadLock)
sl@0
    92
			NKern::FMWait(iReadLock);
sl@0
    93
		}
sl@0
    94
sl@0
    95
	/** Release the read lock. */
sl@0
    96
	FORCE_INLINE void ReadUnlock()
sl@0
    97
		{
sl@0
    98
		if(iReadLock)
sl@0
    99
			NKern::FMSignal(iReadLock);
sl@0
   100
		}
sl@0
   101
sl@0
   102
	/** Flash (release and re-acquire) the read lock. */
sl@0
   103
	FORCE_INLINE void ReadFlash()
sl@0
   104
		{
sl@0
   105
		if(iReadLock)
sl@0
   106
			NKern::FMFlash(iReadLock);
sl@0
   107
		}
sl@0
   108
sl@0
   109
	/** The number of objects in the container. */
sl@0
   110
	FORCE_INLINE TUint Count()
sl@0
   111
		{
sl@0
   112
		return iCount;
sl@0
   113
		}
sl@0
   114
private:
sl@0
   115
	TUint FindIndex(TLinAddr aAddress);
sl@0
   116
	TUint CalculateGrow();
sl@0
   117
	TUint CalculateShrink(TUint aCount);
sl@0
   118
	TBool CheckWriteLock();
sl@0
   119
sl@0
   120
	class TEntry
sl@0
   121
		{
sl@0
   122
	public:
sl@0
   123
		TLinAddr	iAddress;
sl@0
   124
		TAny*		iObject;
sl@0
   125
		};
sl@0
   126
private:
sl@0
   127
	TUint iMaxCount;
sl@0
   128
	TUint iCount;
sl@0
   129
	TEntry* iList;
sl@0
   130
	NFastMutex* iReadLock;
sl@0
   131
	DMutex*& iWriteLock;
sl@0
   132
	};
sl@0
   133
sl@0
   134
#endif // MADDRCONT_H