From 9ded40eda95737bb5dfcd9f94d78b4aa0e2868aa Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 13 Oct 2022 00:48:22 -0500 Subject: [PATCH] Move num_cache from lib into its only user. --- lib/lib.h | 9 --------- lib/llist.c | 29 ----------------------------- toys/net/netstat.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/lib/lib.h b/lib/lib.h index 5c68ac9b..83234b3e 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -24,12 +24,6 @@ struct double_list { char *data; }; -struct num_cache { - struct num_cache *next; - long long num; - char data[]; -}; - struct dev_ino { dev_t dev; ino_t ino; @@ -44,9 +38,6 @@ void *dlist_lpop(void *list); // also 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 #define FLAGS_NODASH (1LL<<63) diff --git a/lib/llist.c b/lib/llist.c index 6ceb1ce7..63a98bb6 100644 --- a/lib/llist.c +++ b/lib/llist.c @@ -116,32 +116,3 @@ 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; -} diff --git a/toys/net/netstat.c b/toys/net/netstat.c index 7eed02da..9470e8e1 100644 --- a/toys/net/netstat.c +++ b/toys/net/netstat.c @@ -36,6 +36,42 @@ GLOBALS( int wpad; ) +struct num_cache { + struct num_cache *next; + long long num; + char data[]; +}; + +// Find num in cache +static 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. +static 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; +} + static void addr2str(int af, void *addr, unsigned port, char *buf, int len, char *proto) { -- 2.39.2