os/persistentdata/persistentstorage/dbms/pcdbms/udbms/UD_CACHE.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) 1998-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 "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
// DBMS object cache
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "UD_STD.H"
sl@0
    19
#include "D32CACHE.H"
sl@0
    20
//#include <e32svr.h>
sl@0
    21
sl@0
    22
NONSHARABLE_CLASS(RDbCache::CCache) : private CBase
sl@0
    23
	{
sl@0
    24
public:
sl@0
    25
	static CCache* OpenL( TInt aSize, TBool aUseTimer );
sl@0
    26
	void Close();
sl@0
    27
//
sl@0
    28
	void Flush();
sl@0
    29
	void Hold( CBase* aObject, TUint aMicroSeconds );
sl@0
    30
	void Release ( const CBase& aObject );
sl@0
    31
private:
sl@0
    32
	struct TEntry
sl@0
    33
		{
sl@0
    34
		TEntry* iNext;
sl@0
    35
		TInt iDelta;
sl@0
    36
		CBase* iObject;
sl@0
    37
		};
sl@0
    38
	enum { ETimerPriority = -10 };
sl@0
    39
	enum { ETimerPeriod = 0x100000 };	// ~1.0s
sl@0
    40
private:
sl@0
    41
	static inline TInt TlsHandle();
sl@0
    42
	CCache( TInt aSize );
sl@0
    43
	~CCache();
sl@0
    44
//
sl@0
    45
	static CCache* NewL( TInt aSize, TBool aUseTimer );
sl@0
    46
	inline void Open();
sl@0
    47
	void Expire( TInt aElapsedTime );
sl@0
    48
	void Remove( TEntry*& aRef );
sl@0
    49
	void ExpireFirst();
sl@0
    50
	static void DoFlush( TAny* aPtr );
sl@0
    51
private:
sl@0
    52
	TInt iRef;
sl@0
    53
//	CPeriodic* iTimer;
sl@0
    54
	TTimeIntervalMicroSeconds32 iTickPeriod;
sl@0
    55
	TUint iZeroTime;
sl@0
    56
	TEntry* iCache;
sl@0
    57
	TEntry* iFree;
sl@0
    58
	TEntry iEntries[1];		// or maybe more
sl@0
    59
	};
sl@0
    60
sl@0
    61
// Class CDbObjectCache
sl@0
    62
sl@0
    63
//inline TInt RDbCache::CCache::TlsHandle()
sl@0
    64
//// use the address of a static function for the handle
sl@0
    65
//	{ return TInt( NewL ); }
sl@0
    66
sl@0
    67
TAny* gCachePtr;
sl@0
    68
sl@0
    69
RDbCache::CCache::CCache( TInt aSize )
sl@0
    70
//
sl@0
    71
// Initialise the free entry list
sl@0
    72
//
sl@0
    73
	{
sl@0
    74
	TEntry* entry = iEntries;
sl@0
    75
	while ( --aSize != 0 )
sl@0
    76
		{
sl@0
    77
		entry[1].iNext = entry;
sl@0
    78
		++entry;
sl@0
    79
		}
sl@0
    80
	iFree = entry;
sl@0
    81
	}
sl@0
    82
sl@0
    83
RDbCache::CCache::~CCache()
sl@0
    84
	{
sl@0
    85
	__ASSERT( iRef < 0 );
sl@0
    86
// empty the cache (destory the items now)
sl@0
    87
	Expire( KMaxTInt );
sl@0
    88
	__ASSERT( iCache == 0 );
sl@0
    89
//	delete iTimer;
sl@0
    90
	gCachePtr = NULL;
sl@0
    91
	}
sl@0
    92
sl@0
    93
const TInt KTickPeriod = 10000000;
sl@0
    94
const TTimeIntervalMicroSeconds32 gTickPeriod(KTickPeriod);
sl@0
    95
RDbCache::CCache* RDbCache::CCache::NewL( TInt aSize, TBool aUseTimer )
sl@0
    96
//
sl@0
    97
// Construct a cache with aSize slots and one referee
sl@0
    98
//
sl@0
    99
	{
sl@0
   100
//#warning !! RDbCache::CCache::NewL not implemented (uses UserHal/UserSvr) !!
sl@0
   101
	CCache* cache = new( ELeave, sizeof( TEntry ) * ( aSize - 1 ) ) CCache( aSize );	// get the extra size for the cache entries, leaves on error
sl@0
   102
	CleanupClosePushL( *cache );
sl@0
   103
	cache->iTickPeriod = gTickPeriod;
sl@0
   104
	//User::LeaveIfError( UserHal::TickPeriod( cache->iTickPeriod ) );
sl@0
   105
	//User::LeaveIfError( UserSvr::DllSetTls( TlsHandle(), cache ) );
sl@0
   106
	gCachePtr = cache;
sl@0
   107
//	if (aUseTimer)
sl@0
   108
//		cache->iTimer = CPeriodic::NewL( ETimerPriority );
sl@0
   109
	CleanupStack::Pop();
sl@0
   110
	return cache;
sl@0
   111
	}
sl@0
   112
sl@0
   113
