]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lru: Add lru64_lookup function
authorChristopher Faulet <cfaulet@qualys.com>
Thu, 11 Jun 2015 11:33:13 +0000 (13:33 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 12 Jun 2015 16:06:59 +0000 (18:06 +0200)
It lookup a key in a LRU cache for use with specified domain and revision. It
differs from lru64_get as it does not create missing keys. The function returns
NULL if an error or a cache miss occurs.

include/import/lru.h
src/lru.c

index f922449b24041bc939ad8de0df92e25d1dd7cf7a..220cb17f653f816a7c3acd0548d7e3b6bc011b63 100644 (file)
@@ -66,6 +66,8 @@ struct lru64 {
        void (*free)(void *data);     /* function to release data, if needed */
 };
 
+
+struct lru64 *lru64_lookup(unsigned long long key, struct lru64_head *lru, void *domain, unsigned long long revision);
 struct lru64 *lru64_get(unsigned long long key, struct lru64_head *lru, void *domain, unsigned long long revision);
 void lru64_commit(struct lru64 *elem, void *data, void *domain, unsigned long long revision, void (*free)(void *));
 struct lru64_head *lru64_new(int size);
index 1b997d8b8de3d410312609a0e6bae461b0e404a4..273f3a8863a058fb76f1000dd446a15fb0131704 100644 (file)
--- a/src/lru.c
+++ b/src/lru.c
 #define LIST_ADD(lh, el) ({ (el)->n = (lh)->n; (el)->n->p = (lh)->n = (el); (el)->p = (lh); })
 #define LIST_DEL(el)     ({ (el)->n->p = (el)->p; (el)->p->n = (el)->n; })
 
+
+/* Lookup key <key> in LRU cache <lru> for use with domain <domain> whose data's
+ * current version is <revision>. It differs from lru64_get as it does not
+ * create missing keys. The function returns NULL if an error or a cache miss
+ * occurs. */
+struct lru64 *lru64_lookup(unsigned long long key, struct lru64_head *lru,
+                          void *domain, unsigned long long revision)
+{
+       struct eb64_node *node;
+       struct lru64 *elem;
+
+       if (!lru->spare) {
+               if (!lru->cache_size)
+                       return NULL;
+               lru->spare = malloc(sizeof(*lru->spare));
+               if (!lru->spare)
+                       return NULL;
+               lru->spare->domain = NULL;
+       }
+
+       node = __eb64_lookup(&lru->keys, key);
+       elem = container_of(node, typeof(*elem), node);
+       if (elem) {
+               /* Existing entry found, check validity then move it at the
+                * head of the LRU list.
+                */
+               if (elem->domain == domain && elem->revision == revision) {
+                       LIST_DEL(&elem->lru);
+                       LIST_ADD(&lru->list, &elem->lru);
+                       return elem;
+               }
+       }
+       return NULL;
+}
+
 /* Get key <key> from LRU cache <lru> for use with domain <domain> whose data's
  * current revision is <revision>. If the key doesn't exist it's first created
  * with ->domain = NULL. The caller detects this situation by checking ->domain