Line data Source code
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 <osl/mutex.hxx>
26 : #include <rtl/ustring.hxx>
27 : #include <sal/log.hxx>
28 :
29 : #include <boost/unordered_map.hpp>
30 :
31 : /** Implementation of a least recently used (lru) cache.
32 : <br>
33 : @author Daniel Boelzle
34 : */
35 : template< class t_Key, class t_Val, class t_KeyHash >
36 : class LRU_Cache
37 : {
38 84224 : struct CacheEntry
39 : {
40 : t_Key aKey;
41 : t_Val aVal;
42 : CacheEntry * pPred;
43 : CacheEntry * pSucc;
44 : };
45 : typedef ::boost::unordered_map< t_Key, CacheEntry *, t_KeyHash > t_Key2Element;
46 :
47 : mutable ::osl::Mutex _aCacheMutex;
48 : sal_Int32 _nCachedElements;
49 : t_Key2Element _aKey2Element;
50 :
51 : CacheEntry * _pBlock;
52 : mutable CacheEntry * _pHead;
53 : mutable CacheEntry * _pTail;
54 : inline void toFront( CacheEntry * pEntry ) const;
55 :
56 : public:
57 : /** Constructor:
58 : <br>
59 : @param nCachedElements number of elements to be cached; default param set to 128
60 : */
61 : inline LRU_Cache( sal_Int32 nCachedElements = 128 );
62 : /** Destructor: releases all cached elements and keys.
63 : <br>
64 : */
65 : inline ~LRU_Cache();
66 :
67 : /** Retrieves a value from the cache. Returns default constructed value,
68 : if none was found.
69 : <br>
70 : @param rKey a key
71 : @return value
72 : */
73 : inline t_Val getValue( const t_Key & rKey ) const;
74 : /** Sets a value to be cached for given key.
75 : <br>
76 : @param rKey a key
77 : @param rValue a value
78 : */
79 : inline void setValue( const t_Key & rKey, const t_Val & rValue );
80 : /** Tests whether a value is cached for given key.
81 : <br>
82 : @param rKey a key
83 : @return true, if value is cached
84 : */
85 : inline bool hasValue( const t_Key & rKey ) const;
86 : /** Clears the cache, thus releasing all cached elements and keys.
87 : <br>
88 : */
89 : inline void clear();
90 : };
91 :
92 : template< class t_Key, class t_Val, class t_KeyHash >
93 194 : inline LRU_Cache< t_Key, t_Val, t_KeyHash >::LRU_Cache( sal_Int32 nCachedElements )
94 : #ifdef __CACHE_DIAGNOSE
95 : : _nCachedElements( 4 )
96 : #else
97 : : _nCachedElements( nCachedElements )
98 : #endif
99 : , _pBlock( 0 )
100 : , _pHead( 0 )
101 194 : , _pTail( 0 )
102 : {
103 194 : if (_nCachedElements > 0)
104 : {
105 194 : _pBlock = new CacheEntry[_nCachedElements];
106 194 : _pHead = _pBlock;
107 194 : _pTail = _pBlock + _nCachedElements -1;
108 50052 : for ( sal_Int32 nPos = _nCachedElements; nPos--; )
109 : {
110 49664 : _pBlock[nPos].pPred = _pBlock + nPos -1;
111 49664 : _pBlock[nPos].pSucc = _pBlock + nPos +1;
112 : }
113 : }
114 194 : }
115 :
116 : template< class t_Key, class t_Val, class t_KeyHash >
117 135 : inline LRU_Cache< t_Key, t_Val, t_KeyHash >::~LRU_Cache()
118 : {
119 135 : delete [] _pBlock;
120 135 : }
121 :
122 : template< class t_Key, class t_Val, class t_KeyHash >
123 1094474 : inline void LRU_Cache< t_Key, t_Val, t_KeyHash >::toFront( CacheEntry * pEntry ) const
124 : {
125 1094474 : if (pEntry != _pHead)
126 : {
127 : // cut out element
128 968565 : if (pEntry == _pTail)
129 : {
130 20932 : _pTail = pEntry->pPred;
131 : }
132 : else
133 : {
134 947633 : pEntry->pSucc->pPred = pEntry->pPred;
135 947633 : pEntry->pPred->pSucc = pEntry->pSucc;
136 : }
137 : // push to front
138 968565 : _pHead->pPred = pEntry;
139 968565 : pEntry->pSucc = _pHead;
140 968565 : _pHead = pEntry;
141 : }
142 1094474 : }
143 :
144 : template< class t_Key, class t_Val, class t_KeyHash >
145 : inline bool LRU_Cache< t_Key, t_Val, t_KeyHash >::hasValue( const t_Key & rKey ) const
146 : {
147 : ::osl::MutexGuard aGuard( _aCacheMutex );
148 : const typename t_Key2Element::const_iterator iFind( _aKey2Element.find( rKey ) );
149 : return (iFind != _aKey2Element.end());
150 : }
151 :
152 : template< class t_Key, class t_Val, class t_KeyHash >
153 1094488 : inline t_Val LRU_Cache< t_Key, t_Val, t_KeyHash >::getValue( const t_Key & rKey ) const
154 : {
155 1094488 : ::osl::MutexGuard aGuard( _aCacheMutex );
156 1094488 : const typename t_Key2Element::const_iterator iFind( _aKey2Element.find( rKey ) );
157 1094488 : if (iFind != _aKey2Element.end())
158 : {
159 1073544 : CacheEntry * pEntry = (*iFind).second;
160 1073544 : toFront( pEntry );
161 : #ifdef __CACHE_DIAGNOSE
162 : SAL_INFO("stoc.corerefl", "> retrieved element \"" );
163 : SAL_INFO("stoc.corerefl", "" << pEntry->aKey);
164 : SAL_INFO("stoc.corerefl", "\" from cache <" );
165 : #endif
166 1073544 : return pEntry->aVal;
167 : }
168 20944 : return t_Val();
169 : }
170 :
171 : template< class t_Key, class t_Val, class t_KeyHash >
172 20930 : inline void LRU_Cache< t_Key, t_Val, t_KeyHash >::setValue(
173 : const t_Key & rKey, const t_Val & rValue )
174 : {
175 20930 : ::osl::MutexGuard aGuard( _aCacheMutex );
176 20930 : if (_nCachedElements > 0)
177 : {
178 20930 : const typename t_Key2Element::const_iterator iFind( _aKey2Element.find( rKey ) );
179 :
180 : CacheEntry * pEntry;
181 20930 : if (iFind == _aKey2Element.end())
182 : {
183 20930 : pEntry = _pTail; // erase last element
184 : #ifdef __CACHE_DIAGNOSE
185 : if (pEntry->aKey.getLength())
186 : {
187 : SAL_INFO("stoc.corerefl", "> kicking element \"" );
188 : SAL_INFO("stoc.corerefl", "" << pEntry->aKey);
189 : SAL_INFO("stoc.corerefl", "\" from cache <" );
190 : }
191 : #endif
192 20930 : _aKey2Element.erase( pEntry->aKey );
193 20930 : _aKey2Element[ pEntry->aKey = rKey ] = pEntry;
194 : }
195 : else
196 : {
197 0 : pEntry = (*iFind).second;
198 : #ifdef __CACHE_DIAGNOSE
199 : SAL_INFO("stoc.corerefl", "> replacing element \"" );
200 : SAL_INFO("stoc.corerefl", "" << pEntry->aKey);
201 : SAL_INFO("stoc.corerefl", "\" in cache <" );
202 : #endif
203 : }
204 20930 : pEntry->aVal = rValue;
205 20930 : toFront( pEntry );
206 20930 : }
207 20930 : }
208 :
209 : template< class t_Key, class t_Val, class t_KeyHash >
210 190 : inline void LRU_Cache< t_Key, t_Val, t_KeyHash >::clear()
211 : {
212 190 : ::osl::MutexGuard aGuard( _aCacheMutex );
213 190 : _aKey2Element.clear();
214 49020 : for ( sal_Int32 nPos = _nCachedElements; nPos--; )
215 : {
216 48640 : _pBlock[nPos].aKey = t_Key();
217 48640 : _pBlock[nPos].aVal = t_Val();
218 : }
219 190 : _nCachedElements = 0;
220 : #ifdef __CACHE_DIAGNOSE
221 : SAL_INFO("stoc.corerefl", "> cleared cache <" );
222 : #endif
223 190 : }
224 :
225 :
226 : /** Template instance for OUString keys, Any values.<br>
227 : */
228 : typedef LRU_Cache< OUString, css::uno::Any, OUStringHash >
229 : LRU_CacheAnyByOUString;
230 :
231 :
232 : #endif
233 :
234 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|