From: Luke Heberling Date: Wed, 12 Dec 2007 07:43:34 +0000 (+0100) Subject: src/utils_llist.[ch]: Changed the semantic so module doesn't copy the `key'. X-Git-Tag: collectd-4.3.0beta0~64^2 X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=e01359a9c65e5f7f26a481443504c75afab520d2 src/utils_llist.[ch]: Changed the semantic so module doesn't copy the `key'. Whilst looking at my implementation of the plugins we've discussed which are now using dual avl trees, I saw that the utils_llist module would be a more efficient substitute for the second avl tree. However, it would need to know its size and would preferably not duplicate and own the key for each item. Here's a patch which does this. These changes might make it easier for future plugins to use the linked list for similar purposes, similar to the way the avl tree does not impose any lifetime on its keys. Signed-off-by: Florian Forster --- diff --git a/src/plugin.c b/src/plugin.c index 6b17290f..44bdc8f5 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -79,6 +79,7 @@ static const char *plugin_get_dir (void) static int register_callback (llist_t **list, const char *name, void *callback) { llentry_t *le; + char *key; if ((*list == NULL) && ((*list = llist_create ()) == NULL)) @@ -87,9 +88,16 @@ static int register_callback (llist_t **list, const char *name, void *callback) le = llist_search (*list, name); if (le == NULL) { - le = llentry_create (name, callback); + key = strdup (name); + if (key == NULL) + return (-1); + + le = llentry_create (key, callback); if (le == NULL) + { + free (key); return (-1); + } llist_append (*list, le); } @@ -111,6 +119,7 @@ static int plugin_unregister (llist_t *list, const char *name) return (-1); llist_remove (list, e); + free (e->key); llentry_destroy (e); return (0); @@ -499,6 +508,7 @@ int plugin_unregister_read (const char *name) llist_remove (list_read, e); free (e->value); + free (e->key); llentry_destroy (e); return (0); @@ -529,6 +539,7 @@ int plugin_unregister_data_set (const char *name) llist_remove (list_data_set, e); ds = (data_set_t *) e->value; + free (e->key); llentry_destroy (e); sfree (ds->ds); diff --git a/src/utils_llist.c b/src/utils_llist.c index d8694e3f..f7e03c23 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; }; /* @@ -67,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); @@ -90,7 +85,6 @@ llentry_t *llentry_create (const char *key, void *value) void llentry_destroy (llentry_t *e) { - free (e->key); free (e); } @@ -104,12 +98,15 @@ 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; + ++(l->size); } void llist_remove (llist_t *l, llentry_t *e) @@ -126,6 +123,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) diff --git a/src/utils_llist.h b/src/utils_llist.h index 603fc87b..c3753d83 100644 --- a/src/utils_llist.h +++ b/src/utils_llist.h @@ -44,13 +44,15 @@ typedef struct llist_s llist_t; llist_t *llist_create (void); void llist_destroy (llist_t *l); -llentry_t *llentry_create (const char *key, void *value); +llentry_t *llentry_create (char *key, void *value); void llentry_destroy (llentry_t *e); void llist_append (llist_t *l, llentry_t *e); void llist_prepend (llist_t *l, llentry_t *e); void llist_remove (llist_t *l, llentry_t *e); +int llist_size (llist_t *l); + llentry_t *llist_search (llist_t *l, const char *key); llentry_t *llist_head (llist_t *l);