LibreOffice Module stoc (master) 1
lrucache.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_STOC_SOURCE_COREREFLECTION_LRUCACHE_HXX
20#define INCLUDED_STOC_SOURCE_COREREFLECTION_LRUCACHE_HXX
21
22// __CACHE_DIAGNOSE forces cache size to 4 and works only for OUString keys
23// #define __CACHE_DIAGNOSE 1
24
25#include <rtl/ustring.hxx>
26
27#include <memory>
28#include <mutex>
29#include <unordered_map>
30
31namespace com::sun::star::uno { class Any; }
32
36template< class t_Key, class t_Val, class t_KeyHash >
38{
40 {
41 t_Key aKey;
42 t_Val aVal;
45 };
46 typedef std::unordered_map< t_Key, CacheEntry *, t_KeyHash > t_Key2Element;
47
48 mutable std::mutex _aCacheMutex;
51
52 std::unique_ptr<CacheEntry[]> _pBlock;
53 mutable CacheEntry * _pHead;
54 mutable CacheEntry * _pTail;
55 inline void toFront( CacheEntry * pEntry ) const;
56
57public:
62 explicit inline LRU_Cache();
63
70 inline t_Val getValue( const t_Key & rKey ) const;
76 inline void setValue( const t_Key & rKey, const t_Val & rValue );
80 inline void clear();
81};
82
83template< class t_Key, class t_Val, class t_KeyHash >
85#ifdef __CACHE_DIAGNOSE
86 : _nCachedElements( 4 )
87#else
88 : _nCachedElements( 256 )
89#endif
90 , _pBlock( nullptr )
91 , _pHead( nullptr )
92 , _pTail( nullptr )
93{
94 _pBlock.reset(new CacheEntry[_nCachedElements]);
95 _pHead = _pBlock.get();
96 _pTail = _pBlock.get() + _nCachedElements - 1;
97 for (sal_Int32 nPos = _nCachedElements; nPos--;)
98 {
99 _pBlock[nPos].pPred = _pBlock.get() + nPos - 1;
100 _pBlock[nPos].pSucc = _pBlock.get() + nPos + 1;
101 }
102}
103
104template< class t_Key, class t_Val, class t_KeyHash >
106{
107 if (pEntry != _pHead)
108 {
109 // cut out element
110 if (pEntry == _pTail)
111 {
112 _pTail = pEntry->pPred;
113 }
114 else
115 {
116 pEntry->pSucc->pPred = pEntry->pPred;
117 pEntry->pPred->pSucc = pEntry->pSucc;
118 }
119 // push to front
120 _pHead->pPred = pEntry;
121 pEntry->pSucc = _pHead;
122 _pHead = pEntry;
123 }
124}
125
126template< class t_Key, class t_Val, class t_KeyHash >
127inline t_Val LRU_Cache< t_Key, t_Val, t_KeyHash >::getValue( const t_Key & rKey ) const
128{
129 std::scoped_lock aGuard( _aCacheMutex );
130 const typename t_Key2Element::const_iterator iFind( _aKey2Element.find( rKey ) );
131 if (iFind != _aKey2Element.end())
132 {
133 CacheEntry * pEntry = (*iFind).second;
134 toFront( pEntry );
135#ifdef __CACHE_DIAGNOSE
136 SAL_INFO("stoc.corerefl", "> retrieved element \"" );
137 SAL_INFO("stoc.corerefl", "" << pEntry->aKey);
138 SAL_INFO("stoc.corerefl", "\" from cache <" );
139#endif
140 return pEntry->aVal;
141 }
142 return t_Val();
143}
144
145template< class t_Key, class t_Val, class t_KeyHash >
147 const t_Key & rKey, const t_Val & rValue )
148{
149 std::scoped_lock aGuard( _aCacheMutex );
150 if (_nCachedElements > 0)
151 {
152 const typename t_Key2Element::const_iterator iFind( _aKey2Element.find( rKey ) );
153
154 CacheEntry * pEntry;
155 if (iFind == _aKey2Element.end())
156 {
157 pEntry = _pTail; // erase last element
158#ifdef __CACHE_DIAGNOSE
159 if (pEntry->aKey.getLength())
160 {
161 SAL_INFO("stoc.corerefl", "> kicking element \"" );
162 SAL_INFO("stoc.corerefl", "" << pEntry->aKey);
163 SAL_INFO("stoc.corerefl", "\" from cache <" );
164 }
165#endif
166 _aKey2Element.erase( pEntry->aKey );
167 pEntry->aKey = rKey;
168 _aKey2Element[ rKey ] = pEntry;
169 }
170 else
171 {
172 pEntry = (*iFind).second;
173#ifdef __CACHE_DIAGNOSE
174 SAL_INFO("stoc.corerefl", "> replacing element \"" );
175 SAL_INFO("stoc.corerefl", "" << pEntry->aKey);
176 SAL_INFO("stoc.corerefl", "\" in cache <" );
177#endif
178 }
179 pEntry->aVal = rValue;
180 toFront( pEntry );
181 }
182}
183
184template< class t_Key, class t_Val, class t_KeyHash >
186{
187 std::scoped_lock aGuard( _aCacheMutex );
188 _aKey2Element.clear();
189 for ( sal_Int32 nPos = _nCachedElements; nPos--; )
190 {
191 _pBlock[nPos].aKey = t_Key();
192 _pBlock[nPos].aVal = t_Val();
193 }
194 _nCachedElements = 0;
195#ifdef __CACHE_DIAGNOSE
196 SAL_INFO("stoc.corerefl", "> cleared cache <" );
197#endif
198}
199
200
205
206
207#endif
208
209/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Implementation of a least recently used (lru) cache.
Definition: lrucache.hxx:38
CacheEntry * _pHead
Definition: lrucache.hxx:53
void toFront(CacheEntry *pEntry) const
Definition: lrucache.hxx:105
t_Key2Element _aKey2Element
Definition: lrucache.hxx:50
LRU_Cache()
Constructor:
Definition: lrucache.hxx:84
CacheEntry * _pTail
Definition: lrucache.hxx:54
sal_Int32 _nCachedElements
Definition: lrucache.hxx:49
std::mutex _aCacheMutex
Definition: lrucache.hxx:48
void clear()
Clears the cache, thus releasing all cached elements and keys.
Definition: lrucache.hxx:185
void setValue(const t_Key &rKey, const t_Val &rValue)
Sets a value to be cached for given key.
Definition: lrucache.hxx:146
t_Val getValue(const t_Key &rKey) const
Retrieves a value from the cache.
Definition: lrucache.hxx:127
std::unique_ptr< CacheEntry[]> _pBlock
Definition: lrucache.hxx:52
std::unordered_map< t_Key, CacheEntry *, t_KeyHash > t_Key2Element
Definition: lrucache.hxx:46
sal_uInt16 nPos
#define SAL_INFO(area, stream)
LRU_Cache< OUString, css::uno::Any, OUStringHash > LRU_CacheAnyByOUString
Template instance for OUString keys, Any values.
Definition: lrucache.hxx:204
CacheEntry * pPred
Definition: lrucache.hxx:43
CacheEntry * pSucc
Definition: lrucache.hxx:44