2010-10-14 08:30:41 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2000-09-18 23:08:29 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-10 13:28:30 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2000-09-18 23:08:29 +00:00
|
|
|
*
|
2010-02-12 15:01:35 +01:00
|
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
2000-09-18 23:08:29 +00:00
|
|
|
*
|
2008-04-10 13:28:30 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2000-09-18 23:08:29 +00:00
|
|
|
*
|
2008-04-10 13:28:30 +00:00
|
|
|
* This file is part of OpenOffice.org.
|
2000-09-18 23:08:29 +00:00
|
|
|
*
|
2008-04-10 13:28:30 +00:00
|
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
* only, as published by the Free Software Foundation.
|
2000-09-18 23:08:29 +00:00
|
|
|
*
|
2008-04-10 13:28:30 +00:00
|
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License version 3 for more details
|
|
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
2000-09-18 23:08:29 +00:00
|
|
|
*
|
2008-04-10 13:28:30 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
|
|
* <http://www.openoffice.org/license.html>
|
|
|
|
* for a copy of the LGPLv3 License.
|
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>
|
2009-10-16 00:05:16 +02:00
|
|
|
#include <svl/svstdarr.hxx>
|
2000-09-18 23:08:29 +00:00
|
|
|
|
|
|
|
class SwCacheObj;
|
|
|
|
|
2012-01-21 15:21:16 +01:00
|
|
|
SV_DECL_PTRARR_DEL(SwCacheObjArr,SwCacheObj*,1)
|
2000-09-18 23:08:29 +00:00
|
|
|
|
|
|
|
class SwCache : public SwCacheObjArr
|
|
|
|
{
|
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
|
|
|
const sal_uInt16 nMax; //Mehr sollen nicht aufgenommen werden,
|
2000-09-18 23:08:29 +00:00
|
|
|
//der Cache kann aber dynamisch um jeweils
|
|
|
|
//nMax vergroessert werden.
|
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
|
|
|
|
rtl::OString m_aName;
|
|
|
|
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
|
2012-01-21 15:21:16 +01:00
|
|
|
SwCache( const sal_uInt16 nInitSize, const rtl::OString &rNm );
|
2000-09-18 23:08:29 +00:00
|
|
|
~SwCache();
|
|
|
|
#else
|
2012-01-21 17:51:19 +01:00
|
|
|
SwCache( const sal_uInt16 nInitSize );
|
2000-09-18 23:08:29 +00:00
|
|
|
#endif
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
//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: */
|