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