X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_llist.c;h=bf5f7e4944e335c6660eb0226cd8d43faa9aa2a8;hb=7de4b3692af84751fab5ac46a5da7414346ea50b;hp=d8694e3f6770fb5cba12d48af9a4c15d64a04e35;hpb=14b7c735bf93b5a6260a0e064bccc28dc7581c7f;p=collectd.git diff --git a/src/utils_llist.c b/src/utils_llist.c index d8694e3f..bf5f7e49 100644 --- a/src/utils_llist.c +++ b/src/utils_llist.c @@ -35,6 +35,7 @@ struct llist_s { llentry_t *head; llentry_t *tail; + int size; }; /* @@ -58,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; @@ -67,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); @@ -90,7 +88,6 @@ llentry_t *llentry_create (const char *key, void *value) void llentry_destroy (llentry_t *e) { - free (e->key); free (e); } @@ -104,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) @@ -126,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; @@ -141,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); }