Update Debian Vcs-* fields to point to git repository
[onak.git] / ll.h
1 /**
2  * @file ll.h
3  * @brief Various things of used for dealing with linked lists.
4  *
5  * Copyright 2000-2004 Jonathan McDowell <noodles@earth.li>
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc., 51
18  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #ifndef __LL_H__
22 #define __LL_H__
23
24 /**
25  * @brief Take a packet and add it to a linked list
26  */
27 #define ADD_PACKET_TO_LIST_END(list, name, item)                              \
28         if (list->name##s != NULL) {                                          \
29                 list->last_##name->next = malloc(sizeof (*list->last_##name));\
30                 list->last_##name = list->last_##name->next;                  \
31         } else {                                                              \
32                 list->name##s = list->last_##name =                           \
33                         malloc(sizeof (*list->last_##name));                  \
34         }                                                                     \
35         memset(list->last_##name, 0, sizeof(*list->last_##name));             \
36         list->last_##name->packet = item;
37
38 /**
39  * @brief Add an item to the end of a linked list
40  * @param list A pointer to the last element in the list
41  * @param item the item to add
42  */
43 #define ADD_PACKET_TO_LIST(list, item)                                        \
44         if (list != NULL) {                                                   \
45                 list->next = malloc(sizeof (*list));                          \
46                 list = list->next;                                            \
47         } else {                                                              \
48                 list = malloc(sizeof (*list));                                \
49         }                                                                     \
50         memset(list, 0, sizeof(*list));                                       \
51         list->packet = item;
52
53 /**
54  * @brief A generic linked list structure.
55  */
56 struct ll {
57         /** The object. */
58         void *object;
59         /** A pointer to the next object. */
60         struct ll *next;
61 };
62
63 /**
64  * @brief Add an item to a linked list.
65  * @param curll The list to add to. Can be NULL to create a new list.
66  * @param object The object to add.
67  *
68  * Returns a pointer to the head of the new list.
69  */
70 struct ll *lladd(struct ll *curll, void *object);
71
72 /**
73  * @brief Add an item to the end of a linked list.
74  * @param curll The list to add to. Can be NULL to create a new list.
75  * @param object The object to add.
76  *
77  * Returns a pointer to the head of the new list.
78  */
79 struct ll *lladdend(struct ll *curll, void *object);
80
81 /**
82  * @brief Remove an item from a linked list.
83  * @param curll The list to remove the item from.
84  * @param object The object to remove.
85  * @param objectcmp A pointer to a comparision function for the object type.
86  *
87  * Trawls through the list looking for the object. If it's found then it
88  * is removed from the list. Only one occurance is searched for. Returns
89  * a pointer to the head of the new list.
90  */
91 struct ll *lldel(struct ll *curll, void *object,
92         int (*objectcmp) (const void *object1, const void *object2));
93
94 /**
95  * @brief Find an item in a linked list.
96  * @param curll The list to look in.
97  * @param object The object to look for.
98  * @param objectcmp A pointer to a comparision function for the object type.
99  *
100  * Searches through a list for an object. Returns a pointer to the object
101  * if it's found, otherwise NULL.
102  */
103 struct ll *llfind(struct ll *curll, void *object,
104         int (*objectcmp) (const void *object1, const void *object2));
105
106 /**
107  * @brief Returns the number of elements in a linked list.
108  * @param curll The linked list to count.
109  *
110  * Counts the number of elements in a linked list.
111  */
112 unsigned long llsize(struct ll *curll);
113
114 /**
115  * @brief Frees a linked list.
116  * @param curll The list to free.
117  * @param objectfree A pointer to a free function for the object.
118  *
119  * Walks through a list and free it. If a function is provided for
120  * objectfree then it's called for each element to free them, if it's NULL
121  * just the list is freed.
122  */
123 void llfree(struct ll *curll, void (*objectfree) (void *object));
124
125 #endif /* __LL_H__ */