40 #define LRU_CACHE_THREAD_SAFE
86 "Number of negative inserts");
90 "Number of value changes");
95 "Number of allocated bytes ");
105 template<
class Key,
class Value>
112 template<
class M>
class MemoryAllocator;
145 unsigned int num_bytes_bitmap = num_slots / 8;
148 assert(num_slots >= 2*bits_per_block_);
151 const unsigned int num_bytes_memory =
sizeof(T) * num_slots;
154 bitmap_ =
reinterpret_cast<uint64_t *
>(scalloc(num_bytes_bitmap, 1));
155 memory_ =
reinterpret_cast<T *
>(scalloc(num_bytes_memory, 1));
168 return static_cast<double>(
sizeof(T)) + 1.0/8.0;
188 new (
static_cast<void*
>(
mem)) T(
object);
214 while (~
bitmap_[bitmap_block] == 0)
218 while (this->
GetBit(next_free_slot_))
234 const unsigned int position = slot -
memory_;
253 inline bool GetBit(
const unsigned position) {
263 inline void SetBit(
const unsigned position) {
339 this->
next->prev = entry;
357 this->
prev->next = entry;
379 template<
class T>
class ListEntryContent :
public ListEntry<T> {
470 assert(!entry->IsLonely());
472 entry->RemoveFromList();
494 T result = popped->content();
510 const Key &empty_key,
511 uint32_t (*hasher)(
const Key &key),
527 cache_.bytes_allocated());
529 #ifdef LRU_CACHE_THREAD_SAFE
530 int retval = pthread_mutex_init(&
lock_, NULL);
541 #ifdef LRU_CACHE_THREAD_SAFE
542 pthread_mutex_destroy(&
lock_);
556 virtual bool Insert(
const Key &key,
const Value &value) {
569 cache_.Insert(key, entry);
583 cache_.Insert(key, entry);
600 bool retval =
DoLookup(key, &entry);
627 cache_.Insert(key, entry);
640 virtual bool Lookup(
const Key &key, Value *value,
bool update_lru =
true) {
654 *value = entry.value;
682 entry.list_entry->RemoveFromList();
706 cache_.bytes_allocated());
755 bool rc = this->
DoLookup(*key, &entry);
757 *value = entry.value;
806 inline bool DoLookup(
const Key &key, CacheEntry *entry) {
807 return cache_.Lookup(key, entry);
816 inline void Touch(
const CacheEntry &entry) {
837 #ifdef LRU_CACHE_THREAD_SAFE
838 pthread_mutex_lock(&
lock_);
846 #ifdef LRU_CACHE_THREAD_SAFE
847 pthread_mutex_unlock(&
lock_);
869 #ifdef LRU_CACHE_THREAD_SAFE
876 #endif // CVMFS_LRU_H_
static double GetEntrySize()
virtual ~MemoryAllocator()
int64_t Xadd(class Counter *counter, const int64_t delta)
virtual bool IsListHead() const =0
uint64_t bytes_allocated()
SmallHashFixed< Key, CacheEntry > cache_
void InsertAsSuccessor(ListEntryContent< T > *entry)
Counters(perf::StatisticsTemplate statistics)
const unsigned int cache_size_
uint64_t bytes_allocated_
ListEntryHead< Key > lru_list_
virtual bool Insert(const Key &key, const Value &value)
ListEntry(const ListEntry< T > &other)
assert((mem||(size==0))&&"Out Of Memory")
virtual void FilterGet(Key *key, Value *value)
MemoryAllocator(const unsigned int num_slots)
ListEntryContent< Key > * list_entry
static double GetEntrySize()
virtual bool FilterNext()
ListEntryHead(ConcreteMemoryAllocator *allocator)
ListEntryContent< T > * PushBack(T content)
void UnsetBit(const unsigned position)
Counter * RegisterTemplated(const std::string &name_minor, const std::string &desc)
perf::Counter * sz_allocated
void InsertAsPredecessor(ListEntryContent< T > *entry)
ListEntryContent< Key > ConcreteListEntryContent
T Pop(ListEntry< T > *popped_entry)
virtual void RemoveFromList()=0
void Set(const int64_t val)
unsigned int next_free_slot_
bool GetBit(const unsigned position)
static double GetEntrySize()
bool DoLookup(const Key &key, CacheEntry *entry)
virtual void FilterDelete()
virtual bool UpdateValue(const Key &key, const Value &value)
ListEntryContent(T content)
perf::Counter * n_insert_negative
virtual void FilterBegin()
void MoveToBack(ListEntryContent< T > *entry)
virtual void Update(const Key &key)
void Inc(class Counter *counter)
T * Construct(const T object)
unsigned int num_free_slots_
unsigned int cache_gauge_
ListEntry< Key > * filter_entry_
LruCache(const unsigned cache_size, const Key &empty_key, uint32_t(*hasher)(const Key &key), perf::StatisticsTemplate statistics)
virtual bool Lookup(const Key &key, Value *value, bool update_lru=true)
ConcreteMemoryAllocator * allocator_
void SetBit(const unsigned position)
ConcreteMemoryAllocator allocator_
ListEntry< T > & operator=(const ListEntry< T > &other)
virtual bool Forget(const Key &key)
MemoryAllocator< ConcreteListEntryContent > ConcreteMemoryAllocator
perf::Counter * n_update_value
void Touch(const CacheEntry &entry)
perf::Counter * n_replace