X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_llist.c;h=7fae025d62233ca51613268369239c43f516f973;hb=4f2642f86673329db9f8cf30854bf39bbdc4c2b2;hp=a81503578bc28b8e56da29d17dc72937788a6aa2;hpb=cb7fed8bf0af2646dfcb32844933398c28e39be5;p=collectd.git diff --git a/src/utils_llist.c b/src/utils_llist.c index a8150357..7fae025d 100644 --- a/src/utils_llist.c +++ b/src/utils_llist.c @@ -21,6 +21,8 @@ * Florian Forster */ +#include "config.h" + #include #include @@ -33,6 +35,7 @@ struct llist_s { llentry_t *head; llentry_t *tail; + int size; }; /* @@ -65,22 +68,16 @@ void llist_destroy (llist_t *l) free (l); } -llentry_t *llentry_create (const char *key, void *value) +llentry_t *llentry_create (char *key, void *value) { llentry_t *e; e = (llentry_t *) malloc (sizeof (llentry_t)); - if (e == NULL) - return (NULL); - - e->key = strdup (key); - e->value = value; - e->next = NULL; - - if (e->key == NULL) + if (e) { - free (e); - return (NULL); + e->key = key; + e->value = value; + e->next = NULL; } return (e); @@ -88,7 +85,6 @@ llentry_t *llentry_create (const char *key, void *value) void llentry_destroy (llentry_t *e) { - free (e->key); free (e); } @@ -102,12 +98,19 @@ void llist_append (llist_t *l, llentry_t *e) l->tail->next = e; l->tail = e; + + ++(l->size); } void llist_prepend (llist_t *l, llentry_t *e) { e->next = l->head; l->head = e; + + if (l->tail == NULL) + l->tail = e; + + ++(l->size); } void llist_remove (llist_t *l, llentry_t *e) @@ -124,6 +127,13 @@ void llist_remove (llist_t *l, llentry_t *e) l->head = e->next; if (l->tail == e) l->tail = prev; + + --(l->size); +} + +int llist_size (llist_t *l) +{ + return (l ? l->size : 0); } llentry_t *llist_search (llist_t *l, const char *key)