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, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * @brief Take a packet and add it to a linked list
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; \
34 list->name##s = list->last_##name = \
35 malloc(sizeof (*list->last_##name)); \
37 memset(list->last_##name, 0, sizeof(*list->last_##name)); \
38 list->last_##name->packet = item;
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
45 #define ADD_PACKET_TO_LIST(list, item) \
47 list->next = malloc(sizeof (*list)); \
50 list = malloc(sizeof (*list)); \
52 memset(list, 0, sizeof(*list)); \
56 * @brief A generic linked list structure.
61 /** A pointer to the next object. */
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.
70 * Returns a pointer to the head of the new list.
72 struct ll *lladd(struct ll *curll, void *object);
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.
79 * Returns a pointer to the head of the new list.
81 struct ll *lladdend(struct ll *curll, void *object);
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.
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.
93 struct ll *lldel(struct ll *curll, void *object,
94 int (*objectcmp) (const void *object1, const void *object2));
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.
102 * Searches through a list for an object. Returns a pointer to the object
103 * if it's found, otherwise NULL.
105 struct ll *llfind(struct ll *curll, void *object,
106 int (*objectcmp) (const void *object1, const void *object2));
109 * @brief Returns the number of elements in a linked list.
110 * @param curll The linked list to count.
112 * Counts the number of elements in a linked list.
114 unsigned long llsize(struct ll *curll);
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.
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.
125 void llfree(struct ll *curll, void (*objectfree) (void *object));
127 #endif /* __LL_H__ */