inline void RDbCache::CCache::Open()
sl@0
   114
// add a referee
sl@0
   115
	{ ++iRef; }
sl@0
   116
sl@0
   117
void RDbCache::CCache::Close()
sl@0
   118
//
sl@0
   119
// remove a referee and delete as required
sl@0
   120
//
sl@0
   121
	{
sl@0
   122
	__ASSERT( iRef >= 0 );
sl@0
   123
	if ( --iRef < 0 )
sl@0
   124
		delete this;
sl@0
   125
	}
sl@0
   126
sl@0
   127
RDbCache::CCache* RDbCache::CCache::OpenL( TInt aSize, TBool aUseTimer )
sl@0
   128
//
sl@0
   129
// Grab a reference to the cache, constructing it if required
sl@0
   130
//
sl@0
   131
	{
sl@0
   132
//	CCache* cache = ( CCache* )UserSvr::DllTls( TlsHandle() );
sl@0
   133
	CCache* cache = ( CCache* )gCachePtr;
sl@0
   134
	if (!cache)
sl@0
   135
		return NewL( aSize, aUseTimer );
sl@0
   136
	cache->Open();
sl@0
   137
	return cache;
sl@0
   138
	}
sl@0
   139
sl@0
   140
void RDbCache::CCache::Hold( CBase* aObject, TUint aMicroSeconds )
sl@0
   141
//
sl@0
   142
// Hold aObject in the cache or destroy it
sl@0
   143
//
sl@0
   144
	{
sl@0
   145
	Flush();		// Destroy expired entries and re-assess Zero-time
sl@0
   146
	TInt ticks = aMicroSeconds / TUint( iTickPeriod.Int() );
sl@0
   147
	TEntry* entry = iFree;
sl@0
   148
	if ( entry == 0 )
sl@0
   149
		{	// no free slots: check the first cache entry
sl@0
   150
		__ASSERT( iCache );
sl@0
   151
		if ( iCache->iDelta > ticks )
sl@0
   152
			{				// aObject expires first
sl@0
   153
			delete aObject;
sl@0
   154
			return;
sl@0
   155
			}
sl@0
   156
		ExpireFirst();		// remove the first entry and use it
sl@0
   157
		entry = iFree;
sl@0
   158
		}
sl@0
   159
	iFree = entry->iNext;	// move the free list pointer to the next entry
sl@0
   160
	//
sl@0
   161
	// find the insertion point in the cache delta-list
sl@0
   162
	TEntry** pcache = &iCache;
sl@0
   163
	TEntry* cache;
sl@0
   164
	for ( ; ; )
sl@0
   165
		{
sl@0
   166
		__ASSERT( ticks >= 0 );
sl@0
   167
		cache = *pcache;
sl@0
   168
		if ( !cache )
sl@0
   169
			break;				// add to end
sl@0
   170
		TInt t = ticks - cache->iDelta;
sl@0
   171
		if ( t < 0 )
sl@0
   172
			{					// add to the list here
sl@0
   173
			cache->iDelta = -t;	// reduce the following delta
sl@0
   174
			break;
sl@0
   175
			}
sl@0
   176
		ticks = t;				// reduce the entry delta
sl@0
   177
		pcache = &cache->iNext;
sl@0
   178
		}
sl@0
   179
	*pcache = entry;				// set up the entry
sl@0
   180
	entry->iDelta = ticks;
sl@0
   181
	entry->iNext = cache;
sl@0
   182
	entry->iObject = aObject;
sl@0
   183
//	// kick the timer if we need to
sl@0
   184
//	if ( iTimer && !iTimer->IsActive() )
sl@0
   185
//		iTimer->Start( ETimerPeriod, ETimerPeriod, TCallBack( ( TInt (*)(TAny*) )DoFlush, this ) );
sl@0
   186
	}
sl@0
   187
sl@0
   188
void RDbCache::CCache::Remove( RDbCache::CCache::TEntry*& aRef )
sl@0
   189
//
sl@0
   190
// Remove the entry at aRef from the cache
sl@0
   191
//
sl@0
   192
	{
sl@0
   193
	TEntry& entry = *aRef;
sl@0
   194
	TEntry* next = entry.iNext;
sl@0
   195
	entry.iNext = iFree;
sl@0
   196
	iFree = &entry;
sl@0
   197
	aRef = next;
sl@0
   198
	if ( next )
sl@0
   199
		next->iDelta += entry.iDelta;
sl@0
   200
//	else if ( iTimer )	// the cache is now empty, so stop the timer if we have one
sl@0
   201
//		iTimer->Cancel();
sl@0
   202
	}
sl@0
   203
sl@0
   204
void RDbCache::CCache::ExpireFirst()
sl@0
   205
//
sl@0
   206
// Expire the first entry in the cache
sl@0
   207
//
sl@0
   208
	{
sl@0
   209
	__ASSERT( iCache != 0 );
sl@0
   210
	// the ordering here is important. Removing the entry first allows the
sl@0
   211
	// object d'tor to call Release() without causing re-entrancy problems.
sl@0
   212
	CBase* object = iCache->iObject;
sl@0
   213
	Remove( iCache );
sl@0
   214
	delete object;
sl@0
   215
	}
