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
23 #include <unordered_map>
25 // __CACHE_DIAGNOSE works only for OUString keys
26 #ifdef __CACHE_DIAGNOSE
27 #include <osl/diagnose.h>
28 #include <rtl/ustrbuf.hxx>
29 #include <rtl/ustring.hxx>
30 #include <rtl/string.hxx>
31 #include <sal/log.hxx>
38 /** Implementation of a least recently used (lru) cache.
40 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
50 typedef std::unordered_map
< t_key
, Entry
*, t_hashKey
, t_equalKey
> t_key2element
;
51 t_key2element m_key2element
;
54 std::unique_ptr
<Entry
[]> m_block
;
55 mutable Entry
* m_head
;
56 mutable Entry
* m_tail
;
57 inline void toFront( Entry
* entry
) const;
60 /** Default Ctor. Does not cache.
64 /** Retrieves a pointer to value in cache. Returns 0, if none was found.
67 @return pointer to value or 0
69 inline t_val
const * lookup( t_key
const & key
) const;
71 /** Sets a value to be cached for given key.
76 inline void set( t_key
const & key
, t_val
const & val
);
78 /** Sets the number of elements to be cached. This will clear previous entries.
80 @param cacheSize number of elements to be cached
82 inline void setSize( ::std::size_t size
);
85 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
86 inline void lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::setSize(
89 m_key2element
.clear();
95 m_block
.reset( new Entry
[ m_size
] );
96 m_head
= m_block
.get();
97 m_tail
= m_block
.get() + m_size
-1;
98 for ( ::std::size_t nPos
= m_size
; nPos
--; )
100 m_block
[ nPos
].m_pred
= m_block
.get() + nPos
-1;
101 m_block
[ nPos
].m_succ
= m_block
.get() + nPos
+1;
106 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
107 inline lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::lru_cache()
115 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
116 inline void lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::toFront(
117 Entry
* entry
) const
124 m_tail
= entry
->m_pred
;
128 entry
->m_succ
->m_pred
= entry
->m_pred
;
129 entry
->m_pred
->m_succ
= entry
->m_succ
;
132 m_head
->m_pred
= entry
;
133 entry
->m_succ
= m_head
;
138 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
139 inline t_val
const * lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::lookup(
140 t_key
const & key
) const
144 typename
t_key2element::const_iterator
const iFind( m_key2element
.find( key
) );
145 if (iFind
!= m_key2element
.end())
147 Entry
* entry
= iFind
->second
;
149 #ifdef __CACHE_DIAGNOSE
150 OUStringBuffer
buf( 48 );
151 buf
.appendAscii( "> retrieved element \"" );
152 buf
.append( entry
->m_key
);
153 buf
.appendAscii( "\" from cache" );
154 SAL_INFO("stoc", buf
.makeStringAndClear() );
156 return &entry
->m_val
;
162 template< typename t_key
, typename t_val
, typename t_hashKey
, typename t_equalKey
>
163 inline void lru_cache
< t_key
, t_val
, t_hashKey
, t_equalKey
>::set(
164 t_key
const & key
, t_val
const & val
)
168 typename
t_key2element::const_iterator
const iFind( m_key2element
.find( key
) );
171 if (iFind
== m_key2element
.end())
173 entry
= m_tail
; // erase last element
174 #ifdef __CACHE_DIAGNOSE
175 if (entry
->m_key
.getLength())
177 OUStringBuffer
buf( 48 );
178 buf
.appendAscii( "> kicking element \"" );
179 buf
.append( entry
->m_key
);
180 buf
.appendAscii( "\" from cache" );
181 SAL_INFO("stoc", buf
.makeStringAndClear() );
184 m_key2element
.erase( entry
->m_key
);
186 ::std::pair
< typename
t_key2element::iterator
, bool > insertion(
187 m_key2element
.emplace( key
, entry
) );
188 OSL_ENSURE( insertion
.second
, "### inserting new cache entry failed?!" );
192 entry
= iFind
->second
;
193 #ifdef __CACHE_DIAGNOSE
194 OUStringBuffer
buf( 48 );
195 buf
.appendAscii( "> replacing element \"" );
196 buf
.append( entry
->m_key
);
197 buf
.appendAscii( "\" in cache" );
198 SAL_INFO("stoc", buf
.makeStringAndClear() );
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */