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_SECURITY_LRU_CACHE_H
20 #define INCLUDED_STOC_SOURCE_SECURITY_LRU_CACHE_H
22 #include <unordered_map>
24 // __CACHE_DIAGNOSE works only for OUString keys
25 #ifdef __CACHE_DIAGNOSE
26 #include <osl/diagnose.h>
27 #include <rtl/ustrbuf.hxx>
28 #include <rtl/ustring.hxx>
29 #include <rtl/string.hxx>
36 /** Implementation of a least recently used (lru) cache.
38 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
48 typedef std::unordered_map
< t_key
, Entry
*, t_hashKey
, t_equalKey
> t_key2element
;
49 t_key2element m_key2element
;
53 mutable Entry
* m_head
;
54 mutable Entry
* m_tail
;
55 inline void toFront( Entry
* entry
) const;
58 /** Default Ctor. Does not cache.
62 /** Destructor: releases all cached elements and keys.
66 /** Retrieves a pointer to value in cache. Returns 0, if none was found.
69 @return pointer to value or 0
71 inline t_val
const * lookup( t_key
const & key
) const;
73 /** Sets a value to be cached for given key.
78 inline void set( t_key
const & key
, t_val
const & val
);
80 /** Sets the number of elements to be cached. This will clear previous entries.
82 @param cacheSize number of elements to be cached
84 inline void setSize( ::std::size_t size
);
87 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
88 inline void lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::setSize(
91 m_key2element
.clear();
98 m_block
= new Entry
[ m_size
];
100 m_tail
= m_block
+ m_size
-1;
101 for ( ::std::size_t nPos
= m_size
; nPos
--; )
103 m_block
[ nPos
].m_pred
= m_block
+ nPos
-1;
104 m_block
[ nPos
].m_succ
= m_block
+ nPos
+1;
109 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
110 inline lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::lru_cache()
118 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
119 inline lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::~lru_cache()
124 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
125 inline void lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::toFront(
126 Entry
* entry
) const
133 m_tail
= entry
->m_pred
;
137 entry
->m_succ
->m_pred
= entry
->m_pred
;
138 entry
->m_pred
->m_succ
= entry
->m_succ
;
141 m_head
->m_pred
= entry
;
142 entry
->m_succ
= m_head
;
147 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
148 inline t_val
const * lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::lookup(
149 t_key
const & key
) const
153 typename
t_key2element::const_iterator
const iFind( m_key2element
.find( key
) );
154 if (iFind
!= m_key2element
.end())
156 Entry
* entry
= iFind
->second
;
158 #ifdef __CACHE_DIAGNOSE
159 OUStringBuffer
buf( 48 );
160 buf
.appendAscii( "> retrieved element \"" );
161 buf
.append( entry
->m_key
);
162 buf
.appendAscii( "\" from cache" );
163 SAL_INFO("stoc", buf
.makeStringAndClear() );
165 return &entry
->m_val
;
171 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
172 inline void lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::set(
173 t_key
const & key
, t_val
const & val
)
177 typename
t_key2element::const_iterator
const iFind( m_key2element
.find( key
) );
180 if (iFind
== m_key2element
.end())
182 entry
= m_tail
; // erase last element
183 #ifdef __CACHE_DIAGNOSE
184 if (entry
->m_key
.getLength())
186 OUStringBuffer
buf( 48 );
187 buf
.appendAscii( "> kicking element \"" );
188 buf
.append( entry
->m_key
);
189 buf
.appendAscii( "\" from cache" );
190 SAL_INFO("stoc", buf
.makeStringAndClear() );
193 m_key2element
.erase( entry
->m_key
);
195 ::std::pair
< typename
t_key2element::iterator
, bool > insertion(
196 m_key2element
.insert( typename
t_key2element::value_type( key
, entry
) ) );
197 #ifdef __CACHE_DIAGNOSE
198 OSL_ENSURE( insertion
.second
, "### inserting new cache entry failed?!" );
200 (void) insertion
; // avoid warnings
204 entry
= iFind
->second
;
205 #ifdef __CACHE_DIAGNOSE
206 OUStringBuffer
buf( 48 );
207 buf
.appendAscii( "> replacing element \"" );
208 buf
.append( entry
->m_key
);
209 buf
.appendAscii( "\" in cache" );
210 SAL_INFO("stoc", buf
.makeStringAndClear() );
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */