Skip to content
Snippets Groups Projects
Commit 1ecb33af authored by Marek Vavruša's avatar Marek Vavruša
Browse files

generic/lru: evictions counter to aid efficiency prediction

parent 70d31551
Branches
Tags
No related merge requests found
......@@ -88,6 +88,7 @@ typedef void (*lru_free_f)(void *baton, void *ptr);
/** @brief LRU structure base. */
#define lru_hash_struct \
uint32_t size; /**< Number of slots */ \
uint32_t evictions; /**< Number of evictions. */ \
uint32_t stride; /**< Stride of the 'slots' array */ \
lru_free_f evict; /**< Eviction function */ \
void *baton; /**< Passed to eviction function */
......@@ -107,6 +108,18 @@ struct { \
} slots[]; \
}
/** Get slot at given index. */
static inline void *lru_slot_at(struct lru_hash_base *lru, uint32_t id)
{
return (struct lru_slot *)(lru->slots + (id * lru->stride));
}
/** Get pointer to slot value. */
static inline void *lru_slot_val(struct lru_slot *slot, size_t offset)
{
return ((char *)slot) + offset;
}
/** @internal Slot data getter */
static inline void *lru_slot_get(struct lru_hash_base *lru, const char *key, uint32_t len, size_t offset)
{
......@@ -114,9 +127,9 @@ static inline void *lru_slot_get(struct lru_hash_base *lru, const char *key, uin
return NULL;
}
uint32_t id = hash(key, len) % lru->size;
struct lru_slot *slot = (struct lru_slot *)(lru->slots + (id * lru->stride));
struct lru_slot *slot = lru_slot_at(lru, id);
if (lru_slot_match(slot, key, len)) {
return ((char *)slot) + offset;
return lru_slot_val(slot, offset);
}
return NULL;
}
......@@ -128,12 +141,13 @@ static inline void *lru_slot_set(struct lru_hash_base *lru, const char *key, uin
return NULL;
}
uint32_t id = hash(key, len) % lru->size;
struct lru_slot *slot = (struct lru_slot *)(lru->slots + (id * lru->stride));
struct lru_slot *slot = lru_slot_at(lru, id);
if (!lru_slot_match(slot, key, len)) {
if (slot->key) {
lru->evictions += 1;
free(slot->key);
if (lru->evict) {
lru->evict(lru->baton, ((char *)slot) + offset);
lru->evict(lru->baton, lru_slot_val(slot, offset));
}
}
memset(slot, 0, lru->stride);
......@@ -144,7 +158,7 @@ static inline void *lru_slot_set(struct lru_hash_base *lru, const char *key, uin
memcpy(slot->key, key, len);
slot->len = len;
}
return ((char *)slot) + offset;
return lru_slot_val(slot, offset);
}
/**
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment