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 
52 class SwCacheObj;
53 
54 class SwCache
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 
86 public:
87 
88 // Only add sal_uInt8!!!
89 #ifdef DBG_UTIL
90  SwCache( const sal_uInt16 nInitSize, const OString &rNm );
91 #else
92  SwCache( const sal_uInt16 nInitSize );
93 #endif
94  ~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 
109  void SetLRUOfst( const sal_uInt16 nOfst );
110  void ResetLRUOfst() { m_pFirst = m_pRealFirst; }
111 
112  void IncreaseMax( const sal_uInt16 nAdd );
113  void DecreaseMax( const sal_uInt16 nSub );
114  sal_uInt16 GetCurMax() const { return m_nCurMax; }
116  static inline SwCacheObj *Next( SwCacheObj *pCacheObj);
117  SwCacheObj* operator[](sal_uInt16 nIndex) { return m_aCacheObjects[nIndex].get(); }
118  sal_uInt16 size() { return m_aCacheObjects.size(); }
119 };
120 
123 {
124 public:
127 };
128 
135 {
136  friend class SwCache;
137 
140 
141  sal_uInt16 m_nCachePos;
142 
144 
145  SwCacheObj *GetNext() { return m_pNext; }
146  SwCacheObj *GetPrev() { return m_pPrev; }
147  void SetNext( SwCacheObj *pNew ) { m_pNext = pNew; }
148  void SetPrev( SwCacheObj *pNew ) { m_pPrev = pNew; }
149 
150  void SetCachePos(const sal_uInt16 nNew)
151  {
152  if (m_nCachePos != nNew)
153  {
154  m_nCachePos = nNew;
155  UpdateCachePos();
156  }
157  }
158  virtual void UpdateCachePos() { }
159 
160 protected:
161  const void *m_pOwner;
162 
163 public:
164 
165  SwCacheObj( const void *pOwner );
166  virtual ~SwCacheObj();
167 
168  const void *GetOwner() const { return m_pOwner; }
169  inline bool IsOwner( const void *pNew ) const;
170 
171  sal_uInt16 GetCachePos() const { return m_nCachePos; }
172 
173  bool IsLocked() const { return 0 != m_nLock; }
174 
175 #ifdef DBG_UTIL
176  void Lock();
177  void Unlock();
178 #else
179  void Lock() { ++m_nLock; }
180  void Unlock() { --m_nLock; }
181 #endif
182 };
183 
196 {
198 
199  void Get_(bool isDuplicateOwnerAllowed);
200 
201 protected:
203  const void *m_pOwner;
204 
205  virtual SwCacheObj *NewObj() = 0;
206 
207  inline SwCacheObj *Get(bool isDuplicateOwnerAllowed);
208 
209  inline SwCacheAccess( SwCache &rCache, const void *pOwner, bool bSeek );
210  inline SwCacheAccess( SwCache &rCache, const void* nCacheId, const sal_uInt16 nIndex );
211 
212 public:
213  virtual ~SwCacheAccess();
214 };
215 
216 
217 inline bool SwCacheObj::IsOwner( const void *pNew ) const
218 {
219  return m_pOwner && m_pOwner == pNew;
220 }
221 
222 inline SwCacheObj *SwCache::Next( SwCacheObj *pCacheObj)
223 {
224  if ( pCacheObj )
225  return pCacheObj->GetNext();
226  else
227  return nullptr;
228 }
229 
230 inline SwCacheAccess::SwCacheAccess( SwCache &rC, const void *pOwn, bool bSeek ) :
231  m_rCache( rC ),
232  m_pObj( nullptr ),
233  m_pOwner( pOwn )
234 {
235  if ( bSeek && m_pOwner )
236  {
238  if (m_pObj)
239  m_pObj->Lock();
240  }
241 }
242 
243 inline SwCacheAccess::SwCacheAccess( SwCache &rC, const void* nCacheId,
244  const sal_uInt16 nIndex ) :
245  m_rCache( rC ),
246  m_pObj( nullptr ),
247  m_pOwner( nCacheId )
248 {
249  if ( m_pOwner )
250  {
251  m_pObj = m_rCache.Get( m_pOwner, nIndex );
252  if (m_pObj)
253  m_pObj->Lock();
254  }
255 }
256 
257 inline SwCacheObj *SwCacheAccess::Get(bool const isDuplicateOwnerAllowed = true)
258 {
259  if ( !m_pObj )
260  Get_(isDuplicateOwnerAllowed);
261  return m_pObj;
262 }
263 
264 #endif
265 
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 GetCachePos() const
Definition: swcache.hxx:171
bool Insert(SwCacheObj *pNew, bool isDuplicateOwnerAllowed)
Definition: swcache.cxx:340
sal_uInt16 m_nCachePos
Definition: swcache.hxx:141
SwCacheObj * m_pLast
The virtual first.
Definition: swcache.hxx:61
void SetLRUOfst(const sal_uInt16 nOfst)
Definition: swcache.cxx:442
void Lock()
Definition: swcache.cxx:474
bool IsLocked() const
Definition: swcache.hxx:173
SwCacheObj * m_pObj
Definition: swcache.hxx:202
SwCacheObj * First()
Definition: swcache.hxx:115
void Get_(bool isDuplicateOwnerAllowed)
Definition: swcache.cxx:493
tools::Long m_nInsertFree
number of entries appended
Definition: swcache.hxx:70
SwCacheObj * GetPrev()
Definition: swcache.hxx:146
sal_uInt16 GetCurMax() const
Definition: swcache.hxx:114
static SwCacheObj * Next(SwCacheObj *pCacheObj)
Definition: swcache.hxx:222
long Long
virtual SwCacheObj * NewObj()=0
Can be use in NewObj.
SwCacheObj * m_pFirst
ALWAYS the real first LRU
Definition: swcache.hxx:60
tools::Long m_nToTop
Definition: swcache.hxx:74
const void * m_pOwner
Definition: swcache.hxx:203
SwCacheObj * Get(bool isDuplicateOwnerAllowed)
Definition: swcache.hxx:257
void ResetLRUOfst()
nOfst determines how many are not to be touched
Definition: swcache.hxx:110
const void * m_pOwner
Definition: swcache.hxx:161
void SetNext(SwCacheObj *pNew)
Definition: swcache.hxx:147
virtual ~SwCacheObj()
Definition: swcache.cxx:469
tools::Long m_nGetFail
Definition: swcache.hxx:73
SwCacheObj * m_pPrev
For the LRU chaining.
Definition: swcache.hxx:139
std::vector< sal_uInt16 > m_aFreePositions
Definition: swcache.hxx:57
SwCacheObj * Get(const void *pOwner, const bool bToTop=true)
Definition: swcache.cxx:251
std::vector< std::unique_ptr< SwCacheObj > > m_aCacheObjects
Definition: swcache.hxx:56
SwCacheObj * m_pRealFirst
Free positions for the Insert if the maximum has not been reached.
Definition: swcache.hxx:59
void DecreaseMax(const sal_uInt16 nSub)
Definition: swcache.cxx:139
tools::Long m_nDelete
number of reordering (LRU)
Definition: swcache.hxx:75
tools::Long m_nGetSuccess
number of LRU replacements
Definition: swcache.hxx:72
void DeleteObj(SwCacheObj *pObj)
Definition: swcache.cxx:273
OString m_aName
Definition: swcache.hxx:68
sal_uInt8 m_nLock
Position in the Cache array.
Definition: swcache.hxx:143
void Check()
number of cache size decreases
Definition: swcache.cxx:29
SwCacheObj * operator[](sal_uInt16 nIndex)
Definition: swcache.hxx:117
sal_uInt16 size()
Definition: swcache.hxx:118
bool IsOwner(const void *pNew) const
Definition: swcache.hxx:217
Try to prevent visible SwParaPortions from being deleted.
Definition: swcache.hxx:122
void IncreaseMax(const sal_uInt16 nAdd)
Definition: swcache.cxx:128
SwCacheObj(const void *pOwner)
Definition: swcache.cxx:460
SwCacheObj * m_pNext
Can do everything.
Definition: swcache.hxx:138
ContentProvider * m_pOwner
void ToTop(SwCacheObj *pObj)
Definition: swcache.cxx:167
void SetPrev(SwCacheObj *pNew)
Definition: swcache.hxx:148
sal_uInt16 m_nCurMax
Definition: swcache.hxx:63
tools::Long m_nDecreaseMax
number of cache size increases
Definition: swcache.hxx:81
tools::Long m_nFlushedObjects
number of flush calls
Definition: swcache.hxx:79
SwSaveSetLRUOfst()
Prevent the SwParaPortions of the visible paragraphs from being deleted; they would just be recreated...
Definition: txtcache.cxx:167
void Flush()
Definition: swcache.cxx:148
void Delete(const void *pOwner, sal_uInt16 nIndex)
tools::Long m_nReplace
number of entries inserted on freed position
Definition: swcache.hxx:71
void SetCachePos(const sal_uInt16 nNew)
Definition: swcache.hxx:150
unsigned char sal_uInt8
The Cache object base class Users of the Cache must derive a class from the SwCacheObj and store thei...
Definition: swcache.hxx:134
~SwCache()
The dtor will free all objects still in the vector.
Definition: swcache.cxx:106
SwCacheAccess(SwCache &rCache, const void *pOwner, bool bSeek)
Definition: swcache.hxx:230
tools::Long m_nGetSeek
number of explicit deletes
Definition: swcache.hxx:76
virtual ~SwCacheAccess()
Definition: swcache.cxx:487
void Unlock()
Definition: swcache.cxx:480
tools::Long m_nAverageSeekCnt
number of gets without index
Definition: swcache.hxx:77
tools::Long m_nIncreaseMax
Definition: swcache.hxx:80
Access class for the Cache.
Definition: swcache.hxx:195
tools::Long m_nAppend
Definition: swcache.hxx:69
SwCache & m_rCache
Definition: swcache.hxx:197
tools::Long m_nFlushCnt
number of seeks for all gets without index
Definition: swcache.hxx:78
SwCacheObj * GetNext()
Definition: swcache.hxx:145
virtual void UpdateCachePos()
Definition: swcache.hxx:158
SwCache(const sal_uInt16 nInitSize, const OString &rNm)
Definition: swcache.cxx:77
const void * GetOwner() const
Definition: swcache.hxx:168