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