nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / core / inc / swcache.hxx
blobfba72413c829b1f7d48c8214409f389a331e45e2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_SW_SOURCE_CORE_INC_SWCACHE_HXX
20 #define INCLUDED_SW_SOURCE_CORE_INC_SWCACHE_HXX
22 /**
23 * Here, we manage pointers in a simple PtrArray to objects.
24 * These objects are created (using new) in cache access classes; they are
25 * destroyed by the cache.
27 * One can access these objects by array index or by searching in the array.
28 * If you access it by index, managing the index is the responsibility of
29 * the cache user.
31 * The cached objects are derived from the base class SwCacheObj.
32 * In it, the cache objects are doubly-linked which allows for the use of
33 * an LRU algorithm.
35 * The LRU algorithm can be changed in the base class, by setting a virtual
36 * First Pointer. It can be set to the first real one plus an offset.
37 * By doing so we can protect the start area of the cache and make sure we
38 * don't mess up the cache during some special operations.
39 * E.g.: the Idle Handler should not destroy the cache for the visible area.
41 * The cache can be grown and shrunk in size.
42 * E.g.: The cache for FormatInfo is grown for every new Shell and shrunk
43 * when destroying them.
46 #include <memory>
47 #include <vector>
49 #include <rtl/string.hxx>
50 #include <tools/long.hxx>
52 class SwCacheObj;
54 class SwCache
56 std::vector<std::unique_ptr<SwCacheObj>> m_aCacheObjects;
57 std::vector<sal_uInt16> m_aFreePositions; /// Free positions for the Insert if the maximum has not been reached
58 /// Every time an object is deregistered, its position is added here
59 SwCacheObj *m_pRealFirst; /// _ALWAYS_ the real first LRU
60 SwCacheObj *m_pFirst; /// The virtual first
61 SwCacheObj *m_pLast;
63 sal_uInt16 m_nCurMax; // Maximum of accepted objects
65 void DeleteObj( SwCacheObj *pObj );
67 #ifdef DBG_UTIL
68 OString m_aName;
69 tools::Long m_nAppend; /// number of entries appended
70 tools::Long m_nInsertFree; /// number of entries inserted on freed position
71 tools::Long m_nReplace; /// number of LRU replacements
72 tools::Long m_nGetSuccess;
73 tools::Long m_nGetFail;
74 tools::Long m_nToTop; /// number of reordering (LRU)
75 tools::Long m_nDelete; /// number of explicit deletes
76 tools::Long m_nGetSeek; /// number of gets without index
77 tools::Long m_nAverageSeekCnt; /// number of seeks for all gets without index
78 tools::Long m_nFlushCnt; /// number of flush calls
79 tools::Long m_nFlushedObjects;
80 tools::Long m_nIncreaseMax; /// number of cache size increases
81 tools::Long m_nDecreaseMax; /// number of cache size decreases
83 void Check();
84 #endif
86 public:
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 /// The dtor will free all objects still in the vector
95 ~SwCache();
97 void Flush();
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 );
105 bool Insert(SwCacheObj *pNew, bool isDuplicateOwnerAllowed);
106 void Delete(const void * pOwner, sal_uInt16 nIndex);
107 void Delete( const void *pOwner );
109 void SetLRUOfst( const sal_uInt16 nOfst ); /// nOfst determines how many are not to be touched
110 void ResetLRUOfst() { m_pFirst = m_pRealFirst; }
112 void IncreaseMax( const sal_uInt16 nAdd );
113 void DecreaseMax( const sal_uInt16 nSub );
114 sal_uInt16 GetCurMax() const { return m_nCurMax; }
115 SwCacheObj *First() { return m_pRealFirst; }
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(); }
121 /// Try to prevent visible SwParaPortions from being deleted.
122 class SwSaveSetLRUOfst
124 public:
125 SwSaveSetLRUOfst();
126 ~SwSaveSetLRUOfst();
130 * The Cache object base class
131 * Users of the Cache must derive a class from the SwCacheObj and store
132 * their payload there
134 class SwCacheObj
136 friend class SwCache; /// Can do everything
138 SwCacheObj *m_pNext; /// For the LRU chaining
139 SwCacheObj *m_pPrev;
141 sal_uInt16 m_nCachePos; /// Position in the Cache array
143 sal_uInt8 m_nLock;
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; }
150 void SetCachePos(const sal_uInt16 nNew)
152 if (m_nCachePos != nNew)
154 m_nCachePos = nNew;
155 UpdateCachePos();
158 virtual void UpdateCachePos() { }
160 protected:
161 const void *m_pOwner;
163 public:
165 SwCacheObj( const void *pOwner );
166 virtual ~SwCacheObj();
168 const void *GetOwner() const { return m_pOwner; }
169 inline bool IsOwner( const void *pNew ) const;
171 sal_uInt16 GetCachePos() const { return m_nCachePos; }
173 bool IsLocked() const { return 0 != m_nLock; }
175 #ifdef DBG_UTIL
176 void Lock();
177 void Unlock();
178 #else
179 void Lock() { ++m_nLock; }
180 void Unlock() { --m_nLock; }
181 #endif
185 * Access class for the Cache
187 * The Cache object is created in the ctor.
188 * If the Cache does not return one, the member is set to 0 and one is
189 * created on the Get() and added to the Cache (if possible).
190 * Cache users must derive a class from SwCacheAccess in order to
191 * guarantee type safety. The base class should always be called for the
192 * Get(). A derived Get() should only ever guarantee type safety.
193 * Cache objects are always locked for the instance's life time.
195 class SwCacheAccess
197 SwCache &m_rCache;
199 void Get_(bool isDuplicateOwnerAllowed);
201 protected:
202 SwCacheObj *m_pObj;
203 const void *m_pOwner; /// Can be use in NewObj
205 virtual SwCacheObj *NewObj() = 0;
207 inline SwCacheObj *Get(bool isDuplicateOwnerAllowed);
209 inline SwCacheAccess( SwCache &rCache, const void *pOwner, bool bSeek );
210 inline SwCacheAccess( SwCache &rCache, const void* nCacheId, const sal_uInt16 nIndex );
212 public:
213 virtual ~SwCacheAccess();
217 inline bool SwCacheObj::IsOwner( const void *pNew ) const
219 return m_pOwner && m_pOwner == pNew;
222 inline SwCacheObj *SwCache::Next( SwCacheObj *pCacheObj)
224 if ( pCacheObj )
225 return pCacheObj->GetNext();
226 else
227 return nullptr;
230 inline SwCacheAccess::SwCacheAccess( SwCache &rC, const void *pOwn, bool bSeek ) :
231 m_rCache( rC ),
232 m_pObj( nullptr ),
233 m_pOwner( pOwn )
235 if ( bSeek && m_pOwner )
237 m_pObj = m_rCache.Get( m_pOwner );
238 if (m_pObj)
239 m_pObj->Lock();
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 )
249 if ( m_pOwner )
251 m_pObj = m_rCache.Get( m_pOwner, nIndex );
252 if (m_pObj)
253 m_pObj->Lock();
257 inline SwCacheObj *SwCacheAccess::Get(bool const isDuplicateOwnerAllowed = true)
259 if ( !m_pObj )
260 Get_(isDuplicateOwnerAllowed);
261 return m_pObj;
264 #endif
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */