]> the.earth.li Git - onak.git/blob - ll.h
Cleanup various includes
[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 #include <stdlib.h>
25
26 /**
27  * @brief Take a packet and add it to a linked list
28  */
29 #define ADD_PACKET_TO_LIST_END(list, name, item)                              \
30         if (list->name##s != NULL) {                                          \
31                 list->last_##name->next = malloc(sizeof (*list->last_##name));\
32                 list->last_##name = list->last_##name->next;                  \
33         } else {                                                              \
34                 list->name##s = list->last_##name =                           \
35                         malloc(sizeof (*list->last_##name));                  \
36         }                                                                     \
37         memset(list->last_##name, 0, sizeof(*list->last_##name));             \
38         list->last_##name->packet = item;
39
40 /**
41  * @brief Add an item to the end of a linked list
42  * @param list A pointer to the last element in the list
43  * @param item the item to add
44  */
45 #define ADD_PACKET_TO_LIST(list, item)                                        \
46         if (list != NULL) {                                                   \
47                 list->next = malloc(sizeof (*list));                          \
48                 list = list->next;                                            \
49         } else {                                                              \
50                 list = malloc(sizeof (*list));                                \
51         }                                                                     \
52         memset(list, 0, sizeof(*list));                                       \
53         list->packet = item;
54
55 /**
56  * @brief A generic linked list structure.
57  */
58 struct ll {
59         /** The object. */
60         void *object;
61         /** A pointer to the next object. */
62         struct ll *next;
63 };
64
65 /**
66  * @brief Add an item to a linked list.
67  * @param curll The list to add to. Can be NULL to create a new list.
68  * @param object The object to add.
69  *
70  * Returns a pointer to the head of the new list.
71  */
72 struct ll *lladd(struct ll *curll, void *object);
73
74 /**
75  * @brief Add an item to the end of a linked list.
76  * @param curll The list to add to. Can be NULL to create a new list.
77  * @param object The object to add.
78  *
79  * Returns a pointer to the head of the new list.
80  */
81 struct ll *lladdend(struct ll *curll, void *object);
82
83 /**
84  * @brief Remove an item from a linked list.
85  * @param curll The list to remove the item from.
86  * @param object The object to remove.
87  * @param objectcmp A pointer to a comparision function for the object type.
88  *
89  * Trawls through the list looking for the object. If it's found then it
90  * is removed from the list. Only one occurance is searched for. Returns
91  * a pointer to the head of the new list.
92  */
93 struct ll *lldel(struct ll *curll, void *object,
94         int (*objectcmp) (const void *object1, const void *object2));
95
96 /**
97  * @brief Find an item in a linked list.
98  * @param curll The list to look in.
99  * @param object The object to look for.
100  * @param objectcmp A pointer to a comparision function for the object type.
101  *
102  * Searches through a list for an object. Returns a pointer to the object
103  * if it's found, otherwise NULL.
104  */
105 struct ll *llfind(struct ll *curll, void *object,
106         int (*objectcmp) (const void *object1, const void *object2));
107
108 /**
109  * @brief Returns the number of elements in a linked list.
110  * @param curll The linked list to count.
111  *
112  * Counts the number of elements in a linked list.
113  */
114 unsigned long llsize(struct ll *curll);
115
116 /**
117  * @brief Frees a linked list.
118  * @param curll The list to free.
119  * @param objectfree A pointer to a free function for the object.
120  *
121  * Walks through a list and free it. If a function is provided for
122  * objectfree then it's called for each element to free them, if it's NULL
123  * just the list is freed.
124  */
125 void llfree(struct ll *curll, void (*objectfree) (void *object));
126
127 #endif /* __LL_H__ */