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 .
19 #ifndef INCLUDED_STOC_SOURCE_COREREFLECTION_LRUCACHE_HXX
20 #define INCLUDED_STOC_SOURCE_COREREFLECTION_LRUCACHE_HXX
22 // __CACHE_DIAGNOSE forces cache size to 4 and works only for OUString keys
23 // #define __CACHE_DIAGNOSE 1
25 #include <osl/mutex.hxx>
26 #include <rtl/ustring.hxx>
27 #include <sal/log.hxx>
29 #include <unordered_map>
31 /** Implementation of a least recently used (lru) cache.
33 @author Daniel Boelzle
35 template< class t_Key
, class t_Val
, class t_KeyHash
>
45 typedef std::unordered_map
< t_Key
, CacheEntry
*, t_KeyHash
> t_Key2Element
;
47 mutable ::osl::Mutex _aCacheMutex
;
48 sal_Int32 _nCachedElements
;
49 t_Key2Element _aKey2Element
;
52 mutable CacheEntry
* _pHead
;
53 mutable CacheEntry
* _pTail
;
54 inline void toFront( CacheEntry
* pEntry
) const;
59 @param nCachedElements number of elements to be cached; default param set to 128
61 explicit inline LRU_Cache();
62 /** Destructor: releases all cached elements and keys.
67 /** Retrieves a value from the cache. Returns default constructed value,
73 inline t_Val
getValue( const t_Key
& rKey
) const;
74 /** Sets a value to be cached for given key.
79 inline void setValue( const t_Key
& rKey
, const t_Val
& rValue
);
80 /** Clears the cache, thus releasing all cached elements and keys.
86 template< class t_Key
, class t_Val
, class t_KeyHash
>
87 inline LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::LRU_Cache()
88 #ifdef __CACHE_DIAGNOSE
89 : _nCachedElements( 4 )
91 : _nCachedElements( 256 )
97 if (_nCachedElements
> 0)
99 _pBlock
= new CacheEntry
[_nCachedElements
];
101 _pTail
= _pBlock
+ _nCachedElements
-1;
102 for ( sal_Int32 nPos
= _nCachedElements
; nPos
--; )
104 _pBlock
[nPos
].pPred
= _pBlock
+ nPos
-1;
105 _pBlock
[nPos
].pSucc
= _pBlock
+ nPos
+1;
110 template< class t_Key
, class t_Val
, class t_KeyHash
>
111 inline LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::~LRU_Cache()
116 template< class t_Key
, class t_Val
, class t_KeyHash
>
117 inline void LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::toFront( CacheEntry
* pEntry
) const
119 if (pEntry
!= _pHead
)
122 if (pEntry
== _pTail
)
124 _pTail
= pEntry
->pPred
;
128 pEntry
->pSucc
->pPred
= pEntry
->pPred
;
129 pEntry
->pPred
->pSucc
= pEntry
->pSucc
;
132 _pHead
->pPred
= pEntry
;
133 pEntry
->pSucc
= _pHead
;
138 template< class t_Key
, class t_Val
, class t_KeyHash
>
139 inline t_Val LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::getValue( const t_Key
& rKey
) const
141 ::osl::MutexGuard
aGuard( _aCacheMutex
);
142 const typename
t_Key2Element::const_iterator
iFind( _aKey2Element
.find( rKey
) );
143 if (iFind
!= _aKey2Element
.end())
145 CacheEntry
* pEntry
= (*iFind
).second
;
147 #ifdef __CACHE_DIAGNOSE
148 SAL_INFO("stoc.corerefl", "> retrieved element \"" );
149 SAL_INFO("stoc.corerefl", "" << pEntry
->aKey
);
150 SAL_INFO("stoc.corerefl", "\" from cache <" );
157 template< class t_Key
, class t_Val
, class t_KeyHash
>
158 inline void LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::setValue(
159 const t_Key
& rKey
, const t_Val
& rValue
)
161 ::osl::MutexGuard
aGuard( _aCacheMutex
);
162 if (_nCachedElements
> 0)
164 const typename
t_Key2Element::const_iterator
iFind( _aKey2Element
.find( rKey
) );
167 if (iFind
== _aKey2Element
.end())
169 pEntry
= _pTail
; // erase last element
170 #ifdef __CACHE_DIAGNOSE
171 if (pEntry
->aKey
.getLength())
173 SAL_INFO("stoc.corerefl", "> kicking element \"" );
174 SAL_INFO("stoc.corerefl", "" << pEntry
->aKey
);
175 SAL_INFO("stoc.corerefl", "\" from cache <" );
178 _aKey2Element
.erase( pEntry
->aKey
);
179 _aKey2Element
[ pEntry
->aKey
= rKey
] = pEntry
;
183 pEntry
= (*iFind
).second
;
184 #ifdef __CACHE_DIAGNOSE
185 SAL_INFO("stoc.corerefl", "> replacing element \"" );
186 SAL_INFO("stoc.corerefl", "" << pEntry
->aKey
);
187 SAL_INFO("stoc.corerefl", "\" in cache <" );
190 pEntry
->aVal
= rValue
;
195 template< class t_Key
, class t_Val
, class t_KeyHash
>
196 inline void LRU_Cache
< t_Key
, t_Val
, t_KeyHash
>::clear()
198 ::osl::MutexGuard
aGuard( _aCacheMutex
);
199 _aKey2Element
.clear();
200 for ( sal_Int32 nPos
= _nCachedElements
; nPos
--; )
202 _pBlock
[nPos
].aKey
= t_Key();
203 _pBlock
[nPos
].aVal
= t_Val();
205 _nCachedElements
= 0;
206 #ifdef __CACHE_DIAGNOSE
207 SAL_INFO("stoc.corerefl", "> cleared cache <" );
212 /** Template instance for OUString keys, Any values.<br>
214 typedef LRU_Cache
< OUString
, css::uno::Any
, OUStringHash
>
215 LRU_CacheAnyByOUString
;
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */