2010-10-14 08:30:41 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2011-03-31 10:05:04 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2000-09-18 23:08:29 +00:00
|
|
|
#ifndef _SWCACHE_HXX
|
|
|
|
#define _SWCACHE_HXX
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Es werden Pointer auf Objekte verwaltet. Diese werden in einem einfachen
|
|
|
|
* PtrArray verwaltet.
|
|
|
|
* Angelegt (new) werden die Objekte von Cache-Zugriffsklassen, zuerstoert
|
|
|
|
* werden die Objekte vom Cache.
|
|
|
|
*
|
|
|
|
* Auf die Objekte kann wahlweise per Index in das Array oder per Suche
|
|
|
|
* zugegriffen werden. Soll per Index zugegriffen werden, so obliegt die
|
|
|
|
* Verwaltung des Index dem Anwender des Cache.
|
|
|
|
*
|
|
|
|
* Fuer die verwalteten Cache-Objekte gibt es eine Basisklasse, von dieser
|
|
|
|
* sind spezifische Klassen abzuleiten.
|
|
|
|
* In der Basisklasse werden die Cache-Objekte eines Cache doppelt verkettet,
|
|
|
|
* das ermoeglich die Implementierung eines LRU-Algorithmus.
|
|
|
|
*
|
|
|
|
* Der LRU kann in der Cache-Basisklasse manipuliert werden, indem ein
|
|
|
|
* virtueller First-Pointer gesetzt wird. Dieser kann auf den echten ersten
|
|
|
|
* plus einem Ofst gesetzt werden. Dadurch kann man den Anfangsbereich des
|
|
|
|
* Cache sichern und so dafuer sorgen, dass man waehrend bestimmter
|
|
|
|
* Operationen nicht den Cache versaut. Beispiel: Der Idle-Handler sollte nicht
|
|
|
|
* den Cache fuer den sichtbaren Bereich vernichten.
|
|
|
|
*
|
|
|
|
* Der Cache kann in der Groesse erweitert und wieder verkleinert werden.
|
|
|
|
* Beispiel: Fuer jede neue Shell wird der Cache fuer FormatInfo vergrossert
|
|
|
|
* und beim Destruieren der Shell wieder verkleinert.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-10-20 02:06:49 +01:00
|
|
|
#include <vector>
|
2012-07-25 13:52:18 +02:00
|
|
|
|
|
|
|
#include <rtl/ustring.hxx>
|
2000-09-18 23:08:29 +00:00
|
|
|
|
|
|
|
class SwCacheObj;
|
|
|
|
|
2012-05-23 16:13:53 +02:00
|
|
|
typedef std::vector<SwCacheObj*> SwCacheObjArr;
|
|
|
|
class SwCache
|
2000-09-18 23:08:29 +00:00
|
|
|
{
|
2012-05-23 16:13:53 +02:00
|
|
|
SwCacheObjArr m_aCacheObjects;
|
2011-10-20 02:06:49 +01:00
|
|
|
std::vector<sal_uInt16> aFreePositions; //Freie Positionen fuer das Insert wenn
|
2000-09-18 23:08:29 +00:00
|
|
|
//die Maximalgrenze nicht erreicht ist.
|
|
|
|
//Immer wenn ein Objekt ausgetragen wird,
|
|
|
|
//so wird seine Position hier eingetragen.
|
|
|
|
|
|
|
|
SwCacheObj *pRealFirst; //_immer_ der echte LRU-erste
|
|
|
|
SwCacheObj *pFirst; //der virtuelle erste.
|
|
|
|
SwCacheObj *pLast;
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
sal_uInt16 nCurMax; //Mehr werden nicht aufgenommen.
|
2000-09-18 23:08:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
void DeleteObj( SwCacheObj *pObj );
|
|
|
|
|
2011-11-24 00:52:00 +01:00
|
|
|
#ifdef DBG_UTIL
|
2013-04-07 12:06:47 +02:00
|
|
|
OString m_aName;
|
2011-11-24 00:52:00 +01:00
|
|
|
long m_nAppend; /// number of entries appended
|
|
|
|
long m_nInsertFree; /// number of entries inserted on freed position
|
|
|
|
long m_nReplace; /// number of LRU replacements
|
|
|
|
long m_nGetSuccess;
|
|
|
|
long m_nGetFail;
|
|
|
|
long m_nToTop; /// number of reordering (LRU)
|
|
|
|
long m_nDelete; /// number of explicit deletes
|
|
|
|
long m_nGetSeek; /// number of gets without index
|
|
|
|
long m_nAverageSeekCnt; /// number of seeks for all gets without index
|
|
|
|
long m_nFlushCnt; /// number of flush calls
|
|
|
|
long m_nFlushedObjects;
|
|
|
|
long m_nIncreaseMax; /// number of cache size increases
|
|
|
|
long m_nDecreaseMax; /// number of cache size decreases
|
|
|
|
|
|
|
|
void Check();
|
2000-09-18 23:08:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
//nur sal_uInt8 hineinstecken!!!
|
2011-11-24 00:52:00 +01:00
|
|
|
#ifdef DBG_UTIL
|
2013-04-07 12:06:47 +02:00
|
|
|
SwCache( const sal_uInt16 nInitSize, const OString &rNm );
|
2000-09-18 23:08:29 +00:00
|
|
|
#else
|
2012-01-21 17:51:19 +01:00
|
|
|
SwCache( const sal_uInt16 nInitSize );
|
2000-09-18 23:08:29 +00:00
|
|
|
#endif
|
2012-05-24 22:42:46 +02:00
|
|
|
// the destructor will free all objects still in the vector
|
|
|
|
~SwCache();
|
2000-09-18 23:08:29 +00:00
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
void Flush( const sal_uInt8 nPercent = 100 );
|
2000-09-18 23:08:29 +00:00
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
//bToTop == sal_False -> Keine LRU-Umsortierung!
|
|
|
|
SwCacheObj *Get( const void *pOwner, const sal_Bool bToTop = sal_True );
|
|
|
|
SwCacheObj *Get( const void *pOwner, const sal_uInt16 nIndex,
|
|
|
|
const sal_Bool bToTop = sal_True );
|
2000-09-18 23:08:29 +00:00
|
|
|
void ToTop( SwCacheObj *pObj );
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
sal_Bool Insert( SwCacheObj *pNew );
|
2000-09-18 23:08:29 +00:00
|
|
|
void Delete( const void *pOwner );
|
2011-01-17 15:06:54 +01:00
|
|
|
// void Delete( const void *pOwner, const sal_uInt16 nIndex );
|
2000-09-18 23:08:29 +00:00
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
void SetLRUOfst( const sal_uInt16 nOfst ); //nOfst sagt wieviele unangetastet
|
2000-09-18 23:08:29 +00:00
|
|
|
//bleiben sollen.
|
|
|
|
void ResetLRUOfst() { pFirst = pRealFirst; }
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
inline void IncreaseMax( const sal_uInt16 nAdd );
|
|
|
|
inline void DecreaseMax( const sal_uInt16 nSub );
|
|
|
|
sal_uInt16 GetCurMax() const { return nCurMax; }
|
2000-09-18 23:08:29 +00:00
|
|
|
inline SwCacheObj *First() { return pRealFirst; }
|
|
|
|
inline SwCacheObj *Last() { return pLast; }
|
|
|
|
inline SwCacheObj *Next( SwCacheObj *pCacheObj);
|
2012-05-23 16:13:53 +02:00
|
|
|
inline SwCacheObj* operator[](sal_uInt16 nIndex) { return m_aCacheObjects[nIndex]; }
|
|
|
|
inline sal_uInt16 size() { return m_aCacheObjects.size(); }
|
2000-09-18 23:08:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//Cache-Manipulation auf die sichere Art.
|
|
|
|
class SwSaveSetLRUOfst
|
|
|
|
{
|
|
|
|
SwCache &rCache;
|
|
|
|
public:
|
2011-01-17 15:06:54 +01:00
|
|
|
SwSaveSetLRUOfst( SwCache &rC, const sal_uInt16 nOfst )
|
2000-09-18 23:08:29 +00:00
|
|
|
: rCache( rC ) { rCache.SetLRUOfst( nOfst ); }
|
|
|
|
|
|
|
|
~SwSaveSetLRUOfst() { rCache.ResetLRUOfst(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
//Das allgemeine CacheObjekt. Anwender des Cache muessen eine Klasse vom
|
|
|
|
//CacheObjekt ableiten und dort die Nutzdaten unterbringen.
|
|
|
|
|
|
|
|
class SwCacheObj
|
|
|
|
{
|
|
|
|
friend class SwCache; //Der darf alles
|
|
|
|
|
|
|
|
SwCacheObj *pNext; //Fuer die LRU-Verkettung.
|
|
|
|
SwCacheObj *pPrev;
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
sal_uInt16 nCachePos; //Position im Cache-Array.
|
2000-09-18 23:08:29 +00:00
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
sal_uInt8 nLock;
|
2000-09-18 23:08:29 +00:00
|
|
|
|
|
|
|
inline SwCacheObj *GetNext() { return pNext; }
|
|
|
|
inline SwCacheObj *GetPrev() { return pPrev; }
|
|
|
|
inline void SetNext( SwCacheObj *pNew ) { pNext = pNew; }
|
|
|
|
inline void SetPrev( SwCacheObj *pNew ) { pPrev = pNew; }
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
inline void SetCachePos( const sal_uInt16 nNew ) { nCachePos = nNew; }
|
2000-09-18 23:08:29 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
const void *pOwner;
|
|
|
|
inline void SetOwner( const void *pNew ) { pOwner = pNew; }
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
SwCacheObj( const void *pOwner );
|
|
|
|
virtual ~SwCacheObj();
|
|
|
|
|
|
|
|
inline const void *GetOwner() const { return pOwner; }
|
2011-01-17 15:06:54 +01:00
|
|
|
inline sal_Bool IsOwner( const void *pNew ) const;
|
2000-09-18 23:08:29 +00:00
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
inline sal_uInt16 GetCachePos() const { return nCachePos; }
|
2000-09-18 23:08:29 +00:00
|
|
|
inline void Invalidate() { pOwner = 0; }
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
inline sal_Bool IsLocked() const { return 0 != nLock; }
|
2000-09-18 23:08:29 +00:00
|
|
|
|
2011-11-24 00:52:00 +01:00
|
|
|
#ifdef DBG_UTIL
|
2000-09-18 23:08:29 +00:00
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
2010-11-25 17:08:45 +01:00
|
|
|
#else
|
|
|
|
inline void Lock() { ++nLock; }
|
|
|
|
inline void Unlock() { --nLock; }
|
2000-09-18 23:08:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SwCacheObj *Next() { return pNext; }
|
|
|
|
SwCacheObj *Prev() { return pPrev; }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
//Zugriffsklasse fuer den Cache. Im CTor wird das CacheObjekt erzeugt.
|
|
|
|
//Wenn der Cache keines herausrueckt wird der Member zunaechst auf 0 gesetzt.
|
|
|
|
//Beim Get wird dann eines erzeugt und, falls moeglich, in den Cache
|
|
|
|
//eingetragen.
|
|
|
|
//Anwender der des Cache muessen eine Klasse vom Access ableiten um
|
|
|
|
//fuer Typsicherheit zu sorgen, die Basisklasse sollte fuer das Get aber immer
|
|
|
|
//gerufen werden, ein Abgeleitetes Get sollte nur der Typsicherheit dienen.
|
|
|
|
//Cache-Objekte werden stets gelockt solange die Instanz lebt.
|
|
|
|
|
|
|
|
class SwCacheAccess
|
|
|
|
{
|
|
|
|
SwCache &rCache;
|
|
|
|
|
|
|
|
void _Get();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SwCacheObj *pObj;
|
|
|
|
const void *pOwner; //Kann ggf. in NewObj benutzt werden.
|
|
|
|
|
|
|
|
virtual SwCacheObj *NewObj() = 0;
|
|
|
|
|
|
|
|
inline SwCacheObj *Get();
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
inline SwCacheAccess( SwCache &rCache, const void *pOwner, sal_Bool bSeek = sal_True );
|
|
|
|
inline SwCacheAccess( SwCache &rCache, const void *pOwner, const sal_uInt16 nIndex );
|
2000-09-18 23:08:29 +00:00
|
|
|
|
|
|
|
public:
|
2004-06-16 08:39:35 +00:00
|
|
|
virtual ~SwCacheAccess();
|
2000-09-18 23:08:29 +00:00
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
virtual sal_Bool IsAvailable() const;
|
2000-09-18 23:08:29 +00:00
|
|
|
|
|
|
|
//Abkuerzung fuer diejenigen, die wissen, das die Ableitung das IsAvailable
|
|
|
|
//nicht ueberladen haben.
|
2011-01-17 15:06:54 +01:00
|
|
|
sal_Bool IsAvail() const { return pObj != 0; }
|
2000-09-18 23:08:29 +00:00
|
|
|
};
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
inline void SwCache::IncreaseMax( const sal_uInt16 nAdd )
|
2000-09-18 23:08:29 +00:00
|
|
|
{
|
2011-01-17 15:06:54 +01:00
|
|
|
nCurMax = nCurMax + sal::static_int_cast< sal_uInt16 >(nAdd);
|
2011-11-24 00:52:00 +01:00
|
|
|
#ifdef DBG_UTIL
|
|
|
|
++m_nIncreaseMax;
|
2000-09-18 23:08:29 +00:00
|
|
|
#endif
|
|
|
|
}
|
2011-01-17 15:06:54 +01:00
|
|
|
inline void SwCache::DecreaseMax( const sal_uInt16 nSub )
|
2000-09-18 23:08:29 +00:00
|
|
|
{
|
|
|
|
if ( nCurMax > nSub )
|
2011-01-17 15:06:54 +01:00
|
|
|
nCurMax = nCurMax - sal::static_int_cast< sal_uInt16 >(nSub);
|
2011-11-24 00:52:00 +01:00
|
|
|
#ifdef DBG_UTIL
|
|
|
|
++m_nDecreaseMax;
|
2000-09-18 23:08:29 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
inline sal_Bool SwCacheObj::IsOwner( const void *pNew ) const
|
2000-09-18 23:08:29 +00:00
|
|
|
{
|
|
|
|
return pOwner && pOwner == pNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SwCacheObj *SwCache::Next( SwCacheObj *pCacheObj)
|
|
|
|
{
|
|
|
|
if ( pCacheObj )
|
|
|
|
return pCacheObj->GetNext();
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
inline SwCacheAccess::SwCacheAccess( SwCache &rC, const void *pOwn, sal_Bool bSeek ) :
|
2000-09-18 23:08:29 +00:00
|
|
|
rCache( rC ),
|
2004-06-16 08:39:35 +00:00
|
|
|
pObj( 0 ),
|
|
|
|
pOwner( pOwn )
|
2000-09-18 23:08:29 +00:00
|
|
|
{
|
|
|
|
if ( bSeek && pOwner && 0 != (pObj = rCache.Get( pOwner )) )
|
|
|
|
pObj->Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SwCacheAccess::SwCacheAccess( SwCache &rC, const void *pOwn,
|
2011-01-17 15:06:54 +01:00
|
|
|
const sal_uInt16 nIndex ) :
|
2000-09-18 23:08:29 +00:00
|
|
|
rCache( rC ),
|
2004-06-16 08:39:35 +00:00
|
|
|
pObj( 0 ),
|
|
|
|
pOwner( pOwn )
|
2000-09-18 23:08:29 +00:00
|
|
|
{
|
|
|
|
if ( pOwner && 0 != (pObj = rCache.Get( pOwner, nIndex )) )
|
|
|
|
pObj->Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SwCacheObj *SwCacheAccess::Get()
|
|
|
|
{
|
|
|
|
if ( !pObj )
|
|
|
|
_Get();
|
|
|
|
return pObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
2010-10-14 08:30:41 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|