diff options
author | Rob Landley <rob@landley.net> | 2016-06-19 07:07:44 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2016-06-19 07:07:44 -0500 |
commit | 57dafe3915339090c6233cc82025adf116ddf667 (patch) | |
tree | 6a419127f124a5710fe05291a988a2db95d33839 | |
parent | 848042af0144629fda0bea8b82d86e850cf29d5e (diff) | |
download | toybox-57dafe3915339090c6233cc82025adf116ddf667.tar.gz |
Last commit depends on new lib code I forgot to check in. (Oops.)
-rw-r--r-- | lib/lib.h | 9 | ||||
-rw-r--r-- | lib/llist.c | 29 |
2 files changed, 38 insertions, 0 deletions
@@ -34,6 +34,12 @@ struct double_list { char *data; }; +struct num_cache { + struct num_cache *next; + long long num; + char data[]; +}; + void llist_free_arg(void *node); void llist_free_double(void *node); void llist_traverse(void *list, void (*using)(void *node)); @@ -42,6 +48,9 @@ void *dlist_pop(void *list); // actually struct double_list **list void dlist_add_nomalloc(struct double_list **list, struct double_list *new); struct double_list *dlist_add(struct double_list **list, char *data); void *dlist_terminate(void *list); +struct num_cache *get_num_cache(struct num_cache *cache, long long num); +struct num_cache *add_num_cache(struct num_cache **cache, long long num, + void *data, int len); // args.c void get_optflags(void); 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; +} |