From 57dafe3915339090c6233cc82025adf116ddf667 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 19 Jun 2016 07:07:44 -0500 Subject: Last commit depends on new lib code I forgot to check in. (Oops.) --- lib/llist.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'lib/llist.c') diff --git a/lib/llist.c b/lib/llist.c index 6b4b8f2c..dbb5352a 100644 --- a/lib/llist.c +++ b/lib/llist.c @@ -99,3 +99,32 @@ void *dlist_terminate(void *list) return end; } + +// Find num in cache +struct num_cache *get_num_cache(struct num_cache *cache, long long num) +{ + while (cache) { + if (num==cache->num) return cache; + cache = cache->next; + } + + return 0; +} + +// Uniquely add num+data to cache. Updates *cache, returns pointer to existing +// entry if it was already there. +struct num_cache *add_num_cache(struct num_cache **cache, long long num, + void *data, int len) +{ + struct num_cache *old = get_num_cache(*cache, num); + + if (old) return old; + + old = xzalloc(sizeof(struct num_cache)+len); + old->next = *cache; + old->num = num; + memcpy(old->data, data, len); + *cache = old; + + return 0; +} -- cgit v1.2.3