3 * @brief Various things of used for dealing with linked lists.
5 * Copyright 2000-2004 Jonathan McDowell <noodles@earth.li>
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.
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
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/>.
26 * @brief Take a packet and add it to a linked list
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; \
33 list->name##s = list->last_##name = \
34 malloc(sizeof (*list->last_##name)); \
36 memset(list->last_##name, 0, sizeof(*list->last_##name)); \
37 list->last_##name->packet = item;
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
44 #define ADD_PACKET_TO_LIST(list, item) \
46 list->next = malloc(sizeof (*list)); \
49 list = malloc(sizeof (*list)); \
51 memset(list, 0, sizeof(*list)); \
55 * @brief A generic linked list structure.
60 /** A pointer to the next object. */
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.
69 * Returns a pointer to the head of the new list.
71 struct ll *lladd(struct ll *curll, void *object);
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.
78 * Returns a pointer to the head of the new list.
80 struct ll *lladdend(struct ll *curll, void *object);
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.
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.
92 struct ll *lldel(struct ll *curll, void *object,
93 int (*objectcmp) (const void *object1, const void *object2));
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.
101 * Searches through a list for an object. Returns a pointer to the object
102 * if it's found, otherwise NULL.
104 struct ll *llfind(struct ll *curll, void *object,
105 int (*objectcmp) (const void *object1, const void *object2));
108 * @brief Returns the number of elements in a linked list.
109 * @param curll The linked list to count.
111 * Counts the number of elements in a linked list.
113 unsigned long llsize(struct ll *curll);
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.
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.
124 void llfree(struct ll *curll, void (*objectfree) (void *object));
126 #endif /* __LL_H__ */