os/persistentdata/persistentstorage/store/INC/S32PAGE.H
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/store/INC/S32PAGE.H	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,283 @@
     1.4 +// Copyright (c) 1998-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 "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 +//
    1.18 +
    1.19 +#if !defined(__S32PAGE_H__)
    1.20 +#define __S32PAGE_H__
    1.21 +#if !defined(__S32STRM_H__)
    1.22 +#include <s32strm.h>
    1.23 +#endif
    1.24 +
    1.25 +/** Size of the pages in the page pool. */
    1.26 +const TInt KPoolPageSize=512;
    1.27 +//
    1.28 +const TUint32 KNullPageRefValue=0;
    1.29 +
    1.30 +/**
    1.31 + * @publishedAll 
    1.32 + * @released
    1.33 + * Encapsulates a page reference.
    1.34 +
    1.35 +A page reference is an integer value that can be used to identify a page. 
    1.36 + */
    1.37 +class TPageRef
    1.38 +	{
    1.39 +public:
    1.40 +	/** Default constructor. */
    1.41 +	inline TPageRef() {}
    1.42 +	inline TPageRef(TUint32 aValue);
    1.43 +//
    1.44 +	inline TBool operator==(TPageRef aRef) const;
    1.45 +	inline TBool operator!=(TPageRef aRef) const;
    1.46 +//
    1.47 +	inline void ExternalizeL(RWriteStream& aStream) const;
    1.48 +	inline void InternalizeL(RReadStream& aStream);
    1.49 +//
    1.50 +	inline TUint32 Value() const;
    1.51 +private:
    1.52 +	TUint32 iVal;
    1.53 +	};
    1.54 +#if defined(__NO_CLASS_CONSTS__)
    1.55 +#define KNullPageRef TPageRef(KNullPageRefValue)
    1.56 +#else
    1.57 +/** Defines a null page reference. */
    1.58 +const TPageRef KNullPageRef=TPageRef(KNullPageRefValue);
    1.59 +#endif
    1.60 +
    1.61 +class MPagePool;
    1.62 +/** Typedef to define a function that abandons pages in page pool. It is used by 
    1.63 +MPagePool::AcquireL().
    1.64 +
    1.65 +@see MPagePool::AcquireL() */
    1.66 +typedef void (*TPageAbandonFunction)(MPagePool& aPool);
    1.67 +
    1.68 +/** Flags that define how allocated pages can be reclaimed.
    1.69 +
    1.70 +A BTree can locate some pages even when the tree is broken, but not all. The 
    1.71 +ones it cannot track must be tracked by the page pool in order to reclaim 
    1.72 +the storage if the tree breaks. */
    1.73 +enum TPageReclamation 
    1.74 +	/** The page can be deleted, but its space not reclaimed.
    1.75 +	
    1.76 +	The page pool will not track these pages, so to retrieve the space the page 
    1.77 +	must be deleted explicitly. */
    1.78 +	{EPageDeleteOnly,
    1.79 +	/** Page can be reclaimed.
    1.80 +	
    1.81 +	The page pool will track these pages, and will be able to reclaim the pages 
    1.82 +	when, for example, RStorePagePool::ReclaimL() is called. */
    1.83 +	EPageReclaimable};
    1.84 +
    1.85 +/** Flags that define how a page should be treated when it is unlocked. */
    1.86 +enum TPageChange 
    1.87 +	/** Unlock only. */
    1.88 +	{EPageNoChange,
    1.89 +	/** Mark the page as dirty. */
    1.90 +	EPageDirty,
    1.91 +	/** Mark the page as needing a safe update. */
    1.92 +	EPageUpdate,
    1.93 +	/** Discard the page. */
    1.94 +	EPageAbandon=-1};
    1.95 +
    1.96 +
    1.97 +/**
    1.98 + * @publishedAll 
    1.99 + * @released
   1.100 + * Interface to a page pool, the storage abstraction used by the B-trees API. 
   1.101 +
   1.102 +The interface is abstract and handles pages as TAny pointers. It is left to 
   1.103 +derived classes to implement page storage in a particular storage medium, 
   1.104 +such as memory or disk.  
   1.105 +*/
   1.106 +class MPagePool
   1.107 +	{
   1.108 +public:
   1.109 +	IMPORT_C void PushL();
   1.110 +	inline void Pop();
   1.111 +
   1.112 +	/** Returns a function that abandons all locked pages for this page pool. 
   1.113 +	
   1.114 +	@return A function that abandons all locked pages for this page pool. */
   1.115 +	virtual TPageAbandonFunction AcquireL()=0;
   1.116 +//
   1.117 +	virtual TAny* AllocL()=0;
   1.118 +
   1.119 +	/** Locks a page and returns a pointer to it.
   1.120 +	
   1.121 +	@param aRef Reference to the page to lock
   1.122 +	@return Locked page */
   1.123 +	virtual TAny* LockL(TPageRef aRef)=0;
   1.124 +//
   1.125 +	virtual TPageRef AssignL(const TAny* aPage,TPageReclamation aReclamation=EPageDeleteOnly)=0;
   1.126 +
   1.127 +	/** Updates a page. 
   1.128 +	
   1.129 +	This can be used for cached pages that may have become outdated.
   1.130 +	
   1.131 +	@param aPage Page to update */
   1.132 +	virtual void UpdateL(const TAny* aPage)=0;
   1.133 +
   1.134 +	/** Unlocks a page.
   1.135 +	
   1.136 +	@param aPage Page to unlock
   1.137 +	@param aChange How the page should be treated once it is unlocked */
   1.138 +	virtual void Unlock(const TAny* aPage,TPageChange aChange=EPageNoChange)=0;
   1.139 +//
   1.140 +	IMPORT_C void Delete(TPageRef aRef);
   1.141 +	IMPORT_C void DeleteL(TPageRef aRef);
   1.142 +protected:
   1.143 +	virtual void DoDeleteL(TPageRef aRef)=0;
   1.144 +	};
   1.145 +
   1.146 +/**
   1.147 + * @publishedAll 
   1.148 + * @released
   1.149 + * Uses memory to implement the MPagePool page pool interface.
   1.150 +
   1.151 +The class allocates pages from the default heap, storing them in an array. 
   1.152 +This pool is not persistent.
   1.153 + */
   1.154 +class CMemPagePool : public CBase,public MPagePool
   1.155 +	{
   1.156 +public:
   1.157 +	IMPORT_C static CMemPagePool* NewL();
   1.158 +	IMPORT_C static CMemPagePool* NewLC();
   1.159 +	IMPORT_C CMemPagePool();
   1.160 +	IMPORT_C ~CMemPagePool();
   1.161 +//
   1.162 +	IMPORT_C TPageAbandonFunction AcquireL();
   1.163 +	IMPORT_C TAny* AllocL();
   1.164 +	IMPORT_C TAny* LockL(TPageRef aRef);
   1.165 +	IMPORT_C TPageRef AssignL(const TAny* aPage,TPageReclamation aReclamation=EPageDeleteOnly);
   1.166 +	IMPORT_C void UpdateL(const TAny* aPage);
   1.167 +	IMPORT_C void Unlock(const TAny* aPage,TPageChange aChange=EPageNoChange);
   1.168 +protected:
   1.169 +	IMPORT_C void DoDeleteL(TPageRef aRef);
   1.170 +private:
   1.171 +	TAny*& PageL(TPageRef aRef);
   1.172 +	static void DoAbandon(MPagePool& aPool);
   1.173 +private:
   1.174 +	CArrayFixSeg<TAny*> iPages;
   1.175 +	};
   1.176 +//
   1.177 +#if defined(_DEBUG)&&!defined(__PAGE_CACHE_STATS)
   1.178 +//#define __PAGE_CACHE_STATS
   1.179 +#endif
   1.180 +//
   1.181 +class TCachePage;
   1.182 +struct SCachePage;
   1.183 +class TCachePagePool;
   1.184 +
   1.185 +/**
   1.186 + * @publishedAll 
   1.187 + * @released
   1.188 + * Provides a cache for page pools. 
   1.189 +
   1.190 +Persistent page pools rely on a cache to provide in-memory space for their 
   1.191 +pages and to cache frequently accessed pages.  
   1.192 +*/
   1.193 +class CPageCache : public CBase
   1.194 +	{
   1.195 +public:
   1.196 +	enum {EDefaultPages=20};
   1.197 +#if defined(__PAGE_CACHE_STATS)
   1.198 +	
   1.199 +	/**
   1.200 +	 * @publishedAll 
   1.201 +	 * @released
   1.202 +	 */
   1.203 +	class TStats
   1.204 +		{
   1.205 +	public:
   1.206 +		inline TInt Requests() const;
   1.207 +		inline TInt Hits() const;
   1.208 +		inline TInt Misses() const;
   1.209 +		inline void Reset();
   1.210 +	private:
   1.211 +		inline void Hit();
   1.212 +		inline void Miss();
   1.213 +	private:
   1.214 +		TInt iRequests;
   1.215 +		TInt iMisses;
   1.216 +	private:
   1.217 +		friend class CPageCache;
   1.218 +		};
   1.219 +#endif
   1.220 +public:
   1.221 +	IMPORT_C static CPageCache* NewL(TInt aPages=EDefaultPages);
   1.222 +	IMPORT_C static CPageCache* NewLC(TInt aPages=EDefaultPages);
   1.223 +	IMPORT_C CPageCache();
   1.224 +	IMPORT_C void ConstructL(TInt aPages=EDefaultPages);
   1.225 +	IMPORT_C ~CPageCache();
   1.226 +//
   1.227 +#if defined(__PAGE_CACHE_STATS)
   1.228 +	inline TStats& Stats();
   1.229 +	inline const TStats& Stats() const;
   1.230 +#endif
   1.231 +private:
   1.232 +	TCachePage* Find(TCachePagePool* aPool,TPageRef aRef);
   1.233 +private:
   1.234 +	SCachePage* iPages;
   1.235 +	SCachePage* iEnd;
   1.236 +	TDblQue<TCachePage> iFree;
   1.237 +#if defined(__PAGE_CACHE_STATS)
   1.238 +	TStats iStats;
   1.239 +#endif
   1.240 +private:
   1.241 +	friend class TCachePagePool;
   1.242 +	};
   1.243 +
   1.244 +/**
   1.245 + * @publishedAll 
   1.246 + * @released
   1.247 + * Provides a page pool with cached pages.
   1.248 +
   1.249 +It is an intermediary class, used in the definition of page pools that use 
   1.250 +a cache, such as RFilePagePool and RStorePagePool.
   1.251 +
   1.252 +@see RFilePagePool
   1.253 +@see RStorePagePool  
   1.254 +*/
   1.255 +class TCachePagePool : public MPagePool
   1.256 +	{
   1.257 +public:
   1.258 +	inline void Set(CPageCache& aCache);
   1.259 +//
   1.260 +	IMPORT_C TPageAbandonFunction AcquireL();
   1.261 +	IMPORT_C TAny* AllocL();
   1.262 +	IMPORT_C TAny* LockL(TPageRef aRef);
   1.263 +	IMPORT_C TPageRef AssignL(const TAny* aPage,TPageReclamation aReclamation=EPageDeleteOnly);
   1.264 +	IMPORT_C void UpdateL(const TAny* aPage);
   1.265 +	IMPORT_C void Unlock(const TAny* aPage,TPageChange aChange=EPageNoChange);
   1.266 +//
   1.267 +	IMPORT_C TInt Flush();
   1.268 +	IMPORT_C void FlushL();
   1.269 +	IMPORT_C void Purge();
   1.270 +protected:
   1.271 +	inline TCachePagePool();
   1.272 +	inline TCachePagePool(CPageCache& aCache);
   1.273 +	IMPORT_C void DoDeleteL(TPageRef aRef);
   1.274 +private:
   1.275 +	virtual TPageRef ExtendL(const TAny* aPage,TPageReclamation aReclamation)=0;
   1.276 +	virtual void WriteL(TPageRef aRef,const TAny* aPage,TPageChange aChange)=0;
   1.277 +	virtual void ReadL(TPageRef aRef,TAny* aPage)=0;
   1.278 +//
   1.279 +	static void DoAbandon(MPagePool& aPool);
   1.280 +	static TCachePage* DoAllocL(CPageCache& aCache);
   1.281 +private:
   1.282 +	CPageCache* iCache;
   1.283 +	};
   1.284 +
   1.285 +#include <s32page.inl>
   1.286 +#endif