sl@0
   216
sl@0
   217
void RDbCache::CCache::Release( const CBase& aObject )
sl@0
   218
//
sl@0
   219
// Remove the cache entry for aObject, if it is in the cache
sl@0
   220
//
sl@0
   221
	{
sl@0
   222
	TEntry** pcache = &iCache;
sl@0
   223
	for ( ; ; )
sl@0
   224
		{
sl@0
   225
		TEntry* entry = *pcache;
sl@0
   226
		if ( !entry )
sl@0
   227
			return;
sl@0
   228
		if ( entry->iObject == &aObject )
sl@0
   229
			{
sl@0
   230
			Remove( *pcache );
sl@0
   231
			return;
sl@0
   232
			}
sl@0
   233
		pcache = &entry->iNext;
sl@0
   234
		}
sl@0
   235
	}
sl@0
   236
sl@0
   237
void RDbCache::CCache::Expire( TInt aElapsedTime )
sl@0
   238
//
sl@0
   239
// Destroy entries which expire with aElapsedTime
sl@0
   240
//
sl@0
   241
	{
sl@0
   242
	__ASSERT( aElapsedTime > 0 );
sl@0
   243
	if ( iCache && ( iCache->iDelta -= aElapsedTime ) < 0 )
sl@0
   244
		{
sl@0
   245
		Open();		// This allows the cache to be owned by an object in the cache
sl@0
   246
		do ExpireFirst();
sl@0
   247
			while ( iCache && iCache->iDelta < 0 );
sl@0
   248
		Close();	// The cache may be destroyed now
sl@0
   249
		}
sl@0
   250
	}
sl@0
   251
sl@0
   252
void RDbCache::CCache::Flush()
sl@0
   253
//
sl@0
   254
// Check the execution clock and destroy any expired entries
sl@0
   255
//
sl@0
   256
// Care has to be taken to handle the 32-bit wraparound of the tick-count
sl@0
   257
// e.g. iZeroTime = 0xffffffffu, now = 0
sl@0
   258
//
sl@0
   259
	{
sl@0
   260
	TUint now = User::TickCount();
sl@0
   261
	TUint elapsed = now - iZeroTime;
sl@0
   262
	iZeroTime = now;
sl@0
   263
	if ( elapsed )
sl@0
   264
		Expire( elapsed <= TUint( KMaxTInt ) ? elapsed : TUint( KMaxTInt ) );
sl@0
   265
	}
sl@0
   266
sl@0
   267
void RDbCache::CCache::DoFlush( TAny* aPtr )
sl@0
   268
//
sl@0
   269
// Callback for the timer
sl@0
   270
//
sl@0
   271
	{
sl@0
   272
	static_cast<CCache*>( aPtr )->Flush();
sl@0
   273
	}
sl@0
   274
sl@0
   275
sl@0
   276
// Class RDbCache
sl@0
   277
sl@0
   278
TInt RDbCache::Open( TInt aSize, TBool aUseTimer )
sl@0
   279
//
sl@0
   280
// Get a handle on the cache
sl@0
   281
//
sl@0
   282
	{
sl@0
   283
	__ASSERT( aSize > 0 );
sl@0
   284
	TRAPD( r, iCache = CCache::OpenL( aSize, aUseTimer ) );
sl@0
   285
	return r;
sl@0
   286
	}
sl@0
   287
sl@0
   288
void RDbCache::Close()
sl@0
   289
//
sl@0
   290
// Close this handle on the cache
sl@0
   291
//
sl@0
   292
	{
sl@0
   293
	CCache* cache = iCache;
sl@0
   294
	if ( cache )
sl@0
   295
		{
sl@0
   296
		iCache = 0;
sl@0
   297
		cache->Close();
sl@0
   298
		}
sl@0
   299
	}
sl@0
   300
sl@0
   301
void RDbCache::Hold( CBase* aObject, TUint aMicroSeconds )
sl@0
   302
//
sl@0
   303
// Hold aObject on the cache, if open
sl@0
   304
// We are now responsible for deleting the object
sl@0
   305
//
sl@0
   306
	{
sl@0
   307
	if ( iCache )
sl@0
   308
		iCache->Hold( aObject, aMicroSeconds );
sl@0
   309
	else
sl@0
   310
		delete aObject;	// no cache available
sl@0
   311
	}
sl@0
   312
sl@0
   313
void RDbCache::Release( const CBase& aObject ) const
sl@0
   314
//
sl@0
   315
// Retrieve aObject from the cache
sl@0
   316
//
sl@0
   317
	{
sl@0
   318
	if ( iCache )
sl@0
   319
		iCache->Release( aObject );
sl@0
   320
	}
sl@0
   321
sl@0
   322
void RDbCache::Flush()
sl@0
   323
//
sl@0
   324
// Destroy any cached objects which have expired
sl@0
   325
//
sl@0
   326
	{
sl@0
   327
	if ( iCache )
sl@0
   328
		iCache->Flush();
sl@0
   329
	}
sl@0
   330
sl@0
   331
sl@0
   332