LibreOffice Module sw (master) 1
swcache.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19#ifndef INCLUDED_SW_SOURCE_CORE_INC_SWCACHE_HXX
20#define INCLUDED_SW_SOURCE_CORE_INC_SWCACHE_HXX
21
46#include <memory>
47#include <vector>
48
49#include <rtl/string.hxx>
50#include <tools/long.hxx>
51
52class SwCacheObj;
53
55{
56 std::vector<std::unique_ptr<SwCacheObj>> m_aCacheObjects;
57 std::vector<sal_uInt16> m_aFreePositions;
62
63 sal_uInt16 m_nCurMax; // Maximum of accepted objects
64
65 void DeleteObj( SwCacheObj *pObj );
66
67#ifdef DBG_UTIL
68 OString m_aName;
82
83 void Check();
84#endif
85
86public:
87
88// Only add sal_uInt8!!!
89#ifdef DBG_UTIL
90 SwCache( const sal_uInt16 nInitSize, OString aNm );
91#else
92 SwCache( const sal_uInt16 nInitSize );
93#endif
95 ~SwCache();
96
97 void Flush();
98
99 //bToTop == false -> No LRU resorting!
100 SwCacheObj *Get( const void *pOwner, const bool bToTop = true );
101 SwCacheObj *Get( const void *pOwner, const sal_uInt16 nIndex,
102 const bool bToTop = true );
103 void ToTop( SwCacheObj *pObj );
104
105 bool Insert(SwCacheObj *pNew, bool isDuplicateOwnerAllowed);
106 void Delete(const void * pOwner, sal_uInt16 nIndex);
107 void Delete( const void *pOwner );
108
111 void SetLRUOfst( const sal_uInt16 nOfst );
113
114 void IncreaseMax( const sal_uInt16 nAdd );
115 void DecreaseMax( const sal_uInt16 nSub );
116 sal_uInt16 GetCurMax() const { return m_nCurMax; }
118 static inline SwCacheObj *Next( SwCacheObj *pCacheObj);
119 SwCacheObj* operator[](sal_uInt16 nIndex) { return m_aCacheObjects[nIndex].get(); }
120 sal_uInt16 size() { return m_aCacheObjects.size(); }
121};
122
125{
126public:
129};
130
137{
138 friend class SwCache;
139
142
143 sal_uInt16 m_nCachePos;
144
146
149 void SetNext( SwCacheObj *pNew ) { m_pNext = pNew; }
150 void SetPrev( SwCacheObj *pNew ) { m_pPrev = pNew; }
151
152 void SetCachePos(const sal_uInt16 nNew)
153 {
154 if (m_nCachePos != nNew)
155 {
156 m_nCachePos = nNew;
158 }
159 }
160 virtual void UpdateCachePos() { }
161
162protected:
163 const void *m_pOwner;
164
165public:
166
167 SwCacheObj( const void *pOwner );
168 virtual ~SwCacheObj();
169
170 const void *GetOwner() const { return m_pOwner; }
171 inline bool IsOwner( const void *pNew ) const;
172
173 sal_uInt16 GetCachePos() const { return m_nCachePos; }
174
175 bool IsLocked() const { return 0 != m_nLock; }
176
177#ifdef DBG_UTIL
178 void Lock();
179 void Unlock();
180#else
181 void Lock() { ++m_nLock; }
182 void Unlock() { --m_nLock; }
183#endif
184};
185
198{
200
201 void Get_(bool isDuplicateOwnerAllowed);
202
203protected:
205 const void *m_pOwner;
206
207 virtual SwCacheObj *NewObj() = 0;
208
209 inline SwCacheObj *Get(bool isDuplicateOwnerAllowed);
210
211 inline SwCacheAccess( SwCache &rCache, const void *pOwner, bool bSeek );
212 inline SwCacheAccess( SwCache &rCache, const void* nCacheId, const sal_uInt16 nIndex );
213
214public:
215 virtual ~SwCacheAccess();
216};
217
218
219inline bool SwCacheObj::IsOwner( const void *pNew ) const
220{
221 return m_pOwner && m_pOwner == pNew;
222}
223
225{
226 if ( pCacheObj )
227 return pCacheObj->GetNext();
228 else
229 return nullptr;
230}
231
232inline SwCacheAccess::SwCacheAccess( SwCache &rC, const void *pOwn, bool bSeek ) :
233 m_rCache( rC ),
234 m_pObj( nullptr ),
235 m_pOwner( pOwn )
236{
237 if ( bSeek && m_pOwner )
238 {
240 if (m_pObj)
241 m_pObj->Lock();
242 }
243}
244
245inline SwCacheAccess::SwCacheAccess( SwCache &rC, const void* nCacheId,
246 const sal_uInt16 nIndex ) :
247 m_rCache( rC ),
248 m_pObj( nullptr ),
249 m_pOwner( nCacheId )
250{
251 if ( m_pOwner )
252 {
254 if (m_pObj)
255 m_pObj->Lock();
256 }
257}
258
259inline SwCacheObj *SwCacheAccess::Get(bool const isDuplicateOwnerAllowed = true)
260{
261 if ( !m_pObj )
262 Get_(isDuplicateOwnerAllowed);
263 return m_pObj;
264}
265
266#endif
267
268/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Access class for the Cache.
Definition: swcache.hxx:198
void Get_(bool isDuplicateOwnerAllowed)
Definition: swcache.cxx:490
virtual ~SwCacheAccess()
Definition: swcache.cxx:484
const void * m_pOwner
Definition: swcache.hxx:205
virtual SwCacheObj * NewObj()=0
Can be use in NewObj.
SwCacheAccess(SwCache &rCache, const void *pOwner, bool bSeek)
Definition: swcache.hxx:232
SwCache & m_rCache
Definition: swcache.hxx:199
SwCacheObj * m_pObj
Definition: swcache.hxx:204
SwCacheObj * Get(bool isDuplicateOwnerAllowed)
Definition: swcache.hxx:259
The Cache object base class Users of the Cache must derive a class from the SwCacheObj and store thei...
Definition: swcache.hxx:137
void SetNext(SwCacheObj *pNew)
Definition: swcache.hxx:149
sal_uInt8 m_nLock
Position in the Cache array.
Definition: swcache.hxx:145
const void * m_pOwner
Definition: swcache.hxx:163
void Lock()
Definition: swcache.cxx:471
sal_uInt16 m_nCachePos
Definition: swcache.hxx:143
virtual ~SwCacheObj()
Definition: swcache.cxx:466
SwCacheObj * GetNext()
Definition: swcache.hxx:147
sal_uInt16 GetCachePos() const
Definition: swcache.hxx:173
const void * GetOwner() const
Definition: swcache.hxx:170
SwCacheObj(const void *pOwner)
Definition: swcache.cxx:457
SwCacheObj * m_pNext
Can do everything.
Definition: swcache.hxx:140
SwCacheObj * m_pPrev
For the LRU chaining.
Definition: swcache.hxx:141
bool IsOwner(const void *pNew) const
Definition: swcache.hxx:219
void Unlock()
Definition: swcache.cxx:477
virtual void UpdateCachePos()
Definition: swcache.hxx:160
void SetPrev(SwCacheObj *pNew)
Definition: swcache.hxx:150
SwCacheObj * GetPrev()
Definition: swcache.hxx:148
void SetCachePos(const sal_uInt16 nNew)
Definition: swcache.hxx:152
bool IsLocked() const
Definition: swcache.hxx:175
std::vector< sal_uInt16 > m_aFreePositions
Definition: swcache.hxx:57
bool Insert(SwCacheObj *pNew, bool isDuplicateOwnerAllowed)
Definition: swcache.cxx:337
tools::Long m_nFlushedObjects
number of flush calls
Definition: swcache.hxx:79
tools::Long m_nGetSeek
number of explicit deletes
Definition: swcache.hxx:76
tools::Long m_nReplace
number of entries inserted on freed position
Definition: swcache.hxx:71
tools::Long m_nAppend
Definition: swcache.hxx:69
tools::Long m_nToTop
Definition: swcache.hxx:74
void Delete(const void *pOwner, sal_uInt16 nIndex)
Definition: swcache.cxx:322
void Flush()
Definition: swcache.cxx:148
void ResetLRUOfst()
Definition: swcache.hxx:112
SwCacheObj * m_pLast
The virtual first, only different to m_pRealFirst when SetLRUOfst has been called.
Definition: swcache.hxx:61
tools::Long m_nFlushCnt
number of seeks for all gets without index
Definition: swcache.hxx:78
SwCacheObj * m_pFirst
ALWAYS the real first LRU
Definition: swcache.hxx:60
void ToTop(SwCacheObj *pObj)
Definition: swcache.cxx:164
sal_uInt16 GetCurMax() const
Definition: swcache.hxx:116
SwCache(const sal_uInt16 nInitSize, OString aNm)
Definition: swcache.cxx:77
sal_uInt16 size()
Definition: swcache.hxx:120
void IncreaseMax(const sal_uInt16 nAdd)
Definition: swcache.cxx:128
sal_uInt16 m_nCurMax
Definition: swcache.hxx:63
std::vector< std::unique_ptr< SwCacheObj > > m_aCacheObjects
Definition: swcache.hxx:56
void DecreaseMax(const sal_uInt16 nSub)
Definition: swcache.cxx:139
tools::Long m_nGetFail
Definition: swcache.hxx:73
tools::Long m_nAverageSeekCnt
number of gets without index
Definition: swcache.hxx:77
void Check()
number of cache size decreases
Definition: swcache.cxx:30
SwCacheObj * First()
Definition: swcache.hxx:117
void SetLRUOfst(const sal_uInt16 nOfst)
Mark some entries as "do not delete".
Definition: swcache.cxx:439
void DeleteObj(SwCacheObj *pObj)
Definition: swcache.cxx:270
~SwCache()
The dtor will free all objects still in the vector.
Definition: swcache.cxx:106
tools::Long m_nDecreaseMax
number of cache size increases
Definition: swcache.hxx:81
tools::Long m_nGetSuccess
number of LRU replacements
Definition: swcache.hxx:72
SwCacheObj * Get(const void *pOwner, const bool bToTop=true)
Definition: swcache.cxx:248
OString m_aName
Definition: swcache.hxx:68
tools::Long m_nDelete
number of reordering (LRU)
Definition: swcache.hxx:75
static SwCacheObj * Next(SwCacheObj *pCacheObj)
Definition: swcache.hxx:224
SwCacheObj * m_pRealFirst
Free positions for the Insert if the maximum has not been reached Every time an object is deregistere...
Definition: swcache.hxx:59
tools::Long m_nInsertFree
number of entries appended
Definition: swcache.hxx:70
SwCacheObj * operator[](sal_uInt16 nIndex)
Definition: swcache.hxx:119
tools::Long m_nIncreaseMax
Definition: swcache.hxx:80
Try to prevent visible SwParaPortions from being deleted.
Definition: swcache.hxx:125
SwSaveSetLRUOfst()
Prevent the SwParaPortions of the visible paragraphs from being deleted; they would just be recreated...
Definition: txtcache.cxx:167
sal_Int32 nIndex
long Long
ContentProvider * m_pOwner
unsigned char sal_uInt8