xref: /AOO41X/main/stoc/source/security/lru_cache.h (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef _STOC_SEC_LRU_CACHE_H_
28*cdf0e10cSrcweir #define _STOC_SEC_LRU_CACHE_H_
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <hash_map>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir // __CACHE_DIAGNOSE works only for OUString keys
33*cdf0e10cSrcweir #ifdef __CACHE_DIAGNOSE
34*cdf0e10cSrcweir #include <osl/diagnose.h>
35*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
36*cdf0e10cSrcweir #include <rtl/ustring.hxx>
37*cdf0e10cSrcweir #include <rtl/string.hxx>
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir namespace stoc_sec
42*cdf0e10cSrcweir {
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir /** Implementation of a least recently used (lru) cache.
45*cdf0e10cSrcweir */
46*cdf0e10cSrcweir template< typename t_key, typename t_val, typename t_hashKey, typename t_equalKey >
47*cdf0e10cSrcweir class lru_cache
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir     struct Entry
50*cdf0e10cSrcweir     {
51*cdf0e10cSrcweir         t_key m_key;
52*cdf0e10cSrcweir         t_val m_val;
53*cdf0e10cSrcweir         Entry * m_pred;
54*cdf0e10cSrcweir         Entry * m_succ;
55*cdf0e10cSrcweir     };
56*cdf0e10cSrcweir     typedef ::std::hash_map< t_key, Entry *, t_hashKey, t_equalKey > t_key2element;
57*cdf0e10cSrcweir     t_key2element m_key2element;
58*cdf0e10cSrcweir     ::std::size_t m_size;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     Entry * m_block;
61*cdf0e10cSrcweir     mutable Entry * m_head;
62*cdf0e10cSrcweir     mutable Entry * m_tail;
63*cdf0e10cSrcweir     inline void toFront( Entry * entry ) const SAL_THROW( () );
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir public:
66*cdf0e10cSrcweir     /** Default Ctor.  Does not cache.
67*cdf0e10cSrcweir     */
68*cdf0e10cSrcweir     inline lru_cache() SAL_THROW( () );
69*cdf0e10cSrcweir     /** Ctor.
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir         @param size number of elements to be cached; default param set to 128
72*cdf0e10cSrcweir     */
73*cdf0e10cSrcweir     inline lru_cache( ::std::size_t size ) SAL_THROW( () );
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     /** Destructor: releases all cached elements and keys.
76*cdf0e10cSrcweir     */
77*cdf0e10cSrcweir     inline ~lru_cache() SAL_THROW( () );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     /** Retrieves a pointer to value in cache.  Returns 0, if none was found.
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir         @param key a key
82*cdf0e10cSrcweir         @return pointer to value or 0
83*cdf0e10cSrcweir     */
84*cdf0e10cSrcweir     inline t_val const * lookup( t_key const & key ) const SAL_THROW( () );
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     /** Sets a value to be cached for given key.
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         @param key a key
89*cdf0e10cSrcweir         @param val a value
90*cdf0e10cSrcweir     */
91*cdf0e10cSrcweir     inline void set( t_key const & key, t_val const & val ) SAL_THROW( () );
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     /** Tests whether a value is cached for given key.
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         @param key a key
96*cdf0e10cSrcweir         @return true, if value is cached
97*cdf0e10cSrcweir     */
98*cdf0e10cSrcweir     inline bool has( t_key const & key ) const SAL_THROW( () );
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     /** Clears the cache, releasing all cached elements and keys.
101*cdf0e10cSrcweir     */
102*cdf0e10cSrcweir     inline void clear() SAL_THROW( () );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     /** Sets the number of elements to be cached.  This will clear previous entries.
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir         @param cacheSize number of elements to be cached
107*cdf0e10cSrcweir     */
108*cdf0e10cSrcweir     inline void setSize( ::std::size_t size ) SAL_THROW( () );
109*cdf0e10cSrcweir };
110*cdf0e10cSrcweir //__________________________________________________________________________________________________
111*cdf0e10cSrcweir template< typename t_key, typename t_val, typename t_hashKey, typename t_equalKey >
112*cdf0e10cSrcweir inline void lru_cache< t_key, t_val, t_hashKey, t_equalKey >::setSize(
113*cdf0e10cSrcweir     ::std::size_t size ) SAL_THROW( () )
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir     m_key2element.clear();
116*cdf0e10cSrcweir     delete [] m_block;
117*cdf0e10cSrcweir     m_block = 0;
118*cdf0e10cSrcweir     m_size = size;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     if (0 < m_size)
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir         m_block = new Entry[ m_size ];
123*cdf0e10cSrcweir         m_head = m_block;
124*cdf0e10cSrcweir         m_tail = m_block + m_size -1;
125*cdf0e10cSrcweir         for ( ::std::size_t nPos = m_size; nPos--; )
126*cdf0e10cSrcweir         {
127*cdf0e10cSrcweir             m_block[ nPos ].m_pred = m_block + nPos -1;
128*cdf0e10cSrcweir             m_block[ nPos ].m_succ = m_block + nPos +1;
129*cdf0e10cSrcweir         }
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir //__________________________________________________________________________________________________
133*cdf0e10cSrcweir template< typename t_key, typename t_val, typename t_hashKey, typename t_equalKey >
134*cdf0e10cSrcweir inline lru_cache< t_key, t_val, t_hashKey, t_equalKey >::lru_cache(
135*cdf0e10cSrcweir     ::std::size_t size ) SAL_THROW( () )
136*cdf0e10cSrcweir     : m_size( 0 )
137*cdf0e10cSrcweir     , m_block( 0 )
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir     setSize( size );
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir //__________________________________________________________________________________________________
142*cdf0e10cSrcweir template< typename t_key, typename t_val, typename t_hashKey, typename t_equalKey >
143*cdf0e10cSrcweir inline lru_cache< t_key, t_val, t_hashKey, t_equalKey >::lru_cache() SAL_THROW( () )
144*cdf0e10cSrcweir     : m_size( 0 )
145*cdf0e10cSrcweir     , m_block( 0 )
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir //__________________________________________________________________________________________________
149*cdf0e10cSrcweir template< typename t_key, typename t_val, typename t_hashKey, typename t_equalKey >
150*cdf0e10cSrcweir inline lru_cache< t_key, t_val, t_hashKey, t_equalKey >::~lru_cache()
151*cdf0e10cSrcweir     SAL_THROW( () )
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir     delete [] m_block;
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir //__________________________________________________________________________________________________
156*cdf0e10cSrcweir template< typename t_key, typename t_val, typename t_hashKey, typename t_equalKey >
157*cdf0e10cSrcweir inline void lru_cache< t_key, t_val, t_hashKey, t_equalKey >::toFront(
158*cdf0e10cSrcweir     Entry * entry ) const SAL_THROW( () )
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir     if (entry != m_head)
161*cdf0e10cSrcweir     {
162*cdf0e10cSrcweir         // cut out element
163*cdf0e10cSrcweir         if (entry == m_tail)
164*cdf0e10cSrcweir         {
165*cdf0e10cSrcweir             m_tail = entry->m_pred;
166*cdf0e10cSrcweir         }
167*cdf0e10cSrcweir         else
168*cdf0e10cSrcweir         {
169*cdf0e10cSrcweir             entry->m_succ->m_pred = entry->m_pred;
170*cdf0e10cSrcweir             entry->m_pred->m_succ = entry->m_succ;
171*cdf0e10cSrcweir         }
172*cdf0e10cSrcweir         // push to front
173*cdf0e10cSrcweir         m_head->m_pred = entry;
174*cdf0e10cSrcweir         entry->m_succ = m_head;
175*cdf0e10cSrcweir         m_head = entry;
176*cdf0e10cSrcweir     }
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir //__________________________________________________________________________________________________
179*cdf0e10cSrcweir template< typename t_key, typename t_val, typename t_hashKey, typename t_equalKey >
180*cdf0e10cSrcweir inline bool lru_cache< t_key, t_val, t_hashKey, t_equalKey >::has(
181*cdf0e10cSrcweir     t_key const & key ) const SAL_THROW( () )
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     typename t_key2element::const_iterator const iFind( m_key2element.find( key ) );
184*cdf0e10cSrcweir     return (iFind != m_key2element.end());
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir //__________________________________________________________________________________________________
187*cdf0e10cSrcweir template< typename t_key, typename t_val, typename t_hashKey, typename t_equalKey >
188*cdf0e10cSrcweir inline t_val const * lru_cache< t_key, t_val, t_hashKey, t_equalKey >::lookup(
189*cdf0e10cSrcweir     t_key const & key ) const SAL_THROW( () )
190*cdf0e10cSrcweir {
191*cdf0e10cSrcweir     if (0 < m_size)
192*cdf0e10cSrcweir     {
193*cdf0e10cSrcweir         typename t_key2element::const_iterator const iFind( m_key2element.find( key ) );
194*cdf0e10cSrcweir         if (iFind != m_key2element.end())
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             Entry * entry = iFind->second;
197*cdf0e10cSrcweir             toFront( entry );
198*cdf0e10cSrcweir #ifdef __CACHE_DIAGNOSE
199*cdf0e10cSrcweir             ::rtl::OUStringBuffer buf( 48 );
200*cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("> retrieved element \"") );
201*cdf0e10cSrcweir             buf.append( entry->m_key );
202*cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" from cache") );
203*cdf0e10cSrcweir             ::rtl::OString str( ::rtl::OUStringToOString(
204*cdf0e10cSrcweir                 buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US ) );
205*cdf0e10cSrcweir             OSL_TRACE( str.getStr() );
206*cdf0e10cSrcweir #endif
207*cdf0e10cSrcweir             return &entry->m_val;
208*cdf0e10cSrcweir         }
209*cdf0e10cSrcweir     }
210*cdf0e10cSrcweir     return 0;
211*cdf0e10cSrcweir }
212*cdf0e10cSrcweir //__________________________________________________________________________________________________
213*cdf0e10cSrcweir template< typename t_key, typename t_val, typename t_hashKey, typename t_equalKey >
214*cdf0e10cSrcweir inline void lru_cache< t_key, t_val, t_hashKey, t_equalKey >::set(
215*cdf0e10cSrcweir     t_key const & key, t_val const & val ) SAL_THROW( () )
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir     if (0 < m_size)
218*cdf0e10cSrcweir     {
219*cdf0e10cSrcweir         typename t_key2element::const_iterator const iFind( m_key2element.find( key ) );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir         Entry * entry;
222*cdf0e10cSrcweir         if (iFind == m_key2element.end())
223*cdf0e10cSrcweir         {
224*cdf0e10cSrcweir             entry = m_tail; // erase last element
225*cdf0e10cSrcweir #ifdef __CACHE_DIAGNOSE
226*cdf0e10cSrcweir             if (entry->m_key.getLength())
227*cdf0e10cSrcweir             {
228*cdf0e10cSrcweir                 ::rtl::OUStringBuffer buf( 48 );
229*cdf0e10cSrcweir                 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("> kicking element \"") );
230*cdf0e10cSrcweir                 buf.append( entry->m_key );
231*cdf0e10cSrcweir                 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" from cache") );
232*cdf0e10cSrcweir                 ::rtl::OString str( ::rtl::OUStringToOString(
233*cdf0e10cSrcweir                     buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US ) );
234*cdf0e10cSrcweir                 OSL_TRACE( str.getStr() );
235*cdf0e10cSrcweir             }
236*cdf0e10cSrcweir #endif
237*cdf0e10cSrcweir             m_key2element.erase( entry->m_key );
238*cdf0e10cSrcweir             entry->m_key = key;
239*cdf0e10cSrcweir             ::std::pair< typename t_key2element::iterator, bool > insertion(
240*cdf0e10cSrcweir                 m_key2element.insert( typename t_key2element::value_type( key, entry ) ) );
241*cdf0e10cSrcweir #ifdef __CACHE_DIAGNOSE
242*cdf0e10cSrcweir             OSL_ENSURE( insertion.second, "### inserting new cache entry failed?!" );
243*cdf0e10cSrcweir #endif
244*cdf0e10cSrcweir         }
245*cdf0e10cSrcweir         else
246*cdf0e10cSrcweir         {
247*cdf0e10cSrcweir             entry = iFind->second;
248*cdf0e10cSrcweir #ifdef __CACHE_DIAGNOSE
249*cdf0e10cSrcweir             ::rtl::OUStringBuffer buf( 48 );
250*cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("> replacing element \"") );
251*cdf0e10cSrcweir             buf.append( entry->m_key );
252*cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" in cache") );
253*cdf0e10cSrcweir             ::rtl::OString str( ::rtl::OUStringToOString(
254*cdf0e10cSrcweir                 buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US ) );
255*cdf0e10cSrcweir             OSL_TRACE( str.getStr() );
256*cdf0e10cSrcweir #endif
257*cdf0e10cSrcweir         }
258*cdf0e10cSrcweir         entry->m_val = val;
259*cdf0e10cSrcweir         toFront( entry );
260*cdf0e10cSrcweir     }
261*cdf0e10cSrcweir }
262*cdf0e10cSrcweir //__________________________________________________________________________________________________
263*cdf0e10cSrcweir template< typename t_key, typename t_val, typename t_hashKey, typename t_equalKey >
264*cdf0e10cSrcweir inline void lru_cache< t_key, t_val, t_hashKey, t_equalKey >::clear() SAL_THROW( () )
265*cdf0e10cSrcweir {
266*cdf0e10cSrcweir     m_key2element.clear();
267*cdf0e10cSrcweir     for ( ::std::size_t nPos = m_size; nPos--; )
268*cdf0e10cSrcweir     {
269*cdf0e10cSrcweir         m_block[ nPos ].m_key = t_key();
270*cdf0e10cSrcweir         m_block[ nPos ].m_val = t_val();
271*cdf0e10cSrcweir     }
272*cdf0e10cSrcweir #ifdef __CACHE_DIAGNOSE
273*cdf0e10cSrcweir     OSL_TRACE( "> cleared cache\n" );
274*cdf0e10cSrcweir #endif
275*cdf0e10cSrcweir }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir }
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir #endif
280