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>
static int register_callback (llist_t **list, const char *name, void *callback)
{
llentry_t *le;
static int register_callback (llist_t **list, const char *name, void *callback)
{
llentry_t *le;
if ((*list == NULL)
&& ((*list = llist_create ()) == NULL))
if ((*list == NULL)
&& ((*list = llist_create ()) == NULL))
le = llist_search (*list, name);
if (le == 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);
llist_append (*list, le);
}
llist_append (*list, le);
}
return (-1);
llist_remove (list, e);
return (-1);
llist_remove (list, e);
llentry_destroy (e);
return (0);
llentry_destroy (e);
return (0);
llist_remove (list_read, e);
free (e->value);
llist_remove (list_read, e);
free (e->value);
llentry_destroy (e);
return (0);
llentry_destroy (e);
return (0);
llist_remove (list_data_set, e);
ds = (data_set_t *) e->value;
llist_remove (list_data_set, e);
ds = (data_set_t *) e->value;
llentry_destroy (e);
sfree (ds->ds);
llentry_destroy (e);
sfree (ds->ds);
{
llentry_t *head;
llentry_t *tail;
{
llentry_t *head;
llentry_t *tail;
-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));
{
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)
- free (e);
- return (NULL);
+ e->key = key;
+ e->value = value;
+ e->next = NULL;
void llentry_destroy (llentry_t *e)
{
void llentry_destroy (llentry_t *e)
{
l->tail->next = e;
l->tail = e;
l->tail->next = e;
l->tail = e;
}
void llist_prepend (llist_t *l, llentry_t *e)
{
e->next = l->head;
l->head = e;
}
void llist_prepend (llist_t *l, llentry_t *e)
{
e->next = l->head;
l->head = e;
}
void llist_remove (llist_t *l, llentry_t *e)
}
void llist_remove (llist_t *l, llentry_t *e)
l->head = e->next;
if (l->tail == e)
l->tail = prev;
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 *llist_search (llist_t *l, const char *key)
llist_t *llist_create (void);
void llist_destroy (llist_t *l);
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);
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);
llentry_t *llist_search (llist_t *l, const char *key);
llentry_t *llist_head (llist_t *l);