From: Marek VavruĊĦa Date: Mon, 13 Jul 2015 13:35:19 +0000 (+0200) Subject: generic/lru: evictions counter to aid efficiency prediction X-Git-Tag: v1.0.0-beta1~77^2~13^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ecb33afc249ae3d2ab399cba0f411a6bf203d4a;p=thirdparty%2Fknot-resolver.git generic/lru: evictions counter to aid efficiency prediction --- diff --git a/lib/generic/lru.h b/lib/generic/lru.h index 2107e9980..b9ab19ac1 100644 --- a/lib/generic/lru.h +++ b/lib/generic/lru.h @@ -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); } /**