| 1 |
/* |
| 2 |
* Copyright 2016 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkLRUCache_DEFINED |
| 9 |
#define SkLRUCache_DEFINED |
| 10 |
|
| 11 |
#include "src/base/SkTInternalLList.h" |
| 12 |
#include "src/core/SkChecksum.h" |
| 13 |
#include "src/core/SkTHash.h" |
| 14 |
|
| 15 |
struct SkNoOpPurge { |
| 16 |
template <typename K, typename V> |
| 17 |
void operator()(void* /* context */, const K& /* k */, const V* /* v */) const {} |
| 18 |
}; |
| 19 |
|
| 20 |
/** |
| 21 |
* A generic LRU cache. |
| 22 |
*/ |
| 23 |
template <typename K, typename V, typename HashK = SkGoodHash, typename PurgeCB = SkNoOpPurge> |
| 24 |
class SkLRUCache { |
| 25 |
private: |
| 26 |
struct Entry { |
| 27 |
template<typename K1, typename V1> |
| 28 |
Entry(K1&& key, V1&& value) |
| 29 |
: fKey(std::forward<K1>(key)) |
| 30 |
, fValue(std::forward<V1>(value)) {} |
| 31 |
|
| 32 |
const K fKey; |
| 33 |
V fValue; |
| 34 |
|
| 35 |
SK_DECLARE_INTERNAL_LLIST_INTERFACE(Entry); |
| 36 |
}; |
| 37 |
|
| 38 |
public: |
| 39 |
explicit SkLRUCache(int maxCount, void* context = nullptr) |
| 40 |
: fMaxCount(maxCount) |
| 41 |
, fContext(context) {} |
| 42 |
SkLRUCache() = delete; |
| 43 |
|
| 44 |
~SkLRUCache() { |
| 45 |
Entry* node = fLRU.head(); |
| 46 |
while (node) { |
| 47 |
fLRU.remove(node); |
| 48 |
delete node; |
| 49 |
node = fLRU.head(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Make noncopyable |
| 54 |
SkLRUCache(const SkLRUCache&) = delete; |
| 55 |
SkLRUCache& operator=(const SkLRUCache&) = delete; |
| 56 |
|
| 57 |
V* find(const K& key) { |
| 58 |
Entry** value = fMap.find(key); |
| 59 |
if (!value) { |
| 60 |
return nullptr; |
| 61 |
} |
| 62 |
Entry* entry = *value; |
| 63 |
if (entry != fLRU.head()) { |
| 64 |
fLRU.remove(entry); |
| 65 |
fLRU.addToHead(entry); |
| 66 |
} // else it's already at head position, don't need to do anything |
| 67 |
return &entry->fValue; |
| 68 |
} |
| 69 |
|
| 70 |
V* insert(Entry* entry) { |
| 71 |
fMap.set(entry); |
| 72 |
fLRU.addToHead(entry); |
| 73 |
while (fMap.count() > fMaxCount) { |
| 74 |
this->remove(fLRU.tail()->fKey); |
| 75 |
} |
| 76 |
return &entry->fValue; |
| 77 |
} |
| 78 |
|
| 79 |
template<typename K1, typename V1> |
| 80 |
V* insert(K1&& key, V1&& value) { |
| 81 |
SkASSERT(!this->find(key)); |
| 82 |
return this->insert(new Entry(std::forward<K1>(key), std::forward<V1>(value))); |
| 83 |
} |
| 84 |
|
| 85 |
template<typename K1, typename V1> |
| 86 |
V* insert_or_update(K1&& key, V1&& value) { |
| 87 |
if (V* found = this->find(key)) { |
| 88 |
*found = std::forward<V1>(value); |
| 89 |
return found; |
| 90 |
} |
| 91 |
return this->insert(new Entry(std::forward<K1>(key), std::forward<V1>(value))); |
| 92 |
} |
| 93 |
|
| 94 |
int count() const { |
| 95 |
return fMap.count(); |
| 96 |
} |
| 97 |
|
| 98 |
template <typename Fn> // f(const K*, V*) |
| 99 |
void foreach(Fn&& fn) { |
| 100 |
typename SkTInternalLList<Entry>::Iter iter; |
| 101 |
for (Entry* e = iter.init(fLRU, SkTInternalLList<Entry>::Iter::kHead_IterStart); e; |
| 102 |
e = iter.next()) { |
| 103 |
fn(&e->fKey, &e->fValue); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void reset() { |
| 108 |
fMap.reset(); |
| 109 |
for (Entry* e = fLRU.head(); e; e = fLRU.head()) { |
| 110 |
fLRU.remove(e); |
| 111 |
delete e; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void remove(const K& key) { |
| 116 |
Entry** value = fMap.find(key); |
| 117 |
SkASSERT(value); |
| 118 |
Entry* entry = *value; |
| 119 |
SkASSERT(key == entry->fKey); |
| 120 |
PurgeCB()(fContext, key, &entry->fValue); |
| 121 |
fMap.remove(key); |
| 122 |
fLRU.remove(entry); |
| 123 |
delete entry; |
| 124 |
} |
| 125 |
|
| 126 |
private: |
| 127 |
struct Traits { |
| 128 |
static const K& GetKey(Entry* e) { |
| 129 |
return e->fKey; |
| 130 |
} |
| 131 |
|
| 132 |
static uint32_t Hash(const K& k) { |
| 133 |
return HashK()(k); |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
int fMaxCount; |
| 138 |
skia_private::THashTable<Entry*, K, Traits> fMap; |
| 139 |
SkTInternalLList<Entry> fLRU; |
| 140 |
void* fContext; |
| 141 |
}; |
| 142 |
|
| 143 |
#endif |