Free nodes of a lif_dict_find_all() result.

Signed-off-by: Maximilian Wilhelm <max@sdn.clinic>
This commit is contained in:
Maximilian Wilhelm 2020-07-28 23:25:01 +02:00
parent 73f3690432
commit 9d958892ed
3 changed files with 33 additions and 2 deletions

View file

@ -14,6 +14,7 @@
* from the use of this software. * from the use of this software.
*/ */
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "libifupdown/dict.h" #include "libifupdown/dict.h"
@ -55,16 +56,24 @@ lif_dict_add_once(struct lif_dict *dict, const char *key, void *data,
int (*compar)(const void *, const void *)) int (*compar)(const void *, const void *))
{ {
struct lif_list *existing = lif_dict_find_all(dict, key); struct lif_list *existing = lif_dict_find_all(dict, key);
if (existing) if (existing != NULL)
{ {
bool found = false;
struct lif_node *iter; struct lif_node *iter;
LIF_LIST_FOREACH(iter, existing->head) LIF_LIST_FOREACH(iter, existing->head)
{ {
if (!compar(data, iter->data)) if (!compar(data, iter->data))
return NULL; {
found = true;
break;
} }
} }
lif_list_free_nodes (&existing);
if (found)
return NULL;
}
struct lif_dict_entry *entry = calloc(1, sizeof *entry); struct lif_dict_entry *entry = calloc(1, sizeof *entry);
entry->key = strdup(key); entry->key = strdup(key);

View file

@ -3,6 +3,7 @@
* Purpose: linked lists * Purpose: linked lists
* *
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org> * Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
* Copyright (c) 2020 Maximilian Wilhelm <max@sdn.clinic>
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -14,8 +15,26 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include "libifupdown/list.h" #include "libifupdown/list.h"
void
lif_list_free_nodes(struct lif_list **list)
{
if (*list == NULL)
return;
struct lif_node *iter;
LIF_LIST_FOREACH (iter, (*list)->head)
{
free (iter->prev);
}
free (iter);
free (*list);
}
void void
lif_node_insert(struct lif_node *node, void *data, struct lif_list *list) lif_node_insert(struct lif_node *node, void *data, struct lif_list *list)
{ {

View file

@ -3,6 +3,7 @@
* Purpose: linked lists * Purpose: linked lists
* *
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org> * Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
* Copyright (c) 2020 Maximilian Wilhelm <max@sdn.clinic>
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -29,6 +30,8 @@ struct lif_list {
size_t length; size_t length;
}; };
extern void lif_list_free_nodes(struct lif_list **list);
extern void lif_node_insert(struct lif_node *node, void *data, struct lif_list *list); extern void lif_node_insert(struct lif_node *node, void *data, struct lif_list *list);
extern void lif_node_insert_tail(struct lif_node *node, void *data, struct lif_list *list); extern void lif_node_insert_tail(struct lif_node *node, void *data, struct lif_list *list);
extern void lif_node_delete(struct lif_node *node, struct lif_list *list); extern void lif_node_delete(struct lif_node *node, struct lif_list *list);