Imported Upstream version 2.4.3
[platform/upstream/audit.git] / tools / auvirt / auvirt-list.c
1 #include "auvirt-list.h"
2 #include <stdlib.h>
3
4 list_t *list_init(list_t *list, list_free_data_fn *free_data_fn)
5 {
6         if (list == NULL)
7                 return NULL;
8         list->head = list->tail = NULL;
9         list->free_data_fn = free_data_fn;
10         return list;
11 }
12
13 list_t *list_new(list_free_data_fn *free_data_fn)
14 {
15         return list_init(malloc(sizeof(list_t)), free_data_fn);
16 }
17
18 void list_free_node(list_node_t *node, list_free_data_fn *free_data_fn)
19 {
20         if (node) {
21                 if (free_data_fn)
22                         free_data_fn(node->data);
23                 free(node);
24         }
25 }
26
27 void list_free_(list_t *list, list_free_data_fn *free_data_fn)
28 {
29         if (list != NULL) {
30                 list_node_t *it = list->head;
31                 while (it && it->next) {
32                         it = it->next;
33                         list_free_node(it->prev, free_data_fn);
34                 }
35                 list_free_node(it, free_data_fn);
36                 free(list);
37         }
38 }
39
40 void list_free(list_t *list)
41 {
42         if (list)
43                 list_free_(list, list->free_data_fn);
44 }
45
46 list_node_t *list_insert_after(list_t *list, list_node_t *it,
47                 void *data)
48 {
49         list_node_t *node = NULL;
50         if (list == NULL)
51                 return NULL;
52
53         /* allocate node */
54         node = malloc(sizeof(list_node_t));
55         if (node == NULL)
56                 return NULL;
57         node->data = data;
58
59         /* insert the new node after it */
60         node->prev = it;
61         if (it) {
62                 node->next = it->next;
63                 it->next = node;
64         }
65         else
66                 node->next = list->head;
67         if (node->next)
68                 node->next->prev = node;
69
70         /* update list's head and tail */
71         if (it == list->tail)
72                 list->tail = node;
73         if (it == NULL)
74                 list->head = node;
75
76         return node;
77 }
78
79 list_node_t *list_append(list_t *list, void *data)
80 {
81         return list_insert_after(list, list->tail, data);
82 }
83
84 int list_remove_(list_t *list, list_node_t *it,
85                 list_free_data_fn *free_data_fn)
86 {
87         if (list == NULL || it == NULL)
88                 return 1;
89         if (list->head == it)
90                 list->head = it->next;
91         if (list->tail == it)
92                 list->tail = it->prev;
93         if (it->next)
94                 it->next->prev = it->prev;
95         if (it->prev)
96                 it->prev->next = it->next;
97         list_free_node(it, free_data_fn);
98         return 0;
99 }
100
101 int list_remove(list_t *list, list_node_t *it)
102 {
103         return list_remove_(list, it, list->free_data_fn);
104 }
105