2016-03-18 23:50:34 +01:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2016-03-18 23:50:34 +01:00
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2016-05-21 01:16:51 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2016-03-18 23:50:34 +01:00
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#include "util/mutexlock.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2016-03-18 23:50:34 +01:00
|
|
|
|
|
|
|
// LRU element definition
|
|
|
|
//
|
|
|
|
// Any object that needs to be part of the LRU algorithm should extend this
|
|
|
|
// class
|
|
|
|
template <class T>
|
|
|
|
struct LRUElement {
|
|
|
|
explicit LRUElement() : next_(nullptr), prev_(nullptr), refs_(0) {}
|
|
|
|
|
|
|
|
virtual ~LRUElement() { assert(!refs_); }
|
|
|
|
|
|
|
|
T* next_;
|
|
|
|
T* prev_;
|
|
|
|
std::atomic<size_t> refs_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// LRU implementation
|
|
|
|
//
|
|
|
|
// In place LRU implementation. There is no copy or allocation involved when
|
|
|
|
// inserting or removing an element. This makes the data structure slim
|
|
|
|
template <class T>
|
|
|
|
class LRUList {
|
|
|
|
public:
|
|
|
|
virtual ~LRUList() {
|
|
|
|
MutexLock _(&lock_);
|
|
|
|
assert(!head_);
|
|
|
|
assert(!tail_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push element into the LRU at the cold end
|
|
|
|
inline void Push(T* const t) {
|
|
|
|
assert(t);
|
|
|
|
assert(!t->next_);
|
|
|
|
assert(!t->prev_);
|
|
|
|
|
|
|
|
MutexLock _(&lock_);
|
|
|
|
|
|
|
|
assert((!head_ && !tail_) || (head_ && tail_));
|
|
|
|
assert(!head_ || !head_->prev_);
|
|
|
|
assert(!tail_ || !tail_->next_);
|
|
|
|
|
|
|
|
t->next_ = head_;
|
|
|
|
if (head_) {
|
|
|
|
head_->prev_ = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
head_ = t;
|
|
|
|
if (!tail_) {
|
|
|
|
tail_ = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlink the element from the LRU
|
|
|
|
inline void Unlink(T* const t) {
|
|
|
|
MutexLock _(&lock_);
|
|
|
|
UnlinkImpl(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evict an element from the LRU
|
|
|
|
inline T* Pop() {
|
|
|
|
MutexLock _(&lock_);
|
|
|
|
|
|
|
|
assert(tail_ && head_);
|
|
|
|
assert(!tail_->next_);
|
|
|
|
assert(!head_->prev_);
|
|
|
|
|
|
|
|
T* t = head_;
|
|
|
|
while (t && t->refs_) {
|
|
|
|
t = t->next_;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!t) {
|
|
|
|
// nothing can be evicted
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!t->refs_);
|
|
|
|
|
|
|
|
// unlike the element
|
|
|
|
UnlinkImpl(t);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the element from the front of the list to the back of the list
|
|
|
|
inline void Touch(T* const t) {
|
|
|
|
MutexLock _(&lock_);
|
|
|
|
UnlinkImpl(t);
|
|
|
|
PushBackImpl(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the LRU is empty
|
|
|
|
inline bool IsEmpty() const {
|
|
|
|
MutexLock _(&lock_);
|
|
|
|
return !head_ && !tail_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Unlink an element from the LRU
|
|
|
|
void UnlinkImpl(T* const t) {
|
|
|
|
assert(t);
|
|
|
|
|
|
|
|
lock_.AssertHeld();
|
|
|
|
|
|
|
|
assert(head_ && tail_);
|
|
|
|
assert(t->prev_ || head_ == t);
|
|
|
|
assert(t->next_ || tail_ == t);
|
|
|
|
|
|
|
|
if (t->prev_) {
|
|
|
|
t->prev_->next_ = t->next_;
|
|
|
|
}
|
|
|
|
if (t->next_) {
|
|
|
|
t->next_->prev_ = t->prev_;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tail_ == t) {
|
|
|
|
tail_ = tail_->prev_;
|
|
|
|
}
|
|
|
|
if (head_ == t) {
|
|
|
|
head_ = head_->next_;
|
|
|
|
}
|
|
|
|
|
|
|
|
t->next_ = t->prev_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert an element at the hot end
|
|
|
|
inline void PushBack(T* const t) {
|
|
|
|
MutexLock _(&lock_);
|
|
|
|
PushBackImpl(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void PushBackImpl(T* const t) {
|
|
|
|
assert(t);
|
|
|
|
assert(!t->next_);
|
|
|
|
assert(!t->prev_);
|
|
|
|
|
|
|
|
lock_.AssertHeld();
|
|
|
|
|
|
|
|
assert((!head_ && !tail_) || (head_ && tail_));
|
|
|
|
assert(!head_ || !head_->prev_);
|
|
|
|
assert(!tail_ || !tail_->next_);
|
|
|
|
|
|
|
|
t->prev_ = tail_;
|
|
|
|
if (tail_) {
|
|
|
|
tail_->next_ = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
tail_ = t;
|
|
|
|
if (!head_) {
|
|
|
|
head_ = tail_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 09:29:40 +02:00
|
|
|
mutable port::Mutex lock_; // synchronization primitive
|
2016-03-18 23:50:34 +01:00
|
|
|
T* head_ = nullptr; // front (cold)
|
|
|
|
T* tail_ = nullptr; // back (hot)
|
|
|
|
};
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2016-05-21 01:16:51 +02:00
|
|
|
|
|
|
|
#endif
|