1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 // __CACHE_DIAGNOSE forces cache size to 4 and works only for OUString keys
23 // #define __CACHE_DIAGNOSE 1
25 #include <rtl/ustring.hxx>
29 #include <unordered_map>
31 namespace com::sun::star::uno
{ class Any
; }
33 /** Implementation of a least recently used (lru) cache.
36 template< class t_Key
, class t_Val
, class t_KeyHash
>
46 typedef std::unordered_map
< t_Key
, CacheEntry
*, t_KeyHash
> t_Key2Element
;
48 mutable std::mutex _aCacheMutex
;
49 sal_Int32 _nCachedElements
;
50 t_Key2Element _aKey2Element
;
52 std::unique_ptr
<CacheEntry
[]> _pBlock
;
53 mutable CacheEntry
* _pHead
;
54 mutable CacheEntry
* _pTail
;
55 inline void toFront( CacheEntry
* pEntry
) const;
60 @param nCachedElements number of elements to be cached; default param set to 128
62 explicit inline LRU_Cache();
64 /** Retrieves a value from the cache. Returns default constructed value,
70 inline t_Val
getValue( const t_Key
& rKey
) const;
71 /** Sets a value to be cached for given key.
76 inline void setValue( const t_Key
& rKey
, const t_Val
& rValue
);
77 /** Clears the cache, thus releasing all cached elements and keys.
83 template< class t_Key
, class t_Val
, class t_KeyHash
>
84 inline LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::LRU_Cache()
85 #ifdef __CACHE_DIAGNOSE
86 : _nCachedElements( 4 )
88 : _nCachedElements( 256 )
94 _pBlock
.reset(new CacheEntry
[_nCachedElements
]);
95 _pHead
= _pBlock
.get();
96 _pTail
= _pBlock
.get() + _nCachedElements
- 1;
97 for (sal_Int32 nPos
= _nCachedElements
; nPos
--;)
99 _pBlock
[nPos
].pPred
= _pBlock
.get() + nPos
- 1;
100 _pBlock
[nPos
].pSucc
= _pBlock
.get() + nPos
+ 1;
104 template< class t_Key
, class t_Val
, class t_KeyHash
>
105 inline void LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::toFront( CacheEntry
* pEntry
) const
107 if (pEntry
!= _pHead
)
110 if (pEntry
== _pTail
)
112 _pTail
= pEntry
->pPred
;
116 pEntry
->pSucc
->pPred
= pEntry
->pPred
;
117 pEntry
->pPred
->pSucc
= pEntry
->pSucc
;
120 _pHead
->pPred
= pEntry
;
121 pEntry
->pSucc
= _pHead
;
126 template< class t_Key
, class t_Val
, class t_KeyHash
>
127 inline t_Val LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::getValue( const t_Key
& rKey
) const
129 std::scoped_lock
aGuard( _aCacheMutex
);
130 const typename
t_Key2Element::const_iterator
iFind( _aKey2Element
.find( rKey
) );
131 if (iFind
!= _aKey2Element
.end())
133 CacheEntry
* pEntry
= (*iFind
).second
;
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 <" );
145 template< class t_Key
, class t_Val
, class t_KeyHash
>
146 inline void LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::setValue(
147 const t_Key
& rKey
, const t_Val
& rValue
)
149 std::scoped_lock
aGuard( _aCacheMutex
);
150 if (_nCachedElements
> 0)
152 const typename
t_Key2Element::const_iterator
iFind( _aKey2Element
.find( rKey
) );
155 if (iFind
== _aKey2Element
.end())
157 pEntry
= _pTail
; // erase last element
158 #ifdef __CACHE_DIAGNOSE
159 if (pEntry
->aKey
.getLength())
161 SAL_INFO("stoc.corerefl", "> kicking element \"" );
162 SAL_INFO("stoc.corerefl", "" << pEntry
->aKey
);
163 SAL_INFO("stoc.corerefl", "\" from cache <" );
166 _aKey2Element
.erase( pEntry
->aKey
);
168 _aKey2Element
[ rKey
] = pEntry
;
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 <" );
179 pEntry
->aVal
= rValue
;
184 template< class t_Key
, class t_Val
, class t_KeyHash
>
185 inline void LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::clear()
187 std::scoped_lock
aGuard( _aCacheMutex
);
188 _aKey2Element
.clear();
189 for ( sal_Int32 nPos
= _nCachedElements
; nPos
--; )
191 _pBlock
[nPos
].aKey
= t_Key();
192 _pBlock
[nPos
].aVal
= t_Val();
194 _nCachedElements
= 0;
195 #ifdef __CACHE_DIAGNOSE
196 SAL_INFO("stoc.corerefl", "> cleared cache <" );
201 /** Template instance for OUString keys, Any values.<br>
203 typedef LRU_Cache
< OUString
, css::uno::Any
, OUStringHash
>
204 LRU_CacheAnyByOUString
;
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */