From b475859cfac3e6c0ad3b16a85905d81b296b0f7a Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 1 Mar 2013 10:14:20 -0600 Subject: [PATCH] cache: use new pkg_list_t framework --- cache.c | 36 ++++++++++++------------------------ iter.h | 2 ++ pkg.h | 2 +- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/cache.c b/cache.c index c768752..40c6f54 100644 --- a/cache.c +++ b/cache.c @@ -15,7 +15,7 @@ #include "pkg.h" -static pkg_t *pkg_cache = NULL; +static pkg_list_t pkg_cache = PKG_LIST_INITIALIZER; /* * pkg_cache_lookup(id) @@ -27,10 +27,12 @@ static pkg_t *pkg_cache = NULL; pkg_t * pkg_cache_lookup(const char *id) { - pkg_t *pkg; + pkg_node_t *node; - PKG_FOREACH_LIST_ENTRY(pkg_cache, pkg) + PKG_FOREACH_LIST_ENTRY(pkg_cache.head, node) { + pkg_t *pkg = node->data; + if (!strcmp(pkg->id, id)) return pkg_ref(pkg); } @@ -49,11 +51,7 @@ pkg_cache_add(pkg_t *pkg) { pkg_ref(pkg); - pkg->next = pkg_cache; - pkg_cache = pkg; - - if (pkg->next != NULL) - pkg->next->prev = pkg; + pkg_node_insert(&pkg->cache_iter, pkg, &pkg_cache); } /* @@ -64,28 +62,18 @@ pkg_cache_add(pkg_t *pkg) void pkg_cache_remove(pkg_t *pkg) { - if (pkg->prev != NULL) - pkg->prev->next = pkg->next; - - if (pkg->next != NULL) - pkg->next->prev = pkg->prev; - - if (pkg == pkg_cache) - { - if (pkg->prev != NULL) - pkg_cache = pkg->prev; - else - pkg_cache = pkg->next; - } + pkg_node_delete(&pkg->cache_iter, &pkg_cache); } void pkg_cache_free(void) { - pkg_t *iter, *iter2; + pkg_node_t *iter, *iter2; - PKG_FOREACH_LIST_ENTRY_SAFE(pkg_cache, iter2, iter) + PKG_FOREACH_LIST_ENTRY_SAFE(pkg_cache.head, iter2, iter) { - pkg_free(iter); + pkg_t *pkg = iter->data; + + pkg_free(pkg); } } diff --git a/iter.h b/iter.h index 43c81ee..f55c0aa 100644 --- a/iter.h +++ b/iter.h @@ -27,6 +27,8 @@ typedef struct { pkg_node_t *head, *tail; } pkg_list_t; +#define PKG_LIST_INITIALIZER { NULL, NULL } + static inline void pkg_node_insert(pkg_node_t *node, void *data, pkg_list_t *list) { diff --git a/pkg.h b/pkg.h index 4ac1922..3d122b6 100644 --- a/pkg.h +++ b/pkg.h @@ -85,7 +85,7 @@ typedef struct pkg_queue_ { #define PKG_PROPF_SEEN 0x4 struct pkg_ { - pkg_t *prev, *next; /* for pkg_cache */ + pkg_node_t cache_iter; int refcount; char *id;