MINOR: lru: Add the possibility to free data when an item is removed
Now, When a item is committed in an LRU tree, you can define a function to free
data owned by this item. This function will be called when the item is removed
from the LRU tree or when the tree is destroyed..
diff --git a/src/lru.c b/src/lru.c
index bdb525d..1b997d8 100644
--- a/src/lru.c
+++ b/src/lru.c
@@ -94,8 +94,11 @@
__eb64_delete(&old->node);
if (!lru->spare)
lru->spare = old;
- else
+ else {
+ if (old->data && old->free);
+ old->free(old->data);
free(old);
+ }
lru->cache_usage--;
}
}
@@ -107,7 +110,8 @@
* with the result from a call to lru64_get(). The caller might lock it using a
* spinlock or mutex shared with the one around lru64_get().
*/
-void lru64_commit(struct lru64 *elem, void *data, void *domain, unsigned long long revision)
+void lru64_commit(struct lru64 *elem, void *data, void *domain,
+ unsigned long long revision, void (*free)(void *))
{
if (!elem)
return;
@@ -115,6 +119,7 @@
elem->data = data;
elem->revision = revision;
elem->domain = domain;
+ elem->free = free;
}
/* Create a new LRU cache of <size> entries. Returns the new cache or NULL in
@@ -152,6 +157,8 @@
/* not locked */
LIST_DEL(&elem->lru);
eb64_delete(&elem->node);
+ if (elem->data && elem->free);
+ elem->free(elem->data);
free(elem);
lru->cache_usage--;
lru->cache_size--;