]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/persistent_cache/lrulist.h
1 // Copyright (c) 2013, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
12 #include "util/mutexlock.h"
16 // LRU element definition
18 // Any object that needs to be part of the LRU algorithm should extend this
22 explicit LRUElement() : next_(nullptr), prev_(nullptr), refs_(0) {}
24 virtual ~LRUElement() { assert(!refs_
); }
28 std::atomic
<size_t> refs_
;
33 // In place LRU implementation. There is no copy or allocation involved when
34 // inserting or removing an element. This makes the data structure slim
44 // Push element into the LRU at the cold end
45 inline void Push(T
* const t
) {
52 assert((!head_
&& !tail_
) || (head_
&& tail_
));
53 assert(!head_
|| !head_
->prev_
);
54 assert(!tail_
|| !tail_
->next_
);
67 // Unlink the element from the LRU
68 inline void Unlink(T
* const t
) {
73 // Evict an element from the LRU
77 assert(tail_
&& head_
);
78 assert(!tail_
->next_
);
79 assert(!head_
->prev_
);
82 while (t
&& t
->refs_
) {
87 // nothing can be evicted
98 // Move the element from the front of the list to the back of the list
99 inline void Touch(T
* const t
) {
105 // Check if the LRU is empty
106 inline bool IsEmpty() const {
108 return !head_
&& !tail_
;
112 // Unlink an element from the LRU
113 void UnlinkImpl(T
* const t
) {
118 assert(head_
&& tail_
);
119 assert(t
->prev_
|| head_
== t
);
120 assert(t
->next_
|| tail_
== t
);
123 t
->prev_
->next_
= t
->next_
;
126 t
->next_
->prev_
= t
->prev_
;
130 tail_
= tail_
->prev_
;
133 head_
= head_
->next_
;
136 t
->next_
= t
->prev_
= nullptr;
139 // Insert an element at the hot end
140 inline void PushBack(T
* const t
) {
145 inline void PushBackImpl(T
* const t
) {
152 assert((!head_
&& !tail_
) || (head_
&& tail_
));
153 assert(!head_
|| !head_
->prev_
);
154 assert(!tail_
|| !tail_
->next_
);
167 mutable port::Mutex lock_
; // synchronization primitive
168 T
* head_
= nullptr; // front (cold)
169 T
* tail_
= nullptr; // back (hot)
172 } // namespace rocksdb