]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
generic/lru: evictions counter to aid efficiency prediction
authorMarek Vavruša <marek.vavrusa@nic.cz>
Mon, 13 Jul 2015 13:35:19 +0000 (15:35 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Mon, 13 Jul 2015 13:35:19 +0000 (15:35 +0200)
lib/generic/lru.h

index 2107e998098c71c115a72919cf72b3aee79c76b4..b9ab19ac15220a97cff62081243cc5dbad477cd5 100644 (file)
@@ -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);
 }
 
 /**