2 * mod_list: Implements linked list module for cep-browser.
4 * The mod_list should be used for basic tests because its
5 * main operations (insert() and lookup()) are quite slow and
8 * Licensed under the GPLv2 license.
10 * Luiz Fernando N. Capitulino
11 * <lcapitulino@gmail.com>
32 int (*compare
)(const void *key1
, const void *key2
);
33 void (*destroy
)(void *data
);
34 void (*dump
)(const void *data
);
37 static struct list cep_list
;
39 static struct node
*list_node_alloc(void *data
, struct node
*next
)
43 new = malloc(sizeof(*new));
45 fatal("Could not allocate memory");
53 static void list_init(int (*compare
)(const void *key1
, const void *key2
),
54 void (*destroy
)(void *data
),
55 void (*dump
)(const void *data
))
59 p
= list_node_alloc(NULL
, NULL
);
61 cep_list
.head
= cep_list
.tail
= p
;
62 cep_list
.compare
= compare
;
63 cep_list
.destroy
= destroy
;
68 static void list_destroy(void)
72 /* free place holder */
73 tmp
= cep_list
.head
->next
;
76 for (p
= tmp
; p
!= NULL
;) {
79 cep_list
.destroy(p
->data
);
86 static size_t list_size(void)
91 static void list_insert(void *data
)
96 * XXX: Accept duplicated entries
98 * Insertions would be considerably slower if we
99 * call list_lookup() to catch duplicated
102 * Instead, we just ignore duplicates and
103 * inserts new nodes in the tail of the list.
106 new = list_node_alloc(data
, NULL
);
108 cep_list
.tail
->next
= new;
113 static void list_dump(void)
117 for (p
= cep_list
.head
->next
; p
!= NULL
; p
= p
->next
)
118 cep_list
.dump(p
->data
);
121 static void *list_lookup(const void *data
)
126 for (p
= cep_list
.head
->next
; p
!= NULL
; p
= p
->next
) {
127 ret
= cep_list
.compare(p
->data
, data
);
135 struct module list_module
= {
137 .mod_init
= list_init
,
138 .mod_destroy
= list_destroy
,
139 .mod_insert
= list_insert
,
140 .mod_lookup
= list_lookup
,
141 .mod_dump
= list_dump
,
142 .mod_size
= list_size
,