X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_llist.c;h=bf5f7e4944e335c6660eb0226cd8d43faa9aa2a8;hb=daa4b5ab990fac569e684ab8850792bec0a19159;hp=a81503578bc28b8e56da29d17dc72937788a6aa2;hpb=d561a8387f633491bdcd7fe6964f0b63ae694af5;p=collectd.git diff --git a/src/utils_llist.c b/src/utils_llist.c index a8150357..bf5f7e49 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; }; /* @@ -56,6 +59,9 @@ void llist_destroy (llist_t *l) llentry_t *e_this; llentry_t *e_next; + if (l == NULL) + return; + for (e_this = l->head; e_this != NULL; e_this = e_next) { e_next = e_this->next; @@ -65,22 +71,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 +88,6 @@ llentry_t *llentry_create (const char *key, void *value) void llentry_destroy (llentry_t *e) { - free (e->key); free (e); } @@ -102,12 +101,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,12 +130,22 @@ 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) { llentry_t *e; + if (l == NULL) + return (NULL); + for (e = l->head; e != NULL; e = e->next) if (strcmp (key, e->key) == 0) break; @@ -139,10 +155,14 @@ llentry_t *llist_search (llist_t *l, const char *key) llentry_t *llist_head (llist_t *l) { + if (l == NULL) + return (NULL); return (l->head); } llentry_t *llist_tail (llist_t *l) { + if (l == NULL) + return (NULL); return (l->tail); }