summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: d32581d)
raw | patch | inline | side by side (parent: d32581d)
author | Luke Heberling <collectd@c-ware.com> | |
Wed, 12 Dec 2007 07:43:34 +0000 (08:43 +0100) | ||
committer | Florian Forster <octo@huhu.verplant.org> | |
Wed, 12 Dec 2007 07:43:34 +0000 (08:43 +0100) |
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 <octo@huhu.verplant.org>
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 <octo@huhu.verplant.org>
src/plugin.c | patch | blob | history | |
src/utils_llist.c | patch | blob | history | |
src/utils_llist.h | patch | blob | history |
diff --git a/src/plugin.c b/src/plugin.c
index 6b17290f32dbe08a9a4bb6406cbc8fef35efdd01..44bdc8f5e6ed285dbecaad84178fc436865f5654 100644 (file)
--- a/src/plugin.c
+++ b/src/plugin.c
static int register_callback (llist_t **list, const char *name, void *callback)
{
llentry_t *le;
+ char *key;
if ((*list == NULL)
&& ((*list = llist_create ()) == NULL))
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);
}
return (-1);
llist_remove (list, e);
+ free (e->key);
llentry_destroy (e);
return (0);
llist_remove (list_read, e);
free (e->value);
+ free (e->key);
llentry_destroy (e);
return (0);
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 d8694e3f6770fb5cba12d48af9a4c15d64a04e35..f7e03c239b9889d4f62ecb62ae34488c868de2eb 100644 (file)
--- a/src/utils_llist.c
+++ b/src/utils_llist.c
{
llentry_t *head;
llentry_t *tail;
+ int size;
};
/*
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);
void llentry_destroy (llentry_t *e)
{
- free (e->key);
free (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)
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 603fc87b552178e342a336539da6de7fa7fe1b9e..c3753d83f25fe579c98af4e52fa7dbfa7fbdbecb 100644 (file)
--- a/src/utils_llist.h
+++ b/src/utils_llist.h
